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