blob: ac5cf3da1ca29cdbf873a675ff629696aaaa98be [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
Ryan Cuicbd9bb62013-04-30 11:17:02 -070021from chromite.lib import stats
22from chromite.lib import stats_unittest
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070023from chromite.scripts import deploy_chrome
24
Ryan Cuief91e702013-02-04 12:06:36 -080025
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070026# pylint: disable=W0212
27
28_REGULAR_TO = ('--to', 'monkey')
29_GS_PATH = 'gs://foon'
30
31
32def _ParseCommandLine(argv):
33 return deploy_chrome._ParseCommandLine(['--log-level', 'debug'] + argv)
34
35
36class InterfaceTest(cros_test_lib.OutputTestCase):
37 """Tests the commandline interface of the script."""
38
Ryan Cui686ec052013-02-12 16:39:41 -080039 BOARD = 'lumpy'
40
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070041 def testGsLocalPathUnSpecified(self):
42 """Test no chrome path specified."""
43 with self.OutputCapturer():
44 self.assertRaises2(SystemExit, _ParseCommandLine, list(_REGULAR_TO),
45 check_attrs={'code': 2})
46
47 def testGsPathSpecified(self):
48 """Test case of GS path specified."""
49 argv = list(_REGULAR_TO) + ['--gs-path', _GS_PATH]
50 _ParseCommandLine(argv)
51
52 def testLocalPathSpecified(self):
53 """Test case of local path specified."""
Mike Frysingerd6e2df02014-11-26 02:55:04 -050054 argv = list(_REGULAR_TO) + ['--local-pkg-path', '/path/to/chrome']
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070055 _ParseCommandLine(argv)
56
57 def testNoTarget(self):
58 """Test no target specified."""
59 argv = ['--gs-path', _GS_PATH]
Ryan Cuief91e702013-02-04 12:06:36 -080060 self.assertParseError(argv)
61
62 def assertParseError(self, argv):
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070063 with self.OutputCapturer():
64 self.assertRaises2(SystemExit, _ParseCommandLine, argv,
65 check_attrs={'code': 2})
66
Ryan Cui686ec052013-02-12 16:39:41 -080067 def testNoBoardBuildDir(self):
68 argv = ['--staging-only', '--build-dir=/path/to/nowhere']
69 self.assertParseError(argv)
70
Thiago Goncales12793312013-05-23 11:26:17 -070071 def testMountOptionSetsTargetDir(self):
72 argv = list(_REGULAR_TO) + ['--gs-path', _GS_PATH, '--mount']
Mike Frysingerc3061a62015-06-04 04:16:18 -040073 options = _ParseCommandLine(argv)
Thiago Goncales12793312013-05-23 11:26:17 -070074 self.assertIsNot(options.target_dir, None)
75
76 def testMountOptionSetsMountDir(self):
77 argv = list(_REGULAR_TO) + ['--gs-path', _GS_PATH, '--mount']
Mike Frysingerc3061a62015-06-04 04:16:18 -040078 options = _ParseCommandLine(argv)
Thiago Goncales12793312013-05-23 11:26:17 -070079 self.assertIsNot(options.mount_dir, None)
80
81 def testMountOptionDoesNotOverrideTargetDir(self):
82 argv = list(_REGULAR_TO) + ['--gs-path', _GS_PATH, '--mount',
83 '--target-dir', '/foo/bar/cow']
Mike Frysingerc3061a62015-06-04 04:16:18 -040084 options = _ParseCommandLine(argv)
Thiago Goncales12793312013-05-23 11:26:17 -070085 self.assertEqual(options.target_dir, '/foo/bar/cow')
86
87 def testMountOptionDoesNotOverrideMountDir(self):
88 argv = list(_REGULAR_TO) + ['--gs-path', _GS_PATH, '--mount',
89 '--mount-dir', '/foo/bar/cow']
Mike Frysingerc3061a62015-06-04 04:16:18 -040090 options = _ParseCommandLine(argv)
Thiago Goncales12793312013-05-23 11:26:17 -070091 self.assertEqual(options.mount_dir, '/foo/bar/cow')
92
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070093
94class DeployChromeMock(partial_mock.PartialMock):
Steve Funge984a532013-11-25 17:09:25 -080095 """Deploy Chrome Mock Class."""
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070096
97 TARGET = 'chromite.scripts.deploy_chrome.DeployChrome'
David James88e6f032013-03-02 08:13:20 -080098 ATTRS = ('_KillProcsIfNeeded', '_DisableRootfsVerification')
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070099
David James88e6f032013-03-02 08:13:20 -0800100 def __init__(self):
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700101 partial_mock.PartialMock.__init__(self)
Robert Flack1dc7ea82014-11-26 13:50:24 -0500102 self.remote_device_mock = remote_access_unittest.RemoteDeviceMock()
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700103 # Target starts off as having rootfs verification enabled.
Ryan Cuie18f24f2012-12-03 18:39:55 -0800104 self.rsh_mock = remote_access_unittest.RemoteShMock()
David James88e6f032013-03-02 08:13:20 -0800105 self.rsh_mock.SetDefaultCmdResult(0)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700106 self.MockMountCmd(1)
Pawel Osciak577773a2013-03-05 10:52:12 -0800107 self.rsh_mock.AddCmdResult(
108 deploy_chrome.LSOF_COMMAND % (deploy_chrome._CHROME_DIR,), 1)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700109
110 def MockMountCmd(self, returnvalue):
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700111 self.rsh_mock.AddCmdResult(deploy_chrome.MOUNT_RW_COMMAND,
David James88e6f032013-03-02 08:13:20 -0800112 returnvalue)
113
114 def _DisableRootfsVerification(self, inst):
115 with mock.patch.object(time, 'sleep'):
116 self.backup['_DisableRootfsVerification'](inst)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700117
Ryan Cui4d6fca92012-12-13 16:41:56 -0800118 def PreStart(self):
Robert Flack1dc7ea82014-11-26 13:50:24 -0500119 self.remote_device_mock.start()
Ryan Cui4d6fca92012-12-13 16:41:56 -0800120 self.rsh_mock.start()
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700121
Ryan Cui4d6fca92012-12-13 16:41:56 -0800122 def PreStop(self):
123 self.rsh_mock.stop()
Robert Flack1dc7ea82014-11-26 13:50:24 -0500124 self.remote_device_mock.stop()
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700125
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700126 def _KillProcsIfNeeded(self, _inst):
127 # Fully stub out for now.
128 pass
129
130
Ryan Cuicbd9bb62013-04-30 11:17:02 -0700131class MainTest(cros_test_lib.MockLoggingTestCase):
Steve Funge984a532013-11-25 17:09:25 -0800132 """Main tests."""
Ryan Cuicbd9bb62013-04-30 11:17:02 -0700133
134 def setUp(self):
135 self.PatchObject(deploy_chrome.DeployChrome, 'Perform', autospec=True)
136 self.stats_module_mock = stats_unittest.StatsModuleMock()
137 self.StartPatcher(self.stats_module_mock)
138
139 def testStatsUpload(self, call_count=1):
140 """The stats upload path."""
141 deploy_chrome.main(['--board=lumpy', '--staging-only',
142 '--build-dir=/tmp/abc'])
143 self.assertEquals(stats.StatsUploader._Upload.call_count, call_count)
144
145 def testStatsUploadError(self):
146 """Don't upload stats if we fail to create it."""
147 self.stats_module_mock.stats_mock.init_exception = True
148 with cros_test_lib.LoggingCapturer():
149 self.testStatsUpload(call_count=0)
150
151
Ryan Cuief91e702013-02-04 12:06:36 -0800152class DeployTest(cros_test_lib.MockTempDirTestCase):
Steve Funge984a532013-11-25 17:09:25 -0800153 """Setup a deploy object with a GS-path for use in tests."""
154
Ryan Cuief91e702013-02-04 12:06:36 -0800155 def _GetDeployChrome(self, args):
Mike Frysingerc3061a62015-06-04 04:16:18 -0400156 options = _ParseCommandLine(args)
Ryan Cuia56a71e2012-10-18 18:40:35 -0700157 return deploy_chrome.DeployChrome(
158 options, self.tempdir, os.path.join(self.tempdir, 'staging'))
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700159
160 def setUp(self):
Ryan Cuif1416f32013-01-22 18:43:41 -0800161 self.deploy_mock = self.StartPatcher(DeployChromeMock())
Ryan Cuief91e702013-02-04 12:06:36 -0800162 self.deploy = self._GetDeployChrome(
David James88e6f032013-03-02 08:13:20 -0800163 list(_REGULAR_TO) + ['--gs-path', _GS_PATH, '--force'])
Luigi Semenzato1bc79b22016-11-22 16:32:17 -0800164 self.remote_reboot_mock = \
165 self.PatchObject(remote_access.RemoteAccess, 'RemoteReboot',
166 return_value=True)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700167
David James88e6f032013-03-02 08:13:20 -0800168class TestDisableRootfsVerification(DeployTest):
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700169 """Testing disabling of rootfs verification and RO mode."""
170
David James88e6f032013-03-02 08:13:20 -0800171 def testDisableRootfsVerificationSuccess(self):
172 """Test the working case, disabling rootfs verification."""
173 self.deploy_mock.MockMountCmd(0)
174 self.deploy._DisableRootfsVerification()
Robert Flack1dc7ea82014-11-26 13:50:24 -0500175 self.assertFalse(self.deploy._target_dir_is_still_readonly.is_set())
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700176
177 def testDisableRootfsVerificationFailure(self):
178 """Test failure to disable rootfs verification."""
Luigi Semenzato1bc79b22016-11-22 16:32:17 -0800179 #pylint: disable=unused-argument
180 def RaiseRunCommandError(timeout_sec=None):
181 raise cros_build_lib.RunCommandError('Mock RunCommandError', 0)
182 self.remote_reboot_mock.side_effect = RaiseRunCommandError
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700183 self.assertRaises(cros_build_lib.RunCommandError,
David James88e6f032013-03-02 08:13:20 -0800184 self.deploy._DisableRootfsVerification)
Luigi Semenzato1bc79b22016-11-22 16:32:17 -0800185 self.remote_reboot_mock.side_effect = None
Robert Flack1dc7ea82014-11-26 13:50:24 -0500186 self.assertFalse(self.deploy._target_dir_is_still_readonly.is_set())
David James88e6f032013-03-02 08:13:20 -0800187
188
189class TestMount(DeployTest):
190 """Testing mount success and failure."""
191
192 def testSuccess(self):
193 """Test case where we are able to mount as writable."""
Robert Flack1dc7ea82014-11-26 13:50:24 -0500194 self.assertFalse(self.deploy._target_dir_is_still_readonly.is_set())
David James88e6f032013-03-02 08:13:20 -0800195 self.deploy_mock.MockMountCmd(0)
196 self.deploy._MountRootfsAsWritable()
Robert Flack1dc7ea82014-11-26 13:50:24 -0500197 self.assertFalse(self.deploy._target_dir_is_still_readonly.is_set())
David James88e6f032013-03-02 08:13:20 -0800198
199 def testMountError(self):
200 """Test that mount failure doesn't raise an exception by default."""
Robert Flack1dc7ea82014-11-26 13:50:24 -0500201 self.assertFalse(self.deploy._target_dir_is_still_readonly.is_set())
Mike Frysinger74ccd572015-05-21 21:18:20 -0400202 self.PatchObject(remote_access.RemoteDevice, 'IsDirWritable',
Robert Flack1dc7ea82014-11-26 13:50:24 -0500203 return_value=False, autospec=True)
David James88e6f032013-03-02 08:13:20 -0800204 self.deploy._MountRootfsAsWritable()
Robert Flack1dc7ea82014-11-26 13:50:24 -0500205 self.assertTrue(self.deploy._target_dir_is_still_readonly.is_set())
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700206
207 def testMountRwFailure(self):
David James88e6f032013-03-02 08:13:20 -0800208 """Test that mount failure raises an exception if error_code_ok=False."""
209 self.assertRaises(cros_build_lib.RunCommandError,
210 self.deploy._MountRootfsAsWritable, error_code_ok=False)
Robert Flack1dc7ea82014-11-26 13:50:24 -0500211 self.assertFalse(self.deploy._target_dir_is_still_readonly.is_set())
212
213 def testMountTempDir(self):
214 """Test that mount succeeds if target dir is writable."""
215 self.assertFalse(self.deploy._target_dir_is_still_readonly.is_set())
Mike Frysinger74ccd572015-05-21 21:18:20 -0400216 self.PatchObject(remote_access.RemoteDevice, 'IsDirWritable',
Robert Flack1dc7ea82014-11-26 13:50:24 -0500217 return_value=True, autospec=True)
218 self.deploy._MountRootfsAsWritable()
219 self.assertFalse(self.deploy._target_dir_is_still_readonly.is_set())
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700220
221
Ryan Cuief91e702013-02-04 12:06:36 -0800222class TestUiJobStarted(DeployTest):
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700223 """Test detection of a running 'ui' job."""
224
Ryan Cuif2d1a582013-02-19 14:08:13 -0800225 def MockStatusUiCmd(self, **kwargs):
226 self.deploy_mock.rsh_mock.AddCmdResult('status ui', **kwargs)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700227
228 def testUiJobStartedFalse(self):
229 """Correct results with a stopped job."""
Ryan Cuif2d1a582013-02-19 14:08:13 -0800230 self.MockStatusUiCmd(output='ui stop/waiting')
231 self.assertFalse(self.deploy._CheckUiJobStarted())
232
233 def testNoUiJob(self):
234 """Correct results when the job doesn't exist."""
235 self.MockStatusUiCmd(error='start: Unknown job: ui', returncode=1)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700236 self.assertFalse(self.deploy._CheckUiJobStarted())
237
238 def testCheckRootfsWriteableTrue(self):
239 """Correct results with a running job."""
Ryan Cuif2d1a582013-02-19 14:08:13 -0800240 self.MockStatusUiCmd(output='ui start/running, process 297')
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700241 self.assertTrue(self.deploy._CheckUiJobStarted())
242
243
Ryan Cuief91e702013-02-04 12:06:36 -0800244class StagingTest(cros_test_lib.MockTempDirTestCase):
245 """Test user-mode and ebuild-mode staging functionality."""
246
247 def setUp(self):
Ryan Cuief91e702013-02-04 12:06:36 -0800248 self.staging_dir = os.path.join(self.tempdir, 'staging')
249 self.build_dir = os.path.join(self.tempdir, 'build_dir')
Ryan Cui686ec052013-02-12 16:39:41 -0800250 self.common_flags = ['--build-dir', self.build_dir,
251 '--board=lumpy', '--staging-only', '--cache-dir',
252 self.tempdir]
Ryan Cuia0215a72013-02-14 16:20:45 -0800253 self.sdk_mock = self.StartPatcher(cros_chrome_sdk_unittest.SDKFetcherMock())
Ryan Cui686ec052013-02-12 16:39:41 -0800254 self.PatchObject(
255 osutils, 'SourceEnvironment', autospec=True,
256 return_value={'STRIP': 'x86_64-cros-linux-gnu-strip'})
Ryan Cuief91e702013-02-04 12:06:36 -0800257
David Jamesa6e08892013-03-01 13:34:11 -0800258 def testSingleFileDeployFailure(self):
259 """Default staging enforces that mandatory files are copied"""
Mike Frysingerc3061a62015-06-04 04:16:18 -0400260 options = _ParseCommandLine(self.common_flags)
David Jamesa6e08892013-03-01 13:34:11 -0800261 osutils.Touch(os.path.join(self.build_dir, 'chrome'), makedirs=True)
262 self.assertRaises(
263 chrome_util.MissingPathError, deploy_chrome._PrepareStagingDir,
Daniel Eratc89829c2014-05-12 17:24:21 -0700264 options, self.tempdir, self.staging_dir, chrome_util._COPY_PATHS_CHROME)
Ryan Cuief91e702013-02-04 12:06:36 -0800265
David Jamesa6e08892013-03-01 13:34:11 -0800266 def testSloppyDeployFailure(self):
267 """Sloppy staging enforces that at least one file is copied."""
Mike Frysingerc3061a62015-06-04 04:16:18 -0400268 options = _ParseCommandLine(self.common_flags + ['--sloppy'])
David Jamesa6e08892013-03-01 13:34:11 -0800269 self.assertRaises(
270 chrome_util.MissingPathError, deploy_chrome._PrepareStagingDir,
Daniel Eratc89829c2014-05-12 17:24:21 -0700271 options, self.tempdir, self.staging_dir, chrome_util._COPY_PATHS_CHROME)
David Jamesa6e08892013-03-01 13:34:11 -0800272
273 def testSloppyDeploySuccess(self):
274 """Sloppy staging - stage one file."""
Mike Frysingerc3061a62015-06-04 04:16:18 -0400275 options = _ParseCommandLine(self.common_flags + ['--sloppy'])
David Jamesa6e08892013-03-01 13:34:11 -0800276 osutils.Touch(os.path.join(self.build_dir, 'chrome'), makedirs=True)
Steve Funge984a532013-11-25 17:09:25 -0800277 deploy_chrome._PrepareStagingDir(options, self.tempdir, self.staging_dir,
Daniel Eratc89829c2014-05-12 17:24:21 -0700278 chrome_util._COPY_PATHS_CHROME)
David Jamesa6e08892013-03-01 13:34:11 -0800279
Steve Funge984a532013-11-25 17:09:25 -0800280
281class DeployTestBuildDir(cros_test_lib.MockTempDirTestCase):
Daniel Erat1ae46382014-08-14 10:23:39 -0700282 """Set up a deploy object with a build-dir for use in deployment type tests"""
Steve Funge984a532013-11-25 17:09:25 -0800283
284 def _GetDeployChrome(self, args):
Mike Frysingerc3061a62015-06-04 04:16:18 -0400285 options = _ParseCommandLine(args)
Steve Funge984a532013-11-25 17:09:25 -0800286 return deploy_chrome.DeployChrome(
287 options, self.tempdir, os.path.join(self.tempdir, 'staging'))
288
289 def setUp(self):
290 self.staging_dir = os.path.join(self.tempdir, 'staging')
291 self.build_dir = os.path.join(self.tempdir, 'build_dir')
292 self.deploy_mock = self.StartPatcher(DeployChromeMock())
293 self.deploy = self._GetDeployChrome(
294 list(_REGULAR_TO) + ['--build-dir', self.build_dir,
295 '--board=lumpy', '--staging-only', '--cache-dir',
296 self.tempdir, '--sloppy'])
297
Daniel Erat1ae46382014-08-14 10:23:39 -0700298 def getCopyPath(self, source_path):
299 """Return a chrome_util.Path or None if not present."""
300 paths = [p for p in self.deploy.copy_paths if p.src == source_path]
301 return paths[0] if paths else None
Steve Funge984a532013-11-25 17:09:25 -0800302
Daniel Erat1ae46382014-08-14 10:23:39 -0700303class TestDeploymentType(DeployTestBuildDir):
Steve Funge984a532013-11-25 17:09:25 -0800304 """Test detection of deployment type using build dir."""
305
Daniel Erat1ae46382014-08-14 10:23:39 -0700306 def testAppShellDetection(self):
307 """Check for an app_shell deployment"""
308 osutils.Touch(os.path.join(self.deploy.options.build_dir, 'app_shell'),
Steve Funge984a532013-11-25 17:09:25 -0800309 makedirs=True)
310 self.deploy._CheckDeployType()
Daniel Erat1ae46382014-08-14 10:23:39 -0700311 self.assertTrue(self.getCopyPath('app_shell'))
312 self.assertFalse(self.getCopyPath('chrome'))
Steve Funge984a532013-11-25 17:09:25 -0800313
Daniel Erat1ae46382014-08-14 10:23:39 -0700314 def testChromeAndAppShellDetection(self):
Daniel Eratf53bd3a2016-12-02 11:28:36 -0700315 """Check for a chrome deployment when app_shell also exists."""
Steve Fung63d705d2014-03-16 03:14:03 -0700316 osutils.Touch(os.path.join(self.deploy.options.build_dir, 'chrome'),
317 makedirs=True)
Daniel Erat1ae46382014-08-14 10:23:39 -0700318 osutils.Touch(os.path.join(self.deploy.options.build_dir, 'app_shell'),
Steve Fung63d705d2014-03-16 03:14:03 -0700319 makedirs=True)
320 self.deploy._CheckDeployType()
Daniel Erat1ae46382014-08-14 10:23:39 -0700321 self.assertTrue(self.getCopyPath('chrome'))
Daniel Erat9813f0e2014-11-12 11:00:28 -0700322 self.assertFalse(self.getCopyPath('app_shell'))
Steve Fung63d705d2014-03-16 03:14:03 -0700323
Steve Funge984a532013-11-25 17:09:25 -0800324 def testChromeDetection(self):
325 """Check for a regular chrome deployment"""
326 osutils.Touch(os.path.join(self.deploy.options.build_dir, 'chrome'),
327 makedirs=True)
328 self.deploy._CheckDeployType()
Daniel Erat1ae46382014-08-14 10:23:39 -0700329 self.assertTrue(self.getCopyPath('chrome'))
Daniel Erat9813f0e2014-11-12 11:00:28 -0700330 self.assertFalse(self.getCopyPath('app_shell'))