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