Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2012 The ChromiumOS Authors |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 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" |
Daniil Lunev | 0c4f65c | 2022-09-19 11:01:34 +1000 | [diff] [blame] | 205 | ATTRS = ( |
| 206 | "_ChromeFileInUse", |
| 207 | "_DisableRootfsVerification", |
| 208 | "_ShouldUseCompressedAsh", |
| 209 | ) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 210 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 211 | def __init__(self): |
| 212 | partial_mock.PartialMock.__init__(self) |
| 213 | self.remote_device_mock = remote_access_unittest.RemoteDeviceMock() |
| 214 | # Target starts off as having rootfs verification enabled. |
| 215 | self.rsh_mock = remote_access_unittest.RemoteShMock() |
| 216 | self.rsh_mock.SetDefaultCmdResult(0) |
| 217 | self.MockMountCmd(1) |
| 218 | self.rsh_mock.AddCmdResult( |
| 219 | deploy_chrome.LSOF_COMMAND_CHROME % (deploy_chrome._CHROME_DIR,), 1 |
| 220 | ) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 221 | |
Daniil Lunev | 0c4f65c | 2022-09-19 11:01:34 +1000 | [diff] [blame] | 222 | self.rsh_mock.AddCmdResult( |
| 223 | "status ui", stdout="ui start/running, process 123" |
| 224 | ) |
| 225 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 226 | def MockMountCmd(self, returnvalue): |
| 227 | self.rsh_mock.AddCmdResult(deploy_chrome.MOUNT_RW_COMMAND, returnvalue) |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 228 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 229 | def _DisableRootfsVerification(self, inst): |
| 230 | with mock.patch.object(time, "sleep"): |
| 231 | self.backup["_DisableRootfsVerification"](inst) |
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 PreStart(self): |
| 234 | self.remote_device_mock.start() |
| 235 | self.rsh_mock.start() |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 236 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 237 | def PreStop(self): |
| 238 | self.rsh_mock.stop() |
| 239 | self.remote_device_mock.stop() |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 240 | |
Daniil Lunev | 0c4f65c | 2022-09-19 11:01:34 +1000 | [diff] [blame] | 241 | def _ChromeFileInUse(self, _inst): |
| 242 | # Fully stub out for now. Can be replaced if further testing is added. |
| 243 | return False |
| 244 | |
| 245 | def _ShouldUseCompressedAsh(self, inst): |
| 246 | with mock.patch.object( |
| 247 | remote_access.RemoteDevice, "IfFileExists" |
| 248 | ) as exists_mock: |
| 249 | exists_mock.return_value = False |
| 250 | self.backup["_ShouldUseCompressedAsh"](inst) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 251 | |
| 252 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 253 | class DeployTest(cros_test_lib.MockTempDirTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 254 | """Setup a deploy object with a GS-path for use in tests.""" |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 255 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 256 | def _GetDeployChrome(self, args): |
| 257 | options = _ParseCommandLine(args) |
| 258 | return deploy_chrome.DeployChrome( |
| 259 | options, self.tempdir, os.path.join(self.tempdir, "staging") |
| 260 | ) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 261 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 262 | def setUp(self): |
| 263 | self.deploy_mock = self.StartPatcher(DeployChromeMock()) |
| 264 | self.deploy = self._GetDeployChrome( |
| 265 | list(_REGULAR_TO) |
| 266 | + [ |
| 267 | "--board", |
| 268 | _TARGET_BOARD, |
| 269 | "--gs-path", |
| 270 | _GS_PATH, |
| 271 | "--force", |
| 272 | "--mount", |
| 273 | ] |
| 274 | ) |
| 275 | self.remote_reboot_mock = self.PatchObject( |
| 276 | remote_access.RemoteAccess, "RemoteReboot", return_value=True |
| 277 | ) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 278 | |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame] | 279 | |
| 280 | class TestCheckIfBoardMatches(DeployTest): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 281 | """Testing checking whether the DUT board matches the target board.""" |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame] | 282 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 283 | def testMatchedBoard(self): |
| 284 | """Test the case where the DUT board matches the target board.""" |
| 285 | self.PatchObject(remote_access.ChromiumOSDevice, "board", _TARGET_BOARD) |
| 286 | self.assertTrue(self.deploy.options.force) |
| 287 | self.deploy._CheckBoard() |
| 288 | self.deploy.options.force = False |
| 289 | self.deploy._CheckBoard() |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame] | 290 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 291 | def testMismatchedBoard(self): |
| 292 | """Test the case where the DUT board does not match the target board.""" |
| 293 | self.PatchObject(remote_access.ChromiumOSDevice, "board", "cedar") |
| 294 | self.assertTrue(self.deploy.options.force) |
| 295 | self.deploy._CheckBoard() |
| 296 | self.deploy.options.force = False |
| 297 | self.PatchObject(cros_build_lib, "BooleanPrompt", return_value=True) |
| 298 | self.deploy._CheckBoard() |
| 299 | self.PatchObject(cros_build_lib, "BooleanPrompt", return_value=False) |
| 300 | self.assertRaises(deploy_chrome.DeployFailure, self.deploy._CheckBoard) |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame] | 301 | |
| 302 | |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 303 | class TestDisableRootfsVerification(DeployTest): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 304 | """Testing disabling of rootfs verification and RO mode.""" |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 305 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 306 | def testDisableRootfsVerificationSuccess(self): |
| 307 | """Test the working case, disabling rootfs verification.""" |
| 308 | self.deploy_mock.MockMountCmd(0) |
| 309 | self.deploy._DisableRootfsVerification() |
| 310 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 311 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 312 | def testDisableRootfsVerificationFailure(self): |
| 313 | """Test failure to disable rootfs verification.""" |
| 314 | # pylint: disable=unused-argument |
| 315 | def RaiseRunCommandError(timeout_sec=None): |
| 316 | raise cros_build_lib.RunCommandError("Mock RunCommandError") |
| 317 | |
| 318 | self.remote_reboot_mock.side_effect = RaiseRunCommandError |
| 319 | self.assertRaises( |
| 320 | cros_build_lib.RunCommandError, |
| 321 | self.deploy._DisableRootfsVerification, |
| 322 | ) |
| 323 | self.remote_reboot_mock.side_effect = None |
| 324 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 325 | |
| 326 | |
Daniil Lunev | 0c4f65c | 2022-09-19 11:01:34 +1000 | [diff] [blame] | 327 | class TestDeployCompressedAsh(DeployTest): |
| 328 | """Testing deployments with --ash-compressed passed.""" |
| 329 | |
| 330 | def _GetDeployChrome(self, args): |
| 331 | args.append("--compressed-ash") |
| 332 | return super(TestDeployCompressedAsh, self)._GetDeployChrome(args) |
| 333 | |
| 334 | def testUnmountSuccess(self): |
| 335 | """Test case for a successful 'umount' call.""" |
| 336 | self.deploy._KillAshChromeIfNeeded() |
| 337 | |
| 338 | def testUnmountFailure(self): |
| 339 | """Test case for a failed 'umount' call.""" |
| 340 | self.deploy_mock.rsh_mock.AddCmdResult( |
| 341 | ["umount", deploy_chrome.RAW_ASH_PATH], |
| 342 | returncode=32, |
| 343 | stderr="umount failure", |
| 344 | ) |
| 345 | self.assertRaises( |
| 346 | deploy_chrome.DeployFailure, self.deploy._KillAshChromeIfNeeded |
| 347 | ) |
| 348 | |
| 349 | |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 350 | class TestMount(DeployTest): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 351 | """Testing mount success and failure.""" |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 352 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 353 | def testSuccess(self): |
| 354 | """Test case where we are able to mount as writable.""" |
| 355 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
| 356 | self.deploy_mock.MockMountCmd(0) |
| 357 | self.deploy._MountRootfsAsWritable() |
| 358 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 359 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 360 | def testMountError(self): |
| 361 | """Test that mount failure doesn't raise an exception by default.""" |
| 362 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
| 363 | self.PatchObject( |
| 364 | remote_access.ChromiumOSDevice, |
| 365 | "IsDirWritable", |
| 366 | return_value=False, |
| 367 | autospec=True, |
| 368 | ) |
| 369 | self.deploy._MountRootfsAsWritable() |
| 370 | self.assertTrue(self.deploy._root_dir_is_still_readonly.is_set()) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 371 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 372 | def testMountRwFailure(self): |
| 373 | """Test that mount failure raises an exception if check=True.""" |
| 374 | self.assertRaises( |
| 375 | cros_build_lib.RunCommandError, |
| 376 | self.deploy._MountRootfsAsWritable, |
| 377 | check=True, |
| 378 | ) |
| 379 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
Robert Flack | 1dc7ea8 | 2014-11-26 13:50:24 -0500 | [diff] [blame] | 380 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 381 | def testMountTempDir(self): |
| 382 | """Test that mount succeeds if target dir is writable.""" |
| 383 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
| 384 | self.PatchObject( |
| 385 | remote_access.ChromiumOSDevice, |
| 386 | "IsDirWritable", |
| 387 | return_value=True, |
| 388 | autospec=True, |
| 389 | ) |
| 390 | self.deploy._MountRootfsAsWritable() |
| 391 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 392 | |
| 393 | |
Anushruth | 8d79767 | 2019-10-17 12:22:31 -0700 | [diff] [blame] | 394 | class TestMountTarget(DeployTest): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 395 | """Testing mount and umount command handling.""" |
Anushruth | 8d79767 | 2019-10-17 12:22:31 -0700 | [diff] [blame] | 396 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 397 | def testMountTargetUmountFailure(self): |
| 398 | """Test error being thrown if umount fails. |
Anushruth | 8d79767 | 2019-10-17 12:22:31 -0700 | [diff] [blame] | 399 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 400 | Test that 'lsof' is run on mount-dir and 'mount -rbind' command is not run |
| 401 | if 'umount' cmd fails. |
| 402 | """ |
| 403 | mount_dir = self.deploy.options.mount_dir |
| 404 | target_dir = self.deploy.options.target_dir |
| 405 | self.deploy_mock.rsh_mock.AddCmdResult( |
| 406 | deploy_chrome._UMOUNT_DIR_IF_MOUNTPOINT_CMD % {"dir": mount_dir}, |
| 407 | returncode=errno.EBUSY, |
| 408 | stderr="Target is Busy", |
| 409 | ) |
| 410 | self.deploy_mock.rsh_mock.AddCmdResult( |
| 411 | deploy_chrome.LSOF_COMMAND % (mount_dir,), |
| 412 | returncode=0, |
| 413 | stdout="process " + mount_dir, |
| 414 | ) |
| 415 | # Check for RunCommandError being thrown. |
| 416 | self.assertRaises( |
| 417 | cros_build_lib.RunCommandError, self.deploy._MountTarget |
| 418 | ) |
| 419 | # Check for the 'mount -rbind' command not run. |
| 420 | self.deploy_mock.rsh_mock.assertCommandContains( |
| 421 | (deploy_chrome._BIND_TO_FINAL_DIR_CMD % (target_dir, mount_dir)), |
| 422 | expected=False, |
| 423 | ) |
| 424 | # Check for lsof command being called. |
| 425 | self.deploy_mock.rsh_mock.assertCommandContains( |
| 426 | (deploy_chrome.LSOF_COMMAND % (mount_dir,)) |
| 427 | ) |
Anushruth | 8d79767 | 2019-10-17 12:22:31 -0700 | [diff] [blame] | 428 | |
| 429 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 430 | class TestUiJobStarted(DeployTest): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 431 | """Test detection of a running 'ui' job.""" |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 432 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 433 | def MockStatusUiCmd(self, **kwargs): |
| 434 | self.deploy_mock.rsh_mock.AddCmdResult("status ui", **kwargs) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 435 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 436 | def testUiJobStartedFalse(self): |
| 437 | """Correct results with a stopped job.""" |
| 438 | self.MockStatusUiCmd(stdout="ui stop/waiting") |
| 439 | self.assertFalse(self.deploy._CheckUiJobStarted()) |
Ryan Cui | f2d1a58 | 2013-02-19 14:08:13 -0800 | [diff] [blame] | 440 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 441 | def testNoUiJob(self): |
| 442 | """Correct results when the job doesn't exist.""" |
| 443 | self.MockStatusUiCmd(stderr="start: Unknown job: ui", returncode=1) |
| 444 | self.assertFalse(self.deploy._CheckUiJobStarted()) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 445 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 446 | def testCheckRootfsWriteableTrue(self): |
| 447 | """Correct results with a running job.""" |
| 448 | self.MockStatusUiCmd(stdout="ui start/running, process 297") |
| 449 | self.assertTrue(self.deploy._CheckUiJobStarted()) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 450 | |
| 451 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 452 | class StagingTest(cros_test_lib.MockTempDirTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 453 | """Test user-mode and ebuild-mode staging functionality.""" |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 454 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 455 | def setUp(self): |
| 456 | self.staging_dir = os.path.join(self.tempdir, "staging") |
| 457 | osutils.SafeMakedirs(self.staging_dir) |
| 458 | self.staging_tarball_path = os.path.join( |
| 459 | self.tempdir, deploy_chrome._CHROME_DIR_STAGING_TARBALL_ZSTD |
| 460 | ) |
| 461 | self.build_dir = os.path.join(self.tempdir, "build_dir") |
| 462 | self.common_flags = [ |
| 463 | "--board", |
| 464 | _TARGET_BOARD, |
| 465 | "--build-dir", |
| 466 | self.build_dir, |
| 467 | "--staging-only", |
| 468 | "--cache-dir", |
| 469 | str(self.tempdir), |
| 470 | ] |
| 471 | self.sdk_mock = self.StartPatcher( |
| 472 | cros_chrome_sdk_unittest.SDKFetcherMock() |
| 473 | ) |
| 474 | self.PatchObject( |
| 475 | osutils, |
| 476 | "SourceEnvironment", |
| 477 | autospec=True, |
| 478 | return_value={"STRIP": "x86_64-cros-linux-gnu-strip"}, |
| 479 | ) |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 480 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 481 | def testSingleFileDeployFailure(self): |
| 482 | """Default staging enforces that mandatory files are copied""" |
| 483 | options = _ParseCommandLine(self.common_flags) |
| 484 | osutils.Touch(os.path.join(self.build_dir, "chrome"), makedirs=True) |
| 485 | self.assertRaises( |
| 486 | chrome_util.MissingPathError, |
| 487 | deploy_chrome._PrepareStagingDir, |
| 488 | options, |
| 489 | self.tempdir, |
| 490 | self.staging_dir, |
| 491 | chrome_util._COPY_PATHS_CHROME, |
| 492 | ) |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 493 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 494 | def testSloppyDeployFailure(self): |
| 495 | """Sloppy staging enforces that at least one file is copied.""" |
| 496 | options = _ParseCommandLine(self.common_flags + ["--sloppy"]) |
| 497 | self.assertRaises( |
| 498 | chrome_util.MissingPathError, |
| 499 | deploy_chrome._PrepareStagingDir, |
| 500 | options, |
| 501 | self.tempdir, |
| 502 | self.staging_dir, |
| 503 | chrome_util._COPY_PATHS_CHROME, |
| 504 | ) |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 505 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 506 | def testSloppyDeploySuccess(self): |
| 507 | """Sloppy staging - stage one file.""" |
| 508 | options = _ParseCommandLine(self.common_flags + ["--sloppy"]) |
| 509 | osutils.Touch(os.path.join(self.build_dir, "chrome"), makedirs=True) |
| 510 | deploy_chrome._PrepareStagingDir( |
| 511 | options, |
| 512 | self.tempdir, |
| 513 | self.staging_dir, |
| 514 | chrome_util._COPY_PATHS_CHROME, |
| 515 | ) |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 516 | |
Daniil Lunev | 0c4f65c | 2022-09-19 11:01:34 +1000 | [diff] [blame] | 517 | def testSloppyDeploySuccessLacros(self): |
| 518 | """Ensure the squashfs mechanism with --compressed-ash doesn't throw.""" |
| 519 | options = _ParseCommandLine( |
| 520 | self.common_flags + ["--sloppy", "--compressed-ash"] |
| 521 | ) |
| 522 | osutils.Touch(os.path.join(self.build_dir, "chrome"), makedirs=True) |
| 523 | deploy_chrome._PrepareStagingDir( |
| 524 | options, |
| 525 | self.tempdir, |
| 526 | self.staging_dir, |
| 527 | chrome_util._COPY_PATHS_CHROME, |
| 528 | ) |
| 529 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 530 | @cros_test_lib.pytestmark_network_test |
| 531 | def testUploadStagingDir(self): |
| 532 | """Upload staging directory.""" |
| 533 | mockGsCopy = self.PatchObject(gs.GSContext, "Copy") |
| 534 | staging_upload = "gs://some-path" |
| 535 | options = _ParseCommandLine( |
| 536 | self.common_flags + ["--staging-upload", staging_upload] |
| 537 | ) |
| 538 | osutils.Touch(os.path.join(self.build_dir, "chrome"), makedirs=True) |
| 539 | deploy_chrome._UploadStagingDir(options, self.tempdir, self.staging_dir) |
| 540 | self.assertEqual( |
| 541 | mockGsCopy.call_args_list, |
| 542 | [ |
| 543 | mock.call(self.staging_tarball_path, staging_upload, acl=""), |
| 544 | ], |
| 545 | ) |
Jae Hoon Kim | df84291 | 2022-05-19 06:40:42 +0000 | [diff] [blame] | 546 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 547 | @cros_test_lib.pytestmark_network_test |
| 548 | def testUploadStagingPublicReadACL(self): |
| 549 | """Upload staging directory with public-read ACL.""" |
| 550 | mockGsCopy = self.PatchObject(gs.GSContext, "Copy") |
| 551 | staging_upload = "gs://some-path" |
| 552 | options = _ParseCommandLine( |
| 553 | self.common_flags |
| 554 | + ["--staging-upload", staging_upload, "--public-read"] |
| 555 | ) |
| 556 | osutils.Touch(os.path.join(self.build_dir, "chrome"), makedirs=True) |
| 557 | deploy_chrome._UploadStagingDir(options, self.tempdir, self.staging_dir) |
| 558 | self.assertEqual( |
| 559 | mockGsCopy.call_args_list, |
| 560 | [ |
| 561 | mock.call( |
| 562 | self.staging_tarball_path, staging_upload, acl="public-read" |
| 563 | ), |
| 564 | ], |
| 565 | ) |
Jae Hoon Kim | df84291 | 2022-05-19 06:40:42 +0000 | [diff] [blame] | 566 | |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 567 | |
| 568 | class DeployTestBuildDir(cros_test_lib.MockTempDirTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 569 | """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] | 570 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 571 | def _GetDeployChrome(self, args): |
| 572 | options = _ParseCommandLine(args) |
| 573 | return deploy_chrome.DeployChrome( |
| 574 | options, self.tempdir, os.path.join(self.tempdir, "staging") |
| 575 | ) |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 576 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 577 | def setUp(self): |
| 578 | self.staging_dir = os.path.join(self.tempdir, "staging") |
| 579 | self.build_dir = os.path.join(self.tempdir, "build_dir") |
| 580 | self.deploy_mock = self.StartPatcher(DeployChromeMock()) |
| 581 | self.deploy = self._GetDeployChrome( |
| 582 | list(_REGULAR_TO) |
| 583 | + [ |
| 584 | "--board", |
| 585 | _TARGET_BOARD, |
| 586 | "--build-dir", |
| 587 | self.build_dir, |
| 588 | "--staging-only", |
| 589 | "--cache-dir", |
| 590 | str(self.tempdir), |
| 591 | "--sloppy", |
| 592 | ] |
| 593 | ) |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 594 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 595 | def getCopyPath(self, source_path): |
| 596 | """Return a chrome_util.Path or None if not present.""" |
| 597 | paths = [p for p in self.deploy.copy_paths if p.src == source_path] |
| 598 | return paths[0] if paths else None |
| 599 | |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 600 | |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 601 | class TestDeploymentType(DeployTestBuildDir): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 602 | """Test detection of deployment type using build dir.""" |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 603 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 604 | def testAppShellDetection(self): |
| 605 | """Check for an app_shell deployment""" |
| 606 | osutils.Touch( |
| 607 | os.path.join(self.deploy.options.build_dir, "app_shell"), |
| 608 | makedirs=True, |
| 609 | ) |
| 610 | self.deploy._CheckDeployType() |
| 611 | self.assertTrue(self.getCopyPath("app_shell")) |
| 612 | self.assertFalse(self.getCopyPath("chrome")) |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 613 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 614 | def testChromeAndAppShellDetection(self): |
| 615 | """Check for a chrome deployment when app_shell also exists.""" |
| 616 | osutils.Touch( |
| 617 | os.path.join(self.deploy.options.build_dir, "chrome"), makedirs=True |
| 618 | ) |
| 619 | osutils.Touch( |
| 620 | os.path.join(self.deploy.options.build_dir, "app_shell"), |
| 621 | makedirs=True, |
| 622 | ) |
| 623 | self.deploy._CheckDeployType() |
| 624 | self.assertTrue(self.getCopyPath("chrome")) |
| 625 | self.assertFalse(self.getCopyPath("app_shell")) |
Steve Fung | 63d705d | 2014-03-16 03:14:03 -0700 | [diff] [blame] | 626 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 627 | def testChromeDetection(self): |
| 628 | """Check for a regular chrome deployment""" |
| 629 | osutils.Touch( |
| 630 | os.path.join(self.deploy.options.build_dir, "chrome"), makedirs=True |
| 631 | ) |
| 632 | self.deploy._CheckDeployType() |
| 633 | self.assertTrue(self.getCopyPath("chrome")) |
| 634 | self.assertFalse(self.getCopyPath("app_shell")) |
Ben Pastene | e484b34 | 2020-06-30 18:29:27 -0700 | [diff] [blame] | 635 | |
| 636 | |
| 637 | class TestDeployTestBinaries(cros_test_lib.RunCommandTempDirTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 638 | """Tests _DeployTestBinaries().""" |
Ben Pastene | e484b34 | 2020-06-30 18:29:27 -0700 | [diff] [blame] | 639 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 640 | def setUp(self): |
| 641 | options = _ParseCommandLine( |
| 642 | list(_REGULAR_TO) |
| 643 | + [ |
| 644 | "--board", |
| 645 | _TARGET_BOARD, |
| 646 | "--force", |
| 647 | "--mount", |
| 648 | "--build-dir", |
| 649 | os.path.join(self.tempdir, "build_dir"), |
| 650 | "--nostrip", |
| 651 | ] |
| 652 | ) |
Daniil Lunev | 0c4f65c | 2022-09-19 11:01:34 +1000 | [diff] [blame] | 653 | self.remote_exists_mock = self.PatchObject( |
| 654 | remote_access.RemoteDevice, "IfFileExists", return_value=False |
| 655 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 656 | self.deploy = deploy_chrome.DeployChrome( |
| 657 | options, self.tempdir, os.path.join(self.tempdir, "staging") |
| 658 | ) |
Ben Pastene | e484b34 | 2020-06-30 18:29:27 -0700 | [diff] [blame] | 659 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 660 | def _SimulateBinaries(self): |
| 661 | # Ensure the staging dir contains the right binaries to copy over. |
| 662 | test_binaries = [ |
| 663 | "run_a_tests", |
| 664 | "run_b_tests", |
| 665 | "run_c_tests", |
| 666 | ] |
| 667 | # Simulate having the binaries both on the device and in our local build |
| 668 | # dir. |
| 669 | self.rc.AddCmdResult( |
| 670 | partial_mock.In(deploy_chrome._FIND_TEST_BIN_CMD), |
| 671 | stdout="\n".join(test_binaries), |
| 672 | ) |
| 673 | for binary in test_binaries: |
| 674 | osutils.Touch( |
| 675 | os.path.join(self.deploy.options.build_dir, binary), |
| 676 | makedirs=True, |
| 677 | mode=0o700, |
| 678 | ) |
| 679 | return test_binaries |
Ben Pastene | e484b34 | 2020-06-30 18:29:27 -0700 | [diff] [blame] | 680 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 681 | def _AssertBinariesInStagingDir(self, test_binaries): |
| 682 | # Ensure the binaries were placed in the staging dir used to copy them over. |
| 683 | staging_dir = os.path.join( |
| 684 | self.tempdir, os.path.basename(deploy_chrome._CHROME_TEST_BIN_DIR) |
| 685 | ) |
| 686 | for binary in test_binaries: |
| 687 | self.assertIn(binary, os.listdir(staging_dir)) |
Erik Chen | 75a2f49 | 2020-08-06 19:15:11 -0700 | [diff] [blame] | 688 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 689 | def testFindError(self): |
| 690 | """Ensure an error is thrown if we can't inspect the device.""" |
| 691 | self.rc.AddCmdResult( |
| 692 | partial_mock.In(deploy_chrome._FIND_TEST_BIN_CMD), 1 |
| 693 | ) |
| 694 | self.assertRaises( |
| 695 | deploy_chrome.DeployFailure, self.deploy._DeployTestBinaries |
| 696 | ) |
Brian Sheedy | 86f1234 | 2020-10-29 15:30:02 -0700 | [diff] [blame] | 697 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 698 | def testSuccess(self): |
| 699 | """Ensure that the happy path succeeds as expected.""" |
| 700 | test_binaries = self._SimulateBinaries() |
Brian Sheedy | 86f1234 | 2020-10-29 15:30:02 -0700 | [diff] [blame] | 701 | self.deploy._DeployTestBinaries() |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 702 | self._AssertBinariesInStagingDir(test_binaries) |
| 703 | |
| 704 | def testRetrySuccess(self): |
| 705 | """Ensure that a transient exception still results in success.""" |
| 706 | # Raises a RunCommandError on its first invocation, but passes on subsequent |
| 707 | # calls. |
| 708 | def SideEffect(*args, **kwargs): |
| 709 | # pylint: disable=unused-argument |
| 710 | if not SideEffect.called: |
| 711 | SideEffect.called = True |
| 712 | raise cros_build_lib.RunCommandError("fail") |
| 713 | |
| 714 | SideEffect.called = False |
| 715 | |
| 716 | test_binaries = self._SimulateBinaries() |
| 717 | with mock.patch.object( |
| 718 | remote_access.ChromiumOSDevice, |
| 719 | "CopyToDevice", |
| 720 | side_effect=SideEffect, |
| 721 | ) as copy_mock: |
| 722 | self.deploy._DeployTestBinaries() |
| 723 | self.assertEqual(copy_mock.call_count, 2) |
| 724 | self._AssertBinariesInStagingDir(test_binaries) |
| 725 | |
| 726 | def testRetryFailure(self): |
| 727 | """Ensure that consistent exceptions result in failure.""" |
| 728 | self._SimulateBinaries() |
| 729 | with self.assertRaises(cros_build_lib.RunCommandError): |
| 730 | with mock.patch.object( |
| 731 | remote_access.ChromiumOSDevice, |
| 732 | "CopyToDevice", |
| 733 | side_effect=cros_build_lib.RunCommandError("fail"), |
| 734 | ): |
| 735 | self.deploy._DeployTestBinaries() |
Brian Sheedy | 86f1234 | 2020-10-29 15:30:02 -0700 | [diff] [blame] | 736 | |
Erik Chen | 75a2f49 | 2020-08-06 19:15:11 -0700 | [diff] [blame] | 737 | |
| 738 | class LacrosPerformTest(cros_test_lib.RunCommandTempDirTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 739 | """Line coverage for Perform() method with --lacros option.""" |
Erik Chen | 75a2f49 | 2020-08-06 19:15:11 -0700 | [diff] [blame] | 740 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 741 | def setUp(self): |
| 742 | self.deploy = None |
| 743 | self._ran_start_command = False |
| 744 | self.StartPatcher(parallel_unittest.ParallelMock()) |
Yuke Liao | 24fc60c | 2020-12-26 22:16:49 -0800 | [diff] [blame] | 745 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 746 | def start_ui_side_effect(*args, **kwargs): |
| 747 | # pylint: disable=unused-argument |
| 748 | self._ran_start_command = True |
Yuke Liao | 24fc60c | 2020-12-26 22:16:49 -0800 | [diff] [blame] | 749 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 750 | self.rc.AddCmdResult( |
| 751 | partial_mock.In("start ui"), side_effect=start_ui_side_effect |
| 752 | ) |
Yuke Liao | 24fc60c | 2020-12-26 22:16:49 -0800 | [diff] [blame] | 753 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 754 | def prepareDeploy(self, options=None): |
| 755 | if not options: |
| 756 | options = _ParseCommandLine( |
| 757 | [ |
| 758 | "--lacros", |
| 759 | "--nostrip", |
| 760 | "--build-dir", |
| 761 | "/path/to/nowhere", |
| 762 | "--device", |
| 763 | "monkey", |
| 764 | ] |
| 765 | ) |
| 766 | self.deploy = deploy_chrome.DeployChrome( |
| 767 | options, self.tempdir, os.path.join(self.tempdir, "staging") |
| 768 | ) |
Erik Chen | 75a2f49 | 2020-08-06 19:15:11 -0700 | [diff] [blame] | 769 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 770 | # These methods being mocked are all side effects expected for a --lacros |
| 771 | # deploy. |
| 772 | self.deploy._EnsureTargetDir = mock.Mock() |
| 773 | self.deploy._GetDeviceInfo = mock.Mock() |
| 774 | self.deploy._CheckConnection = mock.Mock() |
| 775 | self.deploy._MountRootfsAsWritable = mock.Mock() |
| 776 | self.deploy._PrepareStagingDir = mock.Mock() |
| 777 | self.deploy._CheckDeviceFreeSpace = mock.Mock() |
| 778 | self.deploy._KillAshChromeIfNeeded = mock.Mock() |
Erik Chen | 75a2f49 | 2020-08-06 19:15:11 -0700 | [diff] [blame] | 779 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 780 | def testConfNotModified(self): |
| 781 | """When the conf file is not modified we don't restart chrome .""" |
| 782 | self.prepareDeploy() |
| 783 | self.deploy.Perform() |
| 784 | self.deploy._KillAshChromeIfNeeded.assert_not_called() |
| 785 | self.assertFalse(self._ran_start_command) |
Erik Chen | 75a2f49 | 2020-08-06 19:15:11 -0700 | [diff] [blame] | 786 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 787 | def testConfModified(self): |
| 788 | """When the conf file is modified we restart chrome.""" |
| 789 | self.prepareDeploy() |
Erik Chen | 75a2f49 | 2020-08-06 19:15:11 -0700 | [diff] [blame] | 790 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 791 | # We intentionally add '\n' to MODIFIED_CONF_FILE to simulate echo adding a |
| 792 | # newline when invoked in the shell. |
| 793 | self.rc.AddCmdResult( |
| 794 | partial_mock.In(deploy_chrome.ENABLE_LACROS_VIA_CONF_COMMAND), |
| 795 | stdout=deploy_chrome.MODIFIED_CONF_FILE + "\n", |
| 796 | ) |
Erik Chen | 75a2f49 | 2020-08-06 19:15:11 -0700 | [diff] [blame] | 797 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 798 | self.deploy.Perform() |
| 799 | self.deploy._KillAshChromeIfNeeded.assert_called() |
| 800 | self.assertTrue(self._ran_start_command) |
Yuke Liao | 24fc60c | 2020-12-26 22:16:49 -0800 | [diff] [blame] | 801 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 802 | def testSkipModifyingConf(self): |
| 803 | """SKip modifying the config file when the argument is specified.""" |
| 804 | self.prepareDeploy( |
| 805 | _ParseCommandLine( |
| 806 | [ |
| 807 | "--lacros", |
| 808 | "--nostrip", |
| 809 | "--build-dir", |
| 810 | "/path/to/nowhere", |
| 811 | "--device", |
| 812 | "monkey", |
| 813 | "--skip-modifying-config-file", |
| 814 | ] |
| 815 | ) |
| 816 | ) |
Yuke Liao | 24fc60c | 2020-12-26 22:16:49 -0800 | [diff] [blame] | 817 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 818 | self.rc.AddCmdResult( |
| 819 | partial_mock.In(deploy_chrome.ENABLE_LACROS_VIA_CONF_COMMAND), |
| 820 | stdout=deploy_chrome.MODIFIED_CONF_FILE + "\n", |
| 821 | ) |
Yuke Liao | 24fc60c | 2020-12-26 22:16:49 -0800 | [diff] [blame] | 822 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 823 | self.deploy.Perform() |
| 824 | self.deploy._KillAshChromeIfNeeded.assert_not_called() |
| 825 | self.assertFalse(self._ran_start_command) |