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 | '..', '..')) |
| 12 | from chromite.lib import cros_build_lib |
| 13 | from chromite.lib import cros_test_lib |
| 14 | from chromite.lib import partial_mock |
| 15 | from chromite.lib import remote_access_unittest |
| 16 | from chromite.scripts import deploy_chrome |
| 17 | |
Brian Harring | e752437 | 2012-12-23 02:02:56 -0800 | [diff] [blame] | 18 | # TODO(build): Finish test wrapper (http://crosbug.com/37517). |
| 19 | # Until then, this has to be after the chromite imports. |
| 20 | import mock |
| 21 | |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 22 | |
| 23 | # pylint: disable=W0212 |
| 24 | |
| 25 | _REGULAR_TO = ('--to', 'monkey') |
| 26 | _GS_PATH = 'gs://foon' |
| 27 | |
| 28 | |
| 29 | def _ParseCommandLine(argv): |
| 30 | return deploy_chrome._ParseCommandLine(['--log-level', 'debug'] + argv) |
| 31 | |
| 32 | |
| 33 | class InterfaceTest(cros_test_lib.OutputTestCase): |
| 34 | """Tests the commandline interface of the script.""" |
| 35 | |
| 36 | def testGsLocalPathUnSpecified(self): |
| 37 | """Test no chrome path specified.""" |
| 38 | with self.OutputCapturer(): |
| 39 | self.assertRaises2(SystemExit, _ParseCommandLine, list(_REGULAR_TO), |
| 40 | check_attrs={'code': 2}) |
| 41 | |
| 42 | def testGsPathSpecified(self): |
| 43 | """Test case of GS path specified.""" |
| 44 | argv = list(_REGULAR_TO) + ['--gs-path', _GS_PATH] |
| 45 | _ParseCommandLine(argv) |
| 46 | |
| 47 | def testLocalPathSpecified(self): |
| 48 | """Test case of local path specified.""" |
Ryan Cui | a56a71e | 2012-10-18 18:40:35 -0700 | [diff] [blame] | 49 | argv = list(_REGULAR_TO) + ['--local-pkg-path', '/path/to/chrome'] |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 50 | _ParseCommandLine(argv) |
| 51 | |
| 52 | def testNoTarget(self): |
| 53 | """Test no target specified.""" |
| 54 | argv = ['--gs-path', _GS_PATH] |
| 55 | with self.OutputCapturer(): |
| 56 | self.assertRaises2(SystemExit, _ParseCommandLine, argv, |
| 57 | check_attrs={'code': 2}) |
| 58 | |
| 59 | |
| 60 | class DeployChromeMock(partial_mock.PartialMock): |
| 61 | |
| 62 | TARGET = 'chromite.scripts.deploy_chrome.DeployChrome' |
| 63 | ATTRS = ('_CheckRootfsWriteable', '_DisableRootfsVerification', |
| 64 | '_KillProcsIfNeeded') |
| 65 | |
Ryan Cui | e18f24f | 2012-12-03 18:39:55 -0800 | [diff] [blame] | 66 | def __init__(self, disable_ok=True): |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 67 | partial_mock.PartialMock.__init__(self) |
| 68 | self.disable_ok = disable_ok |
| 69 | self.rootfs_writeable = False |
| 70 | # Target starts off as having rootfs verification enabled. |
Ryan Cui | e18f24f | 2012-12-03 18:39:55 -0800 | [diff] [blame] | 71 | self.rsh_mock = remote_access_unittest.RemoteShMock() |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 72 | self.MockMountCmd(1) |
| 73 | |
| 74 | def MockMountCmd(self, returnvalue): |
| 75 | def hook(_inst, *_args, **_kwargs): |
| 76 | self.rootfs_writeable = True |
| 77 | |
| 78 | self.rsh_mock.AddCmdResult(deploy_chrome.MOUNT_RW_COMMAND, |
| 79 | returnvalue, |
| 80 | side_effect=None if returnvalue else hook) |
| 81 | |
Ryan Cui | 4d6fca9 | 2012-12-13 16:41:56 -0800 | [diff] [blame] | 82 | def PreStart(self): |
| 83 | self.rsh_mock.start() |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 84 | |
Ryan Cui | 4d6fca9 | 2012-12-13 16:41:56 -0800 | [diff] [blame] | 85 | def PreStop(self): |
| 86 | self.rsh_mock.stop() |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 87 | |
| 88 | def _CheckRootfsWriteable(self, _inst): |
| 89 | return self.rootfs_writeable |
| 90 | |
| 91 | def _DisableRootfsVerification(self, _inst): |
| 92 | self.MockMountCmd(int(not self.disable_ok)) |
| 93 | |
| 94 | def _KillProcsIfNeeded(self, _inst): |
| 95 | # Fully stub out for now. |
| 96 | pass |
| 97 | |
| 98 | |
Ryan Cui | 4d6fca9 | 2012-12-13 16:41:56 -0800 | [diff] [blame] | 99 | class DeployChromeTest(cros_test_lib.MockTempDirTestCase): |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 100 | |
| 101 | def _GetDeployChrome(self): |
| 102 | options, _ = _ParseCommandLine(list(_REGULAR_TO) + ['--gs-path', _GS_PATH]) |
Ryan Cui | a56a71e | 2012-10-18 18:40:35 -0700 | [diff] [blame] | 103 | return deploy_chrome.DeployChrome( |
| 104 | options, self.tempdir, os.path.join(self.tempdir, 'staging')) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 105 | |
| 106 | def setUp(self): |
Ryan Cui | f1416f3 | 2013-01-22 18:43:41 -0800 | [diff] [blame] | 107 | self.deploy_mock = self.StartPatcher(DeployChromeMock()) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 108 | self.deploy = self._GetDeployChrome() |
| 109 | |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 110 | |
| 111 | class TestPrepareTarget(DeployChromeTest): |
| 112 | """Testing disabling of rootfs verification and RO mode.""" |
| 113 | |
| 114 | def testSuccess(self): |
| 115 | """Test the working case.""" |
| 116 | self.deploy._PrepareTarget() |
| 117 | |
| 118 | def testDisableRootfsVerificationFailure(self): |
| 119 | """Test failure to disable rootfs verification.""" |
| 120 | self.deploy_mock.disable_ok = False |
| 121 | self.assertRaises(cros_build_lib.RunCommandError, |
| 122 | self.deploy._PrepareTarget) |
| 123 | |
| 124 | def testMountRwFailure(self): |
| 125 | """The mount command returncode was 0 but rootfs is still readonly.""" |
| 126 | with mock.patch.object(deploy_chrome.DeployChrome, '_CheckRootfsWriteable', |
| 127 | auto_spec=True) as m: |
| 128 | m.return_value = False |
| 129 | self.assertRaises(SystemExit, self.deploy._PrepareTarget) |
| 130 | |
| 131 | def testMountRwSuccessFirstTime(self): |
| 132 | """We were able to mount as RW the first time.""" |
| 133 | self.deploy_mock.MockMountCmd(0) |
| 134 | self.deploy._PrepareTarget() |
| 135 | |
| 136 | |
| 137 | PROC_MOUNTS = """\ |
| 138 | rootfs / rootfs rw 0 0 |
| 139 | /dev/root / ext2 %s,relatime,user_xattr,acl 0 0 |
| 140 | devtmpfs /dev devtmpfs rw,relatime,size=970032k,nr_inodes=242508,mode=755 0 0 |
| 141 | none /proc proc rw,nosuid,nodev,noexec,relatime 0 0 |
| 142 | """ |
| 143 | |
| 144 | |
| 145 | class TestCheckRootfs(DeployChromeTest): |
| 146 | """Test Rootfs RW check functionality.""" |
| 147 | |
| 148 | def setUp(self): |
| 149 | self.deploy_mock.UnMockAttr('_CheckRootfsWriteable') |
| 150 | |
| 151 | def MockProcMountsCmd(self, output): |
| 152 | self.deploy_mock.rsh_mock.AddCmdResult('cat /proc/mounts', output=output) |
| 153 | |
| 154 | def testCheckRootfsWriteableFalse(self): |
| 155 | """Correct results with RO.""" |
| 156 | self.MockProcMountsCmd(PROC_MOUNTS % 'ro') |
| 157 | self.assertFalse(self.deploy._CheckRootfsWriteable()) |
| 158 | |
| 159 | def testCheckRootfsWriteableTrue(self): |
| 160 | """Correct results with RW.""" |
| 161 | self.MockProcMountsCmd(PROC_MOUNTS % 'rw') |
| 162 | self.assertTrue(self.deploy._CheckRootfsWriteable()) |
| 163 | |
| 164 | |
| 165 | class TestUiJobStarted(DeployChromeTest): |
| 166 | """Test detection of a running 'ui' job.""" |
| 167 | |
| 168 | def MockStatusUiCmd(self, output): |
| 169 | self.deploy_mock.rsh_mock.AddCmdResult('status ui', output=output) |
| 170 | |
| 171 | def testUiJobStartedFalse(self): |
| 172 | """Correct results with a stopped job.""" |
| 173 | self.MockStatusUiCmd('ui stop/waiting') |
| 174 | self.assertFalse(self.deploy._CheckUiJobStarted()) |
| 175 | |
| 176 | def testCheckRootfsWriteableTrue(self): |
| 177 | """Correct results with a running job.""" |
| 178 | self.MockStatusUiCmd('ui start/running, process 297') |
| 179 | self.assertTrue(self.deploy._CheckUiJobStarted()) |
| 180 | |
| 181 | |
| 182 | if __name__ == '__main__': |
| 183 | cros_test_lib.main() |