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