blob: cf1892b5502e32ad8b8259fb76b1d8c48c7b0d5a [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 Cuie18f24f2012-12-03 18:39:55 -0800107 self.deploy_mock = DeployChromeMock()
Ryan Cui4d6fca92012-12-13 16:41:56 -0800108 self.StartPatcher(self.deploy_mock)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700109 self.deploy = self._GetDeployChrome()
110
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700111
112class TestPrepareTarget(DeployChromeTest):
113 """Testing disabling of rootfs verification and RO mode."""
114
115 def testSuccess(self):
116 """Test the working case."""
117 self.deploy._PrepareTarget()
118
119 def testDisableRootfsVerificationFailure(self):
120 """Test failure to disable rootfs verification."""
121 self.deploy_mock.disable_ok = False
122 self.assertRaises(cros_build_lib.RunCommandError,
123 self.deploy._PrepareTarget)
124
125 def testMountRwFailure(self):
126 """The mount command returncode was 0 but rootfs is still readonly."""
127 with mock.patch.object(deploy_chrome.DeployChrome, '_CheckRootfsWriteable',
128 auto_spec=True) as m:
129 m.return_value = False
130 self.assertRaises(SystemExit, self.deploy._PrepareTarget)
131
132 def testMountRwSuccessFirstTime(self):
133 """We were able to mount as RW the first time."""
134 self.deploy_mock.MockMountCmd(0)
135 self.deploy._PrepareTarget()
136
137
138PROC_MOUNTS = """\
139rootfs / rootfs rw 0 0
140/dev/root / ext2 %s,relatime,user_xattr,acl 0 0
141devtmpfs /dev devtmpfs rw,relatime,size=970032k,nr_inodes=242508,mode=755 0 0
142none /proc proc rw,nosuid,nodev,noexec,relatime 0 0
143"""
144
145
146class TestCheckRootfs(DeployChromeTest):
147 """Test Rootfs RW check functionality."""
148
149 def setUp(self):
150 self.deploy_mock.UnMockAttr('_CheckRootfsWriteable')
151
152 def MockProcMountsCmd(self, output):
153 self.deploy_mock.rsh_mock.AddCmdResult('cat /proc/mounts', output=output)
154
155 def testCheckRootfsWriteableFalse(self):
156 """Correct results with RO."""
157 self.MockProcMountsCmd(PROC_MOUNTS % 'ro')
158 self.assertFalse(self.deploy._CheckRootfsWriteable())
159
160 def testCheckRootfsWriteableTrue(self):
161 """Correct results with RW."""
162 self.MockProcMountsCmd(PROC_MOUNTS % 'rw')
163 self.assertTrue(self.deploy._CheckRootfsWriteable())
164
165
166class TestUiJobStarted(DeployChromeTest):
167 """Test detection of a running 'ui' job."""
168
169 def MockStatusUiCmd(self, output):
170 self.deploy_mock.rsh_mock.AddCmdResult('status ui', output=output)
171
172 def testUiJobStartedFalse(self):
173 """Correct results with a stopped job."""
174 self.MockStatusUiCmd('ui stop/waiting')
175 self.assertFalse(self.deploy._CheckUiJobStarted())
176
177 def testCheckRootfsWriteableTrue(self):
178 """Correct results with a running job."""
179 self.MockStatusUiCmd('ui start/running, process 297')
180 self.assertTrue(self.deploy._CheckUiJobStarted())
181
182
183if __name__ == '__main__':
184 cros_test_lib.main()