Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 2 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 6 | """Unit tests for the deploy_chrome script.""" |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 7 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 8 | from __future__ import print_function |
| 9 | |
Anushruth | 8d79767 | 2019-10-17 12:22:31 -0700 | [diff] [blame] | 10 | import errno |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 11 | import os |
Mike Frysinger | 6165cdc | 2020-02-21 02:38:07 -0500 | [diff] [blame] | 12 | import sys |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 13 | import time |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 14 | |
Mike Frysinger | 6db648e | 2018-07-24 19:57:58 -0400 | [diff] [blame] | 15 | import mock |
| 16 | |
David Pursell | cfd5887 | 2015-03-19 09:15:48 -0700 | [diff] [blame] | 17 | from chromite.cli.cros import cros_chrome_sdk_unittest |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 18 | from chromite.lib import chrome_util |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 19 | from chromite.lib import cros_build_lib |
| 20 | from chromite.lib import cros_test_lib |
Ryan Cui | 686ec05 | 2013-02-12 16:39:41 -0800 | [diff] [blame] | 21 | from chromite.lib import osutils |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 22 | from chromite.lib import partial_mock |
Robert Flack | 1dc7ea8 | 2014-11-26 13:50:24 -0500 | [diff] [blame] | 23 | from chromite.lib import remote_access |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 24 | from chromite.lib import remote_access_unittest |
| 25 | from chromite.scripts import deploy_chrome |
| 26 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 27 | |
Mike Frysinger | 6165cdc | 2020-02-21 02:38:07 -0500 | [diff] [blame] | 28 | assert sys.version_info >= (3, 6), 'This module requires Python 3.6+' |
| 29 | |
| 30 | |
Mike Frysinger | 27e21b7 | 2018-07-12 14:20:21 -0400 | [diff] [blame] | 31 | # pylint: disable=protected-access |
| 32 | |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 33 | |
| 34 | _REGULAR_TO = ('--to', 'monkey') |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame^] | 35 | _TARGET_BOARD = 'eve' |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 36 | _GS_PATH = 'gs://foon' |
| 37 | |
| 38 | |
| 39 | def _ParseCommandLine(argv): |
| 40 | return deploy_chrome._ParseCommandLine(['--log-level', 'debug'] + argv) |
| 41 | |
| 42 | |
| 43 | class InterfaceTest(cros_test_lib.OutputTestCase): |
| 44 | """Tests the commandline interface of the script.""" |
| 45 | |
| 46 | def testGsLocalPathUnSpecified(self): |
| 47 | """Test no chrome path specified.""" |
| 48 | with self.OutputCapturer(): |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame^] | 49 | self.assertRaises2(SystemExit, _ParseCommandLine, |
| 50 | list(_REGULAR_TO) + ['--board', _TARGET_BOARD], |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 51 | check_attrs={'code': 2}) |
| 52 | |
| 53 | def testGsPathSpecified(self): |
| 54 | """Test case of GS path specified.""" |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame^] | 55 | argv = list(_REGULAR_TO) + ['--board', _TARGET_BOARD, '--gs-path', _GS_PATH] |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 56 | _ParseCommandLine(argv) |
| 57 | |
| 58 | def testLocalPathSpecified(self): |
| 59 | """Test case of local path specified.""" |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame^] | 60 | argv = list(_REGULAR_TO) + ['--board', _TARGET_BOARD, '--local-pkg-path', |
| 61 | '/path/to/chrome'] |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 62 | _ParseCommandLine(argv) |
| 63 | |
| 64 | def testNoTarget(self): |
| 65 | """Test no target specified.""" |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame^] | 66 | argv = ['--board', _TARGET_BOARD, '--gs-path', _GS_PATH] |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 67 | self.assertParseError(argv) |
| 68 | |
| 69 | def assertParseError(self, argv): |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 70 | with self.OutputCapturer(): |
| 71 | self.assertRaises2(SystemExit, _ParseCommandLine, argv, |
| 72 | check_attrs={'code': 2}) |
| 73 | |
Thiago Goncales | 1279331 | 2013-05-23 11:26:17 -0700 | [diff] [blame] | 74 | def testMountOptionSetsTargetDir(self): |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame^] | 75 | argv = list(_REGULAR_TO) + ['--board', _TARGET_BOARD, '--gs-path', _GS_PATH, |
| 76 | '--mount'] |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 77 | options = _ParseCommandLine(argv) |
Thiago Goncales | 1279331 | 2013-05-23 11:26:17 -0700 | [diff] [blame] | 78 | self.assertIsNot(options.target_dir, None) |
| 79 | |
| 80 | def testMountOptionSetsMountDir(self): |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame^] | 81 | argv = list(_REGULAR_TO) + ['--board', _TARGET_BOARD, '--gs-path', _GS_PATH, |
| 82 | '--mount'] |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 83 | options = _ParseCommandLine(argv) |
Thiago Goncales | 1279331 | 2013-05-23 11:26:17 -0700 | [diff] [blame] | 84 | self.assertIsNot(options.mount_dir, None) |
| 85 | |
| 86 | def testMountOptionDoesNotOverrideTargetDir(self): |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame^] | 87 | argv = list(_REGULAR_TO) + ['--board', _TARGET_BOARD, '--gs-path', _GS_PATH, |
| 88 | '--mount', '--target-dir', '/foo/bar/cow'] |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 89 | options = _ParseCommandLine(argv) |
Thiago Goncales | 1279331 | 2013-05-23 11:26:17 -0700 | [diff] [blame] | 90 | self.assertEqual(options.target_dir, '/foo/bar/cow') |
| 91 | |
| 92 | def testMountOptionDoesNotOverrideMountDir(self): |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame^] | 93 | argv = list(_REGULAR_TO) + ['--board', _TARGET_BOARD, '--gs-path', _GS_PATH, |
| 94 | '--mount', '--mount-dir', '/foo/bar/cow'] |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 95 | options = _ParseCommandLine(argv) |
Thiago Goncales | 1279331 | 2013-05-23 11:26:17 -0700 | [diff] [blame] | 96 | self.assertEqual(options.mount_dir, '/foo/bar/cow') |
| 97 | |
Adrian Elder | a2c548a | 2017-11-07 19:01:29 -0500 | [diff] [blame] | 98 | def testSshIdentityOptionSetsOption(self): |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame^] | 99 | argv = list(_REGULAR_TO) + ['--board', _TARGET_BOARD, |
| 100 | '--private-key', '/foo/bar/key', |
Bernie Thompson | 93b9ee6 | 2018-02-21 14:56:16 -0800 | [diff] [blame] | 101 | '--build-dir', '/path/to/nowhere'] |
Adrian Elder | a2c548a | 2017-11-07 19:01:29 -0500 | [diff] [blame] | 102 | options = _ParseCommandLine(argv) |
| 103 | self.assertEqual(options.private_key, '/foo/bar/key') |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 104 | |
| 105 | class DeployChromeMock(partial_mock.PartialMock): |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 106 | """Deploy Chrome Mock Class.""" |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 107 | |
| 108 | TARGET = 'chromite.scripts.deploy_chrome.DeployChrome' |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 109 | ATTRS = ('_KillProcsIfNeeded', '_DisableRootfsVerification') |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 110 | |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 111 | def __init__(self): |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 112 | partial_mock.PartialMock.__init__(self) |
Robert Flack | 1dc7ea8 | 2014-11-26 13:50:24 -0500 | [diff] [blame] | 113 | self.remote_device_mock = remote_access_unittest.RemoteDeviceMock() |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 114 | # Target starts off as having rootfs verification enabled. |
Ryan Cui | e18f24f | 2012-12-03 18:39:55 -0800 | [diff] [blame] | 115 | self.rsh_mock = remote_access_unittest.RemoteShMock() |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 116 | self.rsh_mock.SetDefaultCmdResult(0) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 117 | self.MockMountCmd(1) |
David Haddock | 3151d91 | 2017-10-24 03:50:32 +0000 | [diff] [blame] | 118 | self.rsh_mock.AddCmdResult( |
Anushruth | 8d79767 | 2019-10-17 12:22:31 -0700 | [diff] [blame] | 119 | deploy_chrome.LSOF_COMMAND_CHROME % (deploy_chrome._CHROME_DIR,), 1) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 120 | |
| 121 | def MockMountCmd(self, returnvalue): |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 122 | self.rsh_mock.AddCmdResult(deploy_chrome.MOUNT_RW_COMMAND, |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 123 | returnvalue) |
| 124 | |
| 125 | def _DisableRootfsVerification(self, inst): |
| 126 | with mock.patch.object(time, 'sleep'): |
| 127 | self.backup['_DisableRootfsVerification'](inst) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 128 | |
Ryan Cui | 4d6fca9 | 2012-12-13 16:41:56 -0800 | [diff] [blame] | 129 | def PreStart(self): |
Robert Flack | 1dc7ea8 | 2014-11-26 13:50:24 -0500 | [diff] [blame] | 130 | self.remote_device_mock.start() |
Ryan Cui | 4d6fca9 | 2012-12-13 16:41:56 -0800 | [diff] [blame] | 131 | self.rsh_mock.start() |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 132 | |
Ryan Cui | 4d6fca9 | 2012-12-13 16:41:56 -0800 | [diff] [blame] | 133 | def PreStop(self): |
| 134 | self.rsh_mock.stop() |
Robert Flack | 1dc7ea8 | 2014-11-26 13:50:24 -0500 | [diff] [blame] | 135 | self.remote_device_mock.stop() |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 136 | |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 137 | def _KillProcsIfNeeded(self, _inst): |
| 138 | # Fully stub out for now. |
| 139 | pass |
| 140 | |
| 141 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 142 | class DeployTest(cros_test_lib.MockTempDirTestCase): |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 143 | """Setup a deploy object with a GS-path for use in tests.""" |
| 144 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 145 | def _GetDeployChrome(self, args): |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 146 | options = _ParseCommandLine(args) |
Ryan Cui | a56a71e | 2012-10-18 18:40:35 -0700 | [diff] [blame] | 147 | return deploy_chrome.DeployChrome( |
| 148 | options, self.tempdir, os.path.join(self.tempdir, 'staging')) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 149 | |
| 150 | def setUp(self): |
Ryan Cui | f1416f3 | 2013-01-22 18:43:41 -0800 | [diff] [blame] | 151 | self.deploy_mock = self.StartPatcher(DeployChromeMock()) |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame^] | 152 | self.deploy = self._GetDeployChrome(list(_REGULAR_TO) + |
| 153 | ['--board', _TARGET_BOARD, '--gs-path', |
| 154 | _GS_PATH, '--force', '--mount']) |
Luigi Semenzato | 1bc79b2 | 2016-11-22 16:32:17 -0800 | [diff] [blame] | 155 | self.remote_reboot_mock = \ |
| 156 | self.PatchObject(remote_access.RemoteAccess, 'RemoteReboot', |
| 157 | return_value=True) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 158 | |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame^] | 159 | |
| 160 | class TestCheckIfBoardMatches(DeployTest): |
| 161 | """Testing checking whether the DUT board matches the target board.""" |
| 162 | |
| 163 | def testMatchedBoard(self): |
| 164 | """Test the case where the DUT board matches the target board.""" |
| 165 | self.PatchObject(remote_access.ChromiumOSDevice, 'board', _TARGET_BOARD) |
| 166 | self.assertTrue(self.deploy.options.force) |
| 167 | self.deploy._CheckBoard() |
| 168 | self.deploy.options.force = False |
| 169 | self.deploy._CheckBoard() |
| 170 | |
| 171 | def testMismatchedBoard(self): |
| 172 | """Test the case where the DUT board does not match the target board.""" |
| 173 | self.PatchObject(remote_access.ChromiumOSDevice, 'board', 'cedar') |
| 174 | self.assertTrue(self.deploy.options.force) |
| 175 | self.deploy._CheckBoard() |
| 176 | self.deploy.options.force = False |
| 177 | self.PatchObject(cros_build_lib, 'BooleanPrompt', return_value=True) |
| 178 | self.deploy._CheckBoard() |
| 179 | self.PatchObject(cros_build_lib, 'BooleanPrompt', return_value=False) |
| 180 | self.assertRaises(deploy_chrome.DeployFailure, self.deploy._CheckBoard) |
| 181 | |
| 182 | |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 183 | class TestDisableRootfsVerification(DeployTest): |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 184 | """Testing disabling of rootfs verification and RO mode.""" |
| 185 | |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 186 | def testDisableRootfsVerificationSuccess(self): |
| 187 | """Test the working case, disabling rootfs verification.""" |
| 188 | self.deploy_mock.MockMountCmd(0) |
| 189 | self.deploy._DisableRootfsVerification() |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 190 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 191 | |
| 192 | def testDisableRootfsVerificationFailure(self): |
| 193 | """Test failure to disable rootfs verification.""" |
Mike Frysinger | 27e21b7 | 2018-07-12 14:20:21 -0400 | [diff] [blame] | 194 | # pylint: disable=unused-argument |
Shuqian Zhao | 14e6109 | 2017-11-17 00:02:16 +0000 | [diff] [blame] | 195 | def RaiseRunCommandError(timeout_sec=None): |
Mike Frysinger | 929f3ba | 2019-09-12 03:24:59 -0400 | [diff] [blame] | 196 | raise cros_build_lib.RunCommandError('Mock RunCommandError') |
Luigi Semenzato | 1bc79b2 | 2016-11-22 16:32:17 -0800 | [diff] [blame] | 197 | self.remote_reboot_mock.side_effect = RaiseRunCommandError |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 198 | self.assertRaises(cros_build_lib.RunCommandError, |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 199 | self.deploy._DisableRootfsVerification) |
Luigi Semenzato | 1bc79b2 | 2016-11-22 16:32:17 -0800 | [diff] [blame] | 200 | self.remote_reboot_mock.side_effect = None |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 201 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 202 | |
| 203 | |
| 204 | class TestMount(DeployTest): |
| 205 | """Testing mount success and failure.""" |
| 206 | |
| 207 | def testSuccess(self): |
| 208 | """Test case where we are able to mount as writable.""" |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 209 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 210 | self.deploy_mock.MockMountCmd(0) |
| 211 | self.deploy._MountRootfsAsWritable() |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 212 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 213 | |
| 214 | def testMountError(self): |
| 215 | """Test that mount failure doesn't raise an exception by default.""" |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 216 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame^] | 217 | self.PatchObject(remote_access.ChromiumOSDevice, 'IsDirWritable', |
Robert Flack | 1dc7ea8 | 2014-11-26 13:50:24 -0500 | [diff] [blame] | 218 | return_value=False, autospec=True) |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 219 | self.deploy._MountRootfsAsWritable() |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 220 | self.assertTrue(self.deploy._root_dir_is_still_readonly.is_set()) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 221 | |
| 222 | def testMountRwFailure(self): |
Mike Frysinger | f5a3b2d | 2019-12-12 14:36:17 -0500 | [diff] [blame] | 223 | """Test that mount failure raises an exception if check=True.""" |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 224 | self.assertRaises(cros_build_lib.RunCommandError, |
Mike Frysinger | f5a3b2d | 2019-12-12 14:36:17 -0500 | [diff] [blame] | 225 | self.deploy._MountRootfsAsWritable, check=True) |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 226 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
Robert Flack | 1dc7ea8 | 2014-11-26 13:50:24 -0500 | [diff] [blame] | 227 | |
| 228 | def testMountTempDir(self): |
| 229 | """Test that mount succeeds if target dir is writable.""" |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 230 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame^] | 231 | self.PatchObject(remote_access.ChromiumOSDevice, 'IsDirWritable', |
Robert Flack | 1dc7ea8 | 2014-11-26 13:50:24 -0500 | [diff] [blame] | 232 | return_value=True, autospec=True) |
| 233 | self.deploy._MountRootfsAsWritable() |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 234 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 235 | |
| 236 | |
Anushruth | 8d79767 | 2019-10-17 12:22:31 -0700 | [diff] [blame] | 237 | class TestMountTarget(DeployTest): |
Mike Frysinger | df1d0b0 | 2019-11-12 17:44:12 -0500 | [diff] [blame] | 238 | """Testing mount and umount command handling.""" |
Anushruth | 8d79767 | 2019-10-17 12:22:31 -0700 | [diff] [blame] | 239 | |
| 240 | def testMountTargetUmountFailure(self): |
| 241 | """Test error being thrown if umount fails. |
| 242 | |
| 243 | Test that 'lsof' is run on mount-dir and 'mount -rbind' command is not run |
| 244 | if 'umount' cmd fails. |
| 245 | """ |
| 246 | mount_dir = self.deploy.options.mount_dir |
| 247 | target_dir = self.deploy.options.target_dir |
| 248 | self.deploy_mock.rsh_mock.AddCmdResult( |
| 249 | deploy_chrome._UMOUNT_DIR_IF_MOUNTPOINT_CMD % |
| 250 | {'dir': mount_dir}, returncode=errno.EBUSY, stderr='Target is Busy') |
| 251 | self.deploy_mock.rsh_mock.AddCmdResult(deploy_chrome.LSOF_COMMAND % |
| 252 | (mount_dir,), returncode=0, |
| 253 | stdout='process ' + mount_dir) |
| 254 | # Check for RunCommandError being thrown. |
| 255 | self.assertRaises(cros_build_lib.RunCommandError, |
| 256 | self.deploy._MountTarget) |
| 257 | # Check for the 'mount -rbind' command not run. |
| 258 | self.deploy_mock.rsh_mock.assertCommandContains( |
| 259 | (deploy_chrome._BIND_TO_FINAL_DIR_CMD % (target_dir, mount_dir)), |
| 260 | expected=False) |
| 261 | # Check for lsof command being called. |
| 262 | self.deploy_mock.rsh_mock.assertCommandContains( |
| 263 | (deploy_chrome.LSOF_COMMAND % (mount_dir,))) |
| 264 | |
| 265 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 266 | class TestUiJobStarted(DeployTest): |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 267 | """Test detection of a running 'ui' job.""" |
| 268 | |
Ryan Cui | f2d1a58 | 2013-02-19 14:08:13 -0800 | [diff] [blame] | 269 | def MockStatusUiCmd(self, **kwargs): |
David Haddock | 3151d91 | 2017-10-24 03:50:32 +0000 | [diff] [blame] | 270 | self.deploy_mock.rsh_mock.AddCmdResult('status ui', **kwargs) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 271 | |
| 272 | def testUiJobStartedFalse(self): |
| 273 | """Correct results with a stopped job.""" |
Ryan Cui | f2d1a58 | 2013-02-19 14:08:13 -0800 | [diff] [blame] | 274 | self.MockStatusUiCmd(output='ui stop/waiting') |
| 275 | self.assertFalse(self.deploy._CheckUiJobStarted()) |
| 276 | |
| 277 | def testNoUiJob(self): |
| 278 | """Correct results when the job doesn't exist.""" |
| 279 | self.MockStatusUiCmd(error='start: Unknown job: ui', returncode=1) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 280 | self.assertFalse(self.deploy._CheckUiJobStarted()) |
| 281 | |
| 282 | def testCheckRootfsWriteableTrue(self): |
| 283 | """Correct results with a running job.""" |
Ryan Cui | f2d1a58 | 2013-02-19 14:08:13 -0800 | [diff] [blame] | 284 | self.MockStatusUiCmd(output='ui start/running, process 297') |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 285 | self.assertTrue(self.deploy._CheckUiJobStarted()) |
| 286 | |
| 287 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 288 | class StagingTest(cros_test_lib.MockTempDirTestCase): |
| 289 | """Test user-mode and ebuild-mode staging functionality.""" |
| 290 | |
| 291 | def setUp(self): |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 292 | self.staging_dir = os.path.join(self.tempdir, 'staging') |
| 293 | self.build_dir = os.path.join(self.tempdir, 'build_dir') |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame^] | 294 | self.common_flags = ['--board', _TARGET_BOARD, |
| 295 | '--build-dir', self.build_dir, '--staging-only', |
| 296 | '--cache-dir', self.tempdir] |
Ryan Cui | a0215a7 | 2013-02-14 16:20:45 -0800 | [diff] [blame] | 297 | self.sdk_mock = self.StartPatcher(cros_chrome_sdk_unittest.SDKFetcherMock()) |
Ryan Cui | 686ec05 | 2013-02-12 16:39:41 -0800 | [diff] [blame] | 298 | self.PatchObject( |
| 299 | osutils, 'SourceEnvironment', autospec=True, |
| 300 | return_value={'STRIP': 'x86_64-cros-linux-gnu-strip'}) |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 301 | |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 302 | def testSingleFileDeployFailure(self): |
| 303 | """Default staging enforces that mandatory files are copied""" |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 304 | options = _ParseCommandLine(self.common_flags) |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 305 | osutils.Touch(os.path.join(self.build_dir, 'chrome'), makedirs=True) |
| 306 | self.assertRaises( |
| 307 | chrome_util.MissingPathError, deploy_chrome._PrepareStagingDir, |
Daniel Erat | c89829c | 2014-05-12 17:24:21 -0700 | [diff] [blame] | 308 | options, self.tempdir, self.staging_dir, chrome_util._COPY_PATHS_CHROME) |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 309 | |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 310 | def testSloppyDeployFailure(self): |
| 311 | """Sloppy staging enforces that at least one file is copied.""" |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 312 | options = _ParseCommandLine(self.common_flags + ['--sloppy']) |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 313 | self.assertRaises( |
| 314 | chrome_util.MissingPathError, deploy_chrome._PrepareStagingDir, |
Daniel Erat | c89829c | 2014-05-12 17:24:21 -0700 | [diff] [blame] | 315 | options, self.tempdir, self.staging_dir, chrome_util._COPY_PATHS_CHROME) |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 316 | |
| 317 | def testSloppyDeploySuccess(self): |
| 318 | """Sloppy staging - stage one file.""" |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 319 | options = _ParseCommandLine(self.common_flags + ['--sloppy']) |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 320 | osutils.Touch(os.path.join(self.build_dir, 'chrome'), makedirs=True) |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 321 | deploy_chrome._PrepareStagingDir(options, self.tempdir, self.staging_dir, |
Daniel Erat | c89829c | 2014-05-12 17:24:21 -0700 | [diff] [blame] | 322 | chrome_util._COPY_PATHS_CHROME) |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 323 | |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 324 | |
| 325 | class DeployTestBuildDir(cros_test_lib.MockTempDirTestCase): |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 326 | """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] | 327 | |
| 328 | def _GetDeployChrome(self, args): |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 329 | options = _ParseCommandLine(args) |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 330 | return deploy_chrome.DeployChrome( |
| 331 | options, self.tempdir, os.path.join(self.tempdir, 'staging')) |
| 332 | |
| 333 | def setUp(self): |
| 334 | self.staging_dir = os.path.join(self.tempdir, 'staging') |
| 335 | self.build_dir = os.path.join(self.tempdir, 'build_dir') |
| 336 | self.deploy_mock = self.StartPatcher(DeployChromeMock()) |
| 337 | self.deploy = self._GetDeployChrome( |
Avery Musbach | 3edff0e | 2020-03-27 13:35:53 -0700 | [diff] [blame^] | 338 | list(_REGULAR_TO) + ['--board', _TARGET_BOARD, |
| 339 | '--build-dir', self.build_dir, '--staging-only', |
| 340 | '--cache-dir', self.tempdir, '--sloppy']) |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 341 | |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 342 | def getCopyPath(self, source_path): |
| 343 | """Return a chrome_util.Path or None if not present.""" |
| 344 | paths = [p for p in self.deploy.copy_paths if p.src == source_path] |
| 345 | return paths[0] if paths else None |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 346 | |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 347 | class TestDeploymentType(DeployTestBuildDir): |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 348 | """Test detection of deployment type using build dir.""" |
| 349 | |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 350 | def testAppShellDetection(self): |
| 351 | """Check for an app_shell deployment""" |
| 352 | osutils.Touch(os.path.join(self.deploy.options.build_dir, 'app_shell'), |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 353 | makedirs=True) |
| 354 | self.deploy._CheckDeployType() |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 355 | self.assertTrue(self.getCopyPath('app_shell')) |
| 356 | self.assertFalse(self.getCopyPath('chrome')) |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 357 | |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 358 | def testChromeAndAppShellDetection(self): |
Daniel Erat | f53bd3a | 2016-12-02 11:28:36 -0700 | [diff] [blame] | 359 | """Check for a chrome deployment when app_shell also exists.""" |
Steve Fung | 63d705d | 2014-03-16 03:14:03 -0700 | [diff] [blame] | 360 | osutils.Touch(os.path.join(self.deploy.options.build_dir, 'chrome'), |
| 361 | makedirs=True) |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 362 | osutils.Touch(os.path.join(self.deploy.options.build_dir, 'app_shell'), |
Steve Fung | 63d705d | 2014-03-16 03:14:03 -0700 | [diff] [blame] | 363 | makedirs=True) |
| 364 | self.deploy._CheckDeployType() |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 365 | self.assertTrue(self.getCopyPath('chrome')) |
Daniel Erat | 9813f0e | 2014-11-12 11:00:28 -0700 | [diff] [blame] | 366 | self.assertFalse(self.getCopyPath('app_shell')) |
Steve Fung | 63d705d | 2014-03-16 03:14:03 -0700 | [diff] [blame] | 367 | |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 368 | def testChromeDetection(self): |
| 369 | """Check for a regular chrome deployment""" |
| 370 | osutils.Touch(os.path.join(self.deploy.options.build_dir, 'chrome'), |
| 371 | makedirs=True) |
| 372 | self.deploy._CheckDeployType() |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 373 | self.assertTrue(self.getCopyPath('chrome')) |
Daniel Erat | 9813f0e | 2014-11-12 11:00:28 -0700 | [diff] [blame] | 374 | self.assertFalse(self.getCopyPath('app_shell')) |