blob: 7f162184307a181fa3866edae456c42c5c814a3c [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
Ryan Cuiafd6c5c2012-07-30 17:48:22 -07002# 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
Steve Funge984a532013-11-25 17:09:25 -08006"""Unit tests for the deploy_chrome script."""
Ryan Cuiafd6c5c2012-07-30 17:48:22 -07007
Mike Frysinger383367e2014-09-16 15:06:17 -04008from __future__ import print_function
9
Mike Frysingerea838d12014-12-08 11:55:32 -050010import mock
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070011import os
David James88e6f032013-03-02 08:13:20 -080012import time
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070013
David Pursellcfd58872015-03-19 09:15:48 -070014from chromite.cli.cros import cros_chrome_sdk_unittest
Ryan Cuief91e702013-02-04 12:06:36 -080015from chromite.lib import chrome_util
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070016from chromite.lib import cros_build_lib
17from chromite.lib import cros_test_lib
Ryan Cui686ec052013-02-12 16:39:41 -080018from chromite.lib import osutils
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070019from chromite.lib import partial_mock
Robert Flack1dc7ea82014-11-26 13:50:24 -050020from chromite.lib import remote_access
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070021from chromite.lib import remote_access_unittest
22from chromite.scripts import deploy_chrome
23
Ryan Cuief91e702013-02-04 12:06:36 -080024
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070025# pylint: disable=W0212
26
27_REGULAR_TO = ('--to', 'monkey')
28_GS_PATH = 'gs://foon'
29
30
31def _ParseCommandLine(argv):
32 return deploy_chrome._ParseCommandLine(['--log-level', 'debug'] + argv)
33
34
35class InterfaceTest(cros_test_lib.OutputTestCase):
36 """Tests the commandline interface of the script."""
37
Bernie Thompson93b9ee62018-02-21 14:56:16 -080038 BOARD = 'eve'
Ryan Cui686ec052013-02-12 16:39:41 -080039
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070040 def testGsLocalPathUnSpecified(self):
41 """Test no chrome path specified."""
42 with self.OutputCapturer():
43 self.assertRaises2(SystemExit, _ParseCommandLine, list(_REGULAR_TO),
44 check_attrs={'code': 2})
45
46 def testGsPathSpecified(self):
47 """Test case of GS path specified."""
48 argv = list(_REGULAR_TO) + ['--gs-path', _GS_PATH]
49 _ParseCommandLine(argv)
50
51 def testLocalPathSpecified(self):
52 """Test case of local path specified."""
Mike Frysingerd6e2df02014-11-26 02:55:04 -050053 argv = list(_REGULAR_TO) + ['--local-pkg-path', '/path/to/chrome']
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070054 _ParseCommandLine(argv)
55
56 def testNoTarget(self):
57 """Test no target specified."""
58 argv = ['--gs-path', _GS_PATH]
Ryan Cuief91e702013-02-04 12:06:36 -080059 self.assertParseError(argv)
60
61 def assertParseError(self, argv):
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070062 with self.OutputCapturer():
63 self.assertRaises2(SystemExit, _ParseCommandLine, argv,
64 check_attrs={'code': 2})
65
Ryan Cui686ec052013-02-12 16:39:41 -080066 def testNoBoardBuildDir(self):
67 argv = ['--staging-only', '--build-dir=/path/to/nowhere']
68 self.assertParseError(argv)
69
Thiago Goncales12793312013-05-23 11:26:17 -070070 def testMountOptionSetsTargetDir(self):
71 argv = list(_REGULAR_TO) + ['--gs-path', _GS_PATH, '--mount']
Mike Frysingerc3061a62015-06-04 04:16:18 -040072 options = _ParseCommandLine(argv)
Thiago Goncales12793312013-05-23 11:26:17 -070073 self.assertIsNot(options.target_dir, None)
74
75 def testMountOptionSetsMountDir(self):
76 argv = list(_REGULAR_TO) + ['--gs-path', _GS_PATH, '--mount']
Mike Frysingerc3061a62015-06-04 04:16:18 -040077 options = _ParseCommandLine(argv)
Thiago Goncales12793312013-05-23 11:26:17 -070078 self.assertIsNot(options.mount_dir, None)
79
80 def testMountOptionDoesNotOverrideTargetDir(self):
81 argv = list(_REGULAR_TO) + ['--gs-path', _GS_PATH, '--mount',
82 '--target-dir', '/foo/bar/cow']
Mike Frysingerc3061a62015-06-04 04:16:18 -040083 options = _ParseCommandLine(argv)
Thiago Goncales12793312013-05-23 11:26:17 -070084 self.assertEqual(options.target_dir, '/foo/bar/cow')
85
86 def testMountOptionDoesNotOverrideMountDir(self):
87 argv = list(_REGULAR_TO) + ['--gs-path', _GS_PATH, '--mount',
88 '--mount-dir', '/foo/bar/cow']
Mike Frysingerc3061a62015-06-04 04:16:18 -040089 options = _ParseCommandLine(argv)
Thiago Goncales12793312013-05-23 11:26:17 -070090 self.assertEqual(options.mount_dir, '/foo/bar/cow')
91
Adrian Eldera2c548a2017-11-07 19:01:29 -050092 def testSshIdentityOptionSetsOption(self):
93 argv = list(_REGULAR_TO) + ['--private-key', '/foo/bar/key',
94 '--board', 'cedar',
Bernie Thompson93b9ee62018-02-21 14:56:16 -080095 '--build-dir', '/path/to/nowhere']
Adrian Eldera2c548a2017-11-07 19:01:29 -050096 options = _ParseCommandLine(argv)
97 self.assertEqual(options.private_key, '/foo/bar/key')
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070098
99class DeployChromeMock(partial_mock.PartialMock):
Steve Funge984a532013-11-25 17:09:25 -0800100 """Deploy Chrome Mock Class."""
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700101
102 TARGET = 'chromite.scripts.deploy_chrome.DeployChrome'
David James88e6f032013-03-02 08:13:20 -0800103 ATTRS = ('_KillProcsIfNeeded', '_DisableRootfsVerification')
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700104
David James88e6f032013-03-02 08:13:20 -0800105 def __init__(self):
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700106 partial_mock.PartialMock.__init__(self)
Robert Flack1dc7ea82014-11-26 13:50:24 -0500107 self.remote_device_mock = remote_access_unittest.RemoteDeviceMock()
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700108 # Target starts off as having rootfs verification enabled.
Ryan Cuie18f24f2012-12-03 18:39:55 -0800109 self.rsh_mock = remote_access_unittest.RemoteShMock()
David James88e6f032013-03-02 08:13:20 -0800110 self.rsh_mock.SetDefaultCmdResult(0)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700111 self.MockMountCmd(1)
David Haddock3151d912017-10-24 03:50:32 +0000112 self.rsh_mock.AddCmdResult(
113 deploy_chrome.LSOF_COMMAND % (deploy_chrome._CHROME_DIR,), 1)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700114
115 def MockMountCmd(self, returnvalue):
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700116 self.rsh_mock.AddCmdResult(deploy_chrome.MOUNT_RW_COMMAND,
David James88e6f032013-03-02 08:13:20 -0800117 returnvalue)
118
119 def _DisableRootfsVerification(self, inst):
120 with mock.patch.object(time, 'sleep'):
121 self.backup['_DisableRootfsVerification'](inst)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700122
Ryan Cui4d6fca92012-12-13 16:41:56 -0800123 def PreStart(self):
Robert Flack1dc7ea82014-11-26 13:50:24 -0500124 self.remote_device_mock.start()
Ryan Cui4d6fca92012-12-13 16:41:56 -0800125 self.rsh_mock.start()
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700126
Ryan Cui4d6fca92012-12-13 16:41:56 -0800127 def PreStop(self):
128 self.rsh_mock.stop()
Robert Flack1dc7ea82014-11-26 13:50:24 -0500129 self.remote_device_mock.stop()
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700130
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700131 def _KillProcsIfNeeded(self, _inst):
132 # Fully stub out for now.
133 pass
134
135
Ryan Cuief91e702013-02-04 12:06:36 -0800136class DeployTest(cros_test_lib.MockTempDirTestCase):
Steve Funge984a532013-11-25 17:09:25 -0800137 """Setup a deploy object with a GS-path for use in tests."""
138
Ryan Cuief91e702013-02-04 12:06:36 -0800139 def _GetDeployChrome(self, args):
Mike Frysingerc3061a62015-06-04 04:16:18 -0400140 options = _ParseCommandLine(args)
Ryan Cuia56a71e2012-10-18 18:40:35 -0700141 return deploy_chrome.DeployChrome(
142 options, self.tempdir, os.path.join(self.tempdir, 'staging'))
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700143
144 def setUp(self):
Ryan Cuif1416f32013-01-22 18:43:41 -0800145 self.deploy_mock = self.StartPatcher(DeployChromeMock())
Ryan Cuief91e702013-02-04 12:06:36 -0800146 self.deploy = self._GetDeployChrome(
David James88e6f032013-03-02 08:13:20 -0800147 list(_REGULAR_TO) + ['--gs-path', _GS_PATH, '--force'])
Luigi Semenzato1bc79b22016-11-22 16:32:17 -0800148 self.remote_reboot_mock = \
149 self.PatchObject(remote_access.RemoteAccess, 'RemoteReboot',
150 return_value=True)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700151
David James88e6f032013-03-02 08:13:20 -0800152class TestDisableRootfsVerification(DeployTest):
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700153 """Testing disabling of rootfs verification and RO mode."""
154
David James88e6f032013-03-02 08:13:20 -0800155 def testDisableRootfsVerificationSuccess(self):
156 """Test the working case, disabling rootfs verification."""
157 self.deploy_mock.MockMountCmd(0)
158 self.deploy._DisableRootfsVerification()
Robert Flack1dc7ea82014-11-26 13:50:24 -0500159 self.assertFalse(self.deploy._target_dir_is_still_readonly.is_set())
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700160
161 def testDisableRootfsVerificationFailure(self):
162 """Test failure to disable rootfs verification."""
Luigi Semenzato1bc79b22016-11-22 16:32:17 -0800163 #pylint: disable=unused-argument
Shuqian Zhao14e61092017-11-17 00:02:16 +0000164 def RaiseRunCommandError(timeout_sec=None):
Luigi Semenzato1bc79b22016-11-22 16:32:17 -0800165 raise cros_build_lib.RunCommandError('Mock RunCommandError', 0)
166 self.remote_reboot_mock.side_effect = RaiseRunCommandError
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700167 self.assertRaises(cros_build_lib.RunCommandError,
David James88e6f032013-03-02 08:13:20 -0800168 self.deploy._DisableRootfsVerification)
Luigi Semenzato1bc79b22016-11-22 16:32:17 -0800169 self.remote_reboot_mock.side_effect = None
Robert Flack1dc7ea82014-11-26 13:50:24 -0500170 self.assertFalse(self.deploy._target_dir_is_still_readonly.is_set())
David James88e6f032013-03-02 08:13:20 -0800171
172
173class TestMount(DeployTest):
174 """Testing mount success and failure."""
175
176 def testSuccess(self):
177 """Test case where we are able to mount as writable."""
Robert Flack1dc7ea82014-11-26 13:50:24 -0500178 self.assertFalse(self.deploy._target_dir_is_still_readonly.is_set())
David James88e6f032013-03-02 08:13:20 -0800179 self.deploy_mock.MockMountCmd(0)
180 self.deploy._MountRootfsAsWritable()
Robert Flack1dc7ea82014-11-26 13:50:24 -0500181 self.assertFalse(self.deploy._target_dir_is_still_readonly.is_set())
David James88e6f032013-03-02 08:13:20 -0800182
183 def testMountError(self):
184 """Test that mount failure doesn't raise an exception by default."""
Robert Flack1dc7ea82014-11-26 13:50:24 -0500185 self.assertFalse(self.deploy._target_dir_is_still_readonly.is_set())
Mike Frysinger74ccd572015-05-21 21:18:20 -0400186 self.PatchObject(remote_access.RemoteDevice, 'IsDirWritable',
Robert Flack1dc7ea82014-11-26 13:50:24 -0500187 return_value=False, autospec=True)
David James88e6f032013-03-02 08:13:20 -0800188 self.deploy._MountRootfsAsWritable()
Robert Flack1dc7ea82014-11-26 13:50:24 -0500189 self.assertTrue(self.deploy._target_dir_is_still_readonly.is_set())
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700190
191 def testMountRwFailure(self):
David James88e6f032013-03-02 08:13:20 -0800192 """Test that mount failure raises an exception if error_code_ok=False."""
193 self.assertRaises(cros_build_lib.RunCommandError,
194 self.deploy._MountRootfsAsWritable, error_code_ok=False)
Robert Flack1dc7ea82014-11-26 13:50:24 -0500195 self.assertFalse(self.deploy._target_dir_is_still_readonly.is_set())
196
197 def testMountTempDir(self):
198 """Test that mount succeeds if target dir is writable."""
199 self.assertFalse(self.deploy._target_dir_is_still_readonly.is_set())
Mike Frysinger74ccd572015-05-21 21:18:20 -0400200 self.PatchObject(remote_access.RemoteDevice, 'IsDirWritable',
Robert Flack1dc7ea82014-11-26 13:50:24 -0500201 return_value=True, autospec=True)
202 self.deploy._MountRootfsAsWritable()
203 self.assertFalse(self.deploy._target_dir_is_still_readonly.is_set())
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700204
205
Ryan Cuief91e702013-02-04 12:06:36 -0800206class TestUiJobStarted(DeployTest):
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700207 """Test detection of a running 'ui' job."""
208
Ryan Cuif2d1a582013-02-19 14:08:13 -0800209 def MockStatusUiCmd(self, **kwargs):
David Haddock3151d912017-10-24 03:50:32 +0000210 self.deploy_mock.rsh_mock.AddCmdResult('status ui', **kwargs)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700211
212 def testUiJobStartedFalse(self):
213 """Correct results with a stopped job."""
Ryan Cuif2d1a582013-02-19 14:08:13 -0800214 self.MockStatusUiCmd(output='ui stop/waiting')
215 self.assertFalse(self.deploy._CheckUiJobStarted())
216
217 def testNoUiJob(self):
218 """Correct results when the job doesn't exist."""
219 self.MockStatusUiCmd(error='start: Unknown job: ui', returncode=1)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700220 self.assertFalse(self.deploy._CheckUiJobStarted())
221
222 def testCheckRootfsWriteableTrue(self):
223 """Correct results with a running job."""
Ryan Cuif2d1a582013-02-19 14:08:13 -0800224 self.MockStatusUiCmd(output='ui start/running, process 297')
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700225 self.assertTrue(self.deploy._CheckUiJobStarted())
226
227
Ryan Cuief91e702013-02-04 12:06:36 -0800228class StagingTest(cros_test_lib.MockTempDirTestCase):
229 """Test user-mode and ebuild-mode staging functionality."""
230
231 def setUp(self):
Ryan Cuief91e702013-02-04 12:06:36 -0800232 self.staging_dir = os.path.join(self.tempdir, 'staging')
233 self.build_dir = os.path.join(self.tempdir, 'build_dir')
Ryan Cui686ec052013-02-12 16:39:41 -0800234 self.common_flags = ['--build-dir', self.build_dir,
Bernie Thompson93b9ee62018-02-21 14:56:16 -0800235 '--board=eve', '--staging-only', '--cache-dir',
Ryan Cui686ec052013-02-12 16:39:41 -0800236 self.tempdir]
Ryan Cuia0215a72013-02-14 16:20:45 -0800237 self.sdk_mock = self.StartPatcher(cros_chrome_sdk_unittest.SDKFetcherMock())
Ryan Cui686ec052013-02-12 16:39:41 -0800238 self.PatchObject(
239 osutils, 'SourceEnvironment', autospec=True,
240 return_value={'STRIP': 'x86_64-cros-linux-gnu-strip'})
Ryan Cuief91e702013-02-04 12:06:36 -0800241
David Jamesa6e08892013-03-01 13:34:11 -0800242 def testSingleFileDeployFailure(self):
243 """Default staging enforces that mandatory files are copied"""
Mike Frysingerc3061a62015-06-04 04:16:18 -0400244 options = _ParseCommandLine(self.common_flags)
David Jamesa6e08892013-03-01 13:34:11 -0800245 osutils.Touch(os.path.join(self.build_dir, 'chrome'), makedirs=True)
246 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)
Ryan Cuief91e702013-02-04 12:06:36 -0800249
David Jamesa6e08892013-03-01 13:34:11 -0800250 def testSloppyDeployFailure(self):
251 """Sloppy staging enforces that at least one file is copied."""
Mike Frysingerc3061a62015-06-04 04:16:18 -0400252 options = _ParseCommandLine(self.common_flags + ['--sloppy'])
David Jamesa6e08892013-03-01 13:34:11 -0800253 self.assertRaises(
254 chrome_util.MissingPathError, deploy_chrome._PrepareStagingDir,
Daniel Eratc89829c2014-05-12 17:24:21 -0700255 options, self.tempdir, self.staging_dir, chrome_util._COPY_PATHS_CHROME)
David Jamesa6e08892013-03-01 13:34:11 -0800256
257 def testSloppyDeploySuccess(self):
258 """Sloppy staging - stage one file."""
Mike Frysingerc3061a62015-06-04 04:16:18 -0400259 options = _ParseCommandLine(self.common_flags + ['--sloppy'])
David Jamesa6e08892013-03-01 13:34:11 -0800260 osutils.Touch(os.path.join(self.build_dir, 'chrome'), makedirs=True)
Steve Funge984a532013-11-25 17:09:25 -0800261 deploy_chrome._PrepareStagingDir(options, self.tempdir, self.staging_dir,
Daniel Eratc89829c2014-05-12 17:24:21 -0700262 chrome_util._COPY_PATHS_CHROME)
David Jamesa6e08892013-03-01 13:34:11 -0800263
Steve Funge984a532013-11-25 17:09:25 -0800264
265class DeployTestBuildDir(cros_test_lib.MockTempDirTestCase):
Daniel Erat1ae46382014-08-14 10:23:39 -0700266 """Set up a deploy object with a build-dir for use in deployment type tests"""
Steve Funge984a532013-11-25 17:09:25 -0800267
268 def _GetDeployChrome(self, args):
Mike Frysingerc3061a62015-06-04 04:16:18 -0400269 options = _ParseCommandLine(args)
Steve Funge984a532013-11-25 17:09:25 -0800270 return deploy_chrome.DeployChrome(
271 options, self.tempdir, os.path.join(self.tempdir, 'staging'))
272
273 def setUp(self):
274 self.staging_dir = os.path.join(self.tempdir, 'staging')
275 self.build_dir = os.path.join(self.tempdir, 'build_dir')
276 self.deploy_mock = self.StartPatcher(DeployChromeMock())
277 self.deploy = self._GetDeployChrome(
278 list(_REGULAR_TO) + ['--build-dir', self.build_dir,
Bernie Thompson93b9ee62018-02-21 14:56:16 -0800279 '--board=eve', '--staging-only', '--cache-dir',
Steve Funge984a532013-11-25 17:09:25 -0800280 self.tempdir, '--sloppy'])
281
Daniel Erat1ae46382014-08-14 10:23:39 -0700282 def getCopyPath(self, source_path):
283 """Return a chrome_util.Path or None if not present."""
284 paths = [p for p in self.deploy.copy_paths if p.src == source_path]
285 return paths[0] if paths else None
Steve Funge984a532013-11-25 17:09:25 -0800286
Daniel Erat1ae46382014-08-14 10:23:39 -0700287class TestDeploymentType(DeployTestBuildDir):
Steve Funge984a532013-11-25 17:09:25 -0800288 """Test detection of deployment type using build dir."""
289
Daniel Erat1ae46382014-08-14 10:23:39 -0700290 def testAppShellDetection(self):
291 """Check for an app_shell deployment"""
292 osutils.Touch(os.path.join(self.deploy.options.build_dir, 'app_shell'),
Steve Funge984a532013-11-25 17:09:25 -0800293 makedirs=True)
294 self.deploy._CheckDeployType()
Daniel Erat1ae46382014-08-14 10:23:39 -0700295 self.assertTrue(self.getCopyPath('app_shell'))
296 self.assertFalse(self.getCopyPath('chrome'))
Steve Funge984a532013-11-25 17:09:25 -0800297
Daniel Erat1ae46382014-08-14 10:23:39 -0700298 def testChromeAndAppShellDetection(self):
Daniel Eratf53bd3a2016-12-02 11:28:36 -0700299 """Check for a chrome deployment when app_shell also exists."""
Steve Fung63d705d2014-03-16 03:14:03 -0700300 osutils.Touch(os.path.join(self.deploy.options.build_dir, 'chrome'),
301 makedirs=True)
Daniel Erat1ae46382014-08-14 10:23:39 -0700302 osutils.Touch(os.path.join(self.deploy.options.build_dir, 'app_shell'),
Steve Fung63d705d2014-03-16 03:14:03 -0700303 makedirs=True)
304 self.deploy._CheckDeployType()
Daniel Erat1ae46382014-08-14 10:23:39 -0700305 self.assertTrue(self.getCopyPath('chrome'))
Daniel Erat9813f0e2014-11-12 11:00:28 -0700306 self.assertFalse(self.getCopyPath('app_shell'))
Steve Fung63d705d2014-03-16 03:14:03 -0700307
Steve Funge984a532013-11-25 17:09:25 -0800308 def testChromeDetection(self):
309 """Check for a regular chrome deployment"""
310 osutils.Touch(os.path.join(self.deploy.options.build_dir, 'chrome'),
311 makedirs=True)
312 self.deploy._CheckDeployType()
Daniel Erat1ae46382014-08-14 10:23:39 -0700313 self.assertTrue(self.getCopyPath('chrome'))
Daniel Erat9813f0e2014-11-12 11:00:28 -0700314 self.assertFalse(self.getCopyPath('app_shell'))