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