-
Notifications
You must be signed in to change notification settings - Fork 19
/
update_manager_unittest.cc
333 lines (292 loc) · 12.1 KB
/
update_manager_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
//
// Copyright (C) 2014 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "update_engine/update_manager/update_manager.h"
#include <unistd.h>
#include <algorithm>
#include <memory>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include <base/bind.h>
#include <base/test/simple_test_clock.h>
#include <base/time/time.h>
#include <brillo/message_loops/fake_message_loop.h>
#include <brillo/message_loops/message_loop.h>
#include <brillo/message_loops/message_loop_utils.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "update_engine/cros/fake_system_state.h"
#include "update_engine/update_manager/default_policy.h"
#include "update_engine/update_manager/fake_state.h"
#include "update_engine/update_manager/mock_policy.h"
#include "update_engine/update_manager/umtest_utils.h"
using base::Bind;
using base::Callback;
using base::Time;
using base::TimeDelta;
using brillo::MessageLoop;
using brillo::MessageLoopRunMaxIterations;
using chromeos_update_engine::ErrorCode;
using chromeos_update_engine::FakeClock;
using chromeos_update_engine::FakeSystemState;
using std::pair;
using std::string;
using std::tuple;
using std::unique_ptr;
using std::vector;
namespace {
// Generates a fixed timestamp for use in faking the current time.
Time FixedTime() {
Time::Exploded now_exp;
now_exp.year = 2014;
now_exp.month = 3;
now_exp.day_of_week = 2;
now_exp.day_of_month = 18;
now_exp.hour = 8;
now_exp.minute = 5;
now_exp.second = 33;
now_exp.millisecond = 675;
Time time;
ignore_result(Time::FromLocalExploded(now_exp, &time));
return time;
}
} // namespace
namespace chromeos_update_manager {
class UmUpdateManagerTest : public ::testing::Test {
protected:
void SetUp() override {
loop_.SetAsCurrent();
FakeSystemState::CreateInstance();
fake_state_ = new FakeState();
umut_.reset(new UpdateManager(
TimeDelta::FromSeconds(5), TimeDelta::FromSeconds(1), fake_state_));
}
void TearDown() override { EXPECT_FALSE(loop_.PendingTasks()); }
base::SimpleTestClock test_clock_;
brillo::FakeMessageLoop loop_{&test_clock_};
FakeState* fake_state_; // Owned by the umut_.
unique_ptr<UpdateManager> umut_;
};
// The FailingPolicy implements a single method and make it always fail. This
// class extends the DefaultPolicy class to allow extensions of the Policy
// class without extending nor changing this test.
class FailingPolicy : public DefaultPolicy {
public:
explicit FailingPolicy(int* num_called_p) : num_called_p_(num_called_p) {}
FailingPolicy() : FailingPolicy(nullptr) {}
EvalStatus UpdateCheckAllowed(EvaluationContext* ec,
State* state,
string* error,
UpdateCheckParams* result) const override {
if (num_called_p_)
(*num_called_p_)++;
*error = "FailingPolicy failed.";
return EvalStatus::kFailed;
}
protected:
string PolicyName() const override { return "FailingPolicy"; }
private:
int* num_called_p_;
};
// The LazyPolicy always returns EvalStatus::kAskMeAgainLater.
class LazyPolicy : public DefaultPolicy {
EvalStatus UpdateCheckAllowed(EvaluationContext* ec,
State* state,
string* error,
UpdateCheckParams* result) const override {
return EvalStatus::kAskMeAgainLater;
}
protected:
string PolicyName() const override { return "LazyPolicy"; }
};
// A policy that sleeps for a predetermined amount of time, then checks for a
// wallclock-based time threshold (if given) and returns
// EvalStatus::kAskMeAgainLater if not passed; otherwise, returns
// EvalStatus::kSucceeded. Increments a counter every time it is being queried,
// if a pointer to it is provided.
class DelayPolicy : public DefaultPolicy {
public:
DelayPolicy(int sleep_secs, Time time_threshold, int* num_called_p)
: sleep_secs_(sleep_secs),
time_threshold_(time_threshold),
num_called_p_(num_called_p) {}
EvalStatus UpdateCheckAllowed(EvaluationContext* ec,
State* state,
string* error,
UpdateCheckParams* result) const override {
if (num_called_p_)
(*num_called_p_)++;
// Sleep for a predetermined amount of time.
if (sleep_secs_ > 0)
sleep(sleep_secs_);
// Check for a time threshold. This can be used to ensure that the policy
// has some non-constant dependency.
if (time_threshold_ < Time::Max() &&
ec->IsWallclockTimeGreaterThan(time_threshold_))
return EvalStatus::kSucceeded;
return EvalStatus::kAskMeAgainLater;
}
protected:
string PolicyName() const override { return "DelayPolicy"; }
private:
int sleep_secs_;
Time time_threshold_;
int* num_called_p_;
};
// AccumulateCallsCallback() adds to the passed |acc| accumulator vector pairs
// of EvalStatus and T instances. This allows to create a callback that keeps
// track of when it is called and the arguments passed to it, to be used with
// the UpdateManager::AsyncPolicyRequest().
template <typename T>
static void AccumulateCallsCallback(vector<pair<EvalStatus, T>>* acc,
EvalStatus status,
const T& result) {
acc->push_back(std::make_pair(status, result));
}
// Tests that policy requests are completed successfully. It is important that
// this tests cover all policy requests as defined in Policy.
TEST_F(UmUpdateManagerTest, PolicyRequestCallUpdateCheckAllowed) {
UpdateCheckParams result;
EXPECT_EQ(EvalStatus::kSucceeded,
umut_->PolicyRequest(&Policy::UpdateCheckAllowed, &result));
}
TEST_F(UmUpdateManagerTest, PolicyRequestCallUpdateCanStart) {
UpdateState update_state = UpdateState();
update_state.interactive = true;
update_state.is_delta_payload = false;
update_state.first_seen = FixedTime();
update_state.num_checks = 1;
update_state.num_failures = 0;
update_state.failures_last_updated = Time();
update_state.download_urls = vector<string>{"http://fake/url/"};
update_state.download_errors_max = 10;
update_state.p2p_downloading_disabled = false;
update_state.p2p_sharing_disabled = false;
update_state.p2p_num_attempts = 0;
update_state.p2p_first_attempted = Time();
update_state.last_download_url_idx = -1;
update_state.last_download_url_num_errors = 0;
update_state.download_errors = vector<tuple<int, ErrorCode, Time>>();
update_state.backoff_expiry = Time();
update_state.is_backoff_disabled = false;
update_state.scatter_wait_period = TimeDelta::FromSeconds(15);
update_state.scatter_check_threshold = 4;
update_state.scatter_wait_period_max = TimeDelta::FromSeconds(60);
update_state.scatter_check_threshold_min = 2;
update_state.scatter_check_threshold_max = 8;
UpdateDownloadParams result;
EXPECT_EQ(
EvalStatus::kSucceeded,
umut_->PolicyRequest(&Policy::UpdateCanStart, &result, update_state));
}
TEST_F(UmUpdateManagerTest, PolicyRequestCallsDefaultOnError) {
umut_->set_policy(new FailingPolicy());
// Tests that the DefaultPolicy instance is called when the method fails,
// which will set this as true.
UpdateCheckParams result;
result.updates_enabled = false;
EvalStatus status =
umut_->PolicyRequest(&Policy::UpdateCheckAllowed, &result);
EXPECT_EQ(EvalStatus::kSucceeded, status);
EXPECT_TRUE(result.updates_enabled);
}
// This test only applies to debug builds where DCHECK is enabled.
#if DCHECK_IS_ON
TEST_F(UmUpdateManagerTest, PolicyRequestDoesntBlockDeathTest) {
// The update manager should die (DCHECK) if a policy called synchronously
// returns a kAskMeAgainLater value.
UpdateCheckParams result;
umut_->set_policy(new LazyPolicy());
EXPECT_DEATH(umut_->PolicyRequest(&Policy::UpdateCheckAllowed, &result), "");
}
#endif // DCHECK_IS_ON
TEST_F(UmUpdateManagerTest, AsyncPolicyRequestDelaysEvaluation) {
// To avoid differences in code execution order between an AsyncPolicyRequest
// call on a policy that returns AskMeAgainLater the first time and one that
// succeeds the first time, we ensure that the passed callback is called from
// the main loop in both cases even when we could evaluate it right now.
umut_->set_policy(new FailingPolicy());
vector<pair<EvalStatus, UpdateCheckParams>> calls;
Callback<void(EvalStatus, const UpdateCheckParams&)> callback =
Bind(AccumulateCallsCallback<UpdateCheckParams>, &calls);
umut_->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed);
// The callback should wait until we run the main loop for it to be executed.
EXPECT_EQ(0U, calls.size());
MessageLoopRunMaxIterations(MessageLoop::current(), 100);
EXPECT_EQ(1U, calls.size());
}
TEST_F(UmUpdateManagerTest, AsyncPolicyRequestTimeoutDoesNotFire) {
// Set up an async policy call to return immediately, then wait a little and
// ensure that the timeout event does not fire.
int num_called = 0;
umut_->set_policy(new FailingPolicy(&num_called));
vector<pair<EvalStatus, UpdateCheckParams>> calls;
Callback<void(EvalStatus, const UpdateCheckParams&)> callback =
Bind(AccumulateCallsCallback<UpdateCheckParams>, &calls);
umut_->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed);
// Run the main loop, ensure that policy was attempted once before deferring
// to the default.
MessageLoopRunMaxIterations(MessageLoop::current(), 100);
EXPECT_EQ(1, num_called);
ASSERT_EQ(1U, calls.size());
EXPECT_EQ(EvalStatus::kSucceeded, calls[0].first);
// Wait for the timeout to expire, run the main loop again, ensure that
// nothing happened.
test_clock_.Advance(TimeDelta::FromSeconds(2));
MessageLoopRunMaxIterations(MessageLoop::current(), 10);
EXPECT_EQ(1, num_called);
EXPECT_EQ(1U, calls.size());
}
TEST_F(UmUpdateManagerTest, AsyncPolicyRequestTimesOut) {
auto* fake_clock = FakeSystemState::Get()->fake_clock();
// Set up an async policy call to exceed its expiration timeout, make sure
// that the default policy was not used (no callback) and that evaluation is
// reattempted.
int num_called = 0;
umut_->set_policy(new DelayPolicy(
0,
fake_clock->GetWallclockTime() + TimeDelta::FromSeconds(3),
&num_called));
vector<pair<EvalStatus, UpdateCheckParams>> calls;
Callback<void(EvalStatus, const UpdateCheckParams&)> callback =
Bind(AccumulateCallsCallback<UpdateCheckParams>, &calls);
umut_->AsyncPolicyRequest(callback, &Policy::UpdateCheckAllowed);
// Run the main loop, ensure that policy was attempted once but the callback
// was not invoked.
MessageLoopRunMaxIterations(MessageLoop::current(), 100);
EXPECT_EQ(1, num_called);
EXPECT_EQ(0U, calls.size());
// Wait for the expiration timeout to expire, run the main loop again,
// ensure that reevaluation occurred but callback was not invoked (i.e.
// default policy was not consulted).
test_clock_.Advance(TimeDelta::FromSeconds(2));
fake_clock->SetWallclockTime(fake_clock->GetWallclockTime() +
TimeDelta::FromSeconds(2));
MessageLoopRunMaxIterations(MessageLoop::current(), 10);
EXPECT_EQ(2, num_called);
EXPECT_EQ(0U, calls.size());
// Wait for reevaluation due to delay to happen, ensure that it occurs and
// that the callback is invoked.
test_clock_.Advance(TimeDelta::FromSeconds(2));
fake_clock->SetWallclockTime(fake_clock->GetWallclockTime() +
TimeDelta::FromSeconds(2));
MessageLoopRunMaxIterations(MessageLoop::current(), 10);
EXPECT_EQ(3, num_called);
ASSERT_EQ(1U, calls.size());
EXPECT_EQ(EvalStatus::kSucceeded, calls[0].first);
}
} // namespace chromeos_update_manager