Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame^] | 1 | # -*- coding: utf-8 -*- |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 2 | # Copyright 2016 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 | |
| 6 | """Unit tests for chromite.lib.git and helpers for testing that module.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 10 | import mock |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 11 | import os |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 12 | |
| 13 | from chromite.cbuildbot import repository |
Don Garrett | 861e918 | 2017-05-15 15:30:23 -0700 | [diff] [blame] | 14 | from chromite.lib import constants |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 15 | from chromite.lib import cros_build_lib |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 16 | from chromite.lib import cros_build_lib_unittest |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 17 | from chromite.lib import cros_test_lib |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 18 | from chromite.lib import osutils |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 19 | from chromite.scripts import cbuildbot_launch |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 20 | |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 21 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 22 | EXPECTED_MANIFEST_URL = 'https://chrome-internal-review.googlesource.com/chromeos/manifest-internal' # pylint: disable=line-too-long |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 23 | |
| 24 | |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 25 | # It's reasonable for unittests to look at internals. |
| 26 | # pylint: disable=protected-access |
| 27 | |
| 28 | |
Don Garrett | 8d31479 | 2017-05-18 13:11:42 -0700 | [diff] [blame] | 29 | class FakeException(Exception): |
| 30 | """Test exception to raise during tests.""" |
| 31 | |
| 32 | |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 33 | class CbuildbotLaunchTest(cros_test_lib.MockTestCase): |
| 34 | """Tests for cbuildbot_launch script.""" |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 35 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 36 | def testPreParseArguments(self): |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 37 | """Test that we can correctly extract branch values from cbuildbot args.""" |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 38 | CASES = ( |
| 39 | (['--buildroot', '/buildroot', 'daisy-incremental'], |
| 40 | (None, '/buildroot', None)), |
| 41 | |
| 42 | (['--buildbot', '--buildroot', '/buildroot', |
| 43 | '--git-cache-dir', '/git-cache', |
| 44 | '-b', 'release-R57-9202.B', |
| 45 | 'daisy-incremental'], |
| 46 | ('release-R57-9202.B', '/buildroot', '/git-cache')), |
| 47 | |
| 48 | (['--debug', '--buildbot', '--notests', |
| 49 | '--buildroot', '/buildroot', |
| 50 | '--git-cache-dir', '/git-cache', |
| 51 | '--branch', 'release-R57-9202.B', |
| 52 | 'daisy-incremental'], |
| 53 | ('release-R57-9202.B', '/buildroot', '/git-cache')), |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 54 | ) |
| 55 | |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 56 | for cmd_args, expected in CASES: |
| 57 | expected_branch, expected_buildroot, expected_cache_dir = expected |
| 58 | |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 59 | options = cbuildbot_launch.PreParseArguments(cmd_args) |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 60 | |
| 61 | self.assertEqual(options.branch, expected_branch) |
| 62 | self.assertEqual(options.buildroot, expected_buildroot) |
| 63 | self.assertEqual(options.git_cache_dir, expected_cache_dir) |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 64 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 65 | def testInitialCheckout(self): |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 66 | """Test InitialCheckout with minimum settings.""" |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 67 | mock_repo = mock.MagicMock() |
| 68 | mock_repo.branch = 'branch' |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 69 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 70 | cbuildbot_launch.InitialCheckout(mock_repo) |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 71 | |
| 72 | self.assertEqual(mock_repo.mock_calls, [ |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 73 | mock.call.Sync(detach=True), |
Don Garrett | 8d31479 | 2017-05-18 13:11:42 -0700 | [diff] [blame] | 74 | ]) |
| 75 | |
Don Garrett | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 76 | def testConfigureGlobalEnvironment(self): |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 77 | """Ensure that we can setup our global runtime environment correctly.""" |
Don Garrett | 86fec48 | 2017-05-17 18:13:33 -0700 | [diff] [blame] | 78 | |
| 79 | os.environ.pop('LANG', None) |
| 80 | os.environ['LC_MONETARY'] = 'bad' |
| 81 | |
Don Garrett | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 82 | cbuildbot_launch.ConfigureGlobalEnvironment() |
| 83 | |
Don Garrett | 86fec48 | 2017-05-17 18:13:33 -0700 | [diff] [blame] | 84 | # Verify umask is updated. |
Don Garrett | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 85 | self.assertEqual(os.umask(0), 0o22) |
| 86 | |
Don Garrett | 86fec48 | 2017-05-17 18:13:33 -0700 | [diff] [blame] | 87 | # Verify ENVs are cleaned up. |
| 88 | self.assertEqual(os.environ['LANG'], 'en_US.UTF-8') |
| 89 | self.assertNotIn('LC_MONETARY', os.environ) |
| 90 | |
Don Garrett | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 91 | |
Don Garrett | 066e6f5 | 2017-09-28 19:14:01 -0700 | [diff] [blame] | 92 | class RunDepotToolsEnsureBootstrap(cros_build_lib_unittest.RunCommandTestCase, |
| 93 | cros_test_lib.TempDirTestCase): |
| 94 | """Test the helper function DepotToolsEnsureBootstrap.""" |
| 95 | |
| 96 | def testEnsureBootstrap(self): |
| 97 | """Verify that the script is run if present.""" |
| 98 | script = os.path.join(self.tempdir, 'ensure_bootstrap') |
| 99 | osutils.Touch(script, makedirs=True) |
| 100 | |
| 101 | cbuildbot_launch.DepotToolsEnsureBootstrap(self.tempdir) |
| 102 | self.assertCommandCalled( |
| 103 | [script], extra_env={'PATH': mock.ANY}, cwd=self.tempdir) |
| 104 | |
| 105 | def testEnsureBootstrapMissing(self): |
| 106 | """Verify that the script is NOT run if not present.""" |
| 107 | cbuildbot_launch.DepotToolsEnsureBootstrap(self.tempdir) |
| 108 | self.assertEqual(self.rc.call_count, 0) |
| 109 | |
| 110 | |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 111 | class RunTests(cros_build_lib_unittest.RunCommandTestCase): |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 112 | """Tests for cbuildbot_launch script.""" |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 113 | |
| 114 | ARGS_BASE = ['--buildroot', '/buildroot'] |
Don Garrett | 5cd946b | 2017-07-20 13:42:20 -0700 | [diff] [blame] | 115 | EXPECTED_ARGS_BASE = ['--buildroot', '/cbuildbot_buildroot'] |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 116 | ARGS_GIT_CACHE = ['--git-cache-dir', '/git-cache'] |
| 117 | ARGS_CONFIG = ['config'] |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 118 | CMD = ['/cbuildbot_buildroot/chromite/bin/cbuildbot'] |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 119 | |
| 120 | def verifyRunCbuildbot(self, args, expected_cmd, version): |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 121 | """Ensure we invoke cbuildbot correctly.""" |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 122 | self.PatchObject( |
| 123 | cros_build_lib, 'GetTargetChromiteApiVersion', autospec=True, |
| 124 | return_value=version) |
| 125 | |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 126 | cbuildbot_launch.RunCbuildbot('/cbuildbot_buildroot', '/depot_tools', args) |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 127 | |
| 128 | self.assertCommandCalled( |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 129 | expected_cmd, extra_env={'PATH': mock.ANY}, |
| 130 | cwd='/cbuildbot_buildroot', error_code_ok=True) |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 131 | |
| 132 | def testRunCbuildbotSimple(self): |
| 133 | """Ensure we invoke cbuildbot correctly.""" |
| 134 | self.verifyRunCbuildbot( |
| 135 | self.ARGS_BASE + self.ARGS_CONFIG, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 136 | self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE, |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 137 | (0, 4)) |
| 138 | |
| 139 | def testRunCbuildbotNotFiltered(self): |
| 140 | """Ensure we invoke cbuildbot correctly.""" |
| 141 | self.verifyRunCbuildbot( |
| 142 | self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 143 | (self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE + |
| 144 | self.ARGS_GIT_CACHE), |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 145 | (0, 4)) |
| 146 | |
| 147 | def testRunCbuildbotFiltered(self): |
| 148 | """Ensure we invoke cbuildbot correctly.""" |
| 149 | self.verifyRunCbuildbot( |
| 150 | self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 151 | self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE, |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 152 | (0, 2)) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 153 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 154 | def testMainMin(self): |
| 155 | """Test a minimal set of command line options.""" |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 156 | self.PatchObject(osutils, 'SafeMakedirs', autospec=True) |
| 157 | self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion', |
Don Garrett | 861e918 | 2017-05-15 15:30:23 -0700 | [diff] [blame] | 158 | autospec=True, return_value=(constants.REEXEC_API_MAJOR, |
| 159 | constants.REEXEC_API_MINOR)) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 160 | mock_repo = mock.MagicMock() |
| 161 | mock_repo.branch = 'master' |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 162 | mock_repo.directory = '/root/repository' |
| 163 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 164 | mock_repo_create = self.PatchObject(repository, 'RepoRepository', |
| 165 | autospec=True, return_value=mock_repo) |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 166 | mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot', |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 167 | autospec=True) |
| 168 | mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout', |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 169 | autospec=True) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 170 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 171 | cbuildbot_launch._main(['-r', '/root', 'config']) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 172 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 173 | # Did we create the repo instance correctly? |
| 174 | self.assertEqual(mock_repo_create.mock_calls, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 175 | [mock.call(EXPECTED_MANIFEST_URL, '/root/repository', |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 176 | git_cache_dir=None, branch='master')]) |
| 177 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 178 | # Ensure we clean, as expected. |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 179 | self.assertEqual(mock_clean.mock_calls, [ |
Don Garrett | d1d90dd | 2017-06-13 17:35:52 -0700 | [diff] [blame] | 180 | mock.call('/root', mock_repo, |
| 181 | { |
| 182 | 'branch_name': 'master', |
| 183 | 'tryjob': False, |
| 184 | 'build_config': 'config', |
| 185 | })]) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 186 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 187 | # Ensure we checkout, as expected. |
| 188 | self.assertEqual(mock_checkout.mock_calls, |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 189 | [mock.call(mock_repo)]) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 190 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 191 | # Ensure we invoke cbuildbot, as expected. |
| 192 | self.assertCommandCalled( |
Don Garrett | 5cd946b | 2017-07-20 13:42:20 -0700 | [diff] [blame] | 193 | [ |
| 194 | '/root/repository/chromite/bin/cbuildbot', |
| 195 | 'config', |
| 196 | '-r', '/root/repository', |
| 197 | '--ts-mon-task-num', '1', |
| 198 | ], |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 199 | extra_env={'PATH': mock.ANY}, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 200 | cwd='/root/repository', |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 201 | error_code_ok=True) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 202 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 203 | def testMainMax(self): |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 204 | """Test a larger set of command line options.""" |
| 205 | self.PatchObject(osutils, 'SafeMakedirs', autospec=True) |
| 206 | self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion', |
Don Garrett | 861e918 | 2017-05-15 15:30:23 -0700 | [diff] [blame] | 207 | autospec=True, return_value=(constants.REEXEC_API_MAJOR, |
| 208 | constants.REEXEC_API_MINOR)) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 209 | mock_repo = mock.MagicMock() |
| 210 | mock_repo.branch = 'branch' |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 211 | mock_repo.directory = '/root/repository' |
| 212 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 213 | mock_repo_create = self.PatchObject(repository, 'RepoRepository', |
| 214 | autospec=True, return_value=mock_repo) |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 215 | mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot', |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 216 | autospec=True) |
| 217 | mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout', |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 218 | autospec=True) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 219 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 220 | cbuildbot_launch._main(['--buildroot', '/root', |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 221 | '--branch', 'branch', |
| 222 | '--git-cache-dir', '/git-cache', |
Don Garrett | d1d90dd | 2017-06-13 17:35:52 -0700 | [diff] [blame] | 223 | '--remote-trybot', |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 224 | 'config']) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 225 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 226 | # Did we create the repo instance correctly? |
| 227 | self.assertEqual(mock_repo_create.mock_calls, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 228 | [mock.call(EXPECTED_MANIFEST_URL, '/root/repository', |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 229 | git_cache_dir='/git-cache', branch='branch')]) |
| 230 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 231 | # Ensure we clean, as expected. |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 232 | self.assertEqual(mock_clean.mock_calls, [ |
Don Garrett | d1d90dd | 2017-06-13 17:35:52 -0700 | [diff] [blame] | 233 | mock.call('/root', |
| 234 | mock_repo, |
| 235 | { |
| 236 | 'branch_name': 'branch', |
| 237 | 'tryjob': True, |
| 238 | 'build_config': 'config', |
| 239 | })]) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 240 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 241 | # Ensure we checkout, as expected. |
| 242 | self.assertEqual(mock_checkout.mock_calls, |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 243 | [mock.call(mock_repo)]) |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 244 | |
| 245 | # Ensure we invoke cbuildbot, as expected. |
| 246 | self.assertCommandCalled( |
Don Garrett | 5cd946b | 2017-07-20 13:42:20 -0700 | [diff] [blame] | 247 | [ |
| 248 | '/root/repository/chromite/bin/cbuildbot', |
| 249 | 'config', |
| 250 | '--buildroot', '/root/repository', |
| 251 | '--branch', 'branch', |
| 252 | '--git-cache-dir', '/git-cache', |
| 253 | '--remote-trybot', |
| 254 | '--ts-mon-task-num', '1', |
| 255 | ], |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 256 | extra_env={'PATH': mock.ANY}, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 257 | cwd='/root/repository', |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 258 | error_code_ok=True) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 259 | |
| 260 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 261 | class CleanBuildRootTest(cros_test_lib.MockTempDirTestCase): |
| 262 | """Tests for CleanBuildRoot method.""" |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 263 | |
| 264 | def setUp(self): |
| 265 | """Create standard buildroot contents for cleanup.""" |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 266 | self.root = os.path.join(self.tempdir) |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 267 | self.state = os.path.join(self.root, '.cbuildbot_launch_state') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 268 | self.buildroot = os.path.join(self.root, 'buildroot') |
| 269 | self.repo = os.path.join(self.buildroot, '.repo/repo') |
| 270 | self.chroot = os.path.join(self.buildroot, 'chroot/chroot') |
| 271 | self.general = os.path.join(self.buildroot, 'general/general') |
Don Garrett | 3996360 | 2017-02-27 14:41:58 -0800 | [diff] [blame] | 272 | # TODO: Add .cache, and distfiles. |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 273 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 274 | self.mock_repo = mock.MagicMock() |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 275 | self.mock_repo.directory = self.buildroot |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 276 | |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 277 | self.metrics = {} |
| 278 | |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 279 | def populateBuildroot(self, state=None): |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 280 | """Create standard buildroot contents for cleanup.""" |
| 281 | if state: |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 282 | osutils.SafeMakedirs(self.root) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 283 | osutils.WriteFile(self.state, state) |
| 284 | |
| 285 | # Create files. |
| 286 | for f in (self.repo, self.chroot, self.general): |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 287 | osutils.Touch(f, makedirs=True) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 288 | |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 289 | def testNoBuildroot(self): |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 290 | """Test CleanBuildRoot with no history.""" |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 291 | self.mock_repo.branch = 'master' |
| 292 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 293 | cbuildbot_launch.CleanBuildRoot( |
| 294 | self.root, self.mock_repo, self.metrics) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 295 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 296 | self.assertEqual(osutils.ReadFile(self.state), '2 master') |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 297 | |
| 298 | def testBuildrootNoState(self): |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 299 | """Test CleanBuildRoot with no state information.""" |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 300 | self.populateBuildroot() |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 301 | self.mock_repo.branch = 'master' |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 302 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 303 | cbuildbot_launch.CleanBuildRoot( |
| 304 | self.root, self.mock_repo, self.metrics) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 305 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 306 | self.assertEqual(osutils.ReadFile(self.state), '2 master') |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 307 | self.assertNotExists(self.repo) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 308 | self.assertNotExists(self.chroot) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 309 | self.assertNotExists(self.general) |
| 310 | |
| 311 | def testBuildrootFormatMismatch(self): |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 312 | """Test CleanBuildRoot with no state information.""" |
Don Garrett | 125d4dc | 2017-04-25 16:26:03 -0700 | [diff] [blame] | 313 | self.populateBuildroot('0 master') |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 314 | self.mock_repo.branch = 'master' |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 315 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 316 | cbuildbot_launch.CleanBuildRoot( |
| 317 | self.root, self.mock_repo, self.metrics) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 318 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 319 | self.assertEqual(osutils.ReadFile(self.state), '2 master') |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 320 | self.assertNotExists(self.repo) |
| 321 | self.assertNotExists(self.chroot) |
| 322 | self.assertNotExists(self.general) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 323 | |
| 324 | def testBuildrootBranchChange(self): |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 325 | """Test CleanBuildRoot with a change in branches.""" |
| 326 | self.populateBuildroot('2 branchA') |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 327 | self.mock_repo.branch = 'branchB' |
Benjamin Gordon | 59ba2f8 | 2017-08-28 15:31:06 -0600 | [diff] [blame] | 328 | m = self.PatchObject(cros_build_lib, 'CleanupChrootMount') |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 329 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 330 | cbuildbot_launch.CleanBuildRoot( |
| 331 | self.root, self.mock_repo, self.metrics) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 332 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 333 | self.assertEqual(osutils.ReadFile(self.state), '2 branchB') |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 334 | self.assertExists(self.repo) |
| 335 | self.assertNotExists(self.chroot) |
| 336 | self.assertExists(self.general) |
Benjamin Gordon | 59ba2f8 | 2017-08-28 15:31:06 -0600 | [diff] [blame] | 337 | m.assert_called() |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 338 | |
| 339 | def testBuildrootBranchMatch(self): |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 340 | """Test CleanBuildRoot with no change in branch.""" |
| 341 | self.populateBuildroot('2 branchA') |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 342 | self.mock_repo.branch = 'branchA' |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 343 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 344 | cbuildbot_launch.CleanBuildRoot( |
| 345 | self.root, self.mock_repo, self.metrics) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 346 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 347 | self.assertEqual(osutils.ReadFile(self.state), '2 branchA') |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 348 | self.assertExists(self.repo) |
| 349 | self.assertExists(self.chroot) |
| 350 | self.assertExists(self.general) |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 351 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 352 | def testBuildrootRepoCleanFailure(self): |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 353 | """Test CleanBuildRoot with repo checkout failure.""" |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 354 | self.populateBuildroot('1 branchA') |
| 355 | self.mock_repo.branch = 'branchA' |
| 356 | self.mock_repo.BuildRootGitCleanup.side_effect = Exception |
| 357 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 358 | cbuildbot_launch.CleanBuildRoot( |
| 359 | self.root, self.mock_repo, self.metrics) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 360 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 361 | self.assertEqual(osutils.ReadFile(self.state), '2 branchA') |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 362 | self.assertNotExists(self.repo) |
| 363 | self.assertNotExists(self.chroot) |
| 364 | self.assertNotExists(self.general) |
| 365 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 366 | def testGetState(self): |
| 367 | """Test GetState.""" |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 368 | # No root dir. |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 369 | results = cbuildbot_launch.GetState(self.root) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 370 | self.assertEqual(results, (0, '')) |
| 371 | |
| 372 | # Empty root dir. |
| 373 | osutils.SafeMakedirs(self.root) |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 374 | results = cbuildbot_launch.GetState(self.root) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 375 | self.assertEqual(results, (0, '')) |
| 376 | |
| 377 | # Empty Contents |
| 378 | osutils.WriteFile(self.state, '') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 379 | results = cbuildbot_launch.GetState(self.root) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 380 | self.assertEqual(results, (0, '')) |
| 381 | |
| 382 | # Old Format Contents |
| 383 | osutils.WriteFile(self.state, 'happy-branch') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 384 | results = cbuildbot_launch.GetState(self.root) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 385 | self.assertEqual(results, (0, '')) |
| 386 | |
| 387 | # Expected Contents |
| 388 | osutils.WriteFile(self.state, '1 happy-branch') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 389 | results = cbuildbot_launch.GetState(self.root) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 390 | self.assertEqual(results, (1, 'happy-branch')) |
| 391 | |
| 392 | # Future Contents |
| 393 | osutils.WriteFile(self.state, '22 master') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 394 | results = cbuildbot_launch.GetState(self.root) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 395 | self.assertEqual(results, (22, 'master')) |
| 396 | |
| 397 | # Read Write |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 398 | cbuildbot_launch.SetState('happy-branch', self.root) |
| 399 | results = cbuildbot_launch.GetState(self.root) |
| 400 | self.assertEqual(results, (2, 'happy-branch')) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 401 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 402 | def testSetState(self): |
| 403 | """Test SetState.""" |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 404 | # Write out a state file. |
| 405 | osutils.SafeMakedirs(self.root) |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 406 | cbuildbot_launch.SetState('happy-branch', self.root) |
| 407 | self.assertEqual(osutils.ReadFile(self.state), '2 happy-branch') |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 408 | |
| 409 | # Change to a future version. |
| 410 | self.PatchObject(cbuildbot_launch, 'BUILDROOT_BUILDROOT_LAYOUT', 22) |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 411 | cbuildbot_launch.SetState('happy-branch', self.root) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 412 | self.assertEqual(osutils.ReadFile(self.state), '22 happy-branch') |