blob: 43409d2346e725177e89cdf70e1dbcab455e77db [file] [log] [blame]
Ryan Cuiafd6c5c2012-07-30 17:48:22 -07001#!/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 Cuiafd6c5c2012-07-30 17:48:22 -07007import os
8import sys
9
10sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
11 '..', '..'))
12from chromite.lib import cros_build_lib
13from chromite.lib import cros_test_lib
14from chromite.lib import partial_mock
15from chromite.lib import remote_access_unittest
16from chromite.scripts import deploy_chrome
17
Brian Harringe7524372012-12-23 02:02:56 -080018# TODO(build): Finish test wrapper (http://crosbug.com/37517).
19# Until then, this has to be after the chromite imports.
20import mock
21
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070022
23# pylint: disable=W0212
24
25_REGULAR_TO = ('--to', 'monkey')
26_GS_PATH = 'gs://foon'
27
28
29def _ParseCommandLine(argv):
30 return deploy_chrome._ParseCommandLine(['--log-level', 'debug'] + argv)
31
32
33class 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 Cuia56a71e2012-10-18 18:40:35 -070049 argv = list(_REGULAR_TO) + ['--local-pkg-path', '/path/to/chrome']
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070050 _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
60class DeployChromeMock(partial_mock.PartialMock):
61
62 TARGET = 'chromite.scripts.deploy_chrome.DeployChrome'
63 ATTRS = ('_CheckRootfsWriteable', '_DisableRootfsVerification',
64 '_KillProcsIfNeeded')
65
Ryan Cuie18f24f2012-12-03 18:39:55 -080066 def __init__(self, disable_ok=True):
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070067 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 Cuie18f24f2012-12-03 18:39:55 -080071 self.rsh_mock = remote_access_unittest.RemoteShMock()
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070072 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 Cui4d6fca92012-12-13 16:41:56 -080082 def PreStart(self):
83 self.rsh_mock.start()
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070084
Ryan Cui4d6fca92012-12-13 16:41:56 -080085 def PreStop(self):
86 self.rsh_mock.stop()
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070087
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 Cui4d6fca92012-12-13 16:41:56 -080099class DeployChromeTest(cros_test_lib.MockTempDirTestCase):
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700100
101 def _GetDeployChrome(self):
102 options, _ = _ParseCommandLine(list(_REGULAR_TO) + ['--gs-path', _GS_PATH])
Ryan Cuia56a71e2012-10-18 18:40:35 -0700103 return deploy_chrome.DeployChrome(
104 options, self.tempdir, os.path.join(self.tempdir, 'staging'))
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700105
106 def setUp(self):
Ryan Cuif1416f32013-01-22 18:43:41 -0800107 self.deploy_mock = self.StartPatcher(DeployChromeMock())
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700108 self.deploy = self._GetDeployChrome()
109
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700110
111class 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
137PROC_MOUNTS = """\
138rootfs / rootfs rw 0 0
139/dev/root / ext2 %s,relatime,user_xattr,acl 0 0
140devtmpfs /dev devtmpfs rw,relatime,size=970032k,nr_inodes=242508,mode=755 0 0
141none /proc proc rw,nosuid,nodev,noexec,relatime 0 0
142"""
143
144
145class 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
165class 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
182if __name__ == '__main__':
183 cros_test_lib.main()