Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 1 | // Copyright (C) 2017 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 15 | #include <gtest/gtest.h> |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 16 | |
| 17 | #include "src/condition/ConditionTracker.h" |
| 18 | #include "src/matchers/LogMatchingTracker.h" |
| 19 | #include "src/metrics/CountMetricProducer.h" |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 20 | #include "src/metrics/GaugeMetricProducer.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 21 | #include "src/metrics/MetricProducer.h" |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 22 | #include "src/metrics/ValueMetricProducer.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 23 | #include "src/metrics/metrics_manager_util.h" |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 24 | |
| 25 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <set> |
| 29 | #include <unordered_map> |
| 30 | #include <vector> |
| 31 | |
| 32 | using namespace android::os::statsd; |
| 33 | using android::sp; |
| 34 | using std::set; |
| 35 | using std::unordered_map; |
| 36 | using std::vector; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 37 | using android::os::statsd::Condition; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 38 | |
| 39 | #ifdef __ANDROID__ |
| 40 | |
| 41 | // TODO: ADD MORE TEST CASES. |
| 42 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 43 | const ConfigKey kConfigKey(0, "test"); |
| 44 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 45 | StatsdConfig buildGoodConfig() { |
| 46 | StatsdConfig config; |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 47 | config.set_name("12345"); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 48 | |
| 49 | LogEntryMatcher* eventMatcher = config.add_log_entry_matcher(); |
| 50 | eventMatcher->set_name("SCREEN_IS_ON"); |
| 51 | |
| 52 | SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 53 | simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 54 | simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key( |
| 55 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
| 56 | simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int( |
| 57 | 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/); |
| 58 | |
| 59 | eventMatcher = config.add_log_entry_matcher(); |
| 60 | eventMatcher->set_name("SCREEN_IS_OFF"); |
| 61 | |
| 62 | simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 63 | simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 64 | simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key( |
| 65 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
| 66 | simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int( |
| 67 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_OFF*/); |
| 68 | |
| 69 | eventMatcher = config.add_log_entry_matcher(); |
| 70 | eventMatcher->set_name("SCREEN_ON_OR_OFF"); |
| 71 | |
| 72 | LogEntryMatcher_Combination* combination = eventMatcher->mutable_combination(); |
| 73 | combination->set_operation(LogicalOperation::OR); |
| 74 | combination->add_matcher("SCREEN_IS_ON"); |
| 75 | combination->add_matcher("SCREEN_IS_OFF"); |
| 76 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 77 | CountMetric* metric = config.add_count_metric(); |
| 78 | metric->set_name("3"); |
| 79 | metric->set_what("SCREEN_IS_ON"); |
| 80 | metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L); |
| 81 | KeyMatcher* keyMatcher = metric->add_dimension(); |
| 82 | keyMatcher->set_key(1); |
| 83 | |
| 84 | auto alert = config.add_alert(); |
| 85 | alert->set_name("3"); |
| 86 | alert->set_metric_name("3"); |
| 87 | alert->set_number_of_buckets(10); |
| 88 | alert->set_refractory_period_secs(100); |
| 89 | alert->set_trigger_if_sum_gt(100); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 90 | return config; |
| 91 | } |
| 92 | |
| 93 | StatsdConfig buildCircleMatchers() { |
| 94 | StatsdConfig config; |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 95 | config.set_name("12345"); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 96 | |
| 97 | LogEntryMatcher* eventMatcher = config.add_log_entry_matcher(); |
| 98 | eventMatcher->set_name("SCREEN_IS_ON"); |
| 99 | |
| 100 | SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 101 | simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 102 | simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key( |
| 103 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
| 104 | simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int( |
| 105 | 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/); |
| 106 | |
| 107 | eventMatcher = config.add_log_entry_matcher(); |
| 108 | eventMatcher->set_name("SCREEN_ON_OR_OFF"); |
| 109 | |
| 110 | LogEntryMatcher_Combination* combination = eventMatcher->mutable_combination(); |
| 111 | combination->set_operation(LogicalOperation::OR); |
| 112 | combination->add_matcher("SCREEN_IS_ON"); |
| 113 | // Circle dependency |
| 114 | combination->add_matcher("SCREEN_ON_OR_OFF"); |
| 115 | |
| 116 | return config; |
| 117 | } |
| 118 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 119 | StatsdConfig buildAlertWithUnknownMetric() { |
| 120 | StatsdConfig config; |
| 121 | config.set_name("12345"); |
| 122 | |
| 123 | LogEntryMatcher* eventMatcher = config.add_log_entry_matcher(); |
| 124 | eventMatcher->set_name("SCREEN_IS_ON"); |
| 125 | |
| 126 | CountMetric* metric = config.add_count_metric(); |
| 127 | metric->set_name("3"); |
| 128 | metric->set_what("SCREEN_IS_ON"); |
| 129 | metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L); |
| 130 | KeyMatcher* keyMatcher = metric->add_dimension(); |
| 131 | keyMatcher->set_key(1); |
| 132 | |
| 133 | auto alert = config.add_alert(); |
| 134 | alert->set_name("3"); |
| 135 | alert->set_metric_name("2"); |
| 136 | alert->set_number_of_buckets(10); |
| 137 | alert->set_refractory_period_secs(100); |
| 138 | alert->set_trigger_if_sum_gt(100); |
| 139 | return config; |
| 140 | } |
| 141 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 142 | StatsdConfig buildMissingMatchers() { |
| 143 | StatsdConfig config; |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 144 | config.set_name("12345"); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 145 | |
| 146 | LogEntryMatcher* eventMatcher = config.add_log_entry_matcher(); |
| 147 | eventMatcher->set_name("SCREEN_IS_ON"); |
| 148 | |
| 149 | SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 150 | simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 151 | simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key( |
| 152 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
| 153 | simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int( |
| 154 | 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/); |
| 155 | |
| 156 | eventMatcher = config.add_log_entry_matcher(); |
| 157 | eventMatcher->set_name("SCREEN_ON_OR_OFF"); |
| 158 | |
| 159 | LogEntryMatcher_Combination* combination = eventMatcher->mutable_combination(); |
| 160 | combination->set_operation(LogicalOperation::OR); |
| 161 | combination->add_matcher("SCREEN_IS_ON"); |
| 162 | // undefined matcher |
| 163 | combination->add_matcher("ABC"); |
| 164 | |
| 165 | return config; |
| 166 | } |
| 167 | |
Yao Chen | 10535b9 | 2017-11-27 11:31:55 -0800 | [diff] [blame] | 168 | StatsdConfig buildMissingCondition() { |
| 169 | StatsdConfig config; |
| 170 | config.set_name("12345"); |
| 171 | |
| 172 | CountMetric* metric = config.add_count_metric(); |
| 173 | metric->set_name("3"); |
| 174 | metric->set_what("SCREEN_EVENT"); |
| 175 | metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L); |
| 176 | metric->set_condition("SOME_CONDITION"); |
| 177 | |
| 178 | LogEntryMatcher* eventMatcher = config.add_log_entry_matcher(); |
| 179 | eventMatcher->set_name("SCREEN_EVENT"); |
| 180 | |
| 181 | SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
| 182 | simpleLogEntryMatcher->set_tag(2); |
| 183 | |
| 184 | return config; |
| 185 | } |
| 186 | |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 187 | StatsdConfig buildDimensionMetricsWithMultiTags() { |
| 188 | StatsdConfig config; |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 189 | config.set_name("12345"); |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 190 | |
| 191 | LogEntryMatcher* eventMatcher = config.add_log_entry_matcher(); |
| 192 | eventMatcher->set_name("BATTERY_VERY_LOW"); |
| 193 | SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
| 194 | simpleLogEntryMatcher->set_tag(2); |
| 195 | |
| 196 | eventMatcher = config.add_log_entry_matcher(); |
| 197 | eventMatcher->set_name("BATTERY_VERY_VERY_LOW"); |
| 198 | simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
| 199 | simpleLogEntryMatcher->set_tag(3); |
| 200 | |
| 201 | eventMatcher = config.add_log_entry_matcher(); |
| 202 | eventMatcher->set_name("BATTERY_LOW"); |
| 203 | |
| 204 | LogEntryMatcher_Combination* combination = eventMatcher->mutable_combination(); |
| 205 | combination->set_operation(LogicalOperation::OR); |
| 206 | combination->add_matcher("BATTERY_VERY_LOW"); |
| 207 | combination->add_matcher("BATTERY_VERY_VERY_LOW"); |
| 208 | |
| 209 | // Count process state changes, slice by uid, while SCREEN_IS_OFF |
| 210 | CountMetric* metric = config.add_count_metric(); |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 211 | metric->set_name("3"); |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 212 | metric->set_what("BATTERY_LOW"); |
| 213 | metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L); |
| 214 | KeyMatcher* keyMatcher = metric->add_dimension(); |
| 215 | keyMatcher->set_key(1); |
| 216 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 217 | auto alert = config.add_alert(); |
| 218 | alert->set_name("3"); |
| 219 | alert->set_metric_name("3"); |
| 220 | alert->set_number_of_buckets(10); |
| 221 | alert->set_refractory_period_secs(100); |
| 222 | alert->set_trigger_if_sum_gt(100); |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 223 | return config; |
| 224 | } |
| 225 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 226 | StatsdConfig buildCircleConditions() { |
| 227 | StatsdConfig config; |
Yangster-mac | d1815dc | 2017-11-13 21:43:15 -0800 | [diff] [blame] | 228 | config.set_name("12345"); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 229 | |
| 230 | LogEntryMatcher* eventMatcher = config.add_log_entry_matcher(); |
| 231 | eventMatcher->set_name("SCREEN_IS_ON"); |
| 232 | |
| 233 | SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 234 | simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 235 | simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key( |
| 236 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
| 237 | simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int( |
| 238 | 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/); |
| 239 | |
| 240 | eventMatcher = config.add_log_entry_matcher(); |
| 241 | eventMatcher->set_name("SCREEN_IS_OFF"); |
| 242 | |
| 243 | simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 244 | simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 245 | simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key( |
| 246 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
| 247 | simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int( |
| 248 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_OFF*/); |
| 249 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 250 | auto condition = config.add_condition(); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 251 | condition->set_name("SCREEN_IS_ON"); |
| 252 | SimpleCondition* simpleCondition = condition->mutable_simple_condition(); |
| 253 | simpleCondition->set_start("SCREEN_IS_ON"); |
| 254 | simpleCondition->set_stop("SCREEN_IS_OFF"); |
| 255 | |
| 256 | condition = config.add_condition(); |
| 257 | condition->set_name("SCREEN_IS_EITHER_ON_OFF"); |
| 258 | |
| 259 | Condition_Combination* combination = condition->mutable_combination(); |
| 260 | combination->set_operation(LogicalOperation::OR); |
| 261 | combination->add_condition("SCREEN_IS_ON"); |
| 262 | combination->add_condition("SCREEN_IS_EITHER_ON_OFF"); |
| 263 | |
| 264 | return config; |
| 265 | } |
| 266 | |
| 267 | TEST(MetricsManagerTest, TestGoodConfig) { |
| 268 | StatsdConfig config = buildGoodConfig(); |
| 269 | set<int> allTagIds; |
| 270 | vector<sp<LogMatchingTracker>> allLogEntryMatchers; |
| 271 | vector<sp<ConditionTracker>> allConditionTrackers; |
| 272 | vector<sp<MetricProducer>> allMetricProducers; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 273 | std::vector<sp<AnomalyTracker>> allAnomalyTrackers; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 274 | unordered_map<int, std::vector<int>> conditionToMetricMap; |
| 275 | unordered_map<int, std::vector<int>> trackerToMetricMap; |
| 276 | unordered_map<int, std::vector<int>> trackerToConditionMap; |
| 277 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 278 | EXPECT_TRUE(initStatsdConfig(kConfigKey, config, allTagIds, allLogEntryMatchers, |
| 279 | allConditionTrackers, allMetricProducers, allAnomalyTrackers, |
| 280 | conditionToMetricMap, trackerToMetricMap, trackerToConditionMap)); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 281 | EXPECT_EQ(1u, allMetricProducers.size()); |
| 282 | EXPECT_EQ(1u, allAnomalyTrackers.size()); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 285 | TEST(MetricsManagerTest, TestDimensionMetricsWithMultiTags) { |
| 286 | StatsdConfig config = buildDimensionMetricsWithMultiTags(); |
| 287 | set<int> allTagIds; |
| 288 | vector<sp<LogMatchingTracker>> allLogEntryMatchers; |
| 289 | vector<sp<ConditionTracker>> allConditionTrackers; |
| 290 | vector<sp<MetricProducer>> allMetricProducers; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 291 | std::vector<sp<AnomalyTracker>> allAnomalyTrackers; |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 292 | unordered_map<int, std::vector<int>> conditionToMetricMap; |
| 293 | unordered_map<int, std::vector<int>> trackerToMetricMap; |
| 294 | unordered_map<int, std::vector<int>> trackerToConditionMap; |
| 295 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 296 | EXPECT_FALSE(initStatsdConfig(kConfigKey, config, allTagIds, allLogEntryMatchers, |
| 297 | allConditionTrackers, allMetricProducers, allAnomalyTrackers, |
| 298 | conditionToMetricMap, trackerToMetricMap, trackerToConditionMap)); |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 301 | TEST(MetricsManagerTest, TestCircleLogMatcherDependency) { |
| 302 | StatsdConfig config = buildCircleMatchers(); |
| 303 | set<int> allTagIds; |
| 304 | vector<sp<LogMatchingTracker>> allLogEntryMatchers; |
| 305 | vector<sp<ConditionTracker>> allConditionTrackers; |
| 306 | vector<sp<MetricProducer>> allMetricProducers; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 307 | std::vector<sp<AnomalyTracker>> allAnomalyTrackers; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 308 | unordered_map<int, std::vector<int>> conditionToMetricMap; |
| 309 | unordered_map<int, std::vector<int>> trackerToMetricMap; |
| 310 | unordered_map<int, std::vector<int>> trackerToConditionMap; |
| 311 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 312 | EXPECT_FALSE(initStatsdConfig(kConfigKey, config, allTagIds, allLogEntryMatchers, |
| 313 | allConditionTrackers, allMetricProducers, allAnomalyTrackers, |
| 314 | conditionToMetricMap, trackerToMetricMap, trackerToConditionMap)); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | TEST(MetricsManagerTest, TestMissingMatchers) { |
| 318 | StatsdConfig config = buildMissingMatchers(); |
| 319 | set<int> allTagIds; |
| 320 | vector<sp<LogMatchingTracker>> allLogEntryMatchers; |
| 321 | vector<sp<ConditionTracker>> allConditionTrackers; |
| 322 | vector<sp<MetricProducer>> allMetricProducers; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 323 | std::vector<sp<AnomalyTracker>> allAnomalyTrackers; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 324 | unordered_map<int, std::vector<int>> conditionToMetricMap; |
| 325 | unordered_map<int, std::vector<int>> trackerToMetricMap; |
| 326 | unordered_map<int, std::vector<int>> trackerToConditionMap; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 327 | EXPECT_FALSE(initStatsdConfig(kConfigKey, config, allTagIds, allLogEntryMatchers, |
| 328 | allConditionTrackers, allMetricProducers, allAnomalyTrackers, |
| 329 | conditionToMetricMap, trackerToMetricMap, trackerToConditionMap)); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Yao Chen | 10535b9 | 2017-11-27 11:31:55 -0800 | [diff] [blame] | 332 | TEST(MetricsManagerTest, TestMissingCondition) { |
| 333 | StatsdConfig config = buildMissingCondition(); |
| 334 | set<int> allTagIds; |
| 335 | vector<sp<LogMatchingTracker>> allLogEntryMatchers; |
| 336 | vector<sp<ConditionTracker>> allConditionTrackers; |
| 337 | vector<sp<MetricProducer>> allMetricProducers; |
| 338 | std::vector<sp<AnomalyTracker>> allAnomalyTrackers; |
| 339 | unordered_map<int, std::vector<int>> conditionToMetricMap; |
| 340 | unordered_map<int, std::vector<int>> trackerToMetricMap; |
| 341 | unordered_map<int, std::vector<int>> trackerToConditionMap; |
| 342 | EXPECT_FALSE(initStatsdConfig(kConfigKey, config, allTagIds, allLogEntryMatchers, |
| 343 | allConditionTrackers, allMetricProducers, allAnomalyTrackers, |
| 344 | conditionToMetricMap, trackerToMetricMap, trackerToConditionMap)); |
| 345 | } |
| 346 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 347 | TEST(MetricsManagerTest, TestCircleConditionDependency) { |
| 348 | StatsdConfig config = buildCircleConditions(); |
| 349 | set<int> allTagIds; |
| 350 | vector<sp<LogMatchingTracker>> allLogEntryMatchers; |
| 351 | vector<sp<ConditionTracker>> allConditionTrackers; |
| 352 | vector<sp<MetricProducer>> allMetricProducers; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 353 | std::vector<sp<AnomalyTracker>> allAnomalyTrackers; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 354 | unordered_map<int, std::vector<int>> conditionToMetricMap; |
| 355 | unordered_map<int, std::vector<int>> trackerToMetricMap; |
| 356 | unordered_map<int, std::vector<int>> trackerToConditionMap; |
| 357 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 358 | EXPECT_FALSE(initStatsdConfig(kConfigKey, config, allTagIds, allLogEntryMatchers, |
| 359 | allConditionTrackers, allMetricProducers, allAnomalyTrackers, |
| 360 | conditionToMetricMap, trackerToMetricMap, trackerToConditionMap)); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | TEST(MetricsManagerTest, testAlertWithUnknownMetric) { |
| 364 | StatsdConfig config = buildAlertWithUnknownMetric(); |
| 365 | set<int> allTagIds; |
| 366 | vector<sp<LogMatchingTracker>> allLogEntryMatchers; |
| 367 | vector<sp<ConditionTracker>> allConditionTrackers; |
| 368 | vector<sp<MetricProducer>> allMetricProducers; |
| 369 | std::vector<sp<AnomalyTracker>> allAnomalyTrackers; |
| 370 | unordered_map<int, std::vector<int>> conditionToMetricMap; |
| 371 | unordered_map<int, std::vector<int>> trackerToMetricMap; |
| 372 | unordered_map<int, std::vector<int>> trackerToConditionMap; |
| 373 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 374 | EXPECT_FALSE(initStatsdConfig(kConfigKey, config, allTagIds, allLogEntryMatchers, |
| 375 | allConditionTrackers, allMetricProducers, allAnomalyTrackers, |
| 376 | conditionToMetricMap, trackerToMetricMap, trackerToConditionMap)); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | #else |
| 380 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 381 | #endif |