blob: 09f6f0a206d30df3fcaea265157056dddac4bc8c [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
Richard Barnette3e8b2282018-05-15 20:42:20 +000033 Test _check_version for:
Dan Shi190c7802013-04-04 13:05:30 -070034 1. trybot paladin build.
35 update version: trybot-lumpy-paladin/R27-3837.0.0-b123
36 booted version: 3837.0.2013_03_21_1340
Dan Shib95bb862013-03-22 16:29:28 -070037
38 """
Dan Shi190c7802013-04-04 13:05:30 -070039 update_url = ('http://172.22.50.205:8082/update/trybot-lumpy-paladin/'
40 'R27-1111.0.0-b123')
Dan Shi549fb822015-03-24 18:01:11 -070041 updater = autoupdater.ChromiumOSUpdater(
42 update_url, host=self.mox.CreateMockAnything())
Dan Shib95bb862013-03-22 16:29:28 -070043
Dan Shi775ecd52013-03-27 11:51:52 -070044 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -070045 self.mox.StubOutWithMock(updater.host, 'get_release_version')
46 updater.host.get_release_version().MultipleTimes().AndReturn(
Dan Shi190c7802013-04-04 13:05:30 -070047 '1111.0.2013_03_21_1340')
Dan Shib95bb862013-03-22 16:29:28 -070048 self.mox.ReplayAll()
49
Richard Barnette3e8b2282018-05-15 20:42:20 +000050 self.assertFalse(updater._check_version())
Dan Shib95bb862013-03-22 16:29:28 -070051
Dan Shi190c7802013-04-04 13:05:30 -070052 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -070053 self.mox.StubOutWithMock(updater.host, 'get_release_version')
54 updater.host.get_release_version().MultipleTimes().AndReturn(
55 '1111.0.0-rc1')
Dan Shi190c7802013-04-04 13:05:30 -070056 self.mox.ReplayAll()
57
Richard Barnette3e8b2282018-05-15 20:42:20 +000058 self.assertFalse(updater._check_version())
Dan Shi190c7802013-04-04 13:05:30 -070059
60 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -070061 self.mox.StubOutWithMock(updater.host, 'get_release_version')
62 updater.host.get_release_version().MultipleTimes().AndReturn('1111.0.0')
Dan Shi190c7802013-04-04 13:05:30 -070063 self.mox.ReplayAll()
64
Richard Barnette3e8b2282018-05-15 20:42:20 +000065 self.assertFalse(updater._check_version())
Dan Shi190c7802013-04-04 13:05:30 -070066
Dan Shi73aa2902013-05-03 11:22:11 -070067 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -070068 self.mox.StubOutWithMock(updater.host, 'get_release_version')
69 updater.host.get_release_version().MultipleTimes().AndReturn(
Dan Shi73aa2902013-05-03 11:22:11 -070070 '4444.0.0-pgo-generate')
71 self.mox.ReplayAll()
72
Richard Barnette3e8b2282018-05-15 20:42:20 +000073 self.assertFalse(updater._check_version())
Dan Shi73aa2902013-05-03 11:22:11 -070074
75
Dan Shi190c7802013-04-04 13:05:30 -070076 def testCheckVersion_2(self):
77 """Test version check methods work for any build.
78
Richard Barnette3e8b2282018-05-15 20:42:20 +000079 Test _check_version for:
80 1. trybot paladin build.
81 update version: trybot-lumpy-paladin/R27-3837.0.0-b123
82 booted version: 3837.0.2013_03_21_1340
83
Dan Shi190c7802013-04-04 13:05:30 -070084 2. trybot release build.
85 update version: trybot-lumpy-release/R27-3837.0.0-b456
86 booted version: 3837.0.0
87
88 """
89 update_url = ('http://172.22.50.205:8082/update/trybot-lumpy-release/'
90 'R27-2222.0.0-b456')
Dan Shi549fb822015-03-24 18:01:11 -070091 updater = autoupdater.ChromiumOSUpdater(
92 update_url, host=self.mox.CreateMockAnything())
Dan Shi190c7802013-04-04 13:05:30 -070093
94 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -070095 self.mox.StubOutWithMock(updater.host, 'get_release_version')
96 updater.host.get_release_version().MultipleTimes().AndReturn(
Dan Shi190c7802013-04-04 13:05:30 -070097 '2222.0.2013_03_21_1340')
98 self.mox.ReplayAll()
99
Richard Barnette3e8b2282018-05-15 20:42:20 +0000100 self.assertFalse(updater._check_version())
Dan Shi190c7802013-04-04 13:05:30 -0700101
102 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700103 self.mox.StubOutWithMock(updater.host, 'get_release_version')
104 updater.host.get_release_version().MultipleTimes().AndReturn(
105 '2222.0.0-rc1')
Dan Shi190c7802013-04-04 13:05:30 -0700106 self.mox.ReplayAll()
107
Richard Barnette3e8b2282018-05-15 20:42:20 +0000108 self.assertFalse(updater._check_version())
Dan Shi190c7802013-04-04 13:05:30 -0700109
110 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700111 self.mox.StubOutWithMock(updater.host, 'get_release_version')
112 updater.host.get_release_version().MultipleTimes().AndReturn('2222.0.0')
Dan Shi190c7802013-04-04 13:05:30 -0700113 self.mox.ReplayAll()
114
Richard Barnette3e8b2282018-05-15 20:42:20 +0000115 self.assertFalse(updater._check_version())
Dan Shi190c7802013-04-04 13:05:30 -0700116
Dan Shi73aa2902013-05-03 11:22:11 -0700117 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700118 self.mox.StubOutWithMock(updater.host, 'get_release_version')
119 updater.host.get_release_version().MultipleTimes().AndReturn(
Dan Shi73aa2902013-05-03 11:22:11 -0700120 '4444.0.0-pgo-generate')
121 self.mox.ReplayAll()
122
Richard Barnette3e8b2282018-05-15 20:42:20 +0000123 self.assertFalse(updater._check_version())
Dan Shi73aa2902013-05-03 11:22:11 -0700124
Dan Shi190c7802013-04-04 13:05:30 -0700125
126 def testCheckVersion_3(self):
127 """Test version check methods work for any build.
128
Richard Barnette3e8b2282018-05-15 20:42:20 +0000129 Test _check_version for:
Dan Shi190c7802013-04-04 13:05:30 -0700130 3. buildbot official release build.
131 update version: lumpy-release/R27-3837.0.0
132 booted version: 3837.0.0
133
134 """
135 update_url = ('http://172.22.50.205:8082/update/lumpy-release/'
136 'R27-3333.0.0')
Dan Shi549fb822015-03-24 18:01:11 -0700137 updater = autoupdater.ChromiumOSUpdater(
138 update_url, host=self.mox.CreateMockAnything())
Dan Shi190c7802013-04-04 13:05:30 -0700139
140 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700141 self.mox.StubOutWithMock(updater.host, 'get_release_version')
142 updater.host.get_release_version().MultipleTimes().AndReturn(
Dan Shi190c7802013-04-04 13:05:30 -0700143 '3333.0.2013_03_21_1340')
144 self.mox.ReplayAll()
145
Richard Barnette3e8b2282018-05-15 20:42:20 +0000146 self.assertFalse(updater._check_version())
Dan Shi190c7802013-04-04 13:05:30 -0700147
148 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700149 self.mox.StubOutWithMock(updater.host, 'get_release_version')
150 updater.host.get_release_version().MultipleTimes().AndReturn(
151 '3333.0.0-rc1')
Dan Shi190c7802013-04-04 13:05:30 -0700152 self.mox.ReplayAll()
153
Richard Barnette3e8b2282018-05-15 20:42:20 +0000154 self.assertFalse(updater._check_version())
Dan Shi190c7802013-04-04 13:05:30 -0700155
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('3333.0.0')
Dan Shi190c7802013-04-04 13:05:30 -0700159 self.mox.ReplayAll()
160
Richard Barnette3e8b2282018-05-15 20:42:20 +0000161 self.assertTrue(updater._check_version())
Dan Shi190c7802013-04-04 13:05:30 -0700162
Dan Shi73aa2902013-05-03 11:22:11 -0700163 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700164 self.mox.StubOutWithMock(updater.host, 'get_release_version')
165 updater.host.get_release_version().MultipleTimes().AndReturn(
Dan Shi73aa2902013-05-03 11:22:11 -0700166 '4444.0.0-pgo-generate')
167 self.mox.ReplayAll()
168
Richard Barnette3e8b2282018-05-15 20:42:20 +0000169 self.assertFalse(updater._check_version())
Dan Shi73aa2902013-05-03 11:22:11 -0700170
Dan Shi190c7802013-04-04 13:05:30 -0700171
172 def testCheckVersion_4(self):
173 """Test version check methods work for any build.
174
Richard Barnette3e8b2282018-05-15 20:42:20 +0000175 Test _check_version for:
Dan Shi190c7802013-04-04 13:05:30 -0700176 4. non-official paladin rc build.
177 update version: lumpy-paladin/R27-3837.0.0-rc7
178 booted version: 3837.0.0-rc7
179
180 """
181 update_url = ('http://172.22.50.205:8082/update/lumpy-paladin/'
182 'R27-4444.0.0-rc7')
Dan Shi549fb822015-03-24 18:01:11 -0700183 updater = autoupdater.ChromiumOSUpdater(
184 update_url, host=self.mox.CreateMockAnything())
Dan Shi190c7802013-04-04 13:05:30 -0700185
186 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700187 self.mox.StubOutWithMock(updater.host, 'get_release_version')
188 updater.host.get_release_version().MultipleTimes().AndReturn(
Dan Shi190c7802013-04-04 13:05:30 -0700189 '4444.0.2013_03_21_1340')
190 self.mox.ReplayAll()
191
Richard Barnette3e8b2282018-05-15 20:42:20 +0000192 self.assertFalse(updater._check_version())
Dan Shi190c7802013-04-04 13:05:30 -0700193
194 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700195 self.mox.StubOutWithMock(updater.host, 'get_release_version')
196 updater.host.get_release_version().MultipleTimes().AndReturn(
197 '4444.0.0-rc7')
Dan Shi190c7802013-04-04 13:05:30 -0700198 self.mox.ReplayAll()
199
Richard Barnette3e8b2282018-05-15 20:42:20 +0000200 self.assertTrue(updater._check_version())
Dan Shi190c7802013-04-04 13:05:30 -0700201
202 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700203 self.mox.StubOutWithMock(updater.host, 'get_release_version')
204 updater.host.get_release_version().MultipleTimes().AndReturn('4444.0.0')
Dan Shi190c7802013-04-04 13:05:30 -0700205 self.mox.ReplayAll()
206
Richard Barnette3e8b2282018-05-15 20:42:20 +0000207 self.assertFalse(updater._check_version())
Dan Shib95bb862013-03-22 16:29:28 -0700208
Dan Shi73aa2902013-05-03 11:22:11 -0700209 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700210 self.mox.StubOutWithMock(updater.host, 'get_release_version')
211 updater.host.get_release_version().MultipleTimes().AndReturn(
Dan Shi73aa2902013-05-03 11:22:11 -0700212 '4444.0.0-pgo-generate')
213 self.mox.ReplayAll()
214
Richard Barnette3e8b2282018-05-15 20:42:20 +0000215 self.assertFalse(updater._check_version())
Dan Shi73aa2902013-05-03 11:22:11 -0700216
Dan Shib95bb862013-03-22 16:29:28 -0700217
Dan Shi7f795512013-04-12 10:08:17 -0700218 def testCheckVersion_5(self):
219 """Test version check methods work for any build.
220
Richard Barnette3e8b2282018-05-15 20:42:20 +0000221 Test _check_version for:
Dan Shi7f795512013-04-12 10:08:17 -0700222 5. chrome-perf build.
223 update version: lumpy-chrome-perf/R28-3837.0.0-b2996
224 booted version: 3837.0.0
225
226 """
227 update_url = ('http://172.22.50.205:8082/update/lumpy-chrome-perf/'
228 'R28-4444.0.0-b2996')
Dan Shi549fb822015-03-24 18:01:11 -0700229 updater = autoupdater.ChromiumOSUpdater(
230 update_url, host=self.mox.CreateMockAnything())
Dan Shi7f795512013-04-12 10:08:17 -0700231
232 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700233 self.mox.StubOutWithMock(updater.host, 'get_release_version')
234 updater.host.get_release_version().MultipleTimes().AndReturn(
Dan Shi7f795512013-04-12 10:08:17 -0700235 '4444.0.2013_03_21_1340')
236 self.mox.ReplayAll()
237
Richard Barnette3e8b2282018-05-15 20:42:20 +0000238 self.assertFalse(updater._check_version())
Dan Shi7f795512013-04-12 10:08:17 -0700239
240 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700241 self.mox.StubOutWithMock(updater.host, 'get_release_version')
242 updater.host.get_release_version().MultipleTimes().AndReturn(
243 '4444.0.0-rc7')
Dan Shi7f795512013-04-12 10:08:17 -0700244 self.mox.ReplayAll()
245
Richard Barnette3e8b2282018-05-15 20:42:20 +0000246 self.assertFalse(updater._check_version())
Dan Shi7f795512013-04-12 10:08:17 -0700247
248 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700249 self.mox.StubOutWithMock(updater.host, 'get_release_version')
250 updater.host.get_release_version().MultipleTimes().AndReturn('4444.0.0')
Dan Shi7f795512013-04-12 10:08:17 -0700251 self.mox.ReplayAll()
252
Richard Barnette3e8b2282018-05-15 20:42:20 +0000253 self.assertFalse(updater._check_version())
Dan Shi7f795512013-04-12 10:08:17 -0700254
Dan Shi73aa2902013-05-03 11:22:11 -0700255 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700256 self.mox.StubOutWithMock(updater.host, 'get_release_version')
257 updater.host.get_release_version().MultipleTimes().AndReturn(
Dan Shi73aa2902013-05-03 11:22:11 -0700258 '4444.0.0-pgo-generate')
259 self.mox.ReplayAll()
260
Richard Barnette3e8b2282018-05-15 20:42:20 +0000261 self.assertFalse(updater._check_version())
Dan Shi73aa2902013-05-03 11:22:11 -0700262
263
264 def testCheckVersion_6(self):
265 """Test version check methods work for any build.
266
Richard Barnette3e8b2282018-05-15 20:42:20 +0000267 Test _check_version for:
Dan Shi73aa2902013-05-03 11:22:11 -0700268 6. pgo-generate build.
269 update version: lumpy-release-pgo-generate/R28-3837.0.0-b2996
270 booted version: 3837.0.0-pgo-generate
271
272 """
273 update_url = ('http://172.22.50.205:8082/update/lumpy-release-pgo-'
274 'generate/R28-4444.0.0-b2996')
Dan Shi549fb822015-03-24 18:01:11 -0700275 updater = autoupdater.ChromiumOSUpdater(
276 update_url, host=self.mox.CreateMockAnything())
Dan Shi73aa2902013-05-03 11:22:11 -0700277
278 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700279 self.mox.StubOutWithMock(updater.host, 'get_release_version')
280 updater.host.get_release_version().MultipleTimes().AndReturn(
Dan Shi73aa2902013-05-03 11:22:11 -0700281 '4444.0.0-2013_03_21_1340')
282 self.mox.ReplayAll()
283
Richard Barnette3e8b2282018-05-15 20:42:20 +0000284 self.assertFalse(updater._check_version())
Dan Shi73aa2902013-05-03 11:22:11 -0700285
286 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700287 self.mox.StubOutWithMock(updater.host, 'get_release_version')
288 updater.host.get_release_version().MultipleTimes().AndReturn(
289 '4444.0.0-rc7')
Dan Shi73aa2902013-05-03 11:22:11 -0700290 self.mox.ReplayAll()
291
Richard Barnette3e8b2282018-05-15 20:42:20 +0000292 self.assertFalse(updater._check_version())
Dan Shi73aa2902013-05-03 11:22:11 -0700293
294 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700295 self.mox.StubOutWithMock(updater.host, 'get_release_version')
296 updater.host.get_release_version().MultipleTimes().AndReturn('4444.0.0')
Dan Shi73aa2902013-05-03 11:22:11 -0700297 self.mox.ReplayAll()
298
Richard Barnette3e8b2282018-05-15 20:42:20 +0000299 self.assertFalse(updater._check_version())
Dan Shi73aa2902013-05-03 11:22:11 -0700300
301 self.mox.UnsetStubs()
Dan Shi549fb822015-03-24 18:01:11 -0700302 self.mox.StubOutWithMock(updater.host, 'get_release_version')
303 updater.host.get_release_version().MultipleTimes().AndReturn(
Dan Shi73aa2902013-05-03 11:22:11 -0700304 '4444.0.0-pgo-generate')
305 self.mox.ReplayAll()
306
Richard Barnette3e8b2282018-05-15 20:42:20 +0000307 self.assertFalse(updater._check_version())
Dan Shi73aa2902013-05-03 11:22:11 -0700308
Dan Shi7f795512013-04-12 10:08:17 -0700309
Rebecca Silbersteinb2d204b2015-08-04 10:45:50 -0700310 def testCheckVersion_7(self):
311 """Test version check methods work for a test-ap build.
312
Richard Barnette3e8b2282018-05-15 20:42:20 +0000313 Test _check_version for:
Rebecca Silbersteinb2d204b2015-08-04 10:45:50 -0700314 6. test-ap build.
315 update version: trybot-stumpy-test-ap/R46-7298.0.0-b23
316 booted version: 7298.0.0
317
318 """
319 update_url = ('http://100.107.160.2:8082/update/trybot-stumpy-test-api'
320 '/R46-7298.0.0-b23')
321 updater = autoupdater.ChromiumOSUpdater(
322 update_url, host=self.mox.CreateMockAnything())
323
324 self.mox.UnsetStubs()
325 self.mox.StubOutWithMock(updater.host, 'get_release_version')
326 updater.host.get_release_version().MultipleTimes().AndReturn(
327 '7298.0.2015_07_24_1640')
328 self.mox.ReplayAll()
329
Richard Barnette3e8b2282018-05-15 20:42:20 +0000330 self.assertFalse(updater._check_version())
Rebecca Silbersteinb2d204b2015-08-04 10:45:50 -0700331
332 self.mox.UnsetStubs()
333 self.mox.StubOutWithMock(updater.host, 'get_release_version')
334 updater.host.get_release_version().MultipleTimes().AndReturn(
335 '7298.0.2015_07_24_1640')
336 self.mox.ReplayAll()
337
Richard Barnette3e8b2282018-05-15 20:42:20 +0000338 self.assertFalse(updater._check_version())
Rebecca Silbersteinb2d204b2015-08-04 10:45:50 -0700339
340 self.mox.UnsetStubs()
341 self.mox.StubOutWithMock(updater.host, 'get_release_version')
342 updater.host.get_release_version().MultipleTimes().AndReturn('7298.0.0')
343 self.mox.ReplayAll()
344
Richard Barnette3e8b2282018-05-15 20:42:20 +0000345 self.assertFalse(updater._check_version())
Rebecca Silbersteinb2d204b2015-08-04 10:45:50 -0700346
347 self.mox.UnsetStubs()
348 self.mox.StubOutWithMock(updater.host, 'get_release_version')
349 updater.host.get_release_version().MultipleTimes().AndReturn(
350 '7298.0.0')
351 self.mox.ReplayAll()
352
Richard Barnette3e8b2282018-05-15 20:42:20 +0000353 self.assertFalse(updater._check_version())
Rebecca Silbersteinb2d204b2015-08-04 10:45:50 -0700354
355
Luigi Semenzatof15c8fc2017-03-03 14:12:40 -0800356 def _host_run_for_update(self, cmd, exception=None,
357 bad_update_status=False):
358 """Helper function for AU tests.
359
360 @param host: the test host
361 @param cmd: the command to be recorded
362 @param exception: the exception to be recorded, or None
363 """
364 if exception:
365 self.host.run(command=cmd).AndRaise(exception)
366 else:
367 result = self.mox.CreateMockAnything()
368 if bad_update_status:
369 # Pick randomly one unexpected status
370 result.stdout = 'UPDATE_STATUS_UPDATED_NEED_REBOOT'
371 else:
372 result.stdout = 'UPDATE_STATUS_IDLE'
373 result.status = 0
374 self.host.run(command=cmd).AndReturn(result)
375
376
Don Garrettdf8aef72013-12-16 11:12:41 -0800377 def testTriggerUpdate(self):
378 """Tests that we correctly handle updater errors."""
Don Garrettdf8aef72013-12-16 11:12:41 -0800379 update_url = 'http://server/test/url'
Luigi Semenzatof15c8fc2017-03-03 14:12:40 -0800380 self.host = self.mox.CreateMockAnything()
381 self.mox.StubOutWithMock(self.host, 'run')
Shuqian Zhaod9992722016-02-29 12:26:38 -0800382 self.mox.StubOutWithMock(autoupdater.ChromiumOSUpdater,
Richard Barnette3e8b2282018-05-15 20:42:20 +0000383 '_get_last_update_error')
Luigi Semenzatof15c8fc2017-03-03 14:12:40 -0800384 self.host.hostname = 'test_host'
385 updater_control_bin = '/usr/bin/update_engine_client'
386 test_url = 'http://server/test/url'
387 expected_wait_cmd = ('%s -status | grep CURRENT_OP' %
388 updater_control_bin)
389 expected_cmd = ('%s --check_for_update --omaha_url=%s' %
390 (updater_control_bin, test_url))
391 self.mox.StubOutWithMock(time, "sleep")
392 UPDATE_ENGINE_RETRY_WAIT_TIME=5
Don Garrettdf8aef72013-12-16 11:12:41 -0800393
Luigi Semenzatoe76d9f82016-11-21 11:15:10 -0800394 # Generic SSH Error.
Don Garrettdf8aef72013-12-16 11:12:41 -0800395 cmd_result_255 = self.mox.CreateMockAnything()
396 cmd_result_255.exit_status = 255
397
Shuqian Zhaod9992722016-02-29 12:26:38 -0800398 # Command Failed Error
399 cmd_result_1 = self.mox.CreateMockAnything()
400 cmd_result_1.exit_status = 1
Luigi Semenzatof15c8fc2017-03-03 14:12:40 -0800401
402 # Error 37
403 cmd_result_37 = self.mox.CreateMockAnything()
404 cmd_result_37.exit_status = 37
405
406 updater = autoupdater.ChromiumOSUpdater(update_url, host=self.host)
407
408 # (SUCCESS) Expect one wait command and one status command.
409 self._host_run_for_update(expected_wait_cmd)
410 self._host_run_for_update(expected_cmd)
411
412 # (SUCCESS) Test with one retry to wait for update-engine.
413 self._host_run_for_update(expected_wait_cmd, exception=
414 error.AutoservRunError('non-zero status', cmd_result_1))
415 time.sleep(UPDATE_ENGINE_RETRY_WAIT_TIME)
416 self._host_run_for_update(expected_wait_cmd)
417 self._host_run_for_update(expected_cmd)
418
419 # (SUCCESS) One-time SSH timeout, then success on retry.
420 self._host_run_for_update(expected_wait_cmd)
421 self._host_run_for_update(expected_cmd, exception=
422 error.AutoservSSHTimeout('ssh timed out', cmd_result_255))
423 self._host_run_for_update(expected_cmd)
424
425 # (SUCCESS) One-time ERROR 37, then success.
426 self._host_run_for_update(expected_wait_cmd)
427 self._host_run_for_update(expected_cmd, exception=
428 error.AutoservRunError('ERROR_CODE=37', cmd_result_37))
429 self._host_run_for_update(expected_cmd)
430
431 # (FAILURE) Bad status of update engine.
432 self._host_run_for_update(expected_wait_cmd)
433 self._host_run_for_update(expected_cmd, bad_update_status=True,
434 exception=error.InstallError(
435 'host is not in installable state'))
436
437 # (FAILURE) Two-time SSH timeout.
438 self._host_run_for_update(expected_wait_cmd)
439 self._host_run_for_update(expected_cmd, exception=
440 error.AutoservSSHTimeout('ssh timed out', cmd_result_255))
441 self._host_run_for_update(expected_cmd, exception=
442 error.AutoservSSHTimeout('ssh timed out', cmd_result_255))
443
444 # (FAILURE) SSH Permission Error
445 self._host_run_for_update(expected_wait_cmd)
446 self._host_run_for_update(expected_cmd, exception=
447 error.AutoservSshPermissionDeniedError('no permission',
448 cmd_result_255))
449
450 # (FAILURE) Other ssh failure
451 self._host_run_for_update(expected_wait_cmd)
452 self._host_run_for_update(expected_cmd, exception=
453 error.AutoservSshPermissionDeniedError('no permission',
454 cmd_result_255))
455 # (FAILURE) Other error
456 self._host_run_for_update(expected_wait_cmd)
457 self._host_run_for_update(expected_cmd, exception=
Luigi Semenzatoe76d9f82016-11-21 11:15:10 -0800458 error.AutoservRunError("unknown error", cmd_result_1))
Shuqian Zhaod9992722016-02-29 12:26:38 -0800459
Don Garrettdf8aef72013-12-16 11:12:41 -0800460 self.mox.ReplayAll()
461
Luigi Semenzatoe76d9f82016-11-21 11:15:10 -0800462 # Expect success
463 updater.trigger_update()
464 updater.trigger_update()
Don Garrettdf8aef72013-12-16 11:12:41 -0800465 updater.trigger_update()
Luigi Semenzatof15c8fc2017-03-03 14:12:40 -0800466 updater.trigger_update()
Don Garrettdf8aef72013-12-16 11:12:41 -0800467
Luigi Semenzatoe76d9f82016-11-21 11:15:10 -0800468 # Expect errors as listed above
469 self.assertRaises(autoupdater.RootFSUpdateError, updater.trigger_update)
470 self.assertRaises(autoupdater.RootFSUpdateError, updater.trigger_update)
471 self.assertRaises(autoupdater.RootFSUpdateError, updater.trigger_update)
472 self.assertRaises(autoupdater.RootFSUpdateError, updater.trigger_update)
Luigi Semenzatof15c8fc2017-03-03 14:12:40 -0800473 self.assertRaises(autoupdater.RootFSUpdateError, updater.trigger_update)
Don Garrettdf8aef72013-12-16 11:12:41 -0800474
475 self.mox.VerifyAll()
476
477
Chris Sosa72312602013-04-16 15:01:56 -0700478 def testUpdateStateful(self):
479 """Tests that we call the stateful update script with the correct args.
480 """
481 self.mox.StubOutWithMock(autoupdater.ChromiumOSUpdater, '_run')
Chris Sosab9ada9b2013-06-12 12:47:47 -0700482 self.mox.StubOutWithMock(autoupdater.ChromiumOSUpdater,
483 'get_stateful_update_script')
Chris Sosa72312602013-04-16 15:01:56 -0700484 update_url = ('http://172.22.50.205:8082/update/lumpy-chrome-perf/'
485 'R28-4444.0.0-b2996')
joychen03eaad92013-06-26 09:55:21 -0700486 static_update_url = ('http://172.22.50.205:8082/static/'
Chris Sosa72312602013-04-16 15:01:56 -0700487 'lumpy-chrome-perf/R28-4444.0.0-b2996')
488
489 # Test with clobber=False.
Chris Sosab9ada9b2013-06-12 12:47:47 -0700490 autoupdater.ChromiumOSUpdater.get_stateful_update_script().AndReturn(
Richard Barnette3e8b2282018-05-15 20:42:20 +0000491 autoupdater._REMOTE_STATEFUL_UPDATE_PATH)
Chris Sosa72312602013-04-16 15:01:56 -0700492 autoupdater.ChromiumOSUpdater._run(
493 mox.And(
Gilad Arnold0c0df732015-09-21 06:37:59 -0700494 mox.StrContains(
Richard Barnette3e8b2282018-05-15 20:42:20 +0000495 autoupdater._REMOTE_STATEFUL_UPDATE_PATH),
Chris Sosa72312602013-04-16 15:01:56 -0700496 mox.StrContains(static_update_url),
497 mox.Not(mox.StrContains('--stateful_change=clean'))),
Dan Shi80aa9102016-01-25 13:45:00 -0800498 timeout=mox.IgnoreArg())
Chris Sosa72312602013-04-16 15:01:56 -0700499
500 self.mox.ReplayAll()
501 updater = autoupdater.ChromiumOSUpdater(update_url)
502 updater.update_stateful(clobber=False)
503 self.mox.VerifyAll()
504
505 # Test with clobber=True.
506 self.mox.ResetAll()
Chris Sosab9ada9b2013-06-12 12:47:47 -0700507 autoupdater.ChromiumOSUpdater.get_stateful_update_script().AndReturn(
Richard Barnette3e8b2282018-05-15 20:42:20 +0000508 autoupdater._REMOTE_STATEFUL_UPDATE_PATH)
Chris Sosa72312602013-04-16 15:01:56 -0700509 autoupdater.ChromiumOSUpdater._run(
510 mox.And(
Gilad Arnold0c0df732015-09-21 06:37:59 -0700511 mox.StrContains(
Richard Barnette3e8b2282018-05-15 20:42:20 +0000512 autoupdater._REMOTE_STATEFUL_UPDATE_PATH),
Chris Sosa72312602013-04-16 15:01:56 -0700513 mox.StrContains(static_update_url),
514 mox.StrContains('--stateful_change=clean')),
Dan Shi80aa9102016-01-25 13:45:00 -0800515 timeout=mox.IgnoreArg())
Chris Sosa72312602013-04-16 15:01:56 -0700516 self.mox.ReplayAll()
517 updater = autoupdater.ChromiumOSUpdater(update_url)
518 updater.update_stateful(clobber=True)
519 self.mox.VerifyAll()
520
521
Gwendal Grignou3e96cc22017-06-07 16:22:51 -0700522 def testGetStatefulUpdateScript(self):
523 """ Test that get_stateful_update_script look for stateful_update.
524
525 Check get_stateful_update_script is trying hard to find
526 stateful_update and assert if it can't.
527
528 """
529 update_url = ('http://172.22.50.205:8082/update/lumpy-chrome-perf/'
530 'R28-4444.0.0-b2996')
Richard Barnette3e8b2282018-05-15 20:42:20 +0000531 script_loc = os.path.join(autoupdater._STATEFUL_UPDATE_PATH,
532 autoupdater._STATEFUL_UPDATE_SCRIPT)
Gwendal Grignou3e96cc22017-06-07 16:22:51 -0700533 self.god = mock.mock_god()
534 self.god.stub_function(os.path, 'exists')
535 host = self.mox.CreateMockAnything()
536 updater = autoupdater.ChromiumOSUpdater(update_url, host=host)
537 os.path.exists.expect_call(script_loc).and_return(False)
538 host.path_exists('/usr/local/bin/stateful_update').AndReturn(False)
539
540 self.mox.ReplayAll()
541 # No existing files, no URL, we should assert.
542 self.assertRaises(
543 autoupdater.ChromiumOSError,
544 updater.get_stateful_update_script)
545 self.mox.VerifyAll()
546
547 # No existing files, but stateful URL, we will try.
548 self.mox.ResetAll()
549 os.path.exists.expect_call(script_loc).and_return(True)
550 host.send_file(
551 script_loc,
552 '/tmp/stateful_update', delete_dest=True).AndReturn(True)
553 self.mox.ReplayAll()
554 self.assertEqual(
555 updater.get_stateful_update_script(),
556 '/tmp/stateful_update')
557 self.mox.VerifyAll()
558
559
Chris Sosac8617522014-06-09 23:22:26 +0000560 def testRollbackRootfs(self):
561 """Tests that we correctly rollback the rootfs when requested."""
562 self.mox.StubOutWithMock(autoupdater.ChromiumOSUpdater, '_run')
Chris Sosac8617522014-06-09 23:22:26 +0000563 self.mox.StubOutWithMock(autoupdater.ChromiumOSUpdater,
564 '_verify_update_completed')
565 host = self.mox.CreateMockAnything()
566 update_url = 'http://server/test/url'
567 host.hostname = 'test_host'
568
569 can_rollback_cmd = ('/usr/bin/update_engine_client --can_rollback')
570 rollback_cmd = ('/usr/bin/update_engine_client --rollback '
571 '--follow')
572
573 updater = autoupdater.ChromiumOSUpdater(update_url, host=host)
574
575 # Return an old build which shouldn't call can_rollback.
Dan Shi549fb822015-03-24 18:01:11 -0700576 updater.host.get_release_version().AndReturn('1234.0.0')
Chris Sosac8617522014-06-09 23:22:26 +0000577 autoupdater.ChromiumOSUpdater._run(rollback_cmd)
578 autoupdater.ChromiumOSUpdater._verify_update_completed()
579
580 self.mox.ReplayAll()
581 updater.rollback_rootfs(powerwash=True)
582 self.mox.VerifyAll()
583
584 self.mox.ResetAll()
585 cmd_result_1 = self.mox.CreateMockAnything()
586 cmd_result_1.exit_status = 1
587
588 # Rollback but can_rollback says we can't -- return an error.
Dan Shi549fb822015-03-24 18:01:11 -0700589 updater.host.get_release_version().AndReturn('5775.0.0')
Chris Sosac8617522014-06-09 23:22:26 +0000590 autoupdater.ChromiumOSUpdater._run(can_rollback_cmd).AndRaise(
591 error.AutoservRunError('can_rollback failed', cmd_result_1))
592 self.mox.ReplayAll()
593 self.assertRaises(autoupdater.RootFSUpdateError,
594 updater.rollback_rootfs, True)
595 self.mox.VerifyAll()
596
597 self.mox.ResetAll()
598 # Rollback >= version blacklisted.
Dan Shi549fb822015-03-24 18:01:11 -0700599 updater.host.get_release_version().AndReturn('5775.0.0')
Chris Sosac8617522014-06-09 23:22:26 +0000600 autoupdater.ChromiumOSUpdater._run(can_rollback_cmd)
601 autoupdater.ChromiumOSUpdater._run(rollback_cmd)
602 autoupdater.ChromiumOSUpdater._verify_update_completed()
603 self.mox.ReplayAll()
604 updater.rollback_rootfs(powerwash=True)
605 self.mox.VerifyAll()
606
607
Scott Zawalskieadbf702013-03-14 09:23:06 -0400608if __name__ == '__main__':
609 unittest.main()