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