Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 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 | |
| 6 | |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 7 | import os |
| 8 | import sys |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 9 | import time |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 10 | |
| 11 | sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), |
| 12 | '..', '..')) |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 13 | |
Ryan Cui | 686ec05 | 2013-02-12 16:39:41 -0800 | [diff] [blame] | 14 | from chromite.cros.commands import cros_chrome_sdk_unittest |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 15 | from chromite.lib import chrome_util |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 16 | from chromite.lib import cros_build_lib |
| 17 | from chromite.lib import cros_test_lib |
Ryan Cui | 686ec05 | 2013-02-12 16:39:41 -0800 | [diff] [blame] | 18 | from chromite.lib import osutils |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 19 | from chromite.lib import partial_mock |
| 20 | from chromite.lib import remote_access_unittest |
| 21 | from chromite.scripts import deploy_chrome |
| 22 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 23 | |
Brian Harring | e752437 | 2012-12-23 02:02:56 -0800 | [diff] [blame] | 24 | # TODO(build): Finish test wrapper (http://crosbug.com/37517). |
| 25 | # Until then, this has to be after the chromite imports. |
| 26 | import mock |
| 27 | |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 28 | |
| 29 | # pylint: disable=W0212 |
| 30 | |
| 31 | _REGULAR_TO = ('--to', 'monkey') |
| 32 | _GS_PATH = 'gs://foon' |
| 33 | |
| 34 | |
| 35 | def _ParseCommandLine(argv): |
| 36 | return deploy_chrome._ParseCommandLine(['--log-level', 'debug'] + argv) |
| 37 | |
| 38 | |
| 39 | class InterfaceTest(cros_test_lib.OutputTestCase): |
| 40 | """Tests the commandline interface of the script.""" |
| 41 | |
Ryan Cui | 686ec05 | 2013-02-12 16:39:41 -0800 | [diff] [blame] | 42 | BOARD = 'lumpy' |
| 43 | |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 44 | def testGsLocalPathUnSpecified(self): |
| 45 | """Test no chrome path specified.""" |
| 46 | with self.OutputCapturer(): |
| 47 | self.assertRaises2(SystemExit, _ParseCommandLine, list(_REGULAR_TO), |
| 48 | check_attrs={'code': 2}) |
| 49 | |
| 50 | def testGsPathSpecified(self): |
| 51 | """Test case of GS path specified.""" |
| 52 | argv = list(_REGULAR_TO) + ['--gs-path', _GS_PATH] |
| 53 | _ParseCommandLine(argv) |
| 54 | |
| 55 | def testLocalPathSpecified(self): |
| 56 | """Test case of local path specified.""" |
Ryan Cui | a56a71e | 2012-10-18 18:40:35 -0700 | [diff] [blame] | 57 | argv = list(_REGULAR_TO) + ['--local-pkg-path', '/path/to/chrome'] |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 58 | _ParseCommandLine(argv) |
| 59 | |
| 60 | def testNoTarget(self): |
| 61 | """Test no target specified.""" |
| 62 | argv = ['--gs-path', _GS_PATH] |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 63 | self.assertParseError(argv) |
| 64 | |
| 65 | def assertParseError(self, argv): |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 66 | with self.OutputCapturer(): |
| 67 | self.assertRaises2(SystemExit, _ParseCommandLine, argv, |
| 68 | check_attrs={'code': 2}) |
| 69 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 70 | def testStagingFlagsNoStrict(self): |
| 71 | """Errors out when --staging-flags is set without --strict.""" |
| 72 | argv = ['--staging-only', '--build-dir=/path/to/nowhere', |
Ryan Cui | 686ec05 | 2013-02-12 16:39:41 -0800 | [diff] [blame] | 73 | '--board=%s' % self.BOARD, '--staging-flags=highdpi'] |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 74 | self.assertParseError(argv) |
| 75 | |
| 76 | def testStrictNoBuildDir(self): |
| 77 | """Errors out when --strict is set without --build-dir.""" |
| 78 | argv = ['--staging-only', '--strict', '--gs-path', _GS_PATH] |
| 79 | self.assertParseError(argv) |
| 80 | |
Ryan Cui | 686ec05 | 2013-02-12 16:39:41 -0800 | [diff] [blame] | 81 | def testNoBoardBuildDir(self): |
| 82 | argv = ['--staging-only', '--build-dir=/path/to/nowhere'] |
| 83 | self.assertParseError(argv) |
| 84 | |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 85 | |
| 86 | class DeployChromeMock(partial_mock.PartialMock): |
| 87 | |
| 88 | TARGET = 'chromite.scripts.deploy_chrome.DeployChrome' |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 89 | ATTRS = ('_KillProcsIfNeeded', '_DisableRootfsVerification') |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 90 | |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 91 | def __init__(self): |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 92 | partial_mock.PartialMock.__init__(self) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 93 | # Target starts off as having rootfs verification enabled. |
Ryan Cui | e18f24f | 2012-12-03 18:39:55 -0800 | [diff] [blame] | 94 | self.rsh_mock = remote_access_unittest.RemoteShMock() |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 95 | self.rsh_mock.SetDefaultCmdResult(0) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 96 | self.MockMountCmd(1) |
Pawel Osciak | 577773a | 2013-03-05 10:52:12 -0800 | [diff] [blame^] | 97 | self.rsh_mock.AddCmdResult( |
| 98 | deploy_chrome.LSOF_COMMAND % (deploy_chrome._CHROME_DIR,), 1) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 99 | |
| 100 | def MockMountCmd(self, returnvalue): |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 101 | self.rsh_mock.AddCmdResult(deploy_chrome.MOUNT_RW_COMMAND, |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 102 | returnvalue) |
| 103 | |
| 104 | def _DisableRootfsVerification(self, inst): |
| 105 | with mock.patch.object(time, 'sleep'): |
| 106 | self.backup['_DisableRootfsVerification'](inst) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 107 | |
Ryan Cui | 4d6fca9 | 2012-12-13 16:41:56 -0800 | [diff] [blame] | 108 | def PreStart(self): |
| 109 | self.rsh_mock.start() |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 110 | |
Ryan Cui | 4d6fca9 | 2012-12-13 16:41:56 -0800 | [diff] [blame] | 111 | def PreStop(self): |
| 112 | self.rsh_mock.stop() |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 113 | |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 114 | def _KillProcsIfNeeded(self, _inst): |
| 115 | # Fully stub out for now. |
| 116 | pass |
| 117 | |
| 118 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 119 | class DeployTest(cros_test_lib.MockTempDirTestCase): |
| 120 | def _GetDeployChrome(self, args): |
| 121 | options, _ = _ParseCommandLine(args) |
Ryan Cui | a56a71e | 2012-10-18 18:40:35 -0700 | [diff] [blame] | 122 | return deploy_chrome.DeployChrome( |
| 123 | options, self.tempdir, os.path.join(self.tempdir, 'staging')) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 124 | |
| 125 | def setUp(self): |
Ryan Cui | f1416f3 | 2013-01-22 18:43:41 -0800 | [diff] [blame] | 126 | self.deploy_mock = self.StartPatcher(DeployChromeMock()) |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 127 | self.deploy = self._GetDeployChrome( |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 128 | list(_REGULAR_TO) + ['--gs-path', _GS_PATH, '--force']) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 129 | |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 130 | |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 131 | class TestDisableRootfsVerification(DeployTest): |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 132 | """Testing disabling of rootfs verification and RO mode.""" |
| 133 | |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 134 | def testDisableRootfsVerificationSuccess(self): |
| 135 | """Test the working case, disabling rootfs verification.""" |
| 136 | self.deploy_mock.MockMountCmd(0) |
| 137 | self.deploy._DisableRootfsVerification() |
| 138 | self.assertFalse(self.deploy._rootfs_is_still_readonly.is_set()) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 139 | |
| 140 | def testDisableRootfsVerificationFailure(self): |
| 141 | """Test failure to disable rootfs verification.""" |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 142 | self.assertRaises(cros_build_lib.RunCommandError, |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 143 | self.deploy._DisableRootfsVerification) |
| 144 | self.assertFalse(self.deploy._rootfs_is_still_readonly.is_set()) |
| 145 | |
| 146 | |
| 147 | class TestMount(DeployTest): |
| 148 | """Testing mount success and failure.""" |
| 149 | |
| 150 | def testSuccess(self): |
| 151 | """Test case where we are able to mount as writable.""" |
| 152 | self.assertFalse(self.deploy._rootfs_is_still_readonly.is_set()) |
| 153 | self.deploy_mock.MockMountCmd(0) |
| 154 | self.deploy._MountRootfsAsWritable() |
| 155 | self.assertFalse(self.deploy._rootfs_is_still_readonly.is_set()) |
| 156 | |
| 157 | def testMountError(self): |
| 158 | """Test that mount failure doesn't raise an exception by default.""" |
| 159 | self.assertFalse(self.deploy._rootfs_is_still_readonly.is_set()) |
| 160 | self.deploy._MountRootfsAsWritable() |
| 161 | self.assertTrue(self.deploy._rootfs_is_still_readonly.is_set()) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 162 | |
| 163 | def testMountRwFailure(self): |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 164 | """Test that mount failure raises an exception if error_code_ok=False.""" |
| 165 | self.assertRaises(cros_build_lib.RunCommandError, |
| 166 | self.deploy._MountRootfsAsWritable, error_code_ok=False) |
| 167 | self.assertFalse(self.deploy._rootfs_is_still_readonly.is_set()) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 168 | |
| 169 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 170 | class TestUiJobStarted(DeployTest): |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 171 | """Test detection of a running 'ui' job.""" |
| 172 | |
Ryan Cui | f2d1a58 | 2013-02-19 14:08:13 -0800 | [diff] [blame] | 173 | def MockStatusUiCmd(self, **kwargs): |
| 174 | self.deploy_mock.rsh_mock.AddCmdResult('status ui', **kwargs) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 175 | |
| 176 | def testUiJobStartedFalse(self): |
| 177 | """Correct results with a stopped job.""" |
Ryan Cui | f2d1a58 | 2013-02-19 14:08:13 -0800 | [diff] [blame] | 178 | self.MockStatusUiCmd(output='ui stop/waiting') |
| 179 | self.assertFalse(self.deploy._CheckUiJobStarted()) |
| 180 | |
| 181 | def testNoUiJob(self): |
| 182 | """Correct results when the job doesn't exist.""" |
| 183 | self.MockStatusUiCmd(error='start: Unknown job: ui', returncode=1) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 184 | self.assertFalse(self.deploy._CheckUiJobStarted()) |
| 185 | |
| 186 | def testCheckRootfsWriteableTrue(self): |
| 187 | """Correct results with a running job.""" |
Ryan Cui | f2d1a58 | 2013-02-19 14:08:13 -0800 | [diff] [blame] | 188 | self.MockStatusUiCmd(output='ui start/running, process 297') |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 189 | self.assertTrue(self.deploy._CheckUiJobStarted()) |
| 190 | |
| 191 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 192 | class StagingTest(cros_test_lib.MockTempDirTestCase): |
| 193 | """Test user-mode and ebuild-mode staging functionality.""" |
| 194 | |
| 195 | def setUp(self): |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 196 | self.staging_dir = os.path.join(self.tempdir, 'staging') |
| 197 | self.build_dir = os.path.join(self.tempdir, 'build_dir') |
Ryan Cui | 686ec05 | 2013-02-12 16:39:41 -0800 | [diff] [blame] | 198 | self.common_flags = ['--build-dir', self.build_dir, |
| 199 | '--board=lumpy', '--staging-only', '--cache-dir', |
| 200 | self.tempdir] |
Ryan Cui | a0215a7 | 2013-02-14 16:20:45 -0800 | [diff] [blame] | 201 | self.sdk_mock = self.StartPatcher(cros_chrome_sdk_unittest.SDKFetcherMock()) |
Ryan Cui | 686ec05 | 2013-02-12 16:39:41 -0800 | [diff] [blame] | 202 | self.PatchObject( |
| 203 | osutils, 'SourceEnvironment', autospec=True, |
| 204 | return_value={'STRIP': 'x86_64-cros-linux-gnu-strip'}) |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 205 | |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 206 | def testSingleFileDeployFailure(self): |
| 207 | """Default staging enforces that mandatory files are copied""" |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 208 | options, _ = _ParseCommandLine(self.common_flags) |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 209 | osutils.Touch(os.path.join(self.build_dir, 'chrome'), makedirs=True) |
| 210 | self.assertRaises( |
| 211 | chrome_util.MissingPathError, deploy_chrome._PrepareStagingDir, |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 212 | options, self.tempdir, self.staging_dir) |
| 213 | |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 214 | def testSloppyDeployFailure(self): |
| 215 | """Sloppy staging enforces that at least one file is copied.""" |
| 216 | options, _ = _ParseCommandLine(self.common_flags + ['--sloppy']) |
| 217 | self.assertRaises( |
| 218 | chrome_util.MissingPathError, deploy_chrome._PrepareStagingDir, |
| 219 | options, self.tempdir, self.staging_dir) |
| 220 | |
| 221 | def testSloppyDeploySuccess(self): |
| 222 | """Sloppy staging - stage one file.""" |
| 223 | options, _ = _ParseCommandLine(self.common_flags + ['--sloppy']) |
| 224 | osutils.Touch(os.path.join(self.build_dir, 'chrome'), makedirs=True) |
| 225 | deploy_chrome._PrepareStagingDir(options, self.tempdir, self.staging_dir) |
| 226 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 227 | def testEmptyDeployStrict(self): |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 228 | """Strict staging fails when there are no files.""" |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 229 | options, _ = _ParseCommandLine( |
| 230 | self.common_flags + ['--gyp-defines', 'chromeos=1', '--strict']) |
Ryan Cui | 686ec05 | 2013-02-12 16:39:41 -0800 | [diff] [blame] | 231 | chrome_util.MissingPathError(deploy_chrome._PrepareStagingDir, |
| 232 | options, self.tempdir, self.staging_dir) |
| 233 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 234 | self.assertRaises( |
| 235 | chrome_util.MissingPathError, deploy_chrome._PrepareStagingDir, |
| 236 | options, self.tempdir, self.staging_dir) |
| 237 | |
| 238 | |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 239 | if __name__ == '__main__': |
| 240 | cros_test_lib.main() |