blob: d29dffd87843c7b65811900bcb2fab248748250f [file] [log] [blame]
Scott Zawalskieadbf702013-03-14 09:23:06 -04001#!/usr/bin/python
2# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
Dan Shib95bb862013-03-22 16:29:28 -07006import mox
Gwendal Grignou3e96cc22017-06-07 16:22:51 -07007import os
Scott Zawalskieadbf702013-03-14 09:23:06 -04008import unittest
9
10import common
Luigi Semenzatof15c8fc2017-03-03 14:12:40 -080011import time
Scott Zawalskieadbf702013-03-14 09:23:06 -040012
13import autoupdater
Don Garrettdf8aef72013-12-16 11:12:41 -080014from autotest_lib.client.common_lib import error
Gwendal Grignou3e96cc22017-06-07 16:22:51 -070015from autotest_lib.client.common_lib.test_utils import mock
Scott Zawalskieadbf702013-03-14 09:23:06 -040016
Dan Shib95bb862013-03-22 16:29:28 -070017class TestAutoUpdater(mox.MoxTestBase):
Scott Zawalskieadbf702013-03-14 09:23:06 -040018 """Test autoupdater module."""
19
20
21 def testParseBuildFromUpdateUrlwithUpdate(self):
22 """Test that we properly parse the build from an update_url."""
23 update_url = ('http://172.22.50.205:8082/update/lumpy-release/'
24 'R27-3837.0.0')
25 expected_value = 'lumpy-release/R27-3837.0.0'
26 self.assertEqual(autoupdater.url_to_image_name(update_url),
27 expected_value)
28
29
Dan Shi190c7802013-04-04 13:05:30 -070030 def testCheckVersion_1(self):
Dan Shib95bb862013-03-22 16:29:28 -070031 """Test version check methods work for any build.
32
33 Test two methods used to check version, check_version and
Dan Shi190c7802013-04-04 13:05:30 -070034 check_version_to_confirm_install, for:
35 1. trybot paladin build.
36 update version: trybot-lumpy-paladin/R27-3837.0.0-b123
37 booted version: 3837.0.2013_03_21_1340
Dan Shib95bb862013-03-22 16:29:28 -070038
39 """
Dan Shi190c7802013-04-04 13:05:30 -070040 update_url = ('http://172.22.50.205:8082/update/trybot-lumpy-paladin/'
41 'R27-1111.0.0-b123')
Dan Shi549fb822015-03-24 18:01:11 -070042 updater = autoupdater.ChromiumOSUpdater(
43 update_url, host=self.mox.CreateMockAnything())
Dan Shib95bb862013-03-22 16:29:28 -070044
Dan Shi775ecd52013-03-27 11:51:52 -070045 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -070046 self.mox.StubOutWithMock(updater.host, 'get_release_version')
47 updater.host.get_release_version().MultipleTimes().AndReturn(
Dan Shi190c7802013-04-04 13:05:30 -070048 '1111.0.2013_03_21_1340')
Dan Shib95bb862013-03-22 16:29:28 -070049 self.mox.ReplayAll()
50
Dan Shib95bb862013-03-22 16:29:28 -070051 self.assertFalse(updater.check_version())
Dan Shib95bb862013-03-22 16:29:28 -070052 self.assertTrue(updater.check_version_to_confirm_install())
53
Dan Shi190c7802013-04-04 13:05:30 -070054 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -070055 self.mox.StubOutWithMock(updater.host, 'get_release_version')
56 updater.host.get_release_version().MultipleTimes().AndReturn(
57 '1111.0.0-rc1')
Dan Shi190c7802013-04-04 13:05:30 -070058 self.mox.ReplayAll()
59
60 self.assertFalse(updater.check_version())
61 self.assertFalse(updater.check_version_to_confirm_install())
62
63 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -070064 self.mox.StubOutWithMock(updater.host, 'get_release_version')
65 updater.host.get_release_version().MultipleTimes().AndReturn('1111.0.0')
Dan Shi190c7802013-04-04 13:05:30 -070066 self.mox.ReplayAll()
67
68 self.assertFalse(updater.check_version())
69 self.assertFalse(updater.check_version_to_confirm_install())
70
Dan Shi73aa2902013-05-03 11:22:11 -070071 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -070072 self.mox.StubOutWithMock(updater.host, 'get_release_version')
73 updater.host.get_release_version().MultipleTimes().AndReturn(
Dan Shi73aa2902013-05-03 11:22:11 -070074 '4444.0.0-pgo-generate')
75 self.mox.ReplayAll()
76
77 self.assertFalse(updater.check_version())
78 self.assertFalse(updater.check_version_to_confirm_install())
79
80
Dan Shi190c7802013-04-04 13:05:30 -070081 def testCheckVersion_2(self):
82 """Test version check methods work for any build.
83
84 Test two methods used to check version, check_version and
85 check_version_to_confirm_install, for:
86 2. trybot release build.
87 update version: trybot-lumpy-release/R27-3837.0.0-b456
88 booted version: 3837.0.0
89
90 """
91 update_url = ('http://172.22.50.205:8082/update/trybot-lumpy-release/'
92 'R27-2222.0.0-b456')
Dan Shi549fb822015-03-24 18:01:11 -070093 updater = autoupdater.ChromiumOSUpdater(
94 update_url, host=self.mox.CreateMockAnything())
Dan Shi190c7802013-04-04 13:05:30 -070095
96 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -070097 self.mox.StubOutWithMock(updater.host, 'get_release_version')
98 updater.host.get_release_version().MultipleTimes().AndReturn(
Dan Shi190c7802013-04-04 13:05:30 -070099 '2222.0.2013_03_21_1340')
100 self.mox.ReplayAll()
101
102 self.assertFalse(updater.check_version())
103 self.assertFalse(updater.check_version_to_confirm_install())
104
105 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700106 self.mox.StubOutWithMock(updater.host, 'get_release_version')
107 updater.host.get_release_version().MultipleTimes().AndReturn(
108 '2222.0.0-rc1')
Dan Shi190c7802013-04-04 13:05:30 -0700109 self.mox.ReplayAll()
110
111 self.assertFalse(updater.check_version())
112 self.assertFalse(updater.check_version_to_confirm_install())
113
114 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700115 self.mox.StubOutWithMock(updater.host, 'get_release_version')
116 updater.host.get_release_version().MultipleTimes().AndReturn('2222.0.0')
Dan Shi190c7802013-04-04 13:05:30 -0700117 self.mox.ReplayAll()
118
119 self.assertFalse(updater.check_version())
120 self.assertTrue(updater.check_version_to_confirm_install())
121
Dan Shi73aa2902013-05-03 11:22:11 -0700122 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700123 self.mox.StubOutWithMock(updater.host, 'get_release_version')
124 updater.host.get_release_version().MultipleTimes().AndReturn(
Dan Shi73aa2902013-05-03 11:22:11 -0700125 '4444.0.0-pgo-generate')
126 self.mox.ReplayAll()
127
128 self.assertFalse(updater.check_version())
129 self.assertFalse(updater.check_version_to_confirm_install())
130
Dan Shi190c7802013-04-04 13:05:30 -0700131
132 def testCheckVersion_3(self):
133 """Test version check methods work for any build.
134
135 Test two methods used to check version, check_version and
136 check_version_to_confirm_install, for:
137 3. buildbot official release build.
138 update version: lumpy-release/R27-3837.0.0
139 booted version: 3837.0.0
140
141 """
142 update_url = ('http://172.22.50.205:8082/update/lumpy-release/'
143 'R27-3333.0.0')
Dan Shi549fb822015-03-24 18:01:11 -0700144 updater = autoupdater.ChromiumOSUpdater(
145 update_url, host=self.mox.CreateMockAnything())
Dan Shi190c7802013-04-04 13:05:30 -0700146
147 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700148 self.mox.StubOutWithMock(updater.host, 'get_release_version')
149 updater.host.get_release_version().MultipleTimes().AndReturn(
Dan Shi190c7802013-04-04 13:05:30 -0700150 '3333.0.2013_03_21_1340')
151 self.mox.ReplayAll()
152
153 self.assertFalse(updater.check_version())
154 self.assertFalse(updater.check_version_to_confirm_install())
155
156 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700157 self.mox.StubOutWithMock(updater.host, 'get_release_version')
158 updater.host.get_release_version().MultipleTimes().AndReturn(
159 '3333.0.0-rc1')
Dan Shi190c7802013-04-04 13:05:30 -0700160 self.mox.ReplayAll()
161
162 self.assertFalse(updater.check_version())
163 self.assertFalse(updater.check_version_to_confirm_install())
164
165 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700166 self.mox.StubOutWithMock(updater.host, 'get_release_version')
167 updater.host.get_release_version().MultipleTimes().AndReturn('3333.0.0')
Dan Shi190c7802013-04-04 13:05:30 -0700168 self.mox.ReplayAll()
169
170 self.assertTrue(updater.check_version())
171 self.assertTrue(updater.check_version_to_confirm_install())
172
Dan Shi73aa2902013-05-03 11:22:11 -0700173 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700174 self.mox.StubOutWithMock(updater.host, 'get_release_version')
175 updater.host.get_release_version().MultipleTimes().AndReturn(
Dan Shi73aa2902013-05-03 11:22:11 -0700176 '4444.0.0-pgo-generate')
177 self.mox.ReplayAll()
178
179 self.assertFalse(updater.check_version())
180 self.assertFalse(updater.check_version_to_confirm_install())
181
Dan Shi190c7802013-04-04 13:05:30 -0700182
183 def testCheckVersion_4(self):
184 """Test version check methods work for any build.
185
186 Test two methods used to check version, check_version and
187 check_version_to_confirm_install, for:
188 4. non-official paladin rc build.
189 update version: lumpy-paladin/R27-3837.0.0-rc7
190 booted version: 3837.0.0-rc7
191
192 """
193 update_url = ('http://172.22.50.205:8082/update/lumpy-paladin/'
194 'R27-4444.0.0-rc7')
Dan Shi549fb822015-03-24 18:01:11 -0700195 updater = autoupdater.ChromiumOSUpdater(
196 update_url, host=self.mox.CreateMockAnything())
Dan Shi190c7802013-04-04 13:05:30 -0700197
198 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700199 self.mox.StubOutWithMock(updater.host, 'get_release_version')
200 updater.host.get_release_version().MultipleTimes().AndReturn(
Dan Shi190c7802013-04-04 13:05:30 -0700201 '4444.0.2013_03_21_1340')
202 self.mox.ReplayAll()
203
204 self.assertFalse(updater.check_version())
205 self.assertFalse(updater.check_version_to_confirm_install())
206
207 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700208 self.mox.StubOutWithMock(updater.host, 'get_release_version')
209 updater.host.get_release_version().MultipleTimes().AndReturn(
210 '4444.0.0-rc7')
Dan Shi190c7802013-04-04 13:05:30 -0700211 self.mox.ReplayAll()
212
213 self.assertTrue(updater.check_version())
214 self.assertTrue(updater.check_version_to_confirm_install())
215
216 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700217 self.mox.StubOutWithMock(updater.host, 'get_release_version')
218 updater.host.get_release_version().MultipleTimes().AndReturn('4444.0.0')
Dan Shi190c7802013-04-04 13:05:30 -0700219 self.mox.ReplayAll()
220
221 self.assertFalse(updater.check_version())
Dan Shib95bb862013-03-22 16:29:28 -0700222 self.assertFalse(updater.check_version_to_confirm_install())
223
Dan Shi73aa2902013-05-03 11:22:11 -0700224 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700225 self.mox.StubOutWithMock(updater.host, 'get_release_version')
226 updater.host.get_release_version().MultipleTimes().AndReturn(
Dan Shi73aa2902013-05-03 11:22:11 -0700227 '4444.0.0-pgo-generate')
228 self.mox.ReplayAll()
229
230 self.assertFalse(updater.check_version())
231 self.assertFalse(updater.check_version_to_confirm_install())
232
Dan Shib95bb862013-03-22 16:29:28 -0700233
Dan Shi7f795512013-04-12 10:08:17 -0700234 def testCheckVersion_5(self):
235 """Test version check methods work for any build.
236
237 Test two methods used to check version, check_version and
238 check_version_to_confirm_install, for:
239 5. chrome-perf build.
240 update version: lumpy-chrome-perf/R28-3837.0.0-b2996
241 booted version: 3837.0.0
242
243 """
244 update_url = ('http://172.22.50.205:8082/update/lumpy-chrome-perf/'
245 'R28-4444.0.0-b2996')
Dan Shi549fb822015-03-24 18:01:11 -0700246 updater = autoupdater.ChromiumOSUpdater(
247 update_url, host=self.mox.CreateMockAnything())
Dan Shi7f795512013-04-12 10:08:17 -0700248
249 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700250 self.mox.StubOutWithMock(updater.host, 'get_release_version')
251 updater.host.get_release_version().MultipleTimes().AndReturn(
Dan Shi7f795512013-04-12 10:08:17 -0700252 '4444.0.2013_03_21_1340')
253 self.mox.ReplayAll()
254
255 self.assertFalse(updater.check_version())
256 self.assertFalse(updater.check_version_to_confirm_install())
257
258 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700259 self.mox.StubOutWithMock(updater.host, 'get_release_version')
260 updater.host.get_release_version().MultipleTimes().AndReturn(
261 '4444.0.0-rc7')
Dan Shi7f795512013-04-12 10:08:17 -0700262 self.mox.ReplayAll()
263
264 self.assertFalse(updater.check_version())
265 self.assertFalse(updater.check_version_to_confirm_install())
266
267 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700268 self.mox.StubOutWithMock(updater.host, 'get_release_version')
269 updater.host.get_release_version().MultipleTimes().AndReturn('4444.0.0')
Dan Shi7f795512013-04-12 10:08:17 -0700270 self.mox.ReplayAll()
271
272 self.assertFalse(updater.check_version())
273 self.assertTrue(updater.check_version_to_confirm_install())
274
Dan Shi73aa2902013-05-03 11:22:11 -0700275 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700276 self.mox.StubOutWithMock(updater.host, 'get_release_version')
277 updater.host.get_release_version().MultipleTimes().AndReturn(
Dan Shi73aa2902013-05-03 11:22:11 -0700278 '4444.0.0-pgo-generate')
279 self.mox.ReplayAll()
280
281 self.assertFalse(updater.check_version())
282 self.assertFalse(updater.check_version_to_confirm_install())
283
284
285 def testCheckVersion_6(self):
286 """Test version check methods work for any build.
287
288 Test two methods used to check version, check_version and
289 check_version_to_confirm_install, for:
290 6. pgo-generate build.
291 update version: lumpy-release-pgo-generate/R28-3837.0.0-b2996
292 booted version: 3837.0.0-pgo-generate
293
294 """
295 update_url = ('http://172.22.50.205:8082/update/lumpy-release-pgo-'
296 'generate/R28-4444.0.0-b2996')
Dan Shi549fb822015-03-24 18:01:11 -0700297 updater = autoupdater.ChromiumOSUpdater(
298 update_url, host=self.mox.CreateMockAnything())
Dan Shi73aa2902013-05-03 11:22:11 -0700299
300 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700301 self.mox.StubOutWithMock(updater.host, 'get_release_version')
302 updater.host.get_release_version().MultipleTimes().AndReturn(
Dan Shi73aa2902013-05-03 11:22:11 -0700303 '4444.0.0-2013_03_21_1340')
304 self.mox.ReplayAll()
305
306 self.assertFalse(updater.check_version())
307 self.assertFalse(updater.check_version_to_confirm_install())
308
309 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700310 self.mox.StubOutWithMock(updater.host, 'get_release_version')
311 updater.host.get_release_version().MultipleTimes().AndReturn(
312 '4444.0.0-rc7')
Dan Shi73aa2902013-05-03 11:22:11 -0700313 self.mox.ReplayAll()
314
315 self.assertFalse(updater.check_version())
316 self.assertFalse(updater.check_version_to_confirm_install())
317
318 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700319 self.mox.StubOutWithMock(updater.host, 'get_release_version')
320 updater.host.get_release_version().MultipleTimes().AndReturn('4444.0.0')
Dan Shi73aa2902013-05-03 11:22:11 -0700321 self.mox.ReplayAll()
322
323 self.assertFalse(updater.check_version())
324 self.assertFalse(updater.check_version_to_confirm_install())
325
326 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700327 self.mox.StubOutWithMock(updater.host, 'get_release_version')
328 updater.host.get_release_version().MultipleTimes().AndReturn(
Dan Shi73aa2902013-05-03 11:22:11 -0700329 '4444.0.0-pgo-generate')
330 self.mox.ReplayAll()
331
332 self.assertFalse(updater.check_version())
333 self.assertTrue(updater.check_version_to_confirm_install())
334
Dan Shi7f795512013-04-12 10:08:17 -0700335
Rebecca Silbersteinb2d204b2015-08-04 10:45:50 -0700336 def testCheckVersion_7(self):
337 """Test version check methods work for a test-ap build.
338
339 Test two methods used to check version, check_version and
340 check_version_to_confirm_install, for:
341 6. test-ap build.
342 update version: trybot-stumpy-test-ap/R46-7298.0.0-b23
343 booted version: 7298.0.0
344
345 """
346 update_url = ('http://100.107.160.2:8082/update/trybot-stumpy-test-api'
347 '/R46-7298.0.0-b23')
348 updater = autoupdater.ChromiumOSUpdater(
349 update_url, host=self.mox.CreateMockAnything())
350
351 self.mox.UnsetStubs()
352 self.mox.StubOutWithMock(updater.host, 'get_release_version')
353 updater.host.get_release_version().MultipleTimes().AndReturn(
354 '7298.0.2015_07_24_1640')
355 self.mox.ReplayAll()
356
357 self.assertFalse(updater.check_version())
358 self.assertTrue(updater.check_version_to_confirm_install())
359
360 self.mox.UnsetStubs()
361 self.mox.StubOutWithMock(updater.host, 'get_release_version')
362 updater.host.get_release_version().MultipleTimes().AndReturn(
363 '7298.0.2015_07_24_1640')
364 self.mox.ReplayAll()
365
366 self.assertFalse(updater.check_version())
367 self.assertTrue(updater.check_version_to_confirm_install())
368
369 self.mox.UnsetStubs()
370 self.mox.StubOutWithMock(updater.host, 'get_release_version')
371 updater.host.get_release_version().MultipleTimes().AndReturn('7298.0.0')
372 self.mox.ReplayAll()
373
374 self.assertFalse(updater.check_version())
375 self.assertFalse(updater.check_version_to_confirm_install())
376
377 self.mox.UnsetStubs()
378 self.mox.StubOutWithMock(updater.host, 'get_release_version')
379 updater.host.get_release_version().MultipleTimes().AndReturn(
380 '7298.0.0')
381 self.mox.ReplayAll()
382
383 self.assertFalse(updater.check_version())
384 self.assertFalse(updater.check_version_to_confirm_install())
385
386
Luigi Semenzatof15c8fc2017-03-03 14:12:40 -0800387 def _host_run_for_update(self, cmd, exception=None,
388 bad_update_status=False):
389 """Helper function for AU tests.
390
391 @param host: the test host
392 @param cmd: the command to be recorded
393 @param exception: the exception to be recorded, or None
394 """
395 if exception:
396 self.host.run(command=cmd).AndRaise(exception)
397 else:
398 result = self.mox.CreateMockAnything()
399 if bad_update_status:
400 # Pick randomly one unexpected status
401 result.stdout = 'UPDATE_STATUS_UPDATED_NEED_REBOOT'
402 else:
403 result.stdout = 'UPDATE_STATUS_IDLE'
404 result.status = 0
405 self.host.run(command=cmd).AndReturn(result)
406
407
Don Garrettdf8aef72013-12-16 11:12:41 -0800408 def testTriggerUpdate(self):
409 """Tests that we correctly handle updater errors."""
Don Garrettdf8aef72013-12-16 11:12:41 -0800410 update_url = 'http://server/test/url'
Luigi Semenzatof15c8fc2017-03-03 14:12:40 -0800411 self.host = self.mox.CreateMockAnything()
412 self.mox.StubOutWithMock(self.host, 'run')
Shuqian Zhaod9992722016-02-29 12:26:38 -0800413 self.mox.StubOutWithMock(autoupdater.ChromiumOSUpdater,
414 'get_last_update_error')
Luigi Semenzatof15c8fc2017-03-03 14:12:40 -0800415 self.host.hostname = 'test_host'
416 updater_control_bin = '/usr/bin/update_engine_client'
417 test_url = 'http://server/test/url'
418 expected_wait_cmd = ('%s -status | grep CURRENT_OP' %
419 updater_control_bin)
420 expected_cmd = ('%s --check_for_update --omaha_url=%s' %
421 (updater_control_bin, test_url))
422 self.mox.StubOutWithMock(time, "sleep")
423 UPDATE_ENGINE_RETRY_WAIT_TIME=5
Don Garrettdf8aef72013-12-16 11:12:41 -0800424
Luigi Semenzatoe76d9f82016-11-21 11:15:10 -0800425 # Generic SSH Error.
Don Garrettdf8aef72013-12-16 11:12:41 -0800426 cmd_result_255 = self.mox.CreateMockAnything()
427 cmd_result_255.exit_status = 255
428
Shuqian Zhaod9992722016-02-29 12:26:38 -0800429 # Command Failed Error
430 cmd_result_1 = self.mox.CreateMockAnything()
431 cmd_result_1.exit_status = 1
Luigi Semenzatof15c8fc2017-03-03 14:12:40 -0800432
433 # Error 37
434 cmd_result_37 = self.mox.CreateMockAnything()
435 cmd_result_37.exit_status = 37
436
437 updater = autoupdater.ChromiumOSUpdater(update_url, host=self.host)
438
439 # (SUCCESS) Expect one wait command and one status command.
440 self._host_run_for_update(expected_wait_cmd)
441 self._host_run_for_update(expected_cmd)
442
443 # (SUCCESS) Test with one retry to wait for update-engine.
444 self._host_run_for_update(expected_wait_cmd, exception=
445 error.AutoservRunError('non-zero status', cmd_result_1))
446 time.sleep(UPDATE_ENGINE_RETRY_WAIT_TIME)
447 self._host_run_for_update(expected_wait_cmd)
448 self._host_run_for_update(expected_cmd)
449
450 # (SUCCESS) One-time SSH timeout, then success on retry.
451 self._host_run_for_update(expected_wait_cmd)
452 self._host_run_for_update(expected_cmd, exception=
453 error.AutoservSSHTimeout('ssh timed out', cmd_result_255))
454 self._host_run_for_update(expected_cmd)
455
456 # (SUCCESS) One-time ERROR 37, then success.
457 self._host_run_for_update(expected_wait_cmd)
458 self._host_run_for_update(expected_cmd, exception=
459 error.AutoservRunError('ERROR_CODE=37', cmd_result_37))
460 self._host_run_for_update(expected_cmd)
461
462 # (FAILURE) Bad status of update engine.
463 self._host_run_for_update(expected_wait_cmd)
464 self._host_run_for_update(expected_cmd, bad_update_status=True,
465 exception=error.InstallError(
466 'host is not in installable state'))
467
468 # (FAILURE) Two-time SSH timeout.
469 self._host_run_for_update(expected_wait_cmd)
470 self._host_run_for_update(expected_cmd, exception=
471 error.AutoservSSHTimeout('ssh timed out', cmd_result_255))
472 self._host_run_for_update(expected_cmd, exception=
473 error.AutoservSSHTimeout('ssh timed out', cmd_result_255))
474
475 # (FAILURE) SSH Permission Error
476 self._host_run_for_update(expected_wait_cmd)
477 self._host_run_for_update(expected_cmd, exception=
478 error.AutoservSshPermissionDeniedError('no permission',
479 cmd_result_255))
480
481 # (FAILURE) Other ssh failure
482 self._host_run_for_update(expected_wait_cmd)
483 self._host_run_for_update(expected_cmd, exception=
484 error.AutoservSshPermissionDeniedError('no permission',
485 cmd_result_255))
486 # (FAILURE) Other error
487 self._host_run_for_update(expected_wait_cmd)
488 self._host_run_for_update(expected_cmd, exception=
Luigi Semenzatoe76d9f82016-11-21 11:15:10 -0800489 error.AutoservRunError("unknown error", cmd_result_1))
Shuqian Zhaod9992722016-02-29 12:26:38 -0800490
Don Garrettdf8aef72013-12-16 11:12:41 -0800491 self.mox.ReplayAll()
492
Luigi Semenzatoe76d9f82016-11-21 11:15:10 -0800493 # Expect success
494 updater.trigger_update()
495 updater.trigger_update()
Don Garrettdf8aef72013-12-16 11:12:41 -0800496 updater.trigger_update()
Luigi Semenzatof15c8fc2017-03-03 14:12:40 -0800497 updater.trigger_update()
Don Garrettdf8aef72013-12-16 11:12:41 -0800498
Luigi Semenzatoe76d9f82016-11-21 11:15:10 -0800499 # Expect errors as listed above
500 self.assertRaises(autoupdater.RootFSUpdateError, updater.trigger_update)
501 self.assertRaises(autoupdater.RootFSUpdateError, updater.trigger_update)
502 self.assertRaises(autoupdater.RootFSUpdateError, updater.trigger_update)
503 self.assertRaises(autoupdater.RootFSUpdateError, updater.trigger_update)
Luigi Semenzatof15c8fc2017-03-03 14:12:40 -0800504 self.assertRaises(autoupdater.RootFSUpdateError, updater.trigger_update)
Don Garrettdf8aef72013-12-16 11:12:41 -0800505
506 self.mox.VerifyAll()
507
508
Chris Sosa72312602013-04-16 15:01:56 -0700509 def testUpdateStateful(self):
510 """Tests that we call the stateful update script with the correct args.
511 """
512 self.mox.StubOutWithMock(autoupdater.ChromiumOSUpdater, '_run')
Chris Sosab9ada9b2013-06-12 12:47:47 -0700513 self.mox.StubOutWithMock(autoupdater.ChromiumOSUpdater,
514 'get_stateful_update_script')
Chris Sosa72312602013-04-16 15:01:56 -0700515 update_url = ('http://172.22.50.205:8082/update/lumpy-chrome-perf/'
516 'R28-4444.0.0-b2996')
joychen03eaad92013-06-26 09:55:21 -0700517 static_update_url = ('http://172.22.50.205:8082/static/'
Chris Sosa72312602013-04-16 15:01:56 -0700518 'lumpy-chrome-perf/R28-4444.0.0-b2996')
519
520 # Test with clobber=False.
Chris Sosab9ada9b2013-06-12 12:47:47 -0700521 autoupdater.ChromiumOSUpdater.get_stateful_update_script().AndReturn(
Gwendal Grignou3e96cc22017-06-07 16:22:51 -0700522 autoupdater.ChromiumOSUpdater.REMOTE_STATEFUL_UPDATE_PATH)
Chris Sosa72312602013-04-16 15:01:56 -0700523 autoupdater.ChromiumOSUpdater._run(
524 mox.And(
Gilad Arnold0c0df732015-09-21 06:37:59 -0700525 mox.StrContains(
526 autoupdater.ChromiumOSUpdater.
Gwendal Grignou3e96cc22017-06-07 16:22:51 -0700527 REMOTE_STATEFUL_UPDATE_PATH),
Chris Sosa72312602013-04-16 15:01:56 -0700528 mox.StrContains(static_update_url),
529 mox.Not(mox.StrContains('--stateful_change=clean'))),
Dan Shi80aa9102016-01-25 13:45:00 -0800530 timeout=mox.IgnoreArg())
Chris Sosa72312602013-04-16 15:01:56 -0700531
532 self.mox.ReplayAll()
533 updater = autoupdater.ChromiumOSUpdater(update_url)
534 updater.update_stateful(clobber=False)
535 self.mox.VerifyAll()
536
537 # Test with clobber=True.
538 self.mox.ResetAll()
Chris Sosab9ada9b2013-06-12 12:47:47 -0700539 autoupdater.ChromiumOSUpdater.get_stateful_update_script().AndReturn(
Gwendal Grignou3e96cc22017-06-07 16:22:51 -0700540 autoupdater.ChromiumOSUpdater.REMOTE_STATEFUL_UPDATE_PATH)
Chris Sosa72312602013-04-16 15:01:56 -0700541 autoupdater.ChromiumOSUpdater._run(
542 mox.And(
Gilad Arnold0c0df732015-09-21 06:37:59 -0700543 mox.StrContains(
544 autoupdater.ChromiumOSUpdater.
Gwendal Grignou3e96cc22017-06-07 16:22:51 -0700545 REMOTE_STATEFUL_UPDATE_PATH),
Chris Sosa72312602013-04-16 15:01:56 -0700546 mox.StrContains(static_update_url),
547 mox.StrContains('--stateful_change=clean')),
Dan Shi80aa9102016-01-25 13:45:00 -0800548 timeout=mox.IgnoreArg())
Chris Sosa72312602013-04-16 15:01:56 -0700549 self.mox.ReplayAll()
550 updater = autoupdater.ChromiumOSUpdater(update_url)
551 updater.update_stateful(clobber=True)
552 self.mox.VerifyAll()
553
554
Gwendal Grignou3e96cc22017-06-07 16:22:51 -0700555 def testGetStatefulUpdateScript(self):
556 """ Test that get_stateful_update_script look for stateful_update.
557
558 Check get_stateful_update_script is trying hard to find
559 stateful_update and assert if it can't.
560
561 """
562 update_url = ('http://172.22.50.205:8082/update/lumpy-chrome-perf/'
563 'R28-4444.0.0-b2996')
564 script_loc = os.path.join(autoupdater.STATEFUL_UPDATE_PATH,
565 autoupdater.STATEFUL_UPDATE_SCRIPT)
566 self.god = mock.mock_god()
567 self.god.stub_function(os.path, 'exists')
568 host = self.mox.CreateMockAnything()
569 updater = autoupdater.ChromiumOSUpdater(update_url, host=host)
570 os.path.exists.expect_call(script_loc).and_return(False)
571 host.path_exists('/usr/local/bin/stateful_update').AndReturn(False)
572
573 self.mox.ReplayAll()
574 # No existing files, no URL, we should assert.
575 self.assertRaises(
576 autoupdater.ChromiumOSError,
577 updater.get_stateful_update_script)
578 self.mox.VerifyAll()
579
580 # No existing files, but stateful URL, we will try.
581 self.mox.ResetAll()
582 os.path.exists.expect_call(script_loc).and_return(True)
583 host.send_file(
584 script_loc,
585 '/tmp/stateful_update', delete_dest=True).AndReturn(True)
586 self.mox.ReplayAll()
587 self.assertEqual(
588 updater.get_stateful_update_script(),
589 '/tmp/stateful_update')
590 self.mox.VerifyAll()
591
592
Chris Sosac8617522014-06-09 23:22:26 +0000593 def testRollbackRootfs(self):
594 """Tests that we correctly rollback the rootfs when requested."""
595 self.mox.StubOutWithMock(autoupdater.ChromiumOSUpdater, '_run')
Chris Sosac8617522014-06-09 23:22:26 +0000596 self.mox.StubOutWithMock(autoupdater.ChromiumOSUpdater,
597 '_verify_update_completed')
598 host = self.mox.CreateMockAnything()
599 update_url = 'http://server/test/url'
600 host.hostname = 'test_host'
601
602 can_rollback_cmd = ('/usr/bin/update_engine_client --can_rollback')
603 rollback_cmd = ('/usr/bin/update_engine_client --rollback '
604 '--follow')
605
606 updater = autoupdater.ChromiumOSUpdater(update_url, host=host)
607
608 # Return an old build which shouldn't call can_rollback.
Dan Shi549fb822015-03-24 18:01:11 -0700609 updater.host.get_release_version().AndReturn('1234.0.0')
Chris Sosac8617522014-06-09 23:22:26 +0000610 autoupdater.ChromiumOSUpdater._run(rollback_cmd)
611 autoupdater.ChromiumOSUpdater._verify_update_completed()
612
613 self.mox.ReplayAll()
614 updater.rollback_rootfs(powerwash=True)
615 self.mox.VerifyAll()
616
617 self.mox.ResetAll()
618 cmd_result_1 = self.mox.CreateMockAnything()
619 cmd_result_1.exit_status = 1
620
621 # Rollback but can_rollback says we can't -- return an error.
Dan Shi549fb822015-03-24 18:01:11 -0700622 updater.host.get_release_version().AndReturn('5775.0.0')
Chris Sosac8617522014-06-09 23:22:26 +0000623 autoupdater.ChromiumOSUpdater._run(can_rollback_cmd).AndRaise(
624 error.AutoservRunError('can_rollback failed', cmd_result_1))
625 self.mox.ReplayAll()
626 self.assertRaises(autoupdater.RootFSUpdateError,
627 updater.rollback_rootfs, True)
628 self.mox.VerifyAll()
629
630 self.mox.ResetAll()
631 # Rollback >= version blacklisted.
Dan Shi549fb822015-03-24 18:01:11 -0700632 updater.host.get_release_version().AndReturn('5775.0.0')
Chris Sosac8617522014-06-09 23:22:26 +0000633 autoupdater.ChromiumOSUpdater._run(can_rollback_cmd)
634 autoupdater.ChromiumOSUpdater._run(rollback_cmd)
635 autoupdater.ChromiumOSUpdater._verify_update_completed()
636 self.mox.ReplayAll()
637 updater.rollback_rootfs(powerwash=True)
638 self.mox.VerifyAll()
639
640
Scott Zawalskieadbf702013-03-14 09:23:06 -0400641if __name__ == '__main__':
642 unittest.main()