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