Tianjie Xu | 98333a8 | 2017-09-22 21:29:29 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2017 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | #include "update_engine/metrics_reporter_omaha.h" |
| 18 | |
| 19 | #include <memory> |
| 20 | #include <string> |
| 21 | |
| 22 | #include <base/time/time.h> |
| 23 | #include <gmock/gmock.h> |
| 24 | #include <gtest/gtest.h> |
| 25 | #include <metrics/metrics_library_mock.h> |
| 26 | |
| 27 | #include "update_engine/common/fake_clock.h" |
| 28 | #include "update_engine/common/fake_prefs.h" |
| 29 | #include "update_engine/fake_system_state.h" |
| 30 | |
| 31 | using base::TimeDelta; |
| 32 | using testing::AnyNumber; |
| 33 | using testing::_; |
| 34 | |
| 35 | namespace chromeos_update_engine { |
| 36 | class MetricsReporterOmahaTest : public ::testing::Test { |
| 37 | protected: |
| 38 | MetricsReporterOmahaTest() = default; |
| 39 | |
| 40 | // Reset the metrics_lib_ to a mock library. |
| 41 | void SetUp() override { |
| 42 | mock_metrics_lib_ = new testing::NiceMock<MetricsLibraryMock>(); |
| 43 | reporter_.metrics_lib_.reset(mock_metrics_lib_); |
| 44 | } |
| 45 | |
| 46 | testing::NiceMock<MetricsLibraryMock>* mock_metrics_lib_; |
| 47 | MetricsReporterOmaha reporter_; |
| 48 | }; |
| 49 | |
| 50 | TEST_F(MetricsReporterOmahaTest, ReportDailyMetrics) { |
| 51 | TimeDelta age = TimeDelta::FromDays(10); |
| 52 | EXPECT_CALL(*mock_metrics_lib_, |
| 53 | SendToUMA(metrics::kMetricDailyOSAgeDays, _, _, _, _)) |
| 54 | .Times(1); |
| 55 | |
| 56 | reporter_.ReportDailyMetrics(age); |
| 57 | } |
| 58 | |
| 59 | TEST_F(MetricsReporterOmahaTest, ReportUpdateCheckMetrics) { |
| 60 | FakeSystemState fake_system_state; |
| 61 | FakeClock fake_clock; |
| 62 | FakePrefs fake_prefs; |
| 63 | |
| 64 | // We need to execute the report twice to test the time since last report. |
| 65 | fake_system_state.set_clock(&fake_clock); |
| 66 | fake_system_state.set_prefs(&fake_prefs); |
| 67 | fake_clock.SetWallclockTime(base::Time::FromInternalValue(1000000)); |
| 68 | fake_clock.SetMonotonicTime(base::Time::FromInternalValue(1000000)); |
| 69 | |
| 70 | metrics::CheckResult result = metrics::CheckResult::kUpdateAvailable; |
| 71 | metrics::CheckReaction reaction = metrics::CheckReaction::kIgnored; |
| 72 | metrics::DownloadErrorCode error_code = |
| 73 | metrics::DownloadErrorCode::kHttpStatus200; |
| 74 | |
| 75 | EXPECT_CALL( |
| 76 | *mock_metrics_lib_, |
| 77 | SendEnumToUMA(metrics::kMetricCheckResult, static_cast<int>(result), _)) |
| 78 | .Times(2); |
| 79 | EXPECT_CALL(*mock_metrics_lib_, |
| 80 | SendEnumToUMA( |
| 81 | metrics::kMetricCheckReaction, static_cast<int>(reaction), _)) |
| 82 | .Times(2); |
| 83 | EXPECT_CALL(*mock_metrics_lib_, |
| 84 | SendSparseToUMA(metrics::kMetricCheckDownloadErrorCode, |
| 85 | static_cast<int>(error_code))) |
| 86 | .Times(2); |
| 87 | |
| 88 | EXPECT_CALL( |
| 89 | *mock_metrics_lib_, |
| 90 | SendToUMA(metrics::kMetricCheckTimeSinceLastCheckMinutes, 1, _, _, _)) |
| 91 | .Times(1); |
| 92 | EXPECT_CALL( |
| 93 | *mock_metrics_lib_, |
| 94 | SendToUMA( |
| 95 | metrics::kMetricCheckTimeSinceLastCheckUptimeMinutes, 1, _, _, _)) |
| 96 | .Times(1); |
| 97 | |
| 98 | reporter_.ReportUpdateCheckMetrics( |
| 99 | &fake_system_state, result, reaction, error_code); |
| 100 | |
| 101 | // Advance the clock by 1 minute and report the same metrics again. |
| 102 | fake_clock.SetWallclockTime(base::Time::FromInternalValue(61000000)); |
| 103 | fake_clock.SetMonotonicTime(base::Time::FromInternalValue(61000000)); |
| 104 | reporter_.ReportUpdateCheckMetrics( |
| 105 | &fake_system_state, result, reaction, error_code); |
| 106 | } |
| 107 | |
| 108 | TEST_F(MetricsReporterOmahaTest, |
| 109 | ReportAbnormallyTerminatedUpdateAttemptMetrics) { |
| 110 | EXPECT_CALL(*mock_metrics_lib_, |
| 111 | SendEnumToUMA(metrics::kMetricAttemptResult, |
| 112 | static_cast<int>( |
| 113 | metrics::AttemptResult::kAbnormalTermination), |
| 114 | _)) |
| 115 | .Times(1); |
| 116 | |
| 117 | reporter_.ReportAbnormallyTerminatedUpdateAttemptMetrics(); |
| 118 | } |
| 119 | |
| 120 | TEST_F(MetricsReporterOmahaTest, ReportUpdateAttemptMetrics) { |
| 121 | FakeSystemState fake_system_state; |
| 122 | FakeClock fake_clock; |
| 123 | FakePrefs fake_prefs; |
| 124 | |
| 125 | fake_system_state.set_clock(&fake_clock); |
| 126 | fake_system_state.set_prefs(&fake_prefs); |
| 127 | fake_clock.SetWallclockTime(base::Time::FromInternalValue(1000000)); |
| 128 | fake_clock.SetMonotonicTime(base::Time::FromInternalValue(1000000)); |
| 129 | |
| 130 | int attempt_number = 1; |
| 131 | PayloadType payload_type = kPayloadTypeFull; |
| 132 | TimeDelta duration = TimeDelta::FromMinutes(1000); |
| 133 | TimeDelta duration_uptime = TimeDelta::FromMinutes(1000); |
| 134 | |
| 135 | int64_t payload_size = 100 * kNumBytesInOneMiB; |
Tianjie Xu | 1f93d09 | 2017-10-09 12:13:29 -0700 | [diff] [blame^] | 136 | |
Tianjie Xu | 98333a8 | 2017-09-22 21:29:29 -0700 | [diff] [blame] | 137 | metrics::AttemptResult attempt_result = |
| 138 | metrics::AttemptResult::kInternalError; |
| 139 | ErrorCode internal_error_code = ErrorCode::kDownloadInvalidMetadataSignature; |
Tianjie Xu | 98333a8 | 2017-09-22 21:29:29 -0700 | [diff] [blame] | 140 | |
| 141 | EXPECT_CALL(*mock_metrics_lib_, |
| 142 | SendToUMA(metrics::kMetricAttemptNumber, attempt_number, _, _, _)) |
| 143 | .Times(2); |
| 144 | EXPECT_CALL(*mock_metrics_lib_, |
| 145 | SendEnumToUMA(metrics::kMetricAttemptPayloadType, |
| 146 | static_cast<int>(payload_type), |
| 147 | _)) |
| 148 | .Times(2); |
| 149 | EXPECT_CALL(*mock_metrics_lib_, |
| 150 | SendToUMA(metrics::kMetricAttemptDurationMinutes, |
| 151 | duration.InMinutes(), |
| 152 | _, |
| 153 | _, |
| 154 | _)) |
| 155 | .Times(2); |
| 156 | EXPECT_CALL(*mock_metrics_lib_, |
| 157 | SendToUMA(metrics::kMetricAttemptDurationUptimeMinutes, |
| 158 | duration_uptime.InMinutes(), |
| 159 | _, |
| 160 | _, |
| 161 | _)) |
| 162 | .Times(2); |
| 163 | |
Tianjie Xu | 98333a8 | 2017-09-22 21:29:29 -0700 | [diff] [blame] | 164 | |
| 165 | // Check the report of attempt result. |
| 166 | EXPECT_CALL( |
| 167 | *mock_metrics_lib_, |
| 168 | SendEnumToUMA( |
| 169 | metrics::kMetricAttemptResult, static_cast<int>(attempt_result), _)) |
| 170 | .Times(2); |
| 171 | EXPECT_CALL(*mock_metrics_lib_, |
| 172 | SendEnumToUMA(metrics::kMetricAttemptInternalErrorCode, |
| 173 | static_cast<int>(internal_error_code), |
| 174 | _)) |
| 175 | .Times(2); |
| 176 | EXPECT_CALL(*mock_metrics_lib_, |
Tianjie Xu | 1f93d09 | 2017-10-09 12:13:29 -0700 | [diff] [blame^] | 177 | SendToUMA(metrics::kMetricAttemptPayloadSizeMiB, 100, _, _, _)) |
Tianjie Xu | 98333a8 | 2017-09-22 21:29:29 -0700 | [diff] [blame] | 178 | .Times(2); |
| 179 | |
| 180 | // Check the duration between two reports. |
| 181 | EXPECT_CALL( |
| 182 | *mock_metrics_lib_, |
| 183 | SendToUMA(metrics::kMetricAttemptTimeSinceLastAttemptMinutes, 1, _, _, _)) |
| 184 | .Times(1); |
| 185 | EXPECT_CALL( |
| 186 | *mock_metrics_lib_, |
| 187 | SendToUMA( |
| 188 | metrics::kMetricAttemptTimeSinceLastAttemptUptimeMinutes, 1, _, _, _)) |
| 189 | .Times(1); |
| 190 | |
Tianjie Xu | 98333a8 | 2017-09-22 21:29:29 -0700 | [diff] [blame] | 191 | reporter_.ReportUpdateAttemptMetrics(&fake_system_state, |
| 192 | attempt_number, |
| 193 | payload_type, |
| 194 | duration, |
| 195 | duration_uptime, |
| 196 | payload_size, |
Tianjie Xu | 98333a8 | 2017-09-22 21:29:29 -0700 | [diff] [blame] | 197 | attempt_result, |
Tianjie Xu | 1f93d09 | 2017-10-09 12:13:29 -0700 | [diff] [blame^] | 198 | internal_error_code); |
Tianjie Xu | 98333a8 | 2017-09-22 21:29:29 -0700 | [diff] [blame] | 199 | |
| 200 | // Advance the clock by 1 minute and report the same metrics again. |
| 201 | fake_clock.SetWallclockTime(base::Time::FromInternalValue(61000000)); |
| 202 | fake_clock.SetMonotonicTime(base::Time::FromInternalValue(61000000)); |
| 203 | reporter_.ReportUpdateAttemptMetrics(&fake_system_state, |
| 204 | attempt_number, |
| 205 | payload_type, |
| 206 | duration, |
| 207 | duration_uptime, |
| 208 | payload_size, |
Tianjie Xu | 98333a8 | 2017-09-22 21:29:29 -0700 | [diff] [blame] | 209 | attempt_result, |
Tianjie Xu | 1f93d09 | 2017-10-09 12:13:29 -0700 | [diff] [blame^] | 210 | internal_error_code); |
| 211 | } |
| 212 | |
| 213 | TEST_F(MetricsReporterOmahaTest, ReportUpdateAttemptDownloadMetrics) { |
| 214 | int64_t payload_bytes_downloaded = 200 * kNumBytesInOneMiB; |
| 215 | int64_t payload_download_speed_bps = 100 * 1000; |
| 216 | DownloadSource download_source = kDownloadSourceHttpServer; |
| 217 | metrics::DownloadErrorCode payload_download_error_code = |
| 218 | metrics::DownloadErrorCode::kDownloadError; |
| 219 | metrics::ConnectionType connection_type = metrics::ConnectionType::kCellular; |
| 220 | |
| 221 | EXPECT_CALL( |
| 222 | *mock_metrics_lib_, |
| 223 | SendToUMA(metrics::kMetricAttemptPayloadBytesDownloadedMiB, 200, _, _, _)) |
| 224 | .Times(1); |
| 225 | EXPECT_CALL( |
| 226 | *mock_metrics_lib_, |
| 227 | SendToUMA(metrics::kMetricAttemptPayloadDownloadSpeedKBps, 100, _, _, _)) |
| 228 | .Times(1); |
| 229 | EXPECT_CALL(*mock_metrics_lib_, |
| 230 | SendEnumToUMA(metrics::kMetricAttemptDownloadSource, |
| 231 | static_cast<int>(download_source), |
| 232 | _)) |
| 233 | .Times(1); |
| 234 | EXPECT_CALL(*mock_metrics_lib_, |
| 235 | SendSparseToUMA(metrics::kMetricAttemptDownloadErrorCode, |
| 236 | static_cast<int>(payload_download_error_code))) |
| 237 | .Times(1); |
| 238 | EXPECT_CALL(*mock_metrics_lib_, |
| 239 | SendEnumToUMA(metrics::kMetricAttemptConnectionType, |
| 240 | static_cast<int>(connection_type), |
| 241 | _)) |
| 242 | .Times(1); |
| 243 | |
| 244 | reporter_.ReportUpdateAttemptDownloadMetrics(payload_bytes_downloaded, |
| 245 | payload_download_speed_bps, |
| 246 | download_source, |
| 247 | payload_download_error_code, |
| 248 | connection_type); |
Tianjie Xu | 98333a8 | 2017-09-22 21:29:29 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | TEST_F(MetricsReporterOmahaTest, ReportSuccessfulUpdateMetrics) { |
| 252 | int attempt_count = 3; |
| 253 | int updates_abandoned_count = 2; |
| 254 | PayloadType payload_type = kPayloadTypeDelta; |
| 255 | int64_t payload_size = 200 * kNumBytesInOneMiB; |
| 256 | int64_t num_bytes_downloaded[kNumDownloadSources] = {}; |
| 257 | // 200MiB payload downloaded from HttpsServer. |
| 258 | num_bytes_downloaded[0] = 200 * kNumBytesInOneMiB; |
| 259 | int download_overhead_percentage = 20; |
| 260 | TimeDelta total_duration = TimeDelta::FromMinutes(30); |
| 261 | int reboot_count = 2; |
| 262 | int url_switch_count = 2; |
| 263 | |
| 264 | EXPECT_CALL( |
| 265 | *mock_metrics_lib_, |
| 266 | SendToUMA(metrics::kMetricSuccessfulUpdatePayloadSizeMiB, 200, _, _, _)) |
| 267 | .Times(1); |
| 268 | |
| 269 | // Check the report to both BytesDownloadedMiBHttpsServer and |
| 270 | // BytesDownloadedMiB |
| 271 | std::string DownloadedMiBMetric = |
| 272 | metrics::kMetricSuccessfulUpdateBytesDownloadedMiB; |
| 273 | DownloadedMiBMetric += "HttpsServer"; |
| 274 | EXPECT_CALL(*mock_metrics_lib_, SendToUMA(DownloadedMiBMetric, 200, _, _, _)) |
| 275 | .Times(1); |
| 276 | EXPECT_CALL( |
| 277 | *mock_metrics_lib_, |
| 278 | SendToUMA( |
| 279 | metrics::kMetricSuccessfulUpdateBytesDownloadedMiB, 200, _, _, _)) |
| 280 | .Times(1); |
| 281 | |
| 282 | EXPECT_CALL( |
| 283 | *mock_metrics_lib_, |
| 284 | SendToUMA( |
| 285 | metrics::kMetricSuccessfulUpdateDownloadSourcesUsed, 1, _, _, _)) |
| 286 | .Times(1); |
| 287 | EXPECT_CALL( |
| 288 | *mock_metrics_lib_, |
| 289 | SendToUMA(metrics::kMetricSuccessfulUpdateDownloadOverheadPercentage, |
| 290 | 20, |
| 291 | _, |
| 292 | _, |
| 293 | _)); |
| 294 | |
| 295 | EXPECT_CALL(*mock_metrics_lib_, |
| 296 | SendToUMA(metrics::kMetricSuccessfulUpdateUrlSwitchCount, |
| 297 | url_switch_count, |
| 298 | _, |
| 299 | _, |
| 300 | _)) |
| 301 | .Times(1); |
| 302 | EXPECT_CALL( |
| 303 | *mock_metrics_lib_, |
| 304 | SendToUMA( |
| 305 | metrics::kMetricSuccessfulUpdateTotalDurationMinutes, 30, _, _, _)) |
| 306 | .Times(1); |
| 307 | EXPECT_CALL( |
| 308 | *mock_metrics_lib_, |
| 309 | SendToUMA( |
| 310 | metrics::kMetricSuccessfulUpdateRebootCount, reboot_count, _, _, _)) |
| 311 | .Times(1); |
| 312 | EXPECT_CALL(*mock_metrics_lib_, |
| 313 | SendEnumToUMA( |
| 314 | metrics::kMetricSuccessfulUpdatePayloadType, payload_type, _)) |
| 315 | .Times(1); |
| 316 | EXPECT_CALL( |
| 317 | *mock_metrics_lib_, |
| 318 | SendToUMA( |
| 319 | metrics::kMetricSuccessfulUpdateAttemptCount, attempt_count, _, _, _)) |
| 320 | .Times(1); |
| 321 | EXPECT_CALL(*mock_metrics_lib_, |
| 322 | SendToUMA(metrics::kMetricSuccessfulUpdateUpdatesAbandonedCount, |
| 323 | updates_abandoned_count, |
| 324 | _, |
| 325 | _, |
| 326 | _)) |
| 327 | .Times(1); |
| 328 | |
| 329 | reporter_.ReportSuccessfulUpdateMetrics(attempt_count, |
| 330 | updates_abandoned_count, |
| 331 | payload_type, |
| 332 | payload_size, |
| 333 | num_bytes_downloaded, |
| 334 | download_overhead_percentage, |
| 335 | total_duration, |
| 336 | reboot_count, |
| 337 | url_switch_count); |
| 338 | } |
| 339 | |
| 340 | TEST_F(MetricsReporterOmahaTest, ReportRollbackMetrics) { |
| 341 | metrics::RollbackResult result = metrics::RollbackResult::kSuccess; |
| 342 | EXPECT_CALL(*mock_metrics_lib_, |
| 343 | SendEnumToUMA( |
| 344 | metrics::kMetricRollbackResult, static_cast<int>(result), _)) |
| 345 | .Times(1); |
| 346 | |
| 347 | reporter_.ReportRollbackMetrics(result); |
| 348 | } |
| 349 | |
| 350 | TEST_F(MetricsReporterOmahaTest, ReportCertificateCheckMetrics) { |
| 351 | ServerToCheck server_to_check = ServerToCheck::kUpdate; |
| 352 | CertificateCheckResult result = CertificateCheckResult::kValid; |
| 353 | EXPECT_CALL(*mock_metrics_lib_, |
| 354 | SendEnumToUMA(metrics::kMetricCertificateCheckUpdateCheck, |
| 355 | static_cast<int>(result), |
| 356 | _)) |
| 357 | .Times(1); |
| 358 | |
| 359 | reporter_.ReportCertificateCheckMetrics(server_to_check, result); |
| 360 | } |
| 361 | |
| 362 | TEST_F(MetricsReporterOmahaTest, ReportFailedUpdateCount) { |
| 363 | int target_attempt = 3; |
| 364 | EXPECT_CALL( |
| 365 | *mock_metrics_lib_, |
| 366 | SendToUMA(metrics::kMetricFailedUpdateCount, target_attempt, _, _, _)) |
| 367 | .Times(1); |
| 368 | |
| 369 | reporter_.ReportFailedUpdateCount(target_attempt); |
| 370 | } |
| 371 | |
| 372 | TEST_F(MetricsReporterOmahaTest, ReportTimeToReboot) { |
| 373 | int time_to_reboot_minutes = 1000; |
| 374 | EXPECT_CALL( |
| 375 | *mock_metrics_lib_, |
| 376 | SendToUMA( |
| 377 | metrics::kMetricTimeToRebootMinutes, time_to_reboot_minutes, _, _, _)) |
| 378 | .Times(1); |
| 379 | |
| 380 | reporter_.ReportTimeToReboot(time_to_reboot_minutes); |
| 381 | } |
| 382 | |
| 383 | TEST_F(MetricsReporterOmahaTest, ReportInstallDateProvisioningSource) { |
| 384 | int source = 2; |
| 385 | int max = 5; |
| 386 | EXPECT_CALL( |
| 387 | *mock_metrics_lib_, |
| 388 | SendEnumToUMA(metrics::kMetricInstallDateProvisioningSource, source, max)) |
| 389 | .Times(1); |
| 390 | |
| 391 | reporter_.ReportInstallDateProvisioningSource(source, max); |
| 392 | } |
| 393 | |
| 394 | } // namespace chromeos_update_engine |