blob: 2fe5d66fa9f954206d1eb19ab4cad32c57df75c1 [file] [log] [blame]
Tianjie Xu98333a82017-09-22 21:29:29 -07001//
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
31using base::TimeDelta;
32using testing::AnyNumber;
33using testing::_;
34
35namespace chromeos_update_engine {
36class 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
50TEST_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
59TEST_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
108TEST_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
120TEST_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 Xu1f93d092017-10-09 12:13:29 -0700136
Tianjie Xu98333a82017-09-22 21:29:29 -0700137 metrics::AttemptResult attempt_result =
138 metrics::AttemptResult::kInternalError;
139 ErrorCode internal_error_code = ErrorCode::kDownloadInvalidMetadataSignature;
Tianjie Xu98333a82017-09-22 21:29:29 -0700140
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 Xu98333a82017-09-22 21:29:29 -0700164
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 Xu1f93d092017-10-09 12:13:29 -0700177 SendToUMA(metrics::kMetricAttemptPayloadSizeMiB, 100, _, _, _))
Tianjie Xu98333a82017-09-22 21:29:29 -0700178 .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 Xu98333a82017-09-22 21:29:29 -0700191 reporter_.ReportUpdateAttemptMetrics(&fake_system_state,
192 attempt_number,
193 payload_type,
194 duration,
195 duration_uptime,
196 payload_size,
Tianjie Xu98333a82017-09-22 21:29:29 -0700197 attempt_result,
Tianjie Xu1f93d092017-10-09 12:13:29 -0700198 internal_error_code);
Tianjie Xu98333a82017-09-22 21:29:29 -0700199
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 Xu98333a82017-09-22 21:29:29 -0700209 attempt_result,
Tianjie Xu1f93d092017-10-09 12:13:29 -0700210 internal_error_code);
211}
212
213TEST_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 Xu98333a82017-09-22 21:29:29 -0700249}
250
251TEST_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);
Sen Jiang8712e962018-05-08 12:12:28 -0700261 TimeDelta total_duration_uptime = TimeDelta::FromMinutes(20);
Tianjie Xu98333a82017-09-22 21:29:29 -0700262 int reboot_count = 2;
263 int url_switch_count = 2;
264
265 EXPECT_CALL(
266 *mock_metrics_lib_,
267 SendToUMA(metrics::kMetricSuccessfulUpdatePayloadSizeMiB, 200, _, _, _))
268 .Times(1);
269
270 // Check the report to both BytesDownloadedMiBHttpsServer and
271 // BytesDownloadedMiB
272 std::string DownloadedMiBMetric =
273 metrics::kMetricSuccessfulUpdateBytesDownloadedMiB;
274 DownloadedMiBMetric += "HttpsServer";
275 EXPECT_CALL(*mock_metrics_lib_, SendToUMA(DownloadedMiBMetric, 200, _, _, _))
276 .Times(1);
277 EXPECT_CALL(
278 *mock_metrics_lib_,
279 SendToUMA(
280 metrics::kMetricSuccessfulUpdateBytesDownloadedMiB, 200, _, _, _))
281 .Times(1);
282
283 EXPECT_CALL(
284 *mock_metrics_lib_,
285 SendToUMA(
286 metrics::kMetricSuccessfulUpdateDownloadSourcesUsed, 1, _, _, _))
287 .Times(1);
288 EXPECT_CALL(
289 *mock_metrics_lib_,
290 SendToUMA(metrics::kMetricSuccessfulUpdateDownloadOverheadPercentage,
291 20,
292 _,
293 _,
294 _));
295
296 EXPECT_CALL(*mock_metrics_lib_,
297 SendToUMA(metrics::kMetricSuccessfulUpdateUrlSwitchCount,
298 url_switch_count,
299 _,
300 _,
301 _))
302 .Times(1);
303 EXPECT_CALL(
304 *mock_metrics_lib_,
305 SendToUMA(
306 metrics::kMetricSuccessfulUpdateTotalDurationMinutes, 30, _, _, _))
307 .Times(1);
308 EXPECT_CALL(
309 *mock_metrics_lib_,
Sen Jiang8712e962018-05-08 12:12:28 -0700310 SendToUMA(metrics::kMetricSuccessfulUpdateTotalDurationUptimeMinutes,
311 20,
312 _,
313 _,
314 _))
315 .Times(1);
316 EXPECT_CALL(
317 *mock_metrics_lib_,
Tianjie Xu98333a82017-09-22 21:29:29 -0700318 SendToUMA(
319 metrics::kMetricSuccessfulUpdateRebootCount, reboot_count, _, _, _))
320 .Times(1);
321 EXPECT_CALL(*mock_metrics_lib_,
322 SendEnumToUMA(
323 metrics::kMetricSuccessfulUpdatePayloadType, payload_type, _))
324 .Times(1);
325 EXPECT_CALL(
326 *mock_metrics_lib_,
327 SendToUMA(
328 metrics::kMetricSuccessfulUpdateAttemptCount, attempt_count, _, _, _))
329 .Times(1);
330 EXPECT_CALL(*mock_metrics_lib_,
331 SendToUMA(metrics::kMetricSuccessfulUpdateUpdatesAbandonedCount,
332 updates_abandoned_count,
333 _,
334 _,
335 _))
336 .Times(1);
337
338 reporter_.ReportSuccessfulUpdateMetrics(attempt_count,
339 updates_abandoned_count,
340 payload_type,
341 payload_size,
342 num_bytes_downloaded,
343 download_overhead_percentage,
344 total_duration,
Sen Jiang8712e962018-05-08 12:12:28 -0700345 total_duration_uptime,
Tianjie Xu98333a82017-09-22 21:29:29 -0700346 reboot_count,
347 url_switch_count);
348}
349
350TEST_F(MetricsReporterOmahaTest, ReportRollbackMetrics) {
351 metrics::RollbackResult result = metrics::RollbackResult::kSuccess;
352 EXPECT_CALL(*mock_metrics_lib_,
353 SendEnumToUMA(
354 metrics::kMetricRollbackResult, static_cast<int>(result), _))
355 .Times(1);
356
357 reporter_.ReportRollbackMetrics(result);
358}
359
360TEST_F(MetricsReporterOmahaTest, ReportCertificateCheckMetrics) {
361 ServerToCheck server_to_check = ServerToCheck::kUpdate;
362 CertificateCheckResult result = CertificateCheckResult::kValid;
363 EXPECT_CALL(*mock_metrics_lib_,
364 SendEnumToUMA(metrics::kMetricCertificateCheckUpdateCheck,
365 static_cast<int>(result),
366 _))
367 .Times(1);
368
369 reporter_.ReportCertificateCheckMetrics(server_to_check, result);
370}
371
372TEST_F(MetricsReporterOmahaTest, ReportFailedUpdateCount) {
373 int target_attempt = 3;
374 EXPECT_CALL(
375 *mock_metrics_lib_,
376 SendToUMA(metrics::kMetricFailedUpdateCount, target_attempt, _, _, _))
377 .Times(1);
378
379 reporter_.ReportFailedUpdateCount(target_attempt);
380}
381
382TEST_F(MetricsReporterOmahaTest, ReportTimeToReboot) {
383 int time_to_reboot_minutes = 1000;
384 EXPECT_CALL(
385 *mock_metrics_lib_,
386 SendToUMA(
387 metrics::kMetricTimeToRebootMinutes, time_to_reboot_minutes, _, _, _))
388 .Times(1);
389
390 reporter_.ReportTimeToReboot(time_to_reboot_minutes);
391}
392
393TEST_F(MetricsReporterOmahaTest, ReportInstallDateProvisioningSource) {
394 int source = 2;
395 int max = 5;
396 EXPECT_CALL(
397 *mock_metrics_lib_,
398 SendEnumToUMA(metrics::kMetricInstallDateProvisioningSource, source, max))
399 .Times(1);
400
401 reporter_.ReportInstallDateProvisioningSource(source, max);
402}
403
404} // namespace chromeos_update_engine