blob: 528915acbbdb8d2c3d22d19131bfa3ee4ae783b1 [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
7import mock
8import os
9import sys
10
11sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
12 '..', '..'))
13from chromite.lib import cros_build_lib
14from chromite.lib import cros_test_lib
15from chromite.lib import partial_mock
16from chromite.lib import remote_access_unittest
17from chromite.scripts import deploy_chrome
18
19
20# pylint: disable=W0212
21
22_REGULAR_TO = ('--to', 'monkey')
23_GS_PATH = 'gs://foon'
24
25
26def _ParseCommandLine(argv):
27 return deploy_chrome._ParseCommandLine(['--log-level', 'debug'] + argv)
28
29
30class 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."""
46 argv = list(_REGULAR_TO) + ['--local-path', '/path/to/chrome']
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
57class DeployChromeMock(partial_mock.PartialMock):
58
59 TARGET = 'chromite.scripts.deploy_chrome.DeployChrome'
60 ATTRS = ('_CheckRootfsWriteable', '_DisableRootfsVerification',
61 '_KillProcsIfNeeded')
62
63 def __init__(self, tempdir, disable_ok=True):
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.
68 self.rsh_mock = remote_access_unittest.RemoteShMock(tempdir)
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
79 def Start(self):
80 partial_mock.PartialMock.Start(self)
81 self.rsh_mock.Start()
82
83 def Stop(self):
84 partial_mock.PartialMock.Stop(self)
85 self.rsh_mock.Stop()
86
87 def _CheckRootfsWriteable(self, _inst):
88 return self.rootfs_writeable
89
90 def _DisableRootfsVerification(self, _inst):
91 self.MockMountCmd(int(not self.disable_ok))
92
93 def _KillProcsIfNeeded(self, _inst):
94 # Fully stub out for now.
95 pass
96
97
98class DeployChromeTest(cros_test_lib.TempDirTestCase):
99
100 def _GetDeployChrome(self):
101 options, _ = _ParseCommandLine(list(_REGULAR_TO) + ['--gs-path', _GS_PATH])
102 return deploy_chrome.DeployChrome(options, self.tempdir)
103
104 def setUp(self):
105 self.deploy_mock = DeployChromeMock(self.tempdir)
106 self.deploy_mock.Start()
107 self.deploy = self._GetDeployChrome()
108
109 def tearDown(self):
110 self.deploy_mock.Stop()
111
112
113class TestPrepareTarget(DeployChromeTest):
114 """Testing disabling of rootfs verification and RO mode."""
115
116 def testSuccess(self):
117 """Test the working case."""
118 self.deploy._PrepareTarget()
119
120 def testDisableRootfsVerificationFailure(self):
121 """Test failure to disable rootfs verification."""
122 self.deploy_mock.disable_ok = False
123 self.assertRaises(cros_build_lib.RunCommandError,
124 self.deploy._PrepareTarget)
125
126 def testMountRwFailure(self):
127 """The mount command returncode was 0 but rootfs is still readonly."""
128 with mock.patch.object(deploy_chrome.DeployChrome, '_CheckRootfsWriteable',
129 auto_spec=True) as m:
130 m.return_value = False
131 self.assertRaises(SystemExit, self.deploy._PrepareTarget)
132
133 def testMountRwSuccessFirstTime(self):
134 """We were able to mount as RW the first time."""
135 self.deploy_mock.MockMountCmd(0)
136 self.deploy._PrepareTarget()
137
138
139PROC_MOUNTS = """\
140rootfs / rootfs rw 0 0
141/dev/root / ext2 %s,relatime,user_xattr,acl 0 0
142devtmpfs /dev devtmpfs rw,relatime,size=970032k,nr_inodes=242508,mode=755 0 0
143none /proc proc rw,nosuid,nodev,noexec,relatime 0 0
144"""
145
146
147class TestCheckRootfs(DeployChromeTest):
148 """Test Rootfs RW check functionality."""
149
150 def setUp(self):
151 self.deploy_mock.UnMockAttr('_CheckRootfsWriteable')
152
153 def MockProcMountsCmd(self, output):
154 self.deploy_mock.rsh_mock.AddCmdResult('cat /proc/mounts', output=output)
155
156 def testCheckRootfsWriteableFalse(self):
157 """Correct results with RO."""
158 self.MockProcMountsCmd(PROC_MOUNTS % 'ro')
159 self.assertFalse(self.deploy._CheckRootfsWriteable())
160
161 def testCheckRootfsWriteableTrue(self):
162 """Correct results with RW."""
163 self.MockProcMountsCmd(PROC_MOUNTS % 'rw')
164 self.assertTrue(self.deploy._CheckRootfsWriteable())
165
166
167class TestUiJobStarted(DeployChromeTest):
168 """Test detection of a running 'ui' job."""
169
170 def MockStatusUiCmd(self, output):
171 self.deploy_mock.rsh_mock.AddCmdResult('status ui', output=output)
172
173 def testUiJobStartedFalse(self):
174 """Correct results with a stopped job."""
175 self.MockStatusUiCmd('ui stop/waiting')
176 self.assertFalse(self.deploy._CheckUiJobStarted())
177
178 def testCheckRootfsWriteableTrue(self):
179 """Correct results with a running job."""
180 self.MockStatusUiCmd('ui start/running, process 297')
181 self.assertTrue(self.deploy._CheckUiJobStarted())
182
183
184if __name__ == '__main__':
185 cros_test_lib.main()