Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 1 | #!/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 Shi | b95bb86 | 2013-03-22 16:29:28 -0700 | [diff] [blame] | 6 | import mox |
Gwendal Grignou | 3e96cc2 | 2017-06-07 16:22:51 -0700 | [diff] [blame] | 7 | import os |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 8 | import unittest |
| 9 | |
| 10 | import common |
Luigi Semenzato | f15c8fc | 2017-03-03 14:12:40 -0800 | [diff] [blame] | 11 | import time |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 12 | |
| 13 | import autoupdater |
Don Garrett | df8aef7 | 2013-12-16 11:12:41 -0800 | [diff] [blame] | 14 | from autotest_lib.client.common_lib import error |
Gwendal Grignou | 3e96cc2 | 2017-06-07 16:22:51 -0700 | [diff] [blame] | 15 | from autotest_lib.client.common_lib.test_utils import mock |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 16 | |
Dan Shi | b95bb86 | 2013-03-22 16:29:28 -0700 | [diff] [blame] | 17 | class TestAutoUpdater(mox.MoxTestBase): |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 18 | """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 | |
Luigi Semenzato | f15c8fc | 2017-03-03 14:12:40 -0800 | [diff] [blame] | 30 | def _host_run_for_update(self, cmd, exception=None, |
| 31 | bad_update_status=False): |
| 32 | """Helper function for AU tests. |
| 33 | |
| 34 | @param host: the test host |
| 35 | @param cmd: the command to be recorded |
| 36 | @param exception: the exception to be recorded, or None |
| 37 | """ |
| 38 | if exception: |
| 39 | self.host.run(command=cmd).AndRaise(exception) |
| 40 | else: |
| 41 | result = self.mox.CreateMockAnything() |
| 42 | if bad_update_status: |
| 43 | # Pick randomly one unexpected status |
| 44 | result.stdout = 'UPDATE_STATUS_UPDATED_NEED_REBOOT' |
| 45 | else: |
| 46 | result.stdout = 'UPDATE_STATUS_IDLE' |
| 47 | result.status = 0 |
| 48 | self.host.run(command=cmd).AndReturn(result) |
| 49 | |
| 50 | |
Don Garrett | df8aef7 | 2013-12-16 11:12:41 -0800 | [diff] [blame] | 51 | def testTriggerUpdate(self): |
| 52 | """Tests that we correctly handle updater errors.""" |
Don Garrett | df8aef7 | 2013-12-16 11:12:41 -0800 | [diff] [blame] | 53 | update_url = 'http://server/test/url' |
Luigi Semenzato | f15c8fc | 2017-03-03 14:12:40 -0800 | [diff] [blame] | 54 | self.host = self.mox.CreateMockAnything() |
| 55 | self.mox.StubOutWithMock(self.host, 'run') |
Shuqian Zhao | d999272 | 2016-02-29 12:26:38 -0800 | [diff] [blame] | 56 | self.mox.StubOutWithMock(autoupdater.ChromiumOSUpdater, |
Richard Barnette | 3e8b228 | 2018-05-15 20:42:20 +0000 | [diff] [blame] | 57 | '_get_last_update_error') |
Luigi Semenzato | f15c8fc | 2017-03-03 14:12:40 -0800 | [diff] [blame] | 58 | self.host.hostname = 'test_host' |
| 59 | updater_control_bin = '/usr/bin/update_engine_client' |
| 60 | test_url = 'http://server/test/url' |
| 61 | expected_wait_cmd = ('%s -status | grep CURRENT_OP' % |
| 62 | updater_control_bin) |
| 63 | expected_cmd = ('%s --check_for_update --omaha_url=%s' % |
| 64 | (updater_control_bin, test_url)) |
| 65 | self.mox.StubOutWithMock(time, "sleep") |
| 66 | UPDATE_ENGINE_RETRY_WAIT_TIME=5 |
Don Garrett | df8aef7 | 2013-12-16 11:12:41 -0800 | [diff] [blame] | 67 | |
Luigi Semenzato | e76d9f8 | 2016-11-21 11:15:10 -0800 | [diff] [blame] | 68 | # Generic SSH Error. |
Don Garrett | df8aef7 | 2013-12-16 11:12:41 -0800 | [diff] [blame] | 69 | cmd_result_255 = self.mox.CreateMockAnything() |
| 70 | cmd_result_255.exit_status = 255 |
| 71 | |
Shuqian Zhao | d999272 | 2016-02-29 12:26:38 -0800 | [diff] [blame] | 72 | # Command Failed Error |
| 73 | cmd_result_1 = self.mox.CreateMockAnything() |
| 74 | cmd_result_1.exit_status = 1 |
Luigi Semenzato | f15c8fc | 2017-03-03 14:12:40 -0800 | [diff] [blame] | 75 | |
| 76 | # Error 37 |
| 77 | cmd_result_37 = self.mox.CreateMockAnything() |
| 78 | cmd_result_37.exit_status = 37 |
| 79 | |
| 80 | updater = autoupdater.ChromiumOSUpdater(update_url, host=self.host) |
| 81 | |
| 82 | # (SUCCESS) Expect one wait command and one status command. |
| 83 | self._host_run_for_update(expected_wait_cmd) |
| 84 | self._host_run_for_update(expected_cmd) |
| 85 | |
| 86 | # (SUCCESS) Test with one retry to wait for update-engine. |
| 87 | self._host_run_for_update(expected_wait_cmd, exception= |
| 88 | error.AutoservRunError('non-zero status', cmd_result_1)) |
| 89 | time.sleep(UPDATE_ENGINE_RETRY_WAIT_TIME) |
| 90 | self._host_run_for_update(expected_wait_cmd) |
| 91 | self._host_run_for_update(expected_cmd) |
| 92 | |
| 93 | # (SUCCESS) One-time SSH timeout, then success on retry. |
| 94 | self._host_run_for_update(expected_wait_cmd) |
| 95 | self._host_run_for_update(expected_cmd, exception= |
| 96 | error.AutoservSSHTimeout('ssh timed out', cmd_result_255)) |
| 97 | self._host_run_for_update(expected_cmd) |
| 98 | |
| 99 | # (SUCCESS) One-time ERROR 37, then success. |
| 100 | self._host_run_for_update(expected_wait_cmd) |
| 101 | self._host_run_for_update(expected_cmd, exception= |
| 102 | error.AutoservRunError('ERROR_CODE=37', cmd_result_37)) |
| 103 | self._host_run_for_update(expected_cmd) |
| 104 | |
| 105 | # (FAILURE) Bad status of update engine. |
| 106 | self._host_run_for_update(expected_wait_cmd) |
| 107 | self._host_run_for_update(expected_cmd, bad_update_status=True, |
| 108 | exception=error.InstallError( |
| 109 | 'host is not in installable state')) |
| 110 | |
| 111 | # (FAILURE) Two-time SSH timeout. |
| 112 | self._host_run_for_update(expected_wait_cmd) |
| 113 | self._host_run_for_update(expected_cmd, exception= |
| 114 | error.AutoservSSHTimeout('ssh timed out', cmd_result_255)) |
| 115 | self._host_run_for_update(expected_cmd, exception= |
| 116 | error.AutoservSSHTimeout('ssh timed out', cmd_result_255)) |
| 117 | |
| 118 | # (FAILURE) SSH Permission Error |
| 119 | self._host_run_for_update(expected_wait_cmd) |
| 120 | self._host_run_for_update(expected_cmd, exception= |
| 121 | error.AutoservSshPermissionDeniedError('no permission', |
| 122 | cmd_result_255)) |
| 123 | |
| 124 | # (FAILURE) Other ssh failure |
| 125 | self._host_run_for_update(expected_wait_cmd) |
| 126 | self._host_run_for_update(expected_cmd, exception= |
| 127 | error.AutoservSshPermissionDeniedError('no permission', |
| 128 | cmd_result_255)) |
| 129 | # (FAILURE) Other error |
| 130 | self._host_run_for_update(expected_wait_cmd) |
| 131 | self._host_run_for_update(expected_cmd, exception= |
Luigi Semenzato | e76d9f8 | 2016-11-21 11:15:10 -0800 | [diff] [blame] | 132 | error.AutoservRunError("unknown error", cmd_result_1)) |
Shuqian Zhao | d999272 | 2016-02-29 12:26:38 -0800 | [diff] [blame] | 133 | |
Don Garrett | df8aef7 | 2013-12-16 11:12:41 -0800 | [diff] [blame] | 134 | self.mox.ReplayAll() |
| 135 | |
Luigi Semenzato | e76d9f8 | 2016-11-21 11:15:10 -0800 | [diff] [blame] | 136 | # Expect success |
| 137 | updater.trigger_update() |
| 138 | updater.trigger_update() |
Don Garrett | df8aef7 | 2013-12-16 11:12:41 -0800 | [diff] [blame] | 139 | updater.trigger_update() |
Luigi Semenzato | f15c8fc | 2017-03-03 14:12:40 -0800 | [diff] [blame] | 140 | updater.trigger_update() |
Don Garrett | df8aef7 | 2013-12-16 11:12:41 -0800 | [diff] [blame] | 141 | |
Luigi Semenzato | e76d9f8 | 2016-11-21 11:15:10 -0800 | [diff] [blame] | 142 | # Expect errors as listed above |
| 143 | self.assertRaises(autoupdater.RootFSUpdateError, updater.trigger_update) |
| 144 | self.assertRaises(autoupdater.RootFSUpdateError, updater.trigger_update) |
| 145 | self.assertRaises(autoupdater.RootFSUpdateError, updater.trigger_update) |
| 146 | self.assertRaises(autoupdater.RootFSUpdateError, updater.trigger_update) |
Luigi Semenzato | f15c8fc | 2017-03-03 14:12:40 -0800 | [diff] [blame] | 147 | self.assertRaises(autoupdater.RootFSUpdateError, updater.trigger_update) |
Don Garrett | df8aef7 | 2013-12-16 11:12:41 -0800 | [diff] [blame] | 148 | |
| 149 | self.mox.VerifyAll() |
| 150 | |
| 151 | |
Chris Sosa | 7231260 | 2013-04-16 15:01:56 -0700 | [diff] [blame] | 152 | def testUpdateStateful(self): |
| 153 | """Tests that we call the stateful update script with the correct args. |
| 154 | """ |
| 155 | self.mox.StubOutWithMock(autoupdater.ChromiumOSUpdater, '_run') |
Chris Sosa | b9ada9b | 2013-06-12 12:47:47 -0700 | [diff] [blame] | 156 | self.mox.StubOutWithMock(autoupdater.ChromiumOSUpdater, |
| 157 | 'get_stateful_update_script') |
Chris Sosa | 7231260 | 2013-04-16 15:01:56 -0700 | [diff] [blame] | 158 | update_url = ('http://172.22.50.205:8082/update/lumpy-chrome-perf/' |
| 159 | 'R28-4444.0.0-b2996') |
joychen | 03eaad9 | 2013-06-26 09:55:21 -0700 | [diff] [blame] | 160 | static_update_url = ('http://172.22.50.205:8082/static/' |
Chris Sosa | 7231260 | 2013-04-16 15:01:56 -0700 | [diff] [blame] | 161 | 'lumpy-chrome-perf/R28-4444.0.0-b2996') |
| 162 | |
| 163 | # Test with clobber=False. |
Chris Sosa | b9ada9b | 2013-06-12 12:47:47 -0700 | [diff] [blame] | 164 | autoupdater.ChromiumOSUpdater.get_stateful_update_script().AndReturn( |
Richard Barnette | 3e8b228 | 2018-05-15 20:42:20 +0000 | [diff] [blame] | 165 | autoupdater._REMOTE_STATEFUL_UPDATE_PATH) |
Chris Sosa | 7231260 | 2013-04-16 15:01:56 -0700 | [diff] [blame] | 166 | autoupdater.ChromiumOSUpdater._run( |
| 167 | mox.And( |
Gilad Arnold | 0c0df73 | 2015-09-21 06:37:59 -0700 | [diff] [blame] | 168 | mox.StrContains( |
Richard Barnette | 3e8b228 | 2018-05-15 20:42:20 +0000 | [diff] [blame] | 169 | autoupdater._REMOTE_STATEFUL_UPDATE_PATH), |
Chris Sosa | 7231260 | 2013-04-16 15:01:56 -0700 | [diff] [blame] | 170 | mox.StrContains(static_update_url), |
| 171 | mox.Not(mox.StrContains('--stateful_change=clean'))), |
Dan Shi | 80aa910 | 2016-01-25 13:45:00 -0800 | [diff] [blame] | 172 | timeout=mox.IgnoreArg()) |
Chris Sosa | 7231260 | 2013-04-16 15:01:56 -0700 | [diff] [blame] | 173 | |
| 174 | self.mox.ReplayAll() |
| 175 | updater = autoupdater.ChromiumOSUpdater(update_url) |
| 176 | updater.update_stateful(clobber=False) |
| 177 | self.mox.VerifyAll() |
| 178 | |
| 179 | # Test with clobber=True. |
| 180 | self.mox.ResetAll() |
Chris Sosa | b9ada9b | 2013-06-12 12:47:47 -0700 | [diff] [blame] | 181 | autoupdater.ChromiumOSUpdater.get_stateful_update_script().AndReturn( |
Richard Barnette | 3e8b228 | 2018-05-15 20:42:20 +0000 | [diff] [blame] | 182 | autoupdater._REMOTE_STATEFUL_UPDATE_PATH) |
Chris Sosa | 7231260 | 2013-04-16 15:01:56 -0700 | [diff] [blame] | 183 | autoupdater.ChromiumOSUpdater._run( |
| 184 | mox.And( |
Gilad Arnold | 0c0df73 | 2015-09-21 06:37:59 -0700 | [diff] [blame] | 185 | mox.StrContains( |
Richard Barnette | 3e8b228 | 2018-05-15 20:42:20 +0000 | [diff] [blame] | 186 | autoupdater._REMOTE_STATEFUL_UPDATE_PATH), |
Chris Sosa | 7231260 | 2013-04-16 15:01:56 -0700 | [diff] [blame] | 187 | mox.StrContains(static_update_url), |
| 188 | mox.StrContains('--stateful_change=clean')), |
Dan Shi | 80aa910 | 2016-01-25 13:45:00 -0800 | [diff] [blame] | 189 | timeout=mox.IgnoreArg()) |
Chris Sosa | 7231260 | 2013-04-16 15:01:56 -0700 | [diff] [blame] | 190 | self.mox.ReplayAll() |
| 191 | updater = autoupdater.ChromiumOSUpdater(update_url) |
| 192 | updater.update_stateful(clobber=True) |
| 193 | self.mox.VerifyAll() |
| 194 | |
| 195 | |
Gwendal Grignou | 3e96cc2 | 2017-06-07 16:22:51 -0700 | [diff] [blame] | 196 | def testGetStatefulUpdateScript(self): |
| 197 | """ Test that get_stateful_update_script look for stateful_update. |
| 198 | |
| 199 | Check get_stateful_update_script is trying hard to find |
| 200 | stateful_update and assert if it can't. |
| 201 | |
| 202 | """ |
| 203 | update_url = ('http://172.22.50.205:8082/update/lumpy-chrome-perf/' |
| 204 | 'R28-4444.0.0-b2996') |
Richard Barnette | 3e8b228 | 2018-05-15 20:42:20 +0000 | [diff] [blame] | 205 | script_loc = os.path.join(autoupdater._STATEFUL_UPDATE_PATH, |
| 206 | autoupdater._STATEFUL_UPDATE_SCRIPT) |
Gwendal Grignou | 3e96cc2 | 2017-06-07 16:22:51 -0700 | [diff] [blame] | 207 | self.god = mock.mock_god() |
| 208 | self.god.stub_function(os.path, 'exists') |
| 209 | host = self.mox.CreateMockAnything() |
| 210 | updater = autoupdater.ChromiumOSUpdater(update_url, host=host) |
| 211 | os.path.exists.expect_call(script_loc).and_return(False) |
| 212 | host.path_exists('/usr/local/bin/stateful_update').AndReturn(False) |
| 213 | |
| 214 | self.mox.ReplayAll() |
| 215 | # No existing files, no URL, we should assert. |
| 216 | self.assertRaises( |
Richard Barnette | 9d43e56 | 2018-06-05 17:20:10 +0000 | [diff] [blame] | 217 | autoupdater.StatefulUpdateError, |
Gwendal Grignou | 3e96cc2 | 2017-06-07 16:22:51 -0700 | [diff] [blame] | 218 | updater.get_stateful_update_script) |
| 219 | self.mox.VerifyAll() |
| 220 | |
| 221 | # No existing files, but stateful URL, we will try. |
| 222 | self.mox.ResetAll() |
| 223 | os.path.exists.expect_call(script_loc).and_return(True) |
| 224 | host.send_file( |
| 225 | script_loc, |
| 226 | '/tmp/stateful_update', delete_dest=True).AndReturn(True) |
| 227 | self.mox.ReplayAll() |
| 228 | self.assertEqual( |
| 229 | updater.get_stateful_update_script(), |
| 230 | '/tmp/stateful_update') |
| 231 | self.mox.VerifyAll() |
| 232 | |
| 233 | |
Chris Sosa | c861752 | 2014-06-09 23:22:26 +0000 | [diff] [blame] | 234 | def testRollbackRootfs(self): |
| 235 | """Tests that we correctly rollback the rootfs when requested.""" |
| 236 | self.mox.StubOutWithMock(autoupdater.ChromiumOSUpdater, '_run') |
Chris Sosa | c861752 | 2014-06-09 23:22:26 +0000 | [diff] [blame] | 237 | self.mox.StubOutWithMock(autoupdater.ChromiumOSUpdater, |
| 238 | '_verify_update_completed') |
| 239 | host = self.mox.CreateMockAnything() |
| 240 | update_url = 'http://server/test/url' |
| 241 | host.hostname = 'test_host' |
| 242 | |
| 243 | can_rollback_cmd = ('/usr/bin/update_engine_client --can_rollback') |
| 244 | rollback_cmd = ('/usr/bin/update_engine_client --rollback ' |
| 245 | '--follow') |
| 246 | |
| 247 | updater = autoupdater.ChromiumOSUpdater(update_url, host=host) |
| 248 | |
| 249 | # Return an old build which shouldn't call can_rollback. |
Dan Shi | 549fb82 | 2015-03-24 18:01:11 -0700 | [diff] [blame] | 250 | updater.host.get_release_version().AndReturn('1234.0.0') |
Chris Sosa | c861752 | 2014-06-09 23:22:26 +0000 | [diff] [blame] | 251 | autoupdater.ChromiumOSUpdater._run(rollback_cmd) |
| 252 | autoupdater.ChromiumOSUpdater._verify_update_completed() |
| 253 | |
| 254 | self.mox.ReplayAll() |
| 255 | updater.rollback_rootfs(powerwash=True) |
| 256 | self.mox.VerifyAll() |
| 257 | |
| 258 | self.mox.ResetAll() |
| 259 | cmd_result_1 = self.mox.CreateMockAnything() |
| 260 | cmd_result_1.exit_status = 1 |
| 261 | |
| 262 | # Rollback but can_rollback says we can't -- return an error. |
Dan Shi | 549fb82 | 2015-03-24 18:01:11 -0700 | [diff] [blame] | 263 | updater.host.get_release_version().AndReturn('5775.0.0') |
Chris Sosa | c861752 | 2014-06-09 23:22:26 +0000 | [diff] [blame] | 264 | autoupdater.ChromiumOSUpdater._run(can_rollback_cmd).AndRaise( |
| 265 | error.AutoservRunError('can_rollback failed', cmd_result_1)) |
| 266 | self.mox.ReplayAll() |
| 267 | self.assertRaises(autoupdater.RootFSUpdateError, |
| 268 | updater.rollback_rootfs, True) |
| 269 | self.mox.VerifyAll() |
| 270 | |
| 271 | self.mox.ResetAll() |
| 272 | # Rollback >= version blacklisted. |
Dan Shi | 549fb82 | 2015-03-24 18:01:11 -0700 | [diff] [blame] | 273 | updater.host.get_release_version().AndReturn('5775.0.0') |
Chris Sosa | c861752 | 2014-06-09 23:22:26 +0000 | [diff] [blame] | 274 | autoupdater.ChromiumOSUpdater._run(can_rollback_cmd) |
| 275 | autoupdater.ChromiumOSUpdater._run(rollback_cmd) |
| 276 | autoupdater.ChromiumOSUpdater._verify_update_completed() |
| 277 | self.mox.ReplayAll() |
| 278 | updater.rollback_rootfs(powerwash=True) |
| 279 | self.mox.VerifyAll() |
| 280 | |
| 281 | |
Scott Zawalski | eadbf70 | 2013-03-14 09:23:06 -0400 | [diff] [blame] | 282 | if __name__ == '__main__': |
| 283 | unittest.main() |