blob: 7a4102ef388ac2a86806d0e88bbb0efb16575b12 [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;
136 int64_t payload_bytes_downloaded = 200 * kNumBytesInOneMiB;
137 int64_t payload_download_speed_bps = 100 * 1000;
138 DownloadSource download_source = kDownloadSourceHttpServer;
139 metrics::AttemptResult attempt_result =
140 metrics::AttemptResult::kInternalError;
141 ErrorCode internal_error_code = ErrorCode::kDownloadInvalidMetadataSignature;
142 metrics::DownloadErrorCode payload_download_error_code =
143 metrics::DownloadErrorCode::kDownloadError;
144 metrics::ConnectionType connection_type = metrics::ConnectionType::kCellular;
145
146 EXPECT_CALL(*mock_metrics_lib_,
147 SendToUMA(metrics::kMetricAttemptNumber, attempt_number, _, _, _))
148 .Times(2);
149 EXPECT_CALL(*mock_metrics_lib_,
150 SendEnumToUMA(metrics::kMetricAttemptPayloadType,
151 static_cast<int>(payload_type),
152 _))
153 .Times(2);
154 EXPECT_CALL(*mock_metrics_lib_,
155 SendToUMA(metrics::kMetricAttemptDurationMinutes,
156 duration.InMinutes(),
157 _,
158 _,
159 _))
160 .Times(2);
161 EXPECT_CALL(*mock_metrics_lib_,
162 SendToUMA(metrics::kMetricAttemptDurationUptimeMinutes,
163 duration_uptime.InMinutes(),
164 _,
165 _,
166 _))
167 .Times(2);
168
169 // Check the report of payload download metrics.
170 EXPECT_CALL(*mock_metrics_lib_,
171 SendToUMA(metrics::kMetricAttemptPayloadSizeMiB, 100, _, _, _))
172 .Times(2);
173 EXPECT_CALL(
174 *mock_metrics_lib_,
175 SendToUMA(metrics::kMetricAttemptPayloadBytesDownloadedMiB, 200, _, _, _))
176 .Times(2);
177 EXPECT_CALL(
178 *mock_metrics_lib_,
179 SendToUMA(metrics::kMetricAttemptPayloadDownloadSpeedKBps, 100, _, _, _))
180 .Times(2);
181 EXPECT_CALL(*mock_metrics_lib_,
182 SendEnumToUMA(metrics::kMetricAttemptDownloadSource,
183 static_cast<int>(download_source),
184 _))
185 .Times(2);
186
187 // Check the report of attempt result.
188 EXPECT_CALL(
189 *mock_metrics_lib_,
190 SendEnumToUMA(
191 metrics::kMetricAttemptResult, static_cast<int>(attempt_result), _))
192 .Times(2);
193 EXPECT_CALL(*mock_metrics_lib_,
194 SendEnumToUMA(metrics::kMetricAttemptInternalErrorCode,
195 static_cast<int>(internal_error_code),
196 _))
197 .Times(2);
198 EXPECT_CALL(*mock_metrics_lib_,
199 SendSparseToUMA(metrics::kMetricAttemptDownloadErrorCode,
200 static_cast<int>(payload_download_error_code)))
201 .Times(2);
202
203 // Check the duration between two reports.
204 EXPECT_CALL(
205 *mock_metrics_lib_,
206 SendToUMA(metrics::kMetricAttemptTimeSinceLastAttemptMinutes, 1, _, _, _))
207 .Times(1);
208 EXPECT_CALL(
209 *mock_metrics_lib_,
210 SendToUMA(
211 metrics::kMetricAttemptTimeSinceLastAttemptUptimeMinutes, 1, _, _, _))
212 .Times(1);
213
214 EXPECT_CALL(*mock_metrics_lib_,
215 SendEnumToUMA(metrics::kMetricAttemptConnectionType,
216 static_cast<int>(connection_type),
217 _))
218 .Times(2);
219
220 reporter_.ReportUpdateAttemptMetrics(&fake_system_state,
221 attempt_number,
222 payload_type,
223 duration,
224 duration_uptime,
225 payload_size,
226 payload_bytes_downloaded,
227 payload_download_speed_bps,
228 download_source,
229 attempt_result,
230 internal_error_code,
231 payload_download_error_code,
232 connection_type);
233
234 // Advance the clock by 1 minute and report the same metrics again.
235 fake_clock.SetWallclockTime(base::Time::FromInternalValue(61000000));
236 fake_clock.SetMonotonicTime(base::Time::FromInternalValue(61000000));
237 reporter_.ReportUpdateAttemptMetrics(&fake_system_state,
238 attempt_number,
239 payload_type,
240 duration,
241 duration_uptime,
242 payload_size,
243 payload_bytes_downloaded,
244 payload_download_speed_bps,
245 download_source,
246 attempt_result,
247 internal_error_code,
248 payload_download_error_code,
249 connection_type);
250}
251
252TEST_F(MetricsReporterOmahaTest, ReportSuccessfulUpdateMetrics) {
253 int attempt_count = 3;
254 int updates_abandoned_count = 2;
255 PayloadType payload_type = kPayloadTypeDelta;
256 int64_t payload_size = 200 * kNumBytesInOneMiB;
257 int64_t num_bytes_downloaded[kNumDownloadSources] = {};
258 // 200MiB payload downloaded from HttpsServer.
259 num_bytes_downloaded[0] = 200 * kNumBytesInOneMiB;
260 int download_overhead_percentage = 20;
261 TimeDelta total_duration = TimeDelta::FromMinutes(30);
262 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_,
310 SendToUMA(
311 metrics::kMetricSuccessfulUpdateRebootCount, reboot_count, _, _, _))
312 .Times(1);
313 EXPECT_CALL(*mock_metrics_lib_,
314 SendEnumToUMA(
315 metrics::kMetricSuccessfulUpdatePayloadType, payload_type, _))
316 .Times(1);
317 EXPECT_CALL(
318 *mock_metrics_lib_,
319 SendToUMA(
320 metrics::kMetricSuccessfulUpdateAttemptCount, attempt_count, _, _, _))
321 .Times(1);
322 EXPECT_CALL(*mock_metrics_lib_,
323 SendToUMA(metrics::kMetricSuccessfulUpdateUpdatesAbandonedCount,
324 updates_abandoned_count,
325 _,
326 _,
327 _))
328 .Times(1);
329
330 reporter_.ReportSuccessfulUpdateMetrics(attempt_count,
331 updates_abandoned_count,
332 payload_type,
333 payload_size,
334 num_bytes_downloaded,
335 download_overhead_percentage,
336 total_duration,
337 reboot_count,
338 url_switch_count);
339}
340
341TEST_F(MetricsReporterOmahaTest, ReportRollbackMetrics) {
342 metrics::RollbackResult result = metrics::RollbackResult::kSuccess;
343 EXPECT_CALL(*mock_metrics_lib_,
344 SendEnumToUMA(
345 metrics::kMetricRollbackResult, static_cast<int>(result), _))
346 .Times(1);
347
348 reporter_.ReportRollbackMetrics(result);
349}
350
351TEST_F(MetricsReporterOmahaTest, ReportCertificateCheckMetrics) {
352 ServerToCheck server_to_check = ServerToCheck::kUpdate;
353 CertificateCheckResult result = CertificateCheckResult::kValid;
354 EXPECT_CALL(*mock_metrics_lib_,
355 SendEnumToUMA(metrics::kMetricCertificateCheckUpdateCheck,
356 static_cast<int>(result),
357 _))
358 .Times(1);
359
360 reporter_.ReportCertificateCheckMetrics(server_to_check, result);
361}
362
363TEST_F(MetricsReporterOmahaTest, ReportFailedUpdateCount) {
364 int target_attempt = 3;
365 EXPECT_CALL(
366 *mock_metrics_lib_,
367 SendToUMA(metrics::kMetricFailedUpdateCount, target_attempt, _, _, _))
368 .Times(1);
369
370 reporter_.ReportFailedUpdateCount(target_attempt);
371}
372
373TEST_F(MetricsReporterOmahaTest, ReportTimeToReboot) {
374 int time_to_reboot_minutes = 1000;
375 EXPECT_CALL(
376 *mock_metrics_lib_,
377 SendToUMA(
378 metrics::kMetricTimeToRebootMinutes, time_to_reboot_minutes, _, _, _))
379 .Times(1);
380
381 reporter_.ReportTimeToReboot(time_to_reboot_minutes);
382}
383
384TEST_F(MetricsReporterOmahaTest, ReportInstallDateProvisioningSource) {
385 int source = 2;
386 int max = 5;
387 EXPECT_CALL(
388 *mock_metrics_lib_,
389 SendEnumToUMA(metrics::kMetricInstallDateProvisioningSource, source, max))
390 .Times(1);
391
392 reporter_.ReportInstallDateProvisioningSource(source, max);
393}
394
395} // namespace chromeos_update_engine