blob: eda1fb32edaadba56ca23c85da52cd218139b5dd [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
Anushruth8d797672019-10-17 12:22:31 -070010import errno
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070011import os
Mike Frysinger6165cdc2020-02-21 02:38:07 -050012import sys
David James88e6f032013-03-02 08:13:20 -080013import time
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070014
Mike Frysinger6db648e2018-07-24 19:57:58 -040015import mock
16
David Pursellcfd58872015-03-19 09:15:48 -070017from chromite.cli.cros import cros_chrome_sdk_unittest
Ryan Cuief91e702013-02-04 12:06:36 -080018from chromite.lib import chrome_util
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070019from chromite.lib import cros_build_lib
20from chromite.lib import cros_test_lib
Ryan Cui686ec052013-02-12 16:39:41 -080021from chromite.lib import osutils
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070022from chromite.lib import partial_mock
Robert Flack1dc7ea82014-11-26 13:50:24 -050023from chromite.lib import remote_access
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070024from chromite.lib import remote_access_unittest
25from chromite.scripts import deploy_chrome
26
Ryan Cuief91e702013-02-04 12:06:36 -080027
Mike Frysinger6165cdc2020-02-21 02:38:07 -050028assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
29
30
Mike Frysinger27e21b72018-07-12 14:20:21 -040031# pylint: disable=protected-access
32
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070033
34_REGULAR_TO = ('--to', 'monkey')
Avery Musbachb35e6e32020-03-27 13:35:53 -070035_TARGET_BOARD = 'eve'
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070036_GS_PATH = 'gs://foon'
37
38
39def _ParseCommandLine(argv):
40 return deploy_chrome._ParseCommandLine(['--log-level', 'debug'] + argv)
41
42
43class InterfaceTest(cros_test_lib.OutputTestCase):
44 """Tests the commandline interface of the script."""
45
46 def testGsLocalPathUnSpecified(self):
47 """Test no chrome path specified."""
48 with self.OutputCapturer():
Avery Musbachb35e6e32020-03-27 13:35:53 -070049 self.assertRaises2(SystemExit, _ParseCommandLine,
50 list(_REGULAR_TO) + ['--board', _TARGET_BOARD],
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070051 check_attrs={'code': 2})
52
53 def testGsPathSpecified(self):
54 """Test case of GS path specified."""
Avery Musbachb35e6e32020-03-27 13:35:53 -070055 argv = list(_REGULAR_TO) + ['--board', _TARGET_BOARD, '--gs-path', _GS_PATH]
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070056 _ParseCommandLine(argv)
57
58 def testLocalPathSpecified(self):
59 """Test case of local path specified."""
Avery Musbachb35e6e32020-03-27 13:35:53 -070060 argv = list(_REGULAR_TO) + ['--board', _TARGET_BOARD, '--local-pkg-path',
61 '/path/to/chrome']
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070062 _ParseCommandLine(argv)
63
64 def testNoTarget(self):
65 """Test no target specified."""
Avery Musbachb35e6e32020-03-27 13:35:53 -070066 argv = ['--board', _TARGET_BOARD, '--gs-path', _GS_PATH]
Ryan Cuief91e702013-02-04 12:06:36 -080067 self.assertParseError(argv)
68
69 def assertParseError(self, argv):
Ryan Cuiafd6c5c2012-07-30 17:48:22 -070070 with self.OutputCapturer():
71 self.assertRaises2(SystemExit, _ParseCommandLine, argv,
72 check_attrs={'code': 2})
73
Thiago Goncales12793312013-05-23 11:26:17 -070074 def testMountOptionSetsTargetDir(self):
Avery Musbachb35e6e32020-03-27 13:35:53 -070075 argv = list(_REGULAR_TO) + ['--board', _TARGET_BOARD, '--gs-path', _GS_PATH,
76 '--mount']
Mike Frysingerc3061a62015-06-04 04:16:18 -040077 options = _ParseCommandLine(argv)
Thiago Goncales12793312013-05-23 11:26:17 -070078 self.assertIsNot(options.target_dir, None)
79
80 def testMountOptionSetsMountDir(self):
Avery Musbachb35e6e32020-03-27 13:35:53 -070081 argv = list(_REGULAR_TO) + ['--board', _TARGET_BOARD, '--gs-path', _GS_PATH,
82 '--mount']
Mike Frysingerc3061a62015-06-04 04:16:18 -040083 options = _ParseCommandLine(argv)
Thiago Goncales12793312013-05-23 11:26:17 -070084 self.assertIsNot(options.mount_dir, None)
85
86 def testMountOptionDoesNotOverrideTargetDir(self):
Avery Musbachb35e6e32020-03-27 13:35:53 -070087 argv = list(_REGULAR_TO) + ['--board', _TARGET_BOARD, '--gs-path', _GS_PATH,
88 '--mount', '--target-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.target_dir, '/foo/bar/cow')
91
92 def testMountOptionDoesNotOverrideMountDir(self):
Avery Musbachb35e6e32020-03-27 13:35:53 -070093 argv = list(_REGULAR_TO) + ['--board', _TARGET_BOARD, '--gs-path', _GS_PATH,
94 '--mount', '--mount-dir', '/foo/bar/cow']
Mike Frysingerc3061a62015-06-04 04:16:18 -040095 options = _ParseCommandLine(argv)
Thiago Goncales12793312013-05-23 11:26:17 -070096 self.assertEqual(options.mount_dir, '/foo/bar/cow')
97
Adrian Eldera2c548a2017-11-07 19:01:29 -050098 def testSshIdentityOptionSetsOption(self):
Avery Musbachb35e6e32020-03-27 13:35:53 -070099 argv = list(_REGULAR_TO) + ['--board', _TARGET_BOARD,
100 '--private-key', '/foo/bar/key',
Bernie Thompson93b9ee62018-02-21 14:56:16 -0800101 '--build-dir', '/path/to/nowhere']
Adrian Eldera2c548a2017-11-07 19:01:29 -0500102 options = _ParseCommandLine(argv)
103 self.assertEqual(options.private_key, '/foo/bar/key')
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700104
105class DeployChromeMock(partial_mock.PartialMock):
Steve Funge984a532013-11-25 17:09:25 -0800106 """Deploy Chrome Mock Class."""
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700107
108 TARGET = 'chromite.scripts.deploy_chrome.DeployChrome'
David James88e6f032013-03-02 08:13:20 -0800109 ATTRS = ('_KillProcsIfNeeded', '_DisableRootfsVerification')
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700110
David James88e6f032013-03-02 08:13:20 -0800111 def __init__(self):
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700112 partial_mock.PartialMock.__init__(self)
Robert Flack1dc7ea82014-11-26 13:50:24 -0500113 self.remote_device_mock = remote_access_unittest.RemoteDeviceMock()
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700114 # Target starts off as having rootfs verification enabled.
Ryan Cuie18f24f2012-12-03 18:39:55 -0800115 self.rsh_mock = remote_access_unittest.RemoteShMock()
David James88e6f032013-03-02 08:13:20 -0800116 self.rsh_mock.SetDefaultCmdResult(0)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700117 self.MockMountCmd(1)
David Haddock3151d912017-10-24 03:50:32 +0000118 self.rsh_mock.AddCmdResult(
Anushruth8d797672019-10-17 12:22:31 -0700119 deploy_chrome.LSOF_COMMAND_CHROME % (deploy_chrome._CHROME_DIR,), 1)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700120
121 def MockMountCmd(self, returnvalue):
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700122 self.rsh_mock.AddCmdResult(deploy_chrome.MOUNT_RW_COMMAND,
David James88e6f032013-03-02 08:13:20 -0800123 returnvalue)
124
125 def _DisableRootfsVerification(self, inst):
126 with mock.patch.object(time, 'sleep'):
127 self.backup['_DisableRootfsVerification'](inst)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700128
Ryan Cui4d6fca92012-12-13 16:41:56 -0800129 def PreStart(self):
Robert Flack1dc7ea82014-11-26 13:50:24 -0500130 self.remote_device_mock.start()
Ryan Cui4d6fca92012-12-13 16:41:56 -0800131 self.rsh_mock.start()
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700132
Ryan Cui4d6fca92012-12-13 16:41:56 -0800133 def PreStop(self):
134 self.rsh_mock.stop()
Robert Flack1dc7ea82014-11-26 13:50:24 -0500135 self.remote_device_mock.stop()
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700136
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700137 def _KillProcsIfNeeded(self, _inst):
138 # Fully stub out for now.
139 pass
140
141
Ryan Cuief91e702013-02-04 12:06:36 -0800142class DeployTest(cros_test_lib.MockTempDirTestCase):
Steve Funge984a532013-11-25 17:09:25 -0800143 """Setup a deploy object with a GS-path for use in tests."""
144
Ryan Cuief91e702013-02-04 12:06:36 -0800145 def _GetDeployChrome(self, args):
Mike Frysingerc3061a62015-06-04 04:16:18 -0400146 options = _ParseCommandLine(args)
Ryan Cuia56a71e2012-10-18 18:40:35 -0700147 return deploy_chrome.DeployChrome(
148 options, self.tempdir, os.path.join(self.tempdir, 'staging'))
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700149
150 def setUp(self):
Ryan Cuif1416f32013-01-22 18:43:41 -0800151 self.deploy_mock = self.StartPatcher(DeployChromeMock())
Avery Musbachb35e6e32020-03-27 13:35:53 -0700152 self.deploy = self._GetDeployChrome(list(_REGULAR_TO) +
153 ['--board', _TARGET_BOARD, '--gs-path',
154 _GS_PATH, '--force', '--mount'])
Luigi Semenzato1bc79b22016-11-22 16:32:17 -0800155 self.remote_reboot_mock = \
156 self.PatchObject(remote_access.RemoteAccess, 'RemoteReboot',
157 return_value=True)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700158
Avery Musbachb35e6e32020-03-27 13:35:53 -0700159
160class TestCheckIfBoardMatches(DeployTest):
161 """Testing checking whether the DUT board matches the target board."""
162
163 def testMatchedBoard(self):
164 """Test the case where the DUT board matches the target board."""
165 self.PatchObject(remote_access.ChromiumOSDevice, 'board', _TARGET_BOARD)
166 self.assertTrue(self.deploy.options.force)
167 self.assertTrue(self.deploy._CheckBoard())
168 self.deploy.options.force = False
169 self.assertTrue(self.deploy._CheckBoard())
170
171 def testMismatchedBoard(self):
172 """Test the case where the DUT board does not match the target board."""
173 self.PatchObject(remote_access.ChromiumOSDevice, 'board', 'cedar')
174 self.assertTrue(self.deploy.options.force)
175 self.assertTrue(self.deploy._CheckBoard())
176 self.deploy.options.force = False
177 for response in (False, True):
178 self.PatchObject(cros_build_lib, 'BooleanPrompt', return_value=response)
179 self.assertEqual(response, self.deploy._CheckBoard())
180
181
David James88e6f032013-03-02 08:13:20 -0800182class TestDisableRootfsVerification(DeployTest):
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700183 """Testing disabling of rootfs verification and RO mode."""
184
David James88e6f032013-03-02 08:13:20 -0800185 def testDisableRootfsVerificationSuccess(self):
186 """Test the working case, disabling rootfs verification."""
187 self.deploy_mock.MockMountCmd(0)
188 self.deploy._DisableRootfsVerification()
Steven Bennettsca73efa2018-07-10 13:36:56 -0700189 self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set())
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700190
191 def testDisableRootfsVerificationFailure(self):
192 """Test failure to disable rootfs verification."""
Mike Frysinger27e21b72018-07-12 14:20:21 -0400193 # pylint: disable=unused-argument
Shuqian Zhao14e61092017-11-17 00:02:16 +0000194 def RaiseRunCommandError(timeout_sec=None):
Mike Frysinger929f3ba2019-09-12 03:24:59 -0400195 raise cros_build_lib.RunCommandError('Mock RunCommandError')
Luigi Semenzato1bc79b22016-11-22 16:32:17 -0800196 self.remote_reboot_mock.side_effect = RaiseRunCommandError
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700197 self.assertRaises(cros_build_lib.RunCommandError,
David James88e6f032013-03-02 08:13:20 -0800198 self.deploy._DisableRootfsVerification)
Luigi Semenzato1bc79b22016-11-22 16:32:17 -0800199 self.remote_reboot_mock.side_effect = None
Steven Bennettsca73efa2018-07-10 13:36:56 -0700200 self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set())
David James88e6f032013-03-02 08:13:20 -0800201
202
203class TestMount(DeployTest):
204 """Testing mount success and failure."""
205
206 def testSuccess(self):
207 """Test case where we are able to mount as writable."""
Steven Bennettsca73efa2018-07-10 13:36:56 -0700208 self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set())
David James88e6f032013-03-02 08:13:20 -0800209 self.deploy_mock.MockMountCmd(0)
210 self.deploy._MountRootfsAsWritable()
Steven Bennettsca73efa2018-07-10 13:36:56 -0700211 self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set())
David James88e6f032013-03-02 08:13:20 -0800212
213 def testMountError(self):
214 """Test that mount failure doesn't raise an exception by default."""
Steven Bennettsca73efa2018-07-10 13:36:56 -0700215 self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set())
Avery Musbachb35e6e32020-03-27 13:35:53 -0700216 self.PatchObject(remote_access.ChromiumOSDevice, 'IsDirWritable',
Robert Flack1dc7ea82014-11-26 13:50:24 -0500217 return_value=False, autospec=True)
David James88e6f032013-03-02 08:13:20 -0800218 self.deploy._MountRootfsAsWritable()
Steven Bennettsca73efa2018-07-10 13:36:56 -0700219 self.assertTrue(self.deploy._root_dir_is_still_readonly.is_set())
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700220
221 def testMountRwFailure(self):
Mike Frysingerf5a3b2d2019-12-12 14:36:17 -0500222 """Test that mount failure raises an exception if check=True."""
David James88e6f032013-03-02 08:13:20 -0800223 self.assertRaises(cros_build_lib.RunCommandError,
Mike Frysingerf5a3b2d2019-12-12 14:36:17 -0500224 self.deploy._MountRootfsAsWritable, check=True)
Steven Bennettsca73efa2018-07-10 13:36:56 -0700225 self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set())
Robert Flack1dc7ea82014-11-26 13:50:24 -0500226
227 def testMountTempDir(self):
228 """Test that mount succeeds if target dir is writable."""
Steven Bennettsca73efa2018-07-10 13:36:56 -0700229 self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set())
Avery Musbachb35e6e32020-03-27 13:35:53 -0700230 self.PatchObject(remote_access.ChromiumOSDevice, 'IsDirWritable',
Robert Flack1dc7ea82014-11-26 13:50:24 -0500231 return_value=True, autospec=True)
232 self.deploy._MountRootfsAsWritable()
Steven Bennettsca73efa2018-07-10 13:36:56 -0700233 self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set())
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700234
235
Anushruth8d797672019-10-17 12:22:31 -0700236class TestMountTarget(DeployTest):
Mike Frysingerdf1d0b02019-11-12 17:44:12 -0500237 """Testing mount and umount command handling."""
Anushruth8d797672019-10-17 12:22:31 -0700238
239 def testMountTargetUmountFailure(self):
240 """Test error being thrown if umount fails.
241
242 Test that 'lsof' is run on mount-dir and 'mount -rbind' command is not run
243 if 'umount' cmd fails.
244 """
245 mount_dir = self.deploy.options.mount_dir
246 target_dir = self.deploy.options.target_dir
247 self.deploy_mock.rsh_mock.AddCmdResult(
248 deploy_chrome._UMOUNT_DIR_IF_MOUNTPOINT_CMD %
249 {'dir': mount_dir}, returncode=errno.EBUSY, stderr='Target is Busy')
250 self.deploy_mock.rsh_mock.AddCmdResult(deploy_chrome.LSOF_COMMAND %
251 (mount_dir,), returncode=0,
252 stdout='process ' + mount_dir)
253 # Check for RunCommandError being thrown.
254 self.assertRaises(cros_build_lib.RunCommandError,
255 self.deploy._MountTarget)
256 # Check for the 'mount -rbind' command not run.
257 self.deploy_mock.rsh_mock.assertCommandContains(
258 (deploy_chrome._BIND_TO_FINAL_DIR_CMD % (target_dir, mount_dir)),
259 expected=False)
260 # Check for lsof command being called.
261 self.deploy_mock.rsh_mock.assertCommandContains(
262 (deploy_chrome.LSOF_COMMAND % (mount_dir,)))
263
264
Ryan Cuief91e702013-02-04 12:06:36 -0800265class TestUiJobStarted(DeployTest):
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700266 """Test detection of a running 'ui' job."""
267
Ryan Cuif2d1a582013-02-19 14:08:13 -0800268 def MockStatusUiCmd(self, **kwargs):
David Haddock3151d912017-10-24 03:50:32 +0000269 self.deploy_mock.rsh_mock.AddCmdResult('status ui', **kwargs)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700270
271 def testUiJobStartedFalse(self):
272 """Correct results with a stopped job."""
Ryan Cuif2d1a582013-02-19 14:08:13 -0800273 self.MockStatusUiCmd(output='ui stop/waiting')
274 self.assertFalse(self.deploy._CheckUiJobStarted())
275
276 def testNoUiJob(self):
277 """Correct results when the job doesn't exist."""
278 self.MockStatusUiCmd(error='start: Unknown job: ui', returncode=1)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700279 self.assertFalse(self.deploy._CheckUiJobStarted())
280
281 def testCheckRootfsWriteableTrue(self):
282 """Correct results with a running job."""
Ryan Cuif2d1a582013-02-19 14:08:13 -0800283 self.MockStatusUiCmd(output='ui start/running, process 297')
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700284 self.assertTrue(self.deploy._CheckUiJobStarted())
285
286
Ryan Cuief91e702013-02-04 12:06:36 -0800287class StagingTest(cros_test_lib.MockTempDirTestCase):
288 """Test user-mode and ebuild-mode staging functionality."""
289
290 def setUp(self):
Ryan Cuief91e702013-02-04 12:06:36 -0800291 self.staging_dir = os.path.join(self.tempdir, 'staging')
292 self.build_dir = os.path.join(self.tempdir, 'build_dir')
Avery Musbachb35e6e32020-03-27 13:35:53 -0700293 self.common_flags = ['--board', _TARGET_BOARD,
294 '--build-dir', self.build_dir, '--staging-only',
295 '--cache-dir', self.tempdir]
Ryan Cuia0215a72013-02-14 16:20:45 -0800296 self.sdk_mock = self.StartPatcher(cros_chrome_sdk_unittest.SDKFetcherMock())
Ryan Cui686ec052013-02-12 16:39:41 -0800297 self.PatchObject(
298 osutils, 'SourceEnvironment', autospec=True,
299 return_value={'STRIP': 'x86_64-cros-linux-gnu-strip'})
Ryan Cuief91e702013-02-04 12:06:36 -0800300
David Jamesa6e08892013-03-01 13:34:11 -0800301 def testSingleFileDeployFailure(self):
302 """Default staging enforces that mandatory files are copied"""
Mike Frysingerc3061a62015-06-04 04:16:18 -0400303 options = _ParseCommandLine(self.common_flags)
David Jamesa6e08892013-03-01 13:34:11 -0800304 osutils.Touch(os.path.join(self.build_dir, 'chrome'), makedirs=True)
305 self.assertRaises(
306 chrome_util.MissingPathError, deploy_chrome._PrepareStagingDir,
Daniel Eratc89829c2014-05-12 17:24:21 -0700307 options, self.tempdir, self.staging_dir, chrome_util._COPY_PATHS_CHROME)
Ryan Cuief91e702013-02-04 12:06:36 -0800308
David Jamesa6e08892013-03-01 13:34:11 -0800309 def testSloppyDeployFailure(self):
310 """Sloppy staging enforces that at least one file is copied."""
Mike Frysingerc3061a62015-06-04 04:16:18 -0400311 options = _ParseCommandLine(self.common_flags + ['--sloppy'])
David Jamesa6e08892013-03-01 13:34:11 -0800312 self.assertRaises(
313 chrome_util.MissingPathError, deploy_chrome._PrepareStagingDir,
Daniel Eratc89829c2014-05-12 17:24:21 -0700314 options, self.tempdir, self.staging_dir, chrome_util._COPY_PATHS_CHROME)
David Jamesa6e08892013-03-01 13:34:11 -0800315
316 def testSloppyDeploySuccess(self):
317 """Sloppy staging - stage one file."""
Mike Frysingerc3061a62015-06-04 04:16:18 -0400318 options = _ParseCommandLine(self.common_flags + ['--sloppy'])
David Jamesa6e08892013-03-01 13:34:11 -0800319 osutils.Touch(os.path.join(self.build_dir, 'chrome'), makedirs=True)
Steve Funge984a532013-11-25 17:09:25 -0800320 deploy_chrome._PrepareStagingDir(options, self.tempdir, self.staging_dir,
Daniel Eratc89829c2014-05-12 17:24:21 -0700321 chrome_util._COPY_PATHS_CHROME)
David Jamesa6e08892013-03-01 13:34:11 -0800322
Steve Funge984a532013-11-25 17:09:25 -0800323
324class DeployTestBuildDir(cros_test_lib.MockTempDirTestCase):
Daniel Erat1ae46382014-08-14 10:23:39 -0700325 """Set up a deploy object with a build-dir for use in deployment type tests"""
Steve Funge984a532013-11-25 17:09:25 -0800326
327 def _GetDeployChrome(self, args):
Mike Frysingerc3061a62015-06-04 04:16:18 -0400328 options = _ParseCommandLine(args)
Steve Funge984a532013-11-25 17:09:25 -0800329 return deploy_chrome.DeployChrome(
330 options, self.tempdir, os.path.join(self.tempdir, 'staging'))
331
332 def setUp(self):
333 self.staging_dir = os.path.join(self.tempdir, 'staging')
334 self.build_dir = os.path.join(self.tempdir, 'build_dir')
335 self.deploy_mock = self.StartPatcher(DeployChromeMock())
336 self.deploy = self._GetDeployChrome(
Avery Musbachb35e6e32020-03-27 13:35:53 -0700337 list(_REGULAR_TO) + ['--board', _TARGET_BOARD,
338 '--build-dir', self.build_dir, '--staging-only',
339 '--cache-dir', self.tempdir, '--sloppy'])
Steve Funge984a532013-11-25 17:09:25 -0800340
Daniel Erat1ae46382014-08-14 10:23:39 -0700341 def getCopyPath(self, source_path):
342 """Return a chrome_util.Path or None if not present."""
343 paths = [p for p in self.deploy.copy_paths if p.src == source_path]
344 return paths[0] if paths else None
Steve Funge984a532013-11-25 17:09:25 -0800345
Daniel Erat1ae46382014-08-14 10:23:39 -0700346class TestDeploymentType(DeployTestBuildDir):
Steve Funge984a532013-11-25 17:09:25 -0800347 """Test detection of deployment type using build dir."""
348
Daniel Erat1ae46382014-08-14 10:23:39 -0700349 def testAppShellDetection(self):
350 """Check for an app_shell deployment"""
351 osutils.Touch(os.path.join(self.deploy.options.build_dir, 'app_shell'),
Steve Funge984a532013-11-25 17:09:25 -0800352 makedirs=True)
353 self.deploy._CheckDeployType()
Daniel Erat1ae46382014-08-14 10:23:39 -0700354 self.assertTrue(self.getCopyPath('app_shell'))
355 self.assertFalse(self.getCopyPath('chrome'))
Steve Funge984a532013-11-25 17:09:25 -0800356
Daniel Erat1ae46382014-08-14 10:23:39 -0700357 def testChromeAndAppShellDetection(self):
Daniel Eratf53bd3a2016-12-02 11:28:36 -0700358 """Check for a chrome deployment when app_shell also exists."""
Steve Fung63d705d2014-03-16 03:14:03 -0700359 osutils.Touch(os.path.join(self.deploy.options.build_dir, 'chrome'),
360 makedirs=True)
Daniel Erat1ae46382014-08-14 10:23:39 -0700361 osutils.Touch(os.path.join(self.deploy.options.build_dir, 'app_shell'),
Steve Fung63d705d2014-03-16 03:14:03 -0700362 makedirs=True)
363 self.deploy._CheckDeployType()
Daniel Erat1ae46382014-08-14 10:23:39 -0700364 self.assertTrue(self.getCopyPath('chrome'))
Daniel Erat9813f0e2014-11-12 11:00:28 -0700365 self.assertFalse(self.getCopyPath('app_shell'))
Steve Fung63d705d2014-03-16 03:14:03 -0700366
Steve Funge984a532013-11-25 17:09:25 -0800367 def testChromeDetection(self):
368 """Check for a regular chrome deployment"""
369 osutils.Touch(os.path.join(self.deploy.options.build_dir, 'chrome'),
370 makedirs=True)
371 self.deploy._CheckDeployType()
Daniel Erat1ae46382014-08-14 10:23:39 -0700372 self.assertTrue(self.getCopyPath('chrome'))
Daniel Erat9813f0e2014-11-12 11:00:28 -0700373 self.assertFalse(self.getCopyPath('app_shell'))