blob: b8c784a28f510c3ab7a64b6af64841688e9bd344 [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 Musbach3edff0e2020-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 Musbach3edff0e2020-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 Musbach3edff0e2020-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 Musbach3edff0e2020-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 Musbach3edff0e2020-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 Musbach3edff0e2020-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 Musbach3edff0e2020-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 Musbach3edff0e2020-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 Musbach3edff0e2020-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 Musbach3edff0e2020-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 Musbach3edff0e2020-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 Musbach3edff0e2020-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.deploy._CheckBoard()
168 self.deploy.options.force = False
169 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.deploy._CheckBoard()
176 self.deploy.options.force = False
177 self.PatchObject(cros_build_lib, 'BooleanPrompt', return_value=True)
178 self.deploy._CheckBoard()
179 self.PatchObject(cros_build_lib, 'BooleanPrompt', return_value=False)
180 self.assertRaises(deploy_chrome.DeployFailure, self.deploy._CheckBoard)
181
182
David James88e6f032013-03-02 08:13:20 -0800183class TestDisableRootfsVerification(DeployTest):
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700184 """Testing disabling of rootfs verification and RO mode."""
185
David James88e6f032013-03-02 08:13:20 -0800186 def testDisableRootfsVerificationSuccess(self):
187 """Test the working case, disabling rootfs verification."""
188 self.deploy_mock.MockMountCmd(0)
189 self.deploy._DisableRootfsVerification()
Steven Bennettsca73efa2018-07-10 13:36:56 -0700190 self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set())
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700191
192 def testDisableRootfsVerificationFailure(self):
193 """Test failure to disable rootfs verification."""
Mike Frysinger27e21b72018-07-12 14:20:21 -0400194 # pylint: disable=unused-argument
Shuqian Zhao14e61092017-11-17 00:02:16 +0000195 def RaiseRunCommandError(timeout_sec=None):
Mike Frysinger929f3ba2019-09-12 03:24:59 -0400196 raise cros_build_lib.RunCommandError('Mock RunCommandError')
Luigi Semenzato1bc79b22016-11-22 16:32:17 -0800197 self.remote_reboot_mock.side_effect = RaiseRunCommandError
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700198 self.assertRaises(cros_build_lib.RunCommandError,
David James88e6f032013-03-02 08:13:20 -0800199 self.deploy._DisableRootfsVerification)
Luigi Semenzato1bc79b22016-11-22 16:32:17 -0800200 self.remote_reboot_mock.side_effect = None
Steven Bennettsca73efa2018-07-10 13:36:56 -0700201 self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set())
David James88e6f032013-03-02 08:13:20 -0800202
203
204class TestMount(DeployTest):
205 """Testing mount success and failure."""
206
207 def testSuccess(self):
208 """Test case where we are able to mount as writable."""
Steven Bennettsca73efa2018-07-10 13:36:56 -0700209 self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set())
David James88e6f032013-03-02 08:13:20 -0800210 self.deploy_mock.MockMountCmd(0)
211 self.deploy._MountRootfsAsWritable()
Steven Bennettsca73efa2018-07-10 13:36:56 -0700212 self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set())
David James88e6f032013-03-02 08:13:20 -0800213
214 def testMountError(self):
215 """Test that mount failure doesn't raise an exception by default."""
Steven Bennettsca73efa2018-07-10 13:36:56 -0700216 self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set())
Avery Musbach3edff0e2020-03-27 13:35:53 -0700217 self.PatchObject(remote_access.ChromiumOSDevice, 'IsDirWritable',
Robert Flack1dc7ea82014-11-26 13:50:24 -0500218 return_value=False, autospec=True)
David James88e6f032013-03-02 08:13:20 -0800219 self.deploy._MountRootfsAsWritable()
Steven Bennettsca73efa2018-07-10 13:36:56 -0700220 self.assertTrue(self.deploy._root_dir_is_still_readonly.is_set())
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700221
222 def testMountRwFailure(self):
Mike Frysingerf5a3b2d2019-12-12 14:36:17 -0500223 """Test that mount failure raises an exception if check=True."""
David James88e6f032013-03-02 08:13:20 -0800224 self.assertRaises(cros_build_lib.RunCommandError,
Mike Frysingerf5a3b2d2019-12-12 14:36:17 -0500225 self.deploy._MountRootfsAsWritable, check=True)
Steven Bennettsca73efa2018-07-10 13:36:56 -0700226 self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set())
Robert Flack1dc7ea82014-11-26 13:50:24 -0500227
228 def testMountTempDir(self):
229 """Test that mount succeeds if target dir is writable."""
Steven Bennettsca73efa2018-07-10 13:36:56 -0700230 self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set())
Avery Musbach3edff0e2020-03-27 13:35:53 -0700231 self.PatchObject(remote_access.ChromiumOSDevice, 'IsDirWritable',
Robert Flack1dc7ea82014-11-26 13:50:24 -0500232 return_value=True, autospec=True)
233 self.deploy._MountRootfsAsWritable()
Steven Bennettsca73efa2018-07-10 13:36:56 -0700234 self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set())
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700235
236
Anushruth8d797672019-10-17 12:22:31 -0700237class TestMountTarget(DeployTest):
Mike Frysingerdf1d0b02019-11-12 17:44:12 -0500238 """Testing mount and umount command handling."""
Anushruth8d797672019-10-17 12:22:31 -0700239
240 def testMountTargetUmountFailure(self):
241 """Test error being thrown if umount fails.
242
243 Test that 'lsof' is run on mount-dir and 'mount -rbind' command is not run
244 if 'umount' cmd fails.
245 """
246 mount_dir = self.deploy.options.mount_dir
247 target_dir = self.deploy.options.target_dir
248 self.deploy_mock.rsh_mock.AddCmdResult(
249 deploy_chrome._UMOUNT_DIR_IF_MOUNTPOINT_CMD %
250 {'dir': mount_dir}, returncode=errno.EBUSY, stderr='Target is Busy')
251 self.deploy_mock.rsh_mock.AddCmdResult(deploy_chrome.LSOF_COMMAND %
252 (mount_dir,), returncode=0,
253 stdout='process ' + mount_dir)
254 # Check for RunCommandError being thrown.
255 self.assertRaises(cros_build_lib.RunCommandError,
256 self.deploy._MountTarget)
257 # Check for the 'mount -rbind' command not run.
258 self.deploy_mock.rsh_mock.assertCommandContains(
259 (deploy_chrome._BIND_TO_FINAL_DIR_CMD % (target_dir, mount_dir)),
260 expected=False)
261 # Check for lsof command being called.
262 self.deploy_mock.rsh_mock.assertCommandContains(
263 (deploy_chrome.LSOF_COMMAND % (mount_dir,)))
264
265
Ryan Cuief91e702013-02-04 12:06:36 -0800266class TestUiJobStarted(DeployTest):
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700267 """Test detection of a running 'ui' job."""
268
Ryan Cuif2d1a582013-02-19 14:08:13 -0800269 def MockStatusUiCmd(self, **kwargs):
David Haddock3151d912017-10-24 03:50:32 +0000270 self.deploy_mock.rsh_mock.AddCmdResult('status ui', **kwargs)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700271
272 def testUiJobStartedFalse(self):
273 """Correct results with a stopped job."""
Ryan Cuif2d1a582013-02-19 14:08:13 -0800274 self.MockStatusUiCmd(output='ui stop/waiting')
275 self.assertFalse(self.deploy._CheckUiJobStarted())
276
277 def testNoUiJob(self):
278 """Correct results when the job doesn't exist."""
279 self.MockStatusUiCmd(error='start: Unknown job: ui', returncode=1)
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700280 self.assertFalse(self.deploy._CheckUiJobStarted())
281
282 def testCheckRootfsWriteableTrue(self):
283 """Correct results with a running job."""
Ryan Cuif2d1a582013-02-19 14:08:13 -0800284 self.MockStatusUiCmd(output='ui start/running, process 297')
Ryan Cuiafd6c5c2012-07-30 17:48:22 -0700285 self.assertTrue(self.deploy._CheckUiJobStarted())
286
287
Ryan Cuief91e702013-02-04 12:06:36 -0800288class StagingTest(cros_test_lib.MockTempDirTestCase):
289 """Test user-mode and ebuild-mode staging functionality."""
290
291 def setUp(self):
Ryan Cuief91e702013-02-04 12:06:36 -0800292 self.staging_dir = os.path.join(self.tempdir, 'staging')
293 self.build_dir = os.path.join(self.tempdir, 'build_dir')
Avery Musbach3edff0e2020-03-27 13:35:53 -0700294 self.common_flags = ['--board', _TARGET_BOARD,
295 '--build-dir', self.build_dir, '--staging-only',
296 '--cache-dir', self.tempdir]
Ryan Cuia0215a72013-02-14 16:20:45 -0800297 self.sdk_mock = self.StartPatcher(cros_chrome_sdk_unittest.SDKFetcherMock())
Ryan Cui686ec052013-02-12 16:39:41 -0800298 self.PatchObject(
299 osutils, 'SourceEnvironment', autospec=True,
300 return_value={'STRIP': 'x86_64-cros-linux-gnu-strip'})
Ryan Cuief91e702013-02-04 12:06:36 -0800301
David Jamesa6e08892013-03-01 13:34:11 -0800302 def testSingleFileDeployFailure(self):
303 """Default staging enforces that mandatory files are copied"""
Mike Frysingerc3061a62015-06-04 04:16:18 -0400304 options = _ParseCommandLine(self.common_flags)
David Jamesa6e08892013-03-01 13:34:11 -0800305 osutils.Touch(os.path.join(self.build_dir, 'chrome'), makedirs=True)
306 self.assertRaises(
307 chrome_util.MissingPathError, deploy_chrome._PrepareStagingDir,
Daniel Eratc89829c2014-05-12 17:24:21 -0700308 options, self.tempdir, self.staging_dir, chrome_util._COPY_PATHS_CHROME)
Ryan Cuief91e702013-02-04 12:06:36 -0800309
David Jamesa6e08892013-03-01 13:34:11 -0800310 def testSloppyDeployFailure(self):
311 """Sloppy staging enforces that at least one file is copied."""
Mike Frysingerc3061a62015-06-04 04:16:18 -0400312 options = _ParseCommandLine(self.common_flags + ['--sloppy'])
David Jamesa6e08892013-03-01 13:34:11 -0800313 self.assertRaises(
314 chrome_util.MissingPathError, deploy_chrome._PrepareStagingDir,
Daniel Eratc89829c2014-05-12 17:24:21 -0700315 options, self.tempdir, self.staging_dir, chrome_util._COPY_PATHS_CHROME)
David Jamesa6e08892013-03-01 13:34:11 -0800316
317 def testSloppyDeploySuccess(self):
318 """Sloppy staging - stage one file."""
Mike Frysingerc3061a62015-06-04 04:16:18 -0400319 options = _ParseCommandLine(self.common_flags + ['--sloppy'])
David Jamesa6e08892013-03-01 13:34:11 -0800320 osutils.Touch(os.path.join(self.build_dir, 'chrome'), makedirs=True)
Steve Funge984a532013-11-25 17:09:25 -0800321 deploy_chrome._PrepareStagingDir(options, self.tempdir, self.staging_dir,
Daniel Eratc89829c2014-05-12 17:24:21 -0700322 chrome_util._COPY_PATHS_CHROME)
David Jamesa6e08892013-03-01 13:34:11 -0800323
Steve Funge984a532013-11-25 17:09:25 -0800324
325class DeployTestBuildDir(cros_test_lib.MockTempDirTestCase):
Daniel Erat1ae46382014-08-14 10:23:39 -0700326 """Set up a deploy object with a build-dir for use in deployment type tests"""
Steve Funge984a532013-11-25 17:09:25 -0800327
328 def _GetDeployChrome(self, args):
Mike Frysingerc3061a62015-06-04 04:16:18 -0400329 options = _ParseCommandLine(args)
Steve Funge984a532013-11-25 17:09:25 -0800330 return deploy_chrome.DeployChrome(
331 options, self.tempdir, os.path.join(self.tempdir, 'staging'))
332
333 def setUp(self):
334 self.staging_dir = os.path.join(self.tempdir, 'staging')
335 self.build_dir = os.path.join(self.tempdir, 'build_dir')
336 self.deploy_mock = self.StartPatcher(DeployChromeMock())
337 self.deploy = self._GetDeployChrome(
Avery Musbach3edff0e2020-03-27 13:35:53 -0700338 list(_REGULAR_TO) + ['--board', _TARGET_BOARD,
339 '--build-dir', self.build_dir, '--staging-only',
340 '--cache-dir', self.tempdir, '--sloppy'])
Steve Funge984a532013-11-25 17:09:25 -0800341
Daniel Erat1ae46382014-08-14 10:23:39 -0700342 def getCopyPath(self, source_path):
343 """Return a chrome_util.Path or None if not present."""
344 paths = [p for p in self.deploy.copy_paths if p.src == source_path]
345 return paths[0] if paths else None
Steve Funge984a532013-11-25 17:09:25 -0800346
Daniel Erat1ae46382014-08-14 10:23:39 -0700347class TestDeploymentType(DeployTestBuildDir):
Steve Funge984a532013-11-25 17:09:25 -0800348 """Test detection of deployment type using build dir."""
349
Daniel Erat1ae46382014-08-14 10:23:39 -0700350 def testAppShellDetection(self):
351 """Check for an app_shell deployment"""
352 osutils.Touch(os.path.join(self.deploy.options.build_dir, 'app_shell'),
Steve Funge984a532013-11-25 17:09:25 -0800353 makedirs=True)
354 self.deploy._CheckDeployType()
Daniel Erat1ae46382014-08-14 10:23:39 -0700355 self.assertTrue(self.getCopyPath('app_shell'))
356 self.assertFalse(self.getCopyPath('chrome'))
Steve Funge984a532013-11-25 17:09:25 -0800357
Daniel Erat1ae46382014-08-14 10:23:39 -0700358 def testChromeAndAppShellDetection(self):
Daniel Eratf53bd3a2016-12-02 11:28:36 -0700359 """Check for a chrome deployment when app_shell also exists."""
Steve Fung63d705d2014-03-16 03:14:03 -0700360 osutils.Touch(os.path.join(self.deploy.options.build_dir, 'chrome'),
361 makedirs=True)
Daniel Erat1ae46382014-08-14 10:23:39 -0700362 osutils.Touch(os.path.join(self.deploy.options.build_dir, 'app_shell'),
Steve Fung63d705d2014-03-16 03:14:03 -0700363 makedirs=True)
364 self.deploy._CheckDeployType()
Daniel Erat1ae46382014-08-14 10:23:39 -0700365 self.assertTrue(self.getCopyPath('chrome'))
Daniel Erat9813f0e2014-11-12 11:00:28 -0700366 self.assertFalse(self.getCopyPath('app_shell'))
Steve Fung63d705d2014-03-16 03:14:03 -0700367
Steve Funge984a532013-11-25 17:09:25 -0800368 def testChromeDetection(self):
369 """Check for a regular chrome deployment"""
370 osutils.Touch(os.path.join(self.deploy.options.build_dir, 'chrome'),
371 makedirs=True)
372 self.deploy._CheckDeployType()
Daniel Erat1ae46382014-08-14 10:23:39 -0700373 self.assertTrue(self.getCopyPath('chrome'))
Daniel Erat9813f0e2014-11-12 11:00:28 -0700374 self.assertFalse(self.getCopyPath('app_shell'))