blob: 867446c16faa07260e1fa7108d14b8de3239d1c6 [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
Ryan Cui4d6fca92012-12-13 16:41:56 -080079 def PreStart(self):
80 self.rsh_mock.start()
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070081
Ryan Cui4d6fca92012-12-13 16:41:56 -080082 def PreStop(self):
83 self.rsh_mock.stop()
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070084
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 Cui4d6fca92012-12-13 16:41:56 -080096class DeployChromeTest(cros_test_lib.MockTempDirTestCase):
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070097
98 def _GetDeployChrome(self):
99 options, _ = _ParseCommandLine(list(_REGULAR_TO) + ['--gs-path', _GS_PATH])
Ryan Cuia56a71e2012-10-18 18:40:35 -0700100 return deploy_chrome.DeployChrome(
101 options, self.tempdir, os.path.join(self.tempdir, 'staging'))
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700102
103 def setUp(self):
Ryan Cuie18f24f2012-12-03 18:39:55 -0800104 self.deploy_mock = DeployChromeMock()
Ryan Cui4d6fca92012-12-13 16:41:56 -0800105 self.StartPatcher(self.deploy_mock)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700106 self.deploy = self._GetDeployChrome()
107
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700108
109class 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
135PROC_MOUNTS = """\
136rootfs / rootfs rw 0 0
137/dev/root / ext2 %s,relatime,user_xattr,acl 0 0
138devtmpfs /dev devtmpfs rw,relatime,size=970032k,nr_inodes=242508,mode=755 0 0
139none /proc proc rw,nosuid,nodev,noexec,relatime 0 0
140"""
141
142
143class 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
163class 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
180if __name__ == '__main__':
181 cros_test_lib.main()