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