Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 5 | """Unit tests for the deploy_chrome script.""" |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 6 | |
Anushruth | 8d79767 | 2019-10-17 12:22:31 -0700 | [diff] [blame] | 7 | import errno |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 8 | import os |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 9 | import time |
Mike Frysinger | 166fea0 | 2021-02-12 05:30:33 -0500 | [diff] [blame] | 10 | from unittest import mock |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 11 | |
David Pursell | cfd5887 | 2015-03-19 09:15:48 -0700 | [diff] [blame] | 12 | from chromite.cli.cros import cros_chrome_sdk_unittest |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 13 | from chromite.lib import chrome_util |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 14 | from chromite.lib import cros_build_lib |
| 15 | from chromite.lib import cros_test_lib |
Jae Hoon Kim | df84291 | 2022-05-19 06:40:42 +0000 | [diff] [blame] | 16 | from chromite.lib import gs |
Ryan Cui | 686ec05 | 2013-02-12 16:39:41 -0800 | [diff] [blame] | 17 | from chromite.lib import osutils |
Erik Chen | 75a2f49 | 2020-08-06 19:15:11 -0700 | [diff] [blame] | 18 | from chromite.lib import parallel_unittest |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 19 | from chromite.lib import partial_mock |
Robert Flack | 1dc7ea8 | 2014-11-26 13:50:24 -0500 | [diff] [blame] | 20 | from chromite.lib import remote_access |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 21 | from chromite.lib import remote_access_unittest |
| 22 | from chromite.scripts import deploy_chrome |
| 23 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 24 | |
Mike Frysinger | 27e21b7 | 2018-07-12 14:20:21 -0400 | [diff] [blame] | 25 | # pylint: disable=protected-access |
| 26 | |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 27 | |
Ben Pastene | 0ff0fa4 | 2020-08-14 15:10:07 -0700 | [diff] [blame] | 28 | _REGULAR_TO = ('--device', 'monkey') |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame] | 29 | _TARGET_BOARD = 'eve' |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 30 | _GS_PATH = 'gs://foon' |
| 31 | |
| 32 | |
| 33 | def _ParseCommandLine(argv): |
| 34 | return deploy_chrome._ParseCommandLine(['--log-level', 'debug'] + argv) |
| 35 | |
| 36 | |
| 37 | class InterfaceTest(cros_test_lib.OutputTestCase): |
| 38 | """Tests the commandline interface of the script.""" |
| 39 | |
| 40 | def testGsLocalPathUnSpecified(self): |
| 41 | """Test no chrome path specified.""" |
| 42 | with self.OutputCapturer(): |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame] | 43 | self.assertRaises2(SystemExit, _ParseCommandLine, |
| 44 | list(_REGULAR_TO) + ['--board', _TARGET_BOARD], |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 45 | check_attrs={'code': 2}) |
| 46 | |
Ryo Hashimoto | 77f8eca | 2021-04-16 16:43:37 +0900 | [diff] [blame] | 47 | def testBuildDirSpecified(self): |
| 48 | """Test case of build dir specified.""" |
| 49 | argv = list(_REGULAR_TO) + ['--board', _TARGET_BOARD, '--build-dir', |
| 50 | '/path/to/chrome'] |
| 51 | _ParseCommandLine(argv) |
| 52 | |
| 53 | def testBuildDirSpecifiedWithoutBoard(self): |
| 54 | """Test case of build dir specified without --board.""" |
| 55 | argv = list(_REGULAR_TO) + [ |
| 56 | '--build-dir', '/path/to/chrome/out_' + _TARGET_BOARD + '/Release'] |
| 57 | options = _ParseCommandLine(argv) |
| 58 | self.assertEqual(options.board, _TARGET_BOARD) |
| 59 | |
| 60 | def testBuildDirSpecifiedWithoutBoardError(self): |
| 61 | """Test case of irregular build dir specified without --board.""" |
| 62 | argv = list(_REGULAR_TO) + ['--build-dir', '/path/to/chrome/foo/bar'] |
| 63 | self.assertParseError(argv) |
| 64 | |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 65 | def testGsPathSpecified(self): |
| 66 | """Test case of GS path specified.""" |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame] | 67 | argv = list(_REGULAR_TO) + ['--board', _TARGET_BOARD, '--gs-path', _GS_PATH] |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 68 | _ParseCommandLine(argv) |
| 69 | |
| 70 | def testLocalPathSpecified(self): |
| 71 | """Test case of local path specified.""" |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame] | 72 | argv = list(_REGULAR_TO) + ['--board', _TARGET_BOARD, '--local-pkg-path', |
| 73 | '/path/to/chrome'] |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 74 | _ParseCommandLine(argv) |
| 75 | |
Ryo Hashimoto | 77f8eca | 2021-04-16 16:43:37 +0900 | [diff] [blame] | 76 | def testNoBoard(self): |
| 77 | """Test no board specified.""" |
| 78 | argv = list(_REGULAR_TO) + ['--gs-path', _GS_PATH] |
| 79 | self.assertParseError(argv) |
| 80 | |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 81 | def testNoTarget(self): |
| 82 | """Test no target specified.""" |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame] | 83 | argv = ['--board', _TARGET_BOARD, '--gs-path', _GS_PATH] |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 84 | self.assertParseError(argv) |
| 85 | |
Erik Chen | 75a2f49 | 2020-08-06 19:15:11 -0700 | [diff] [blame] | 86 | def testLacros(self): |
| 87 | """Test basic lacros invocation.""" |
Sven Zheng | 022f7ea | 2022-07-20 20:09:11 +0000 | [diff] [blame] | 88 | argv = ['--lacros', '--build-dir', '/path/to/nowhere', |
| 89 | '--device', 'monkey', '--board', 'atlas'] |
Erik Chen | 75a2f49 | 2020-08-06 19:15:11 -0700 | [diff] [blame] | 90 | options = _ParseCommandLine(argv) |
| 91 | self.assertTrue(options.lacros) |
| 92 | self.assertEqual(options.target_dir, deploy_chrome.LACROS_DIR) |
| 93 | |
Sven Zheng | 022f7ea | 2022-07-20 20:09:11 +0000 | [diff] [blame] | 94 | def testLacrosNoStrip(self): |
| 95 | """Test lacros invocation with nostrip.""" |
| 96 | argv = ['--lacros', '--nostrip', '--build-dir', '/path/to/nowhere', |
| 97 | '--device', 'monkey'] |
| 98 | options = _ParseCommandLine(argv) |
| 99 | self.assertTrue(options.lacros) |
| 100 | self.assertFalse(options.dostrip) |
| 101 | self.assertEqual(options.target_dir, deploy_chrome.LACROS_DIR) |
Erik Chen | 75a2f49 | 2020-08-06 19:15:11 -0700 | [diff] [blame] | 102 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 103 | def assertParseError(self, argv): |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 104 | with self.OutputCapturer(): |
| 105 | self.assertRaises2(SystemExit, _ParseCommandLine, argv, |
| 106 | check_attrs={'code': 2}) |
| 107 | |
Thiago Goncales | 1279331 | 2013-05-23 11:26:17 -0700 | [diff] [blame] | 108 | def testMountOptionSetsTargetDir(self): |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame] | 109 | argv = list(_REGULAR_TO) + ['--board', _TARGET_BOARD, '--gs-path', _GS_PATH, |
| 110 | '--mount'] |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 111 | options = _ParseCommandLine(argv) |
Thiago Goncales | 1279331 | 2013-05-23 11:26:17 -0700 | [diff] [blame] | 112 | self.assertIsNot(options.target_dir, None) |
| 113 | |
| 114 | def testMountOptionSetsMountDir(self): |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame] | 115 | argv = list(_REGULAR_TO) + ['--board', _TARGET_BOARD, '--gs-path', _GS_PATH, |
| 116 | '--mount'] |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 117 | options = _ParseCommandLine(argv) |
Thiago Goncales | 1279331 | 2013-05-23 11:26:17 -0700 | [diff] [blame] | 118 | self.assertIsNot(options.mount_dir, None) |
| 119 | |
| 120 | def testMountOptionDoesNotOverrideTargetDir(self): |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame] | 121 | argv = list(_REGULAR_TO) + ['--board', _TARGET_BOARD, '--gs-path', _GS_PATH, |
| 122 | '--mount', '--target-dir', '/foo/bar/cow'] |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 123 | options = _ParseCommandLine(argv) |
Thiago Goncales | 1279331 | 2013-05-23 11:26:17 -0700 | [diff] [blame] | 124 | self.assertEqual(options.target_dir, '/foo/bar/cow') |
| 125 | |
| 126 | def testMountOptionDoesNotOverrideMountDir(self): |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame] | 127 | argv = list(_REGULAR_TO) + ['--board', _TARGET_BOARD, '--gs-path', _GS_PATH, |
| 128 | '--mount', '--mount-dir', '/foo/bar/cow'] |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 129 | options = _ParseCommandLine(argv) |
Thiago Goncales | 1279331 | 2013-05-23 11:26:17 -0700 | [diff] [blame] | 130 | self.assertEqual(options.mount_dir, '/foo/bar/cow') |
| 131 | |
Adrian Elder | a2c548a | 2017-11-07 19:01:29 -0500 | [diff] [blame] | 132 | def testSshIdentityOptionSetsOption(self): |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame] | 133 | argv = list(_REGULAR_TO) + ['--board', _TARGET_BOARD, |
| 134 | '--private-key', '/foo/bar/key', |
Bernie Thompson | 93b9ee6 | 2018-02-21 14:56:16 -0800 | [diff] [blame] | 135 | '--build-dir', '/path/to/nowhere'] |
Adrian Elder | a2c548a | 2017-11-07 19:01:29 -0500 | [diff] [blame] | 136 | options = _ParseCommandLine(argv) |
| 137 | self.assertEqual(options.private_key, '/foo/bar/key') |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 138 | |
| 139 | class DeployChromeMock(partial_mock.PartialMock): |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 140 | """Deploy Chrome Mock Class.""" |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 141 | |
| 142 | TARGET = 'chromite.scripts.deploy_chrome.DeployChrome' |
Erik Chen | 75a2f49 | 2020-08-06 19:15:11 -0700 | [diff] [blame] | 143 | ATTRS = ('_KillAshChromeIfNeeded', '_DisableRootfsVerification') |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 144 | |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 145 | def __init__(self): |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 146 | partial_mock.PartialMock.__init__(self) |
Robert Flack | 1dc7ea8 | 2014-11-26 13:50:24 -0500 | [diff] [blame] | 147 | self.remote_device_mock = remote_access_unittest.RemoteDeviceMock() |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 148 | # Target starts off as having rootfs verification enabled. |
Ryan Cui | e18f24f | 2012-12-03 18:39:55 -0800 | [diff] [blame] | 149 | self.rsh_mock = remote_access_unittest.RemoteShMock() |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 150 | self.rsh_mock.SetDefaultCmdResult(0) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 151 | self.MockMountCmd(1) |
David Haddock | 3151d91 | 2017-10-24 03:50:32 +0000 | [diff] [blame] | 152 | self.rsh_mock.AddCmdResult( |
Anushruth | 8d79767 | 2019-10-17 12:22:31 -0700 | [diff] [blame] | 153 | deploy_chrome.LSOF_COMMAND_CHROME % (deploy_chrome._CHROME_DIR,), 1) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 154 | |
| 155 | def MockMountCmd(self, returnvalue): |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 156 | self.rsh_mock.AddCmdResult(deploy_chrome.MOUNT_RW_COMMAND, |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 157 | returnvalue) |
| 158 | |
| 159 | def _DisableRootfsVerification(self, inst): |
| 160 | with mock.patch.object(time, 'sleep'): |
| 161 | self.backup['_DisableRootfsVerification'](inst) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 162 | |
Ryan Cui | 4d6fca9 | 2012-12-13 16:41:56 -0800 | [diff] [blame] | 163 | def PreStart(self): |
Robert Flack | 1dc7ea8 | 2014-11-26 13:50:24 -0500 | [diff] [blame] | 164 | self.remote_device_mock.start() |
Ryan Cui | 4d6fca9 | 2012-12-13 16:41:56 -0800 | [diff] [blame] | 165 | self.rsh_mock.start() |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 166 | |
Ryan Cui | 4d6fca9 | 2012-12-13 16:41:56 -0800 | [diff] [blame] | 167 | def PreStop(self): |
| 168 | self.rsh_mock.stop() |
Robert Flack | 1dc7ea8 | 2014-11-26 13:50:24 -0500 | [diff] [blame] | 169 | self.remote_device_mock.stop() |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 170 | |
Erik Chen | 75a2f49 | 2020-08-06 19:15:11 -0700 | [diff] [blame] | 171 | def _KillAshChromeIfNeeded(self, _inst): |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 172 | # Fully stub out for now. |
| 173 | pass |
| 174 | |
| 175 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 176 | class DeployTest(cros_test_lib.MockTempDirTestCase): |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 177 | """Setup a deploy object with a GS-path for use in tests.""" |
| 178 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 179 | def _GetDeployChrome(self, args): |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 180 | options = _ParseCommandLine(args) |
Ryan Cui | a56a71e | 2012-10-18 18:40:35 -0700 | [diff] [blame] | 181 | return deploy_chrome.DeployChrome( |
| 182 | options, self.tempdir, os.path.join(self.tempdir, 'staging')) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 183 | |
| 184 | def setUp(self): |
Ryan Cui | f1416f3 | 2013-01-22 18:43:41 -0800 | [diff] [blame] | 185 | self.deploy_mock = self.StartPatcher(DeployChromeMock()) |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame] | 186 | self.deploy = self._GetDeployChrome(list(_REGULAR_TO) + |
| 187 | ['--board', _TARGET_BOARD, '--gs-path', |
| 188 | _GS_PATH, '--force', '--mount']) |
Mike Frysinger | fcca49e | 2021-03-17 01:09:20 -0400 | [diff] [blame] | 189 | self.remote_reboot_mock = self.PatchObject( |
| 190 | remote_access.RemoteAccess, 'RemoteReboot', return_value=True) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 191 | |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame] | 192 | |
| 193 | class TestCheckIfBoardMatches(DeployTest): |
| 194 | """Testing checking whether the DUT board matches the target board.""" |
| 195 | |
| 196 | def testMatchedBoard(self): |
| 197 | """Test the case where the DUT board matches the target board.""" |
| 198 | self.PatchObject(remote_access.ChromiumOSDevice, 'board', _TARGET_BOARD) |
| 199 | self.assertTrue(self.deploy.options.force) |
| 200 | self.deploy._CheckBoard() |
| 201 | self.deploy.options.force = False |
| 202 | self.deploy._CheckBoard() |
| 203 | |
| 204 | def testMismatchedBoard(self): |
| 205 | """Test the case where the DUT board does not match the target board.""" |
| 206 | self.PatchObject(remote_access.ChromiumOSDevice, 'board', 'cedar') |
| 207 | self.assertTrue(self.deploy.options.force) |
| 208 | self.deploy._CheckBoard() |
| 209 | self.deploy.options.force = False |
| 210 | self.PatchObject(cros_build_lib, 'BooleanPrompt', return_value=True) |
| 211 | self.deploy._CheckBoard() |
| 212 | self.PatchObject(cros_build_lib, 'BooleanPrompt', return_value=False) |
| 213 | self.assertRaises(deploy_chrome.DeployFailure, self.deploy._CheckBoard) |
| 214 | |
| 215 | |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 216 | class TestDisableRootfsVerification(DeployTest): |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 217 | """Testing disabling of rootfs verification and RO mode.""" |
| 218 | |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 219 | def testDisableRootfsVerificationSuccess(self): |
| 220 | """Test the working case, disabling rootfs verification.""" |
| 221 | self.deploy_mock.MockMountCmd(0) |
| 222 | self.deploy._DisableRootfsVerification() |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 223 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 224 | |
| 225 | def testDisableRootfsVerificationFailure(self): |
| 226 | """Test failure to disable rootfs verification.""" |
Mike Frysinger | 27e21b7 | 2018-07-12 14:20:21 -0400 | [diff] [blame] | 227 | # pylint: disable=unused-argument |
Shuqian Zhao | 14e6109 | 2017-11-17 00:02:16 +0000 | [diff] [blame] | 228 | def RaiseRunCommandError(timeout_sec=None): |
Mike Frysinger | 929f3ba | 2019-09-12 03:24:59 -0400 | [diff] [blame] | 229 | raise cros_build_lib.RunCommandError('Mock RunCommandError') |
Luigi Semenzato | 1bc79b2 | 2016-11-22 16:32:17 -0800 | [diff] [blame] | 230 | self.remote_reboot_mock.side_effect = RaiseRunCommandError |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 231 | self.assertRaises(cros_build_lib.RunCommandError, |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 232 | self.deploy._DisableRootfsVerification) |
Luigi Semenzato | 1bc79b2 | 2016-11-22 16:32:17 -0800 | [diff] [blame] | 233 | self.remote_reboot_mock.side_effect = None |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 234 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 235 | |
| 236 | |
| 237 | class TestMount(DeployTest): |
| 238 | """Testing mount success and failure.""" |
| 239 | |
| 240 | def testSuccess(self): |
| 241 | """Test case where we are able to mount as writable.""" |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 242 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 243 | self.deploy_mock.MockMountCmd(0) |
| 244 | self.deploy._MountRootfsAsWritable() |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 245 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 246 | |
| 247 | def testMountError(self): |
| 248 | """Test that mount failure doesn't raise an exception by default.""" |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 249 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame] | 250 | self.PatchObject(remote_access.ChromiumOSDevice, 'IsDirWritable', |
Robert Flack | 1dc7ea8 | 2014-11-26 13:50:24 -0500 | [diff] [blame] | 251 | return_value=False, autospec=True) |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 252 | self.deploy._MountRootfsAsWritable() |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 253 | self.assertTrue(self.deploy._root_dir_is_still_readonly.is_set()) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 254 | |
| 255 | def testMountRwFailure(self): |
Mike Frysinger | f5a3b2d | 2019-12-12 14:36:17 -0500 | [diff] [blame] | 256 | """Test that mount failure raises an exception if check=True.""" |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 257 | self.assertRaises(cros_build_lib.RunCommandError, |
Mike Frysinger | f5a3b2d | 2019-12-12 14:36:17 -0500 | [diff] [blame] | 258 | self.deploy._MountRootfsAsWritable, check=True) |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 259 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
Robert Flack | 1dc7ea8 | 2014-11-26 13:50:24 -0500 | [diff] [blame] | 260 | |
| 261 | def testMountTempDir(self): |
| 262 | """Test that mount succeeds if target dir is writable.""" |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 263 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame] | 264 | self.PatchObject(remote_access.ChromiumOSDevice, 'IsDirWritable', |
Robert Flack | 1dc7ea8 | 2014-11-26 13:50:24 -0500 | [diff] [blame] | 265 | return_value=True, autospec=True) |
| 266 | self.deploy._MountRootfsAsWritable() |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 267 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 268 | |
| 269 | |
Anushruth | 8d79767 | 2019-10-17 12:22:31 -0700 | [diff] [blame] | 270 | class TestMountTarget(DeployTest): |
Mike Frysinger | df1d0b0 | 2019-11-12 17:44:12 -0500 | [diff] [blame] | 271 | """Testing mount and umount command handling.""" |
Anushruth | 8d79767 | 2019-10-17 12:22:31 -0700 | [diff] [blame] | 272 | |
| 273 | def testMountTargetUmountFailure(self): |
| 274 | """Test error being thrown if umount fails. |
| 275 | |
| 276 | Test that 'lsof' is run on mount-dir and 'mount -rbind' command is not run |
| 277 | if 'umount' cmd fails. |
| 278 | """ |
| 279 | mount_dir = self.deploy.options.mount_dir |
| 280 | target_dir = self.deploy.options.target_dir |
| 281 | self.deploy_mock.rsh_mock.AddCmdResult( |
| 282 | deploy_chrome._UMOUNT_DIR_IF_MOUNTPOINT_CMD % |
| 283 | {'dir': mount_dir}, returncode=errno.EBUSY, stderr='Target is Busy') |
| 284 | self.deploy_mock.rsh_mock.AddCmdResult(deploy_chrome.LSOF_COMMAND % |
| 285 | (mount_dir,), returncode=0, |
| 286 | stdout='process ' + mount_dir) |
| 287 | # Check for RunCommandError being thrown. |
| 288 | self.assertRaises(cros_build_lib.RunCommandError, |
| 289 | self.deploy._MountTarget) |
| 290 | # Check for the 'mount -rbind' command not run. |
| 291 | self.deploy_mock.rsh_mock.assertCommandContains( |
| 292 | (deploy_chrome._BIND_TO_FINAL_DIR_CMD % (target_dir, mount_dir)), |
| 293 | expected=False) |
| 294 | # Check for lsof command being called. |
| 295 | self.deploy_mock.rsh_mock.assertCommandContains( |
| 296 | (deploy_chrome.LSOF_COMMAND % (mount_dir,))) |
| 297 | |
| 298 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 299 | class TestUiJobStarted(DeployTest): |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 300 | """Test detection of a running 'ui' job.""" |
| 301 | |
Ryan Cui | f2d1a58 | 2013-02-19 14:08:13 -0800 | [diff] [blame] | 302 | def MockStatusUiCmd(self, **kwargs): |
David Haddock | 3151d91 | 2017-10-24 03:50:32 +0000 | [diff] [blame] | 303 | self.deploy_mock.rsh_mock.AddCmdResult('status ui', **kwargs) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 304 | |
| 305 | def testUiJobStartedFalse(self): |
| 306 | """Correct results with a stopped job.""" |
Mike Frysinger | 876a8e5 | 2022-06-23 18:07:30 -0400 | [diff] [blame] | 307 | self.MockStatusUiCmd(stdout='ui stop/waiting') |
Ryan Cui | f2d1a58 | 2013-02-19 14:08:13 -0800 | [diff] [blame] | 308 | self.assertFalse(self.deploy._CheckUiJobStarted()) |
| 309 | |
| 310 | def testNoUiJob(self): |
| 311 | """Correct results when the job doesn't exist.""" |
Mike Frysinger | 876a8e5 | 2022-06-23 18:07:30 -0400 | [diff] [blame] | 312 | self.MockStatusUiCmd(stderr='start: Unknown job: ui', returncode=1) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 313 | self.assertFalse(self.deploy._CheckUiJobStarted()) |
| 314 | |
| 315 | def testCheckRootfsWriteableTrue(self): |
| 316 | """Correct results with a running job.""" |
Mike Frysinger | 876a8e5 | 2022-06-23 18:07:30 -0400 | [diff] [blame] | 317 | self.MockStatusUiCmd(stdout='ui start/running, process 297') |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 318 | self.assertTrue(self.deploy._CheckUiJobStarted()) |
| 319 | |
| 320 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 321 | class StagingTest(cros_test_lib.MockTempDirTestCase): |
| 322 | """Test user-mode and ebuild-mode staging functionality.""" |
| 323 | |
| 324 | def setUp(self): |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 325 | self.staging_dir = os.path.join(self.tempdir, 'staging') |
Jae Hoon Kim | df84291 | 2022-05-19 06:40:42 +0000 | [diff] [blame] | 326 | osutils.SafeMakedirs(self.staging_dir) |
| 327 | self.staging_tarball_path = os.path.join( |
| 328 | self.tempdir, deploy_chrome._CHROME_DIR_STAGING_TARBALL_ZSTD) |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 329 | self.build_dir = os.path.join(self.tempdir, 'build_dir') |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame] | 330 | self.common_flags = ['--board', _TARGET_BOARD, |
| 331 | '--build-dir', self.build_dir, '--staging-only', |
Mike Frysinger | 3bb61cb | 2022-04-14 16:07:44 -0400 | [diff] [blame] | 332 | '--cache-dir', str(self.tempdir)] |
Ryan Cui | a0215a7 | 2013-02-14 16:20:45 -0800 | [diff] [blame] | 333 | self.sdk_mock = self.StartPatcher(cros_chrome_sdk_unittest.SDKFetcherMock()) |
Ryan Cui | 686ec05 | 2013-02-12 16:39:41 -0800 | [diff] [blame] | 334 | self.PatchObject( |
| 335 | osutils, 'SourceEnvironment', autospec=True, |
| 336 | return_value={'STRIP': 'x86_64-cros-linux-gnu-strip'}) |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 337 | |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 338 | def testSingleFileDeployFailure(self): |
| 339 | """Default staging enforces that mandatory files are copied""" |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 340 | options = _ParseCommandLine(self.common_flags) |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 341 | osutils.Touch(os.path.join(self.build_dir, 'chrome'), makedirs=True) |
| 342 | self.assertRaises( |
| 343 | chrome_util.MissingPathError, deploy_chrome._PrepareStagingDir, |
Daniel Erat | c89829c | 2014-05-12 17:24:21 -0700 | [diff] [blame] | 344 | options, self.tempdir, self.staging_dir, chrome_util._COPY_PATHS_CHROME) |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 345 | |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 346 | def testSloppyDeployFailure(self): |
| 347 | """Sloppy staging enforces that at least one file is copied.""" |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 348 | options = _ParseCommandLine(self.common_flags + ['--sloppy']) |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 349 | self.assertRaises( |
| 350 | chrome_util.MissingPathError, deploy_chrome._PrepareStagingDir, |
Daniel Erat | c89829c | 2014-05-12 17:24:21 -0700 | [diff] [blame] | 351 | options, self.tempdir, self.staging_dir, chrome_util._COPY_PATHS_CHROME) |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 352 | |
| 353 | def testSloppyDeploySuccess(self): |
| 354 | """Sloppy staging - stage one file.""" |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 355 | options = _ParseCommandLine(self.common_flags + ['--sloppy']) |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 356 | osutils.Touch(os.path.join(self.build_dir, 'chrome'), makedirs=True) |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 357 | deploy_chrome._PrepareStagingDir(options, self.tempdir, self.staging_dir, |
Daniel Erat | c89829c | 2014-05-12 17:24:21 -0700 | [diff] [blame] | 358 | chrome_util._COPY_PATHS_CHROME) |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 359 | |
Jae Hoon Kim | df84291 | 2022-05-19 06:40:42 +0000 | [diff] [blame] | 360 | @cros_test_lib.pytestmark_network_test |
| 361 | def testUploadStagingDir(self): |
| 362 | """Upload staging directory.""" |
| 363 | mockGsCopy = self.PatchObject(gs.GSContext, 'Copy') |
| 364 | staging_upload = 'gs://some-path' |
| 365 | options = _ParseCommandLine( |
| 366 | self.common_flags + ['--staging-upload', staging_upload]) |
| 367 | osutils.Touch(os.path.join(self.build_dir, 'chrome'), makedirs=True) |
| 368 | deploy_chrome._UploadStagingDir(options, self.tempdir, self.staging_dir) |
| 369 | self.assertEqual(mockGsCopy.call_args_list, [ |
| 370 | mock.call(self.staging_tarball_path, staging_upload, acl=''), |
| 371 | ]) |
| 372 | |
| 373 | @cros_test_lib.pytestmark_network_test |
| 374 | def testUploadStagingPublicReadACL(self): |
| 375 | """Upload staging directory with public-read ACL.""" |
| 376 | mockGsCopy = self.PatchObject(gs.GSContext, 'Copy') |
| 377 | staging_upload = 'gs://some-path' |
| 378 | options = _ParseCommandLine( |
| 379 | self.common_flags + |
| 380 | ['--staging-upload', staging_upload, '--public-read']) |
| 381 | osutils.Touch(os.path.join(self.build_dir, 'chrome'), makedirs=True) |
| 382 | deploy_chrome._UploadStagingDir(options, self.tempdir, self.staging_dir) |
| 383 | self.assertEqual(mockGsCopy.call_args_list, [ |
| 384 | mock.call(self.staging_tarball_path, staging_upload, acl='public-read'), |
| 385 | ]) |
| 386 | |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 387 | |
| 388 | class DeployTestBuildDir(cros_test_lib.MockTempDirTestCase): |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 389 | """Set up a deploy object with a build-dir for use in deployment type tests""" |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 390 | |
| 391 | def _GetDeployChrome(self, args): |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 392 | options = _ParseCommandLine(args) |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 393 | return deploy_chrome.DeployChrome( |
| 394 | options, self.tempdir, os.path.join(self.tempdir, 'staging')) |
| 395 | |
| 396 | def setUp(self): |
| 397 | self.staging_dir = os.path.join(self.tempdir, 'staging') |
| 398 | self.build_dir = os.path.join(self.tempdir, 'build_dir') |
| 399 | self.deploy_mock = self.StartPatcher(DeployChromeMock()) |
| 400 | self.deploy = self._GetDeployChrome( |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame] | 401 | list(_REGULAR_TO) + ['--board', _TARGET_BOARD, |
| 402 | '--build-dir', self.build_dir, '--staging-only', |
Mike Frysinger | 3bb61cb | 2022-04-14 16:07:44 -0400 | [diff] [blame] | 403 | '--cache-dir', str(self.tempdir), '--sloppy']) |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 404 | |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 405 | def getCopyPath(self, source_path): |
| 406 | """Return a chrome_util.Path or None if not present.""" |
| 407 | paths = [p for p in self.deploy.copy_paths if p.src == source_path] |
| 408 | return paths[0] if paths else None |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 409 | |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 410 | class TestDeploymentType(DeployTestBuildDir): |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 411 | """Test detection of deployment type using build dir.""" |
| 412 | |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 413 | def testAppShellDetection(self): |
| 414 | """Check for an app_shell deployment""" |
| 415 | osutils.Touch(os.path.join(self.deploy.options.build_dir, 'app_shell'), |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 416 | makedirs=True) |
| 417 | self.deploy._CheckDeployType() |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 418 | self.assertTrue(self.getCopyPath('app_shell')) |
| 419 | self.assertFalse(self.getCopyPath('chrome')) |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 420 | |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 421 | def testChromeAndAppShellDetection(self): |
Daniel Erat | f53bd3a | 2016-12-02 11:28:36 -0700 | [diff] [blame] | 422 | """Check for a chrome deployment when app_shell also exists.""" |
Steve Fung | 63d705d | 2014-03-16 03:14:03 -0700 | [diff] [blame] | 423 | osutils.Touch(os.path.join(self.deploy.options.build_dir, 'chrome'), |
| 424 | makedirs=True) |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 425 | osutils.Touch(os.path.join(self.deploy.options.build_dir, 'app_shell'), |
Steve Fung | 63d705d | 2014-03-16 03:14:03 -0700 | [diff] [blame] | 426 | makedirs=True) |
| 427 | self.deploy._CheckDeployType() |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 428 | self.assertTrue(self.getCopyPath('chrome')) |
Daniel Erat | 9813f0e | 2014-11-12 11:00:28 -0700 | [diff] [blame] | 429 | self.assertFalse(self.getCopyPath('app_shell')) |
Steve Fung | 63d705d | 2014-03-16 03:14:03 -0700 | [diff] [blame] | 430 | |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 431 | def testChromeDetection(self): |
| 432 | """Check for a regular chrome deployment""" |
| 433 | osutils.Touch(os.path.join(self.deploy.options.build_dir, 'chrome'), |
| 434 | makedirs=True) |
| 435 | self.deploy._CheckDeployType() |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 436 | self.assertTrue(self.getCopyPath('chrome')) |
Daniel Erat | 9813f0e | 2014-11-12 11:00:28 -0700 | [diff] [blame] | 437 | self.assertFalse(self.getCopyPath('app_shell')) |
Ben Pastene | e484b34 | 2020-06-30 18:29:27 -0700 | [diff] [blame] | 438 | |
| 439 | |
| 440 | class TestDeployTestBinaries(cros_test_lib.RunCommandTempDirTestCase): |
| 441 | """Tests _DeployTestBinaries().""" |
| 442 | |
| 443 | def setUp(self): |
| 444 | options = _ParseCommandLine(list(_REGULAR_TO) + [ |
| 445 | '--board', _TARGET_BOARD, '--force', '--mount', |
| 446 | '--build-dir', os.path.join(self.tempdir, 'build_dir'), |
| 447 | '--nostrip']) |
| 448 | self.deploy = deploy_chrome.DeployChrome( |
| 449 | options, self.tempdir, os.path.join(self.tempdir, 'staging')) |
| 450 | |
Brian Sheedy | 86f1234 | 2020-10-29 15:30:02 -0700 | [diff] [blame] | 451 | def _SimulateBinaries(self): |
| 452 | # Ensure the staging dir contains the right binaries to copy over. |
Ben Pastene | e484b34 | 2020-06-30 18:29:27 -0700 | [diff] [blame] | 453 | test_binaries = [ |
| 454 | 'run_a_tests', |
| 455 | 'run_b_tests', |
| 456 | 'run_c_tests', |
| 457 | ] |
| 458 | # Simulate having the binaries both on the device and in our local build |
| 459 | # dir. |
| 460 | self.rc.AddCmdResult( |
| 461 | partial_mock.In(deploy_chrome._FIND_TEST_BIN_CMD), |
| 462 | stdout='\n'.join(test_binaries)) |
| 463 | for binary in test_binaries: |
| 464 | osutils.Touch(os.path.join(self.deploy.options.build_dir, binary), |
| 465 | makedirs=True, mode=0o700) |
Brian Sheedy | 86f1234 | 2020-10-29 15:30:02 -0700 | [diff] [blame] | 466 | return test_binaries |
Ben Pastene | e484b34 | 2020-06-30 18:29:27 -0700 | [diff] [blame] | 467 | |
Brian Sheedy | 86f1234 | 2020-10-29 15:30:02 -0700 | [diff] [blame] | 468 | def _AssertBinariesInStagingDir(self, test_binaries): |
Ben Pastene | e484b34 | 2020-06-30 18:29:27 -0700 | [diff] [blame] | 469 | # Ensure the binaries were placed in the staging dir used to copy them over. |
| 470 | staging_dir = os.path.join( |
| 471 | self.tempdir, os.path.basename(deploy_chrome._CHROME_TEST_BIN_DIR)) |
| 472 | for binary in test_binaries: |
| 473 | self.assertIn(binary, os.listdir(staging_dir)) |
Erik Chen | 75a2f49 | 2020-08-06 19:15:11 -0700 | [diff] [blame] | 474 | |
Brian Sheedy | 86f1234 | 2020-10-29 15:30:02 -0700 | [diff] [blame] | 475 | def testFindError(self): |
| 476 | """Ensure an error is thrown if we can't inspect the device.""" |
| 477 | self.rc.AddCmdResult( |
| 478 | partial_mock.In(deploy_chrome._FIND_TEST_BIN_CMD), 1) |
| 479 | self.assertRaises( |
| 480 | deploy_chrome.DeployFailure, self.deploy._DeployTestBinaries) |
| 481 | |
| 482 | def testSuccess(self): |
| 483 | """Ensure that the happy path succeeds as expected.""" |
| 484 | test_binaries = self._SimulateBinaries() |
| 485 | self.deploy._DeployTestBinaries() |
| 486 | self._AssertBinariesInStagingDir(test_binaries) |
| 487 | |
| 488 | def testRetrySuccess(self): |
| 489 | """Ensure that a transient exception still results in success.""" |
| 490 | # Raises a RunCommandError on its first invocation, but passes on subsequent |
| 491 | # calls. |
| 492 | def SideEffect(*args, **kwargs): |
Yuke Liao | be6bac3 | 2020-12-26 22:16:49 -0800 | [diff] [blame] | 493 | # pylint: disable=unused-argument |
Brian Sheedy | 86f1234 | 2020-10-29 15:30:02 -0700 | [diff] [blame] | 494 | if not SideEffect.called: |
| 495 | SideEffect.called = True |
| 496 | raise cros_build_lib.RunCommandError('fail') |
| 497 | SideEffect.called = False |
| 498 | |
| 499 | test_binaries = self._SimulateBinaries() |
| 500 | with mock.patch.object( |
| 501 | remote_access.ChromiumOSDevice, 'CopyToDevice', |
| 502 | side_effect=SideEffect) as copy_mock: |
| 503 | self.deploy._DeployTestBinaries() |
| 504 | self.assertEqual(copy_mock.call_count, 2) |
| 505 | self._AssertBinariesInStagingDir(test_binaries) |
| 506 | |
| 507 | def testRetryFailure(self): |
| 508 | """Ensure that consistent exceptions result in failure.""" |
| 509 | self._SimulateBinaries() |
| 510 | with self.assertRaises(cros_build_lib.RunCommandError): |
| 511 | with mock.patch.object( |
| 512 | remote_access.ChromiumOSDevice, 'CopyToDevice', |
| 513 | side_effect=cros_build_lib.RunCommandError('fail')): |
| 514 | self.deploy._DeployTestBinaries() |
| 515 | |
Erik Chen | 75a2f49 | 2020-08-06 19:15:11 -0700 | [diff] [blame] | 516 | |
| 517 | class LacrosPerformTest(cros_test_lib.RunCommandTempDirTestCase): |
| 518 | """Line coverage for Perform() method with --lacros option.""" |
| 519 | |
| 520 | def setUp(self): |
Yuke Liao | 24fc60c | 2020-12-26 22:16:49 -0800 | [diff] [blame] | 521 | self.deploy = None |
| 522 | self._ran_start_command = False |
| 523 | self.StartPatcher(parallel_unittest.ParallelMock()) |
| 524 | |
| 525 | def start_ui_side_effect(*args, **kwargs): |
| 526 | # pylint: disable=unused-argument |
| 527 | self._ran_start_command = True |
| 528 | |
| 529 | self.rc.AddCmdResult(partial_mock.In('start ui'), |
| 530 | side_effect=start_ui_side_effect) |
| 531 | |
| 532 | def prepareDeploy(self, options=None): |
| 533 | if not options: |
| 534 | options = _ParseCommandLine([ |
| 535 | '--lacros', '--nostrip', '--build-dir', '/path/to/nowhere', |
| 536 | '--device', 'monkey' |
| 537 | ]) |
Erik Chen | 75a2f49 | 2020-08-06 19:15:11 -0700 | [diff] [blame] | 538 | self.deploy = deploy_chrome.DeployChrome( |
| 539 | options, self.tempdir, os.path.join(self.tempdir, 'staging')) |
| 540 | |
| 541 | # These methods being mocked are all side effects expected for a --lacros |
| 542 | # deploy. |
| 543 | self.deploy._EnsureTargetDir = mock.Mock() |
| 544 | self.deploy._GetDeviceInfo = mock.Mock() |
| 545 | self.deploy._CheckConnection = mock.Mock() |
| 546 | self.deploy._MountRootfsAsWritable = mock.Mock() |
| 547 | self.deploy._PrepareStagingDir = mock.Mock() |
| 548 | self.deploy._CheckDeviceFreeSpace = mock.Mock() |
Yuke Liao | be6bac3 | 2020-12-26 22:16:49 -0800 | [diff] [blame] | 549 | self.deploy._KillAshChromeIfNeeded = mock.Mock() |
Erik Chen | 75a2f49 | 2020-08-06 19:15:11 -0700 | [diff] [blame] | 550 | |
| 551 | def testConfNotModified(self): |
| 552 | """When the conf file is not modified we don't restart chrome .""" |
Yuke Liao | 24fc60c | 2020-12-26 22:16:49 -0800 | [diff] [blame] | 553 | self.prepareDeploy() |
Erik Chen | 75a2f49 | 2020-08-06 19:15:11 -0700 | [diff] [blame] | 554 | self.deploy.Perform() |
| 555 | self.deploy._KillAshChromeIfNeeded.assert_not_called() |
| 556 | self.assertFalse(self._ran_start_command) |
| 557 | |
| 558 | def testConfModified(self): |
| 559 | """When the conf file is modified we restart chrome.""" |
Yuke Liao | 24fc60c | 2020-12-26 22:16:49 -0800 | [diff] [blame] | 560 | self.prepareDeploy() |
Erik Chen | 75a2f49 | 2020-08-06 19:15:11 -0700 | [diff] [blame] | 561 | |
| 562 | # We intentionally add '\n' to MODIFIED_CONF_FILE to simulate echo adding a |
| 563 | # newline when invoked in the shell. |
| 564 | self.rc.AddCmdResult( |
| 565 | partial_mock.In(deploy_chrome.ENABLE_LACROS_VIA_CONF_COMMAND), |
| 566 | stdout=deploy_chrome.MODIFIED_CONF_FILE + '\n') |
| 567 | |
| 568 | self.deploy.Perform() |
| 569 | self.deploy._KillAshChromeIfNeeded.assert_called() |
| 570 | self.assertTrue(self._ran_start_command) |
Yuke Liao | 24fc60c | 2020-12-26 22:16:49 -0800 | [diff] [blame] | 571 | |
| 572 | def testSkipModifyingConf(self): |
| 573 | """SKip modifying the config file when the argument is specified.""" |
| 574 | self.prepareDeploy( |
| 575 | _ParseCommandLine([ |
| 576 | '--lacros', '--nostrip', '--build-dir', '/path/to/nowhere', |
| 577 | '--device', 'monkey', '--skip-modifying-config-file' |
| 578 | ])) |
| 579 | |
| 580 | self.rc.AddCmdResult( |
| 581 | partial_mock.In(deploy_chrome.ENABLE_LACROS_VIA_CONF_COMMAND), |
| 582 | stdout=deploy_chrome.MODIFIED_CONF_FILE + '\n') |
| 583 | |
| 584 | self.deploy.Perform() |
| 585 | self.deploy._KillAshChromeIfNeeded.assert_not_called() |
| 586 | self.assertFalse(self._ran_start_command) |