blob: e628c7046e4ae9e2951827d9fc41dc2ffabd78cf [file] [log] [blame]
Ryan Cuiafd6c5c2012-07-30 17:48:22 -07001# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
Steve Funge984a532013-11-25 17:09:25 -08005"""Unit tests for the deploy_chrome script."""
Ryan Cuiafd6c5c2012-07-30 17:48:22 -07006
Mike Frysinger383367e2014-09-16 15:06:17 -04007from __future__ import print_function
8
Mike Frysingerea838d12014-12-08 11:55:32 -05009import mock
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070010import os
David James88e6f032013-03-02 08:13:20 -080011import time
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070012
David Pursellcfd58872015-03-19 09:15:48 -070013from chromite.cli.cros import cros_chrome_sdk_unittest
Ryan Cuief91e702013-02-04 12:06:36 -080014from chromite.lib import chrome_util
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070015from chromite.lib import cros_build_lib
16from chromite.lib import cros_test_lib
Ryan Cui686ec052013-02-12 16:39:41 -080017from chromite.lib import osutils
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070018from chromite.lib import partial_mock
Robert Flack1dc7ea82014-11-26 13:50:24 -050019from chromite.lib import remote_access
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070020from chromite.lib import remote_access_unittest
21from chromite.scripts import deploy_chrome
22
Ryan Cuief91e702013-02-04 12:06:36 -080023
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070024# pylint: disable=W0212
25
26_REGULAR_TO = ('--to', 'monkey')
27_GS_PATH = 'gs://foon'
28
29
30def _ParseCommandLine(argv):
31 return deploy_chrome._ParseCommandLine(['--log-level', 'debug'] + argv)
32
33
34class InterfaceTest(cros_test_lib.OutputTestCase):
35 """Tests the commandline interface of the script."""
36
Ryan Cui686ec052013-02-12 16:39:41 -080037 BOARD = 'lumpy'
38
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070039 def testGsLocalPathUnSpecified(self):
40 """Test no chrome path specified."""
41 with self.OutputCapturer():
42 self.assertRaises2(SystemExit, _ParseCommandLine, list(_REGULAR_TO),
43 check_attrs={'code': 2})
44
45 def testGsPathSpecified(self):
46 """Test case of GS path specified."""
47 argv = list(_REGULAR_TO) + ['--gs-path', _GS_PATH]
48 _ParseCommandLine(argv)
49
50 def testLocalPathSpecified(self):
51 """Test case of local path specified."""
Mike Frysingerd6e2df02014-11-26 02:55:04 -050052 argv = list(_REGULAR_TO) + ['--local-pkg-path', '/path/to/chrome']
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070053 _ParseCommandLine(argv)
54
55 def testNoTarget(self):
56 """Test no target specified."""
57 argv = ['--gs-path', _GS_PATH]
Ryan Cuief91e702013-02-04 12:06:36 -080058 self.assertParseError(argv)
59
60 def assertParseError(self, argv):
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070061 with self.OutputCapturer():
62 self.assertRaises2(SystemExit, _ParseCommandLine, argv,
63 check_attrs={'code': 2})
64
Ryan Cui686ec052013-02-12 16:39:41 -080065 def testNoBoardBuildDir(self):
66 argv = ['--staging-only', '--build-dir=/path/to/nowhere']
67 self.assertParseError(argv)
68
Thiago Goncales12793312013-05-23 11:26:17 -070069 def testMountOptionSetsTargetDir(self):
70 argv = list(_REGULAR_TO) + ['--gs-path', _GS_PATH, '--mount']
Mike Frysingerc3061a62015-06-04 04:16:18 -040071 options = _ParseCommandLine(argv)
Thiago Goncales12793312013-05-23 11:26:17 -070072 self.assertIsNot(options.target_dir, None)
73
74 def testMountOptionSetsMountDir(self):
75 argv = list(_REGULAR_TO) + ['--gs-path', _GS_PATH, '--mount']
Mike Frysingerc3061a62015-06-04 04:16:18 -040076 options = _ParseCommandLine(argv)
Thiago Goncales12793312013-05-23 11:26:17 -070077 self.assertIsNot(options.mount_dir, None)
78
79 def testMountOptionDoesNotOverrideTargetDir(self):
80 argv = list(_REGULAR_TO) + ['--gs-path', _GS_PATH, '--mount',
81 '--target-dir', '/foo/bar/cow']
Mike Frysingerc3061a62015-06-04 04:16:18 -040082 options = _ParseCommandLine(argv)
Thiago Goncales12793312013-05-23 11:26:17 -070083 self.assertEqual(options.target_dir, '/foo/bar/cow')
84
85 def testMountOptionDoesNotOverrideMountDir(self):
86 argv = list(_REGULAR_TO) + ['--gs-path', _GS_PATH, '--mount',
87 '--mount-dir', '/foo/bar/cow']
Mike Frysingerc3061a62015-06-04 04:16:18 -040088 options = _ParseCommandLine(argv)
Thiago Goncales12793312013-05-23 11:26:17 -070089 self.assertEqual(options.mount_dir, '/foo/bar/cow')
90
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070091
92class DeployChromeMock(partial_mock.PartialMock):
Steve Funge984a532013-11-25 17:09:25 -080093 """Deploy Chrome Mock Class."""
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070094
95 TARGET = 'chromite.scripts.deploy_chrome.DeployChrome'
David James88e6f032013-03-02 08:13:20 -080096 ATTRS = ('_KillProcsIfNeeded', '_DisableRootfsVerification')
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070097
David James88e6f032013-03-02 08:13:20 -080098 def __init__(self):
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070099 partial_mock.PartialMock.__init__(self)
Robert Flack1dc7ea82014-11-26 13:50:24 -0500100 self.remote_device_mock = remote_access_unittest.RemoteDeviceMock()
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700101 # Target starts off as having rootfs verification enabled.
Ryan Cuie18f24f2012-12-03 18:39:55 -0800102 self.rsh_mock = remote_access_unittest.RemoteShMock()
David James88e6f032013-03-02 08:13:20 -0800103 self.rsh_mock.SetDefaultCmdResult(0)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700104 self.MockMountCmd(1)
Pawel Osciak577773a2013-03-05 10:52:12 -0800105 self.rsh_mock.AddCmdResult(
106 deploy_chrome.LSOF_COMMAND % (deploy_chrome._CHROME_DIR,), 1)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700107
108 def MockMountCmd(self, returnvalue):
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700109 self.rsh_mock.AddCmdResult(deploy_chrome.MOUNT_RW_COMMAND,
David James88e6f032013-03-02 08:13:20 -0800110 returnvalue)
111
112 def _DisableRootfsVerification(self, inst):
113 with mock.patch.object(time, 'sleep'):
114 self.backup['_DisableRootfsVerification'](inst)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700115
Ryan Cui4d6fca92012-12-13 16:41:56 -0800116 def PreStart(self):
Robert Flack1dc7ea82014-11-26 13:50:24 -0500117 self.remote_device_mock.start()
Ryan Cui4d6fca92012-12-13 16:41:56 -0800118 self.rsh_mock.start()
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700119
Ryan Cui4d6fca92012-12-13 16:41:56 -0800120 def PreStop(self):
121 self.rsh_mock.stop()
Robert Flack1dc7ea82014-11-26 13:50:24 -0500122 self.remote_device_mock.stop()
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700123
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700124 def _KillProcsIfNeeded(self, _inst):
125 # Fully stub out for now.
126 pass
127
128
Ryan Cuief91e702013-02-04 12:06:36 -0800129class DeployTest(cros_test_lib.MockTempDirTestCase):
Steve Funge984a532013-11-25 17:09:25 -0800130 """Setup a deploy object with a GS-path for use in tests."""
131
Ryan Cuief91e702013-02-04 12:06:36 -0800132 def _GetDeployChrome(self, args):
Mike Frysingerc3061a62015-06-04 04:16:18 -0400133 options = _ParseCommandLine(args)
Ryan Cuia56a71e2012-10-18 18:40:35 -0700134 return deploy_chrome.DeployChrome(
135 options, self.tempdir, os.path.join(self.tempdir, 'staging'))
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700136
137 def setUp(self):
Ryan Cuif1416f32013-01-22 18:43:41 -0800138 self.deploy_mock = self.StartPatcher(DeployChromeMock())
Ryan Cuief91e702013-02-04 12:06:36 -0800139 self.deploy = self._GetDeployChrome(
David James88e6f032013-03-02 08:13:20 -0800140 list(_REGULAR_TO) + ['--gs-path', _GS_PATH, '--force'])
Luigi Semenzato1bc79b22016-11-22 16:32:17 -0800141 self.remote_reboot_mock = \
142 self.PatchObject(remote_access.RemoteAccess, 'RemoteReboot',
143 return_value=True)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700144
David James88e6f032013-03-02 08:13:20 -0800145class TestDisableRootfsVerification(DeployTest):
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700146 """Testing disabling of rootfs verification and RO mode."""
147
David James88e6f032013-03-02 08:13:20 -0800148 def testDisableRootfsVerificationSuccess(self):
149 """Test the working case, disabling rootfs verification."""
150 self.deploy_mock.MockMountCmd(0)
151 self.deploy._DisableRootfsVerification()
Robert Flack1dc7ea82014-11-26 13:50:24 -0500152 self.assertFalse(self.deploy._target_dir_is_still_readonly.is_set())
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700153
154 def testDisableRootfsVerificationFailure(self):
155 """Test failure to disable rootfs verification."""
Luigi Semenzato1bc79b22016-11-22 16:32:17 -0800156 #pylint: disable=unused-argument
157 def RaiseRunCommandError(timeout_sec=None):
158 raise cros_build_lib.RunCommandError('Mock RunCommandError', 0)
159 self.remote_reboot_mock.side_effect = RaiseRunCommandError
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700160 self.assertRaises(cros_build_lib.RunCommandError,
David James88e6f032013-03-02 08:13:20 -0800161 self.deploy._DisableRootfsVerification)
Luigi Semenzato1bc79b22016-11-22 16:32:17 -0800162 self.remote_reboot_mock.side_effect = None
Robert Flack1dc7ea82014-11-26 13:50:24 -0500163 self.assertFalse(self.deploy._target_dir_is_still_readonly.is_set())
David James88e6f032013-03-02 08:13:20 -0800164
165
166class TestMount(DeployTest):
167 """Testing mount success and failure."""
168
169 def testSuccess(self):
170 """Test case where we are able to mount as writable."""
Robert Flack1dc7ea82014-11-26 13:50:24 -0500171 self.assertFalse(self.deploy._target_dir_is_still_readonly.is_set())
David James88e6f032013-03-02 08:13:20 -0800172 self.deploy_mock.MockMountCmd(0)
173 self.deploy._MountRootfsAsWritable()
Robert Flack1dc7ea82014-11-26 13:50:24 -0500174 self.assertFalse(self.deploy._target_dir_is_still_readonly.is_set())
David James88e6f032013-03-02 08:13:20 -0800175
176 def testMountError(self):
177 """Test that mount failure doesn't raise an exception by default."""
Robert Flack1dc7ea82014-11-26 13:50:24 -0500178 self.assertFalse(self.deploy._target_dir_is_still_readonly.is_set())
Mike Frysinger74ccd572015-05-21 21:18:20 -0400179 self.PatchObject(remote_access.RemoteDevice, 'IsDirWritable',
Robert Flack1dc7ea82014-11-26 13:50:24 -0500180 return_value=False, autospec=True)
David James88e6f032013-03-02 08:13:20 -0800181 self.deploy._MountRootfsAsWritable()
Robert Flack1dc7ea82014-11-26 13:50:24 -0500182 self.assertTrue(self.deploy._target_dir_is_still_readonly.is_set())
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700183
184 def testMountRwFailure(self):
David James88e6f032013-03-02 08:13:20 -0800185 """Test that mount failure raises an exception if error_code_ok=False."""
186 self.assertRaises(cros_build_lib.RunCommandError,
187 self.deploy._MountRootfsAsWritable, error_code_ok=False)
Robert Flack1dc7ea82014-11-26 13:50:24 -0500188 self.assertFalse(self.deploy._target_dir_is_still_readonly.is_set())
189
190 def testMountTempDir(self):
191 """Test that mount succeeds if target dir is writable."""
192 self.assertFalse(self.deploy._target_dir_is_still_readonly.is_set())
Mike Frysinger74ccd572015-05-21 21:18:20 -0400193 self.PatchObject(remote_access.RemoteDevice, 'IsDirWritable',
Robert Flack1dc7ea82014-11-26 13:50:24 -0500194 return_value=True, autospec=True)
195 self.deploy._MountRootfsAsWritable()
196 self.assertFalse(self.deploy._target_dir_is_still_readonly.is_set())
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700197
198
Ryan Cuief91e702013-02-04 12:06:36 -0800199class TestUiJobStarted(DeployTest):
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700200 """Test detection of a running 'ui' job."""
201
Ryan Cuif2d1a582013-02-19 14:08:13 -0800202 def MockStatusUiCmd(self, **kwargs):
203 self.deploy_mock.rsh_mock.AddCmdResult('status ui', **kwargs)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700204
205 def testUiJobStartedFalse(self):
206 """Correct results with a stopped job."""
Ryan Cuif2d1a582013-02-19 14:08:13 -0800207 self.MockStatusUiCmd(output='ui stop/waiting')
208 self.assertFalse(self.deploy._CheckUiJobStarted())
209
210 def testNoUiJob(self):
211 """Correct results when the job doesn't exist."""
212 self.MockStatusUiCmd(error='start: Unknown job: ui', returncode=1)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700213 self.assertFalse(self.deploy._CheckUiJobStarted())
214
215 def testCheckRootfsWriteableTrue(self):
216 """Correct results with a running job."""
Ryan Cuif2d1a582013-02-19 14:08:13 -0800217 self.MockStatusUiCmd(output='ui start/running, process 297')
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700218 self.assertTrue(self.deploy._CheckUiJobStarted())
219
220
Ryan Cuief91e702013-02-04 12:06:36 -0800221class StagingTest(cros_test_lib.MockTempDirTestCase):
222 """Test user-mode and ebuild-mode staging functionality."""
223
224 def setUp(self):
Ryan Cuief91e702013-02-04 12:06:36 -0800225 self.staging_dir = os.path.join(self.tempdir, 'staging')
226 self.build_dir = os.path.join(self.tempdir, 'build_dir')
Ryan Cui686ec052013-02-12 16:39:41 -0800227 self.common_flags = ['--build-dir', self.build_dir,
228 '--board=lumpy', '--staging-only', '--cache-dir',
229 self.tempdir]
Ryan Cuia0215a72013-02-14 16:20:45 -0800230 self.sdk_mock = self.StartPatcher(cros_chrome_sdk_unittest.SDKFetcherMock())
Ryan Cui686ec052013-02-12 16:39:41 -0800231 self.PatchObject(
232 osutils, 'SourceEnvironment', autospec=True,
233 return_value={'STRIP': 'x86_64-cros-linux-gnu-strip'})
Ryan Cuief91e702013-02-04 12:06:36 -0800234
David Jamesa6e08892013-03-01 13:34:11 -0800235 def testSingleFileDeployFailure(self):
236 """Default staging enforces that mandatory files are copied"""
Mike Frysingerc3061a62015-06-04 04:16:18 -0400237 options = _ParseCommandLine(self.common_flags)
David Jamesa6e08892013-03-01 13:34:11 -0800238 osutils.Touch(os.path.join(self.build_dir, 'chrome'), makedirs=True)
239 self.assertRaises(
240 chrome_util.MissingPathError, deploy_chrome._PrepareStagingDir,
Daniel Eratc89829c2014-05-12 17:24:21 -0700241 options, self.tempdir, self.staging_dir, chrome_util._COPY_PATHS_CHROME)
Ryan Cuief91e702013-02-04 12:06:36 -0800242
David Jamesa6e08892013-03-01 13:34:11 -0800243 def testSloppyDeployFailure(self):
244 """Sloppy staging enforces that at least one file is copied."""
Mike Frysingerc3061a62015-06-04 04:16:18 -0400245 options = _ParseCommandLine(self.common_flags + ['--sloppy'])
David Jamesa6e08892013-03-01 13:34:11 -0800246 self.assertRaises(
247 chrome_util.MissingPathError, deploy_chrome._PrepareStagingDir,
Daniel Eratc89829c2014-05-12 17:24:21 -0700248 options, self.tempdir, self.staging_dir, chrome_util._COPY_PATHS_CHROME)
David Jamesa6e08892013-03-01 13:34:11 -0800249
250 def testSloppyDeploySuccess(self):
251 """Sloppy staging - stage one file."""
Mike Frysingerc3061a62015-06-04 04:16:18 -0400252 options = _ParseCommandLine(self.common_flags + ['--sloppy'])
David Jamesa6e08892013-03-01 13:34:11 -0800253 osutils.Touch(os.path.join(self.build_dir, 'chrome'), makedirs=True)
Steve Funge984a532013-11-25 17:09:25 -0800254 deploy_chrome._PrepareStagingDir(options, self.tempdir, self.staging_dir,
Daniel Eratc89829c2014-05-12 17:24:21 -0700255 chrome_util._COPY_PATHS_CHROME)
David Jamesa6e08892013-03-01 13:34:11 -0800256
Steve Funge984a532013-11-25 17:09:25 -0800257
258class DeployTestBuildDir(cros_test_lib.MockTempDirTestCase):
Daniel Erat1ae46382014-08-14 10:23:39 -0700259 """Set up a deploy object with a build-dir for use in deployment type tests"""
Steve Funge984a532013-11-25 17:09:25 -0800260
261 def _GetDeployChrome(self, args):
Mike Frysingerc3061a62015-06-04 04:16:18 -0400262 options = _ParseCommandLine(args)
Steve Funge984a532013-11-25 17:09:25 -0800263 return deploy_chrome.DeployChrome(
264 options, self.tempdir, os.path.join(self.tempdir, 'staging'))
265
266 def setUp(self):
267 self.staging_dir = os.path.join(self.tempdir, 'staging')
268 self.build_dir = os.path.join(self.tempdir, 'build_dir')
269 self.deploy_mock = self.StartPatcher(DeployChromeMock())
270 self.deploy = self._GetDeployChrome(
271 list(_REGULAR_TO) + ['--build-dir', self.build_dir,
272 '--board=lumpy', '--staging-only', '--cache-dir',
273 self.tempdir, '--sloppy'])
274
Daniel Erat1ae46382014-08-14 10:23:39 -0700275 def getCopyPath(self, source_path):
276 """Return a chrome_util.Path or None if not present."""
277 paths = [p for p in self.deploy.copy_paths if p.src == source_path]
278 return paths[0] if paths else None
Steve Funge984a532013-11-25 17:09:25 -0800279
Daniel Erat1ae46382014-08-14 10:23:39 -0700280class TestDeploymentType(DeployTestBuildDir):
Steve Funge984a532013-11-25 17:09:25 -0800281 """Test detection of deployment type using build dir."""
282
Daniel Erat1ae46382014-08-14 10:23:39 -0700283 def testAppShellDetection(self):
284 """Check for an app_shell deployment"""
285 osutils.Touch(os.path.join(self.deploy.options.build_dir, 'app_shell'),
Steve Funge984a532013-11-25 17:09:25 -0800286 makedirs=True)
287 self.deploy._CheckDeployType()
Daniel Erat1ae46382014-08-14 10:23:39 -0700288 self.assertTrue(self.getCopyPath('app_shell'))
289 self.assertFalse(self.getCopyPath('chrome'))
Steve Funge984a532013-11-25 17:09:25 -0800290
Daniel Erat1ae46382014-08-14 10:23:39 -0700291 def testChromeAndAppShellDetection(self):
Daniel Eratf53bd3a2016-12-02 11:28:36 -0700292 """Check for a chrome deployment when app_shell also exists."""
Steve Fung63d705d2014-03-16 03:14:03 -0700293 osutils.Touch(os.path.join(self.deploy.options.build_dir, 'chrome'),
294 makedirs=True)
Daniel Erat1ae46382014-08-14 10:23:39 -0700295 osutils.Touch(os.path.join(self.deploy.options.build_dir, 'app_shell'),
Steve Fung63d705d2014-03-16 03:14:03 -0700296 makedirs=True)
297 self.deploy._CheckDeployType()
Daniel Erat1ae46382014-08-14 10:23:39 -0700298 self.assertTrue(self.getCopyPath('chrome'))
Daniel Erat9813f0e2014-11-12 11:00:28 -0700299 self.assertFalse(self.getCopyPath('app_shell'))
Steve Fung63d705d2014-03-16 03:14:03 -0700300
Steve Funge984a532013-11-25 17:09:25 -0800301 def testChromeDetection(self):
302 """Check for a regular chrome deployment"""
303 osutils.Touch(os.path.join(self.deploy.options.build_dir, 'chrome'),
304 makedirs=True)
305 self.deploy._CheckDeployType()
Daniel Erat1ae46382014-08-14 10:23:39 -0700306 self.assertTrue(self.getCopyPath('chrome'))
Daniel Erat9813f0e2014-11-12 11:00:28 -0700307 self.assertFalse(self.getCopyPath('app_shell'))