Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 2 | # 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 Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 6 | """Unit tests for the deploy_chrome script.""" |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 7 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 8 | from __future__ import print_function |
| 9 | |
Mike Frysinger | ea838d1 | 2014-12-08 11:55:32 -0500 | [diff] [blame] | 10 | import mock |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 11 | import os |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 12 | import time |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 13 | |
David Pursell | cfd5887 | 2015-03-19 09:15:48 -0700 | [diff] [blame] | 14 | from chromite.cli.cros import cros_chrome_sdk_unittest |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 15 | from chromite.lib import chrome_util |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 16 | from chromite.lib import cros_build_lib |
| 17 | from chromite.lib import cros_test_lib |
Ryan Cui | 686ec05 | 2013-02-12 16:39:41 -0800 | [diff] [blame] | 18 | from chromite.lib import osutils |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 19 | from chromite.lib import partial_mock |
Robert Flack | 1dc7ea8 | 2014-11-26 13:50:24 -0500 | [diff] [blame] | 20 | from chromite.lib import remote_access |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 21 | from chromite.lib import remote_access_unittest |
| 22 | from chromite.scripts import deploy_chrome |
| 23 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 24 | |
Mike Frysinger | 27e21b7 | 2018-07-12 14:20:21 -0400 | [diff] [blame] | 25 | # pylint: disable=protected-access |
| 26 | |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 27 | |
| 28 | _REGULAR_TO = ('--to', 'monkey') |
| 29 | _GS_PATH = 'gs://foon' |
| 30 | |
| 31 | |
| 32 | def _ParseCommandLine(argv): |
| 33 | return deploy_chrome._ParseCommandLine(['--log-level', 'debug'] + argv) |
| 34 | |
| 35 | |
| 36 | class InterfaceTest(cros_test_lib.OutputTestCase): |
| 37 | """Tests the commandline interface of the script.""" |
| 38 | |
Bernie Thompson | 93b9ee6 | 2018-02-21 14:56:16 -0800 | [diff] [blame] | 39 | BOARD = 'eve' |
Ryan Cui | 686ec05 | 2013-02-12 16:39:41 -0800 | [diff] [blame] | 40 | |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 41 | 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 Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 54 | argv = list(_REGULAR_TO) + ['--local-pkg-path', '/path/to/chrome'] |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 55 | _ParseCommandLine(argv) |
| 56 | |
| 57 | def testNoTarget(self): |
| 58 | """Test no target specified.""" |
| 59 | argv = ['--gs-path', _GS_PATH] |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 60 | self.assertParseError(argv) |
| 61 | |
| 62 | def assertParseError(self, argv): |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 63 | with self.OutputCapturer(): |
| 64 | self.assertRaises2(SystemExit, _ParseCommandLine, argv, |
| 65 | check_attrs={'code': 2}) |
| 66 | |
Achuith Bhandarkar | 31a3eb0 | 2018-03-22 16:33:48 -0700 | [diff] [blame] | 67 | def testNoBoard(self): |
| 68 | """Test cases where --board is not specified.""" |
Ryan Cui | 686ec05 | 2013-02-12 16:39:41 -0800 | [diff] [blame] | 69 | argv = ['--staging-only', '--build-dir=/path/to/nowhere'] |
| 70 | self.assertParseError(argv) |
| 71 | |
Achuith Bhandarkar | 31a3eb0 | 2018-03-22 16:33:48 -0700 | [diff] [blame] | 72 | # Don't need --board if no stripping is necessary. |
| 73 | argv_nostrip = argv + ['--nostrip'] |
| 74 | _ParseCommandLine(argv_nostrip) |
| 75 | |
| 76 | # Don't need --board if strip binary is provided. |
| 77 | argv_strip_bin = argv + ['--strip-bin', 'strip.bin'] |
| 78 | _ParseCommandLine(argv_strip_bin) |
| 79 | |
Thiago Goncales | 1279331 | 2013-05-23 11:26:17 -0700 | [diff] [blame] | 80 | def testMountOptionSetsTargetDir(self): |
| 81 | argv = list(_REGULAR_TO) + ['--gs-path', _GS_PATH, '--mount'] |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 82 | options = _ParseCommandLine(argv) |
Thiago Goncales | 1279331 | 2013-05-23 11:26:17 -0700 | [diff] [blame] | 83 | self.assertIsNot(options.target_dir, None) |
| 84 | |
| 85 | def testMountOptionSetsMountDir(self): |
| 86 | argv = list(_REGULAR_TO) + ['--gs-path', _GS_PATH, '--mount'] |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 87 | options = _ParseCommandLine(argv) |
Thiago Goncales | 1279331 | 2013-05-23 11:26:17 -0700 | [diff] [blame] | 88 | self.assertIsNot(options.mount_dir, None) |
| 89 | |
| 90 | def testMountOptionDoesNotOverrideTargetDir(self): |
| 91 | argv = list(_REGULAR_TO) + ['--gs-path', _GS_PATH, '--mount', |
| 92 | '--target-dir', '/foo/bar/cow'] |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 93 | options = _ParseCommandLine(argv) |
Thiago Goncales | 1279331 | 2013-05-23 11:26:17 -0700 | [diff] [blame] | 94 | self.assertEqual(options.target_dir, '/foo/bar/cow') |
| 95 | |
| 96 | def testMountOptionDoesNotOverrideMountDir(self): |
| 97 | argv = list(_REGULAR_TO) + ['--gs-path', _GS_PATH, '--mount', |
| 98 | '--mount-dir', '/foo/bar/cow'] |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 99 | options = _ParseCommandLine(argv) |
Thiago Goncales | 1279331 | 2013-05-23 11:26:17 -0700 | [diff] [blame] | 100 | self.assertEqual(options.mount_dir, '/foo/bar/cow') |
| 101 | |
Adrian Elder | a2c548a | 2017-11-07 19:01:29 -0500 | [diff] [blame] | 102 | def testSshIdentityOptionSetsOption(self): |
| 103 | argv = list(_REGULAR_TO) + ['--private-key', '/foo/bar/key', |
| 104 | '--board', 'cedar', |
Bernie Thompson | 93b9ee6 | 2018-02-21 14:56:16 -0800 | [diff] [blame] | 105 | '--build-dir', '/path/to/nowhere'] |
Adrian Elder | a2c548a | 2017-11-07 19:01:29 -0500 | [diff] [blame] | 106 | options = _ParseCommandLine(argv) |
| 107 | self.assertEqual(options.private_key, '/foo/bar/key') |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 108 | |
| 109 | class DeployChromeMock(partial_mock.PartialMock): |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 110 | """Deploy Chrome Mock Class.""" |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 111 | |
| 112 | TARGET = 'chromite.scripts.deploy_chrome.DeployChrome' |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 113 | ATTRS = ('_KillProcsIfNeeded', '_DisableRootfsVerification') |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 114 | |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 115 | def __init__(self): |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 116 | partial_mock.PartialMock.__init__(self) |
Robert Flack | 1dc7ea8 | 2014-11-26 13:50:24 -0500 | [diff] [blame] | 117 | self.remote_device_mock = remote_access_unittest.RemoteDeviceMock() |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 118 | # Target starts off as having rootfs verification enabled. |
Ryan Cui | e18f24f | 2012-12-03 18:39:55 -0800 | [diff] [blame] | 119 | self.rsh_mock = remote_access_unittest.RemoteShMock() |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 120 | self.rsh_mock.SetDefaultCmdResult(0) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 121 | self.MockMountCmd(1) |
David Haddock | 3151d91 | 2017-10-24 03:50:32 +0000 | [diff] [blame] | 122 | self.rsh_mock.AddCmdResult( |
| 123 | deploy_chrome.LSOF_COMMAND % (deploy_chrome._CHROME_DIR,), 1) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 124 | |
| 125 | def MockMountCmd(self, returnvalue): |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 126 | self.rsh_mock.AddCmdResult(deploy_chrome.MOUNT_RW_COMMAND, |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 127 | returnvalue) |
| 128 | |
| 129 | def _DisableRootfsVerification(self, inst): |
| 130 | with mock.patch.object(time, 'sleep'): |
| 131 | self.backup['_DisableRootfsVerification'](inst) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 132 | |
Ryan Cui | 4d6fca9 | 2012-12-13 16:41:56 -0800 | [diff] [blame] | 133 | def PreStart(self): |
Robert Flack | 1dc7ea8 | 2014-11-26 13:50:24 -0500 | [diff] [blame] | 134 | self.remote_device_mock.start() |
Ryan Cui | 4d6fca9 | 2012-12-13 16:41:56 -0800 | [diff] [blame] | 135 | self.rsh_mock.start() |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 136 | |
Ryan Cui | 4d6fca9 | 2012-12-13 16:41:56 -0800 | [diff] [blame] | 137 | def PreStop(self): |
| 138 | self.rsh_mock.stop() |
Robert Flack | 1dc7ea8 | 2014-11-26 13:50:24 -0500 | [diff] [blame] | 139 | self.remote_device_mock.stop() |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 140 | |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 141 | def _KillProcsIfNeeded(self, _inst): |
| 142 | # Fully stub out for now. |
| 143 | pass |
| 144 | |
| 145 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 146 | class DeployTest(cros_test_lib.MockTempDirTestCase): |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 147 | """Setup a deploy object with a GS-path for use in tests.""" |
| 148 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 149 | def _GetDeployChrome(self, args): |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 150 | options = _ParseCommandLine(args) |
Ryan Cui | a56a71e | 2012-10-18 18:40:35 -0700 | [diff] [blame] | 151 | return deploy_chrome.DeployChrome( |
| 152 | options, self.tempdir, os.path.join(self.tempdir, 'staging')) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 153 | |
| 154 | def setUp(self): |
Ryan Cui | f1416f3 | 2013-01-22 18:43:41 -0800 | [diff] [blame] | 155 | self.deploy_mock = self.StartPatcher(DeployChromeMock()) |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 156 | self.deploy = self._GetDeployChrome( |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 157 | list(_REGULAR_TO) + ['--gs-path', _GS_PATH, '--force']) |
Luigi Semenzato | 1bc79b2 | 2016-11-22 16:32:17 -0800 | [diff] [blame] | 158 | self.remote_reboot_mock = \ |
| 159 | self.PatchObject(remote_access.RemoteAccess, 'RemoteReboot', |
| 160 | return_value=True) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 161 | |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 162 | class TestDisableRootfsVerification(DeployTest): |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 163 | """Testing disabling of rootfs verification and RO mode.""" |
| 164 | |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 165 | def testDisableRootfsVerificationSuccess(self): |
| 166 | """Test the working case, disabling rootfs verification.""" |
| 167 | self.deploy_mock.MockMountCmd(0) |
| 168 | self.deploy._DisableRootfsVerification() |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 169 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 170 | |
| 171 | def testDisableRootfsVerificationFailure(self): |
| 172 | """Test failure to disable rootfs verification.""" |
Mike Frysinger | 27e21b7 | 2018-07-12 14:20:21 -0400 | [diff] [blame] | 173 | # pylint: disable=unused-argument |
Shuqian Zhao | 14e6109 | 2017-11-17 00:02:16 +0000 | [diff] [blame] | 174 | def RaiseRunCommandError(timeout_sec=None): |
Luigi Semenzato | 1bc79b2 | 2016-11-22 16:32:17 -0800 | [diff] [blame] | 175 | raise cros_build_lib.RunCommandError('Mock RunCommandError', 0) |
| 176 | self.remote_reboot_mock.side_effect = RaiseRunCommandError |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 177 | self.assertRaises(cros_build_lib.RunCommandError, |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 178 | self.deploy._DisableRootfsVerification) |
Luigi Semenzato | 1bc79b2 | 2016-11-22 16:32:17 -0800 | [diff] [blame] | 179 | self.remote_reboot_mock.side_effect = None |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 180 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 181 | |
| 182 | |
| 183 | class TestMount(DeployTest): |
| 184 | """Testing mount success and failure.""" |
| 185 | |
| 186 | def testSuccess(self): |
| 187 | """Test case where we are able to mount as writable.""" |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 188 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 189 | self.deploy_mock.MockMountCmd(0) |
| 190 | self.deploy._MountRootfsAsWritable() |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 191 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 192 | |
| 193 | def testMountError(self): |
| 194 | """Test that mount failure doesn't raise an exception by default.""" |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 195 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
Mike Frysinger | 74ccd57 | 2015-05-21 21:18:20 -0400 | [diff] [blame] | 196 | self.PatchObject(remote_access.RemoteDevice, 'IsDirWritable', |
Robert Flack | 1dc7ea8 | 2014-11-26 13:50:24 -0500 | [diff] [blame] | 197 | return_value=False, autospec=True) |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 198 | self.deploy._MountRootfsAsWritable() |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 199 | self.assertTrue(self.deploy._root_dir_is_still_readonly.is_set()) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 200 | |
| 201 | def testMountRwFailure(self): |
David James | 88e6f03 | 2013-03-02 08:13:20 -0800 | [diff] [blame] | 202 | """Test that mount failure raises an exception if error_code_ok=False.""" |
| 203 | self.assertRaises(cros_build_lib.RunCommandError, |
| 204 | self.deploy._MountRootfsAsWritable, error_code_ok=False) |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 205 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
Robert Flack | 1dc7ea8 | 2014-11-26 13:50:24 -0500 | [diff] [blame] | 206 | |
| 207 | def testMountTempDir(self): |
| 208 | """Test that mount succeeds if target dir is writable.""" |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 209 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
Mike Frysinger | 74ccd57 | 2015-05-21 21:18:20 -0400 | [diff] [blame] | 210 | self.PatchObject(remote_access.RemoteDevice, 'IsDirWritable', |
Robert Flack | 1dc7ea8 | 2014-11-26 13:50:24 -0500 | [diff] [blame] | 211 | return_value=True, autospec=True) |
| 212 | self.deploy._MountRootfsAsWritable() |
Steven Bennetts | ca73efa | 2018-07-10 13:36:56 -0700 | [diff] [blame] | 213 | self.assertFalse(self.deploy._root_dir_is_still_readonly.is_set()) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 214 | |
| 215 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 216 | class TestUiJobStarted(DeployTest): |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 217 | """Test detection of a running 'ui' job.""" |
| 218 | |
Ryan Cui | f2d1a58 | 2013-02-19 14:08:13 -0800 | [diff] [blame] | 219 | def MockStatusUiCmd(self, **kwargs): |
David Haddock | 3151d91 | 2017-10-24 03:50:32 +0000 | [diff] [blame] | 220 | self.deploy_mock.rsh_mock.AddCmdResult('status ui', **kwargs) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 221 | |
| 222 | def testUiJobStartedFalse(self): |
| 223 | """Correct results with a stopped job.""" |
Ryan Cui | f2d1a58 | 2013-02-19 14:08:13 -0800 | [diff] [blame] | 224 | self.MockStatusUiCmd(output='ui stop/waiting') |
| 225 | self.assertFalse(self.deploy._CheckUiJobStarted()) |
| 226 | |
| 227 | def testNoUiJob(self): |
| 228 | """Correct results when the job doesn't exist.""" |
| 229 | self.MockStatusUiCmd(error='start: Unknown job: ui', returncode=1) |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 230 | self.assertFalse(self.deploy._CheckUiJobStarted()) |
| 231 | |
| 232 | def testCheckRootfsWriteableTrue(self): |
| 233 | """Correct results with a running job.""" |
Ryan Cui | f2d1a58 | 2013-02-19 14:08:13 -0800 | [diff] [blame] | 234 | self.MockStatusUiCmd(output='ui start/running, process 297') |
Ryan Cui | afd6c5c | 2012-07-30 17:48:22 -0700 | [diff] [blame] | 235 | self.assertTrue(self.deploy._CheckUiJobStarted()) |
| 236 | |
| 237 | |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 238 | class StagingTest(cros_test_lib.MockTempDirTestCase): |
| 239 | """Test user-mode and ebuild-mode staging functionality.""" |
| 240 | |
| 241 | def setUp(self): |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 242 | self.staging_dir = os.path.join(self.tempdir, 'staging') |
| 243 | self.build_dir = os.path.join(self.tempdir, 'build_dir') |
Ryan Cui | 686ec05 | 2013-02-12 16:39:41 -0800 | [diff] [blame] | 244 | self.common_flags = ['--build-dir', self.build_dir, |
Bernie Thompson | 93b9ee6 | 2018-02-21 14:56:16 -0800 | [diff] [blame] | 245 | '--board=eve', '--staging-only', '--cache-dir', |
Ryan Cui | 686ec05 | 2013-02-12 16:39:41 -0800 | [diff] [blame] | 246 | self.tempdir] |
Ryan Cui | a0215a7 | 2013-02-14 16:20:45 -0800 | [diff] [blame] | 247 | self.sdk_mock = self.StartPatcher(cros_chrome_sdk_unittest.SDKFetcherMock()) |
Ryan Cui | 686ec05 | 2013-02-12 16:39:41 -0800 | [diff] [blame] | 248 | self.PatchObject( |
| 249 | osutils, 'SourceEnvironment', autospec=True, |
| 250 | return_value={'STRIP': 'x86_64-cros-linux-gnu-strip'}) |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 251 | |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 252 | def testSingleFileDeployFailure(self): |
| 253 | """Default staging enforces that mandatory files are copied""" |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 254 | options = _ParseCommandLine(self.common_flags) |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 255 | osutils.Touch(os.path.join(self.build_dir, 'chrome'), makedirs=True) |
| 256 | self.assertRaises( |
| 257 | chrome_util.MissingPathError, deploy_chrome._PrepareStagingDir, |
Daniel Erat | c89829c | 2014-05-12 17:24:21 -0700 | [diff] [blame] | 258 | options, self.tempdir, self.staging_dir, chrome_util._COPY_PATHS_CHROME) |
Ryan Cui | ef91e70 | 2013-02-04 12:06:36 -0800 | [diff] [blame] | 259 | |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 260 | def testSloppyDeployFailure(self): |
| 261 | """Sloppy staging enforces that at least one file is copied.""" |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 262 | options = _ParseCommandLine(self.common_flags + ['--sloppy']) |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 263 | self.assertRaises( |
| 264 | chrome_util.MissingPathError, deploy_chrome._PrepareStagingDir, |
Daniel Erat | c89829c | 2014-05-12 17:24:21 -0700 | [diff] [blame] | 265 | options, self.tempdir, self.staging_dir, chrome_util._COPY_PATHS_CHROME) |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 266 | |
| 267 | def testSloppyDeploySuccess(self): |
| 268 | """Sloppy staging - stage one file.""" |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 269 | options = _ParseCommandLine(self.common_flags + ['--sloppy']) |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 270 | osutils.Touch(os.path.join(self.build_dir, 'chrome'), makedirs=True) |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 271 | deploy_chrome._PrepareStagingDir(options, self.tempdir, self.staging_dir, |
Daniel Erat | c89829c | 2014-05-12 17:24:21 -0700 | [diff] [blame] | 272 | chrome_util._COPY_PATHS_CHROME) |
David James | a6e0889 | 2013-03-01 13:34:11 -0800 | [diff] [blame] | 273 | |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 274 | |
| 275 | class DeployTestBuildDir(cros_test_lib.MockTempDirTestCase): |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 276 | """Set up a deploy object with a build-dir for use in deployment type tests""" |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 277 | |
| 278 | def _GetDeployChrome(self, args): |
Mike Frysinger | c3061a6 | 2015-06-04 04:16:18 -0400 | [diff] [blame] | 279 | options = _ParseCommandLine(args) |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 280 | return deploy_chrome.DeployChrome( |
| 281 | options, self.tempdir, os.path.join(self.tempdir, 'staging')) |
| 282 | |
| 283 | def setUp(self): |
| 284 | self.staging_dir = os.path.join(self.tempdir, 'staging') |
| 285 | self.build_dir = os.path.join(self.tempdir, 'build_dir') |
| 286 | self.deploy_mock = self.StartPatcher(DeployChromeMock()) |
| 287 | self.deploy = self._GetDeployChrome( |
| 288 | list(_REGULAR_TO) + ['--build-dir', self.build_dir, |
Bernie Thompson | 93b9ee6 | 2018-02-21 14:56:16 -0800 | [diff] [blame] | 289 | '--board=eve', '--staging-only', '--cache-dir', |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 290 | self.tempdir, '--sloppy']) |
| 291 | |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 292 | def getCopyPath(self, source_path): |
| 293 | """Return a chrome_util.Path or None if not present.""" |
| 294 | paths = [p for p in self.deploy.copy_paths if p.src == source_path] |
| 295 | return paths[0] if paths else None |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 296 | |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 297 | class TestDeploymentType(DeployTestBuildDir): |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 298 | """Test detection of deployment type using build dir.""" |
| 299 | |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 300 | def testAppShellDetection(self): |
| 301 | """Check for an app_shell deployment""" |
| 302 | osutils.Touch(os.path.join(self.deploy.options.build_dir, 'app_shell'), |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 303 | makedirs=True) |
| 304 | self.deploy._CheckDeployType() |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 305 | self.assertTrue(self.getCopyPath('app_shell')) |
| 306 | self.assertFalse(self.getCopyPath('chrome')) |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 307 | |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 308 | def testChromeAndAppShellDetection(self): |
Daniel Erat | f53bd3a | 2016-12-02 11:28:36 -0700 | [diff] [blame] | 309 | """Check for a chrome deployment when app_shell also exists.""" |
Steve Fung | 63d705d | 2014-03-16 03:14:03 -0700 | [diff] [blame] | 310 | osutils.Touch(os.path.join(self.deploy.options.build_dir, 'chrome'), |
| 311 | makedirs=True) |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 312 | osutils.Touch(os.path.join(self.deploy.options.build_dir, 'app_shell'), |
Steve Fung | 63d705d | 2014-03-16 03:14:03 -0700 | [diff] [blame] | 313 | makedirs=True) |
| 314 | self.deploy._CheckDeployType() |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 315 | self.assertTrue(self.getCopyPath('chrome')) |
Daniel Erat | 9813f0e | 2014-11-12 11:00:28 -0700 | [diff] [blame] | 316 | self.assertFalse(self.getCopyPath('app_shell')) |
Steve Fung | 63d705d | 2014-03-16 03:14:03 -0700 | [diff] [blame] | 317 | |
Steve Fung | e984a53 | 2013-11-25 17:09:25 -0800 | [diff] [blame] | 318 | def testChromeDetection(self): |
| 319 | """Check for a regular chrome deployment""" |
| 320 | osutils.Touch(os.path.join(self.deploy.options.build_dir, 'chrome'), |
| 321 | makedirs=True) |
| 322 | self.deploy._CheckDeployType() |
Daniel Erat | 1ae4638 | 2014-08-14 10:23:39 -0700 | [diff] [blame] | 323 | self.assertTrue(self.getCopyPath('chrome')) |
Daniel Erat | 9813f0e | 2014-11-12 11:00:28 -0700 | [diff] [blame] | 324 | self.assertFalse(self.getCopyPath('app_shell')) |