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 | bf90cdf | 2017-05-19 15:54:02 -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 | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 102 | options = cbuildbot_launch.PreParseArguments(args) |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 103 | |
| 104 | self.PatchObject( |
| 105 | cros_build_lib, 'GetTargetChromiteApiVersion', autospec=True, |
| 106 | return_value=version) |
| 107 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 108 | cbuildbot_launch.RunCbuildbot('/cbuildbot_buildroot', options) |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 109 | |
| 110 | self.assertCommandCalled( |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 111 | expected_cmd, cwd='/cbuildbot_buildroot', error_code_ok=True) |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 112 | |
| 113 | def testRunCbuildbotSimple(self): |
| 114 | """Ensure we invoke cbuildbot correctly.""" |
| 115 | self.verifyRunCbuildbot( |
| 116 | self.ARGS_BASE + self.ARGS_CONFIG, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 117 | self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE, |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 118 | (0, 4)) |
| 119 | |
| 120 | def testRunCbuildbotNotFiltered(self): |
| 121 | """Ensure we invoke cbuildbot correctly.""" |
| 122 | self.verifyRunCbuildbot( |
| 123 | self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 124 | (self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE + |
| 125 | self.ARGS_GIT_CACHE), |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 126 | (0, 4)) |
| 127 | |
| 128 | def testRunCbuildbotFiltered(self): |
| 129 | """Ensure we invoke cbuildbot correctly.""" |
| 130 | self.verifyRunCbuildbot( |
| 131 | self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 132 | self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE, |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 133 | (0, 2)) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 134 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 135 | def testMainMin(self): |
| 136 | """Test a minimal set of command line options.""" |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 137 | self.PatchObject(osutils, 'SafeMakedirs', autospec=True) |
| 138 | self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion', |
Don Garrett | 861e918 | 2017-05-15 15:30:23 -0700 | [diff] [blame] | 139 | autospec=True, return_value=(constants.REEXEC_API_MAJOR, |
| 140 | constants.REEXEC_API_MINOR)) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 141 | mock_repo = mock.MagicMock() |
| 142 | mock_repo.branch = 'master' |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 143 | mock_repo.directory = '/root/repository' |
| 144 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 145 | mock_repo_create = self.PatchObject(repository, 'RepoRepository', |
| 146 | autospec=True, return_value=mock_repo) |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 147 | mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot', |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 148 | autospec=True) |
| 149 | mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout', |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 150 | autospec=True) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 151 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 152 | cbuildbot_launch._main(['-r', '/root', 'config']) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 153 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 154 | # Did we create the repo instance correctly? |
| 155 | self.assertEqual(mock_repo_create.mock_calls, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 156 | [mock.call(EXPECTED_MANIFEST_URL, '/root/repository', |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 157 | git_cache_dir=None, branch='master')]) |
| 158 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 159 | # Ensure we clean, as expected. |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 160 | self.assertEqual(mock_clean.mock_calls, [ |
| 161 | mock.call('/root', mock_repo, {'branch_name': 'master'})]) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 162 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 163 | # Ensure we checkout, as expected. |
| 164 | self.assertEqual(mock_checkout.mock_calls, |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 165 | [mock.call(mock_repo)]) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 166 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 167 | # Ensure we invoke cbuildbot, as expected. |
| 168 | self.assertCommandCalled( |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 169 | ['/root/repository/chromite/bin/cbuildbot', |
| 170 | 'config', '-r', '/root/repository'], |
| 171 | cwd='/root/repository', |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 172 | error_code_ok=True) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 173 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 174 | def testMainMax(self): |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 175 | """Test a larger set of command line options.""" |
| 176 | self.PatchObject(osutils, 'SafeMakedirs', autospec=True) |
| 177 | self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion', |
Don Garrett | 861e918 | 2017-05-15 15:30:23 -0700 | [diff] [blame] | 178 | autospec=True, return_value=(constants.REEXEC_API_MAJOR, |
| 179 | constants.REEXEC_API_MINOR)) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 180 | mock_repo = mock.MagicMock() |
| 181 | mock_repo.branch = 'branch' |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 182 | mock_repo.directory = '/root/repository' |
| 183 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 184 | mock_repo_create = self.PatchObject(repository, 'RepoRepository', |
| 185 | autospec=True, return_value=mock_repo) |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 186 | mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot', |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 187 | autospec=True) |
| 188 | mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout', |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 189 | autospec=True) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 190 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 191 | cbuildbot_launch._main(['--buildroot', '/root', |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 192 | '--branch', 'branch', |
| 193 | '--git-cache-dir', '/git-cache', |
| 194 | 'config']) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 195 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 196 | # Did we create the repo instance correctly? |
| 197 | self.assertEqual(mock_repo_create.mock_calls, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 198 | [mock.call(EXPECTED_MANIFEST_URL, '/root/repository', |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 199 | git_cache_dir='/git-cache', branch='branch')]) |
| 200 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 201 | # Ensure we clean, as expected. |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 202 | self.assertEqual(mock_clean.mock_calls, [ |
| 203 | mock.call('/root', mock_repo, {'branch_name': 'branch'})]) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 204 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 205 | # Ensure we checkout, as expected. |
| 206 | self.assertEqual(mock_checkout.mock_calls, |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 207 | [mock.call(mock_repo)]) |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 208 | |
| 209 | # Ensure we invoke cbuildbot, as expected. |
| 210 | self.assertCommandCalled( |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 211 | ['/root/repository/chromite/bin/cbuildbot', |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 212 | 'config', |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 213 | '--buildroot', '/root/repository', |
Don Garrett | 125d4dc | 2017-04-25 16:26:03 -0700 | [diff] [blame] | 214 | '--branch', 'branch', |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 215 | '--git-cache-dir', '/git-cache'], |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 216 | cwd='/root/repository', |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 217 | error_code_ok=True) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 218 | |
| 219 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 220 | class CleanBuildRootTest(cros_test_lib.MockTempDirTestCase): |
| 221 | """Tests for CleanBuildRoot method.""" |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 222 | |
| 223 | def setUp(self): |
| 224 | """Create standard buildroot contents for cleanup.""" |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 225 | self.root = os.path.join(self.tempdir) |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 226 | self.state = os.path.join(self.root, '.cbuildbot_launch_state') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 227 | self.buildroot = os.path.join(self.root, 'buildroot') |
| 228 | self.repo = os.path.join(self.buildroot, '.repo/repo') |
| 229 | self.chroot = os.path.join(self.buildroot, 'chroot/chroot') |
| 230 | self.general = os.path.join(self.buildroot, 'general/general') |
Don Garrett | 3996360 | 2017-02-27 14:41:58 -0800 | [diff] [blame] | 231 | # TODO: Add .cache, and distfiles. |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 232 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 233 | self.mock_repo = mock.MagicMock() |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 234 | self.mock_repo.directory = self.buildroot |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 235 | |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 236 | self.metrics = {} |
| 237 | |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 238 | def populateBuildroot(self, state=None): |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 239 | """Create standard buildroot contents for cleanup.""" |
| 240 | if state: |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 241 | osutils.SafeMakedirs(self.root) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 242 | osutils.WriteFile(self.state, state) |
| 243 | |
| 244 | # Create files. |
| 245 | for f in (self.repo, self.chroot, self.general): |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 246 | osutils.Touch(f, makedirs=True) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 247 | |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 248 | def testNoBuildroot(self): |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 249 | """Test CleanBuildRoot with no history.""" |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 250 | self.mock_repo.branch = 'master' |
| 251 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 252 | cbuildbot_launch.CleanBuildRoot( |
| 253 | self.root, self.mock_repo, self.metrics) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 254 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 255 | self.assertEqual(osutils.ReadFile(self.state), '2 master') |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 256 | |
| 257 | def testBuildrootNoState(self): |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 258 | """Test CleanBuildRoot with no state information.""" |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 259 | self.populateBuildroot() |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 260 | self.mock_repo.branch = 'master' |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 261 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 262 | cbuildbot_launch.CleanBuildRoot( |
| 263 | self.root, self.mock_repo, self.metrics) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 264 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 265 | self.assertEqual(osutils.ReadFile(self.state), '2 master') |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 266 | self.assertNotExists(self.repo) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 267 | self.assertNotExists(self.chroot) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 268 | self.assertNotExists(self.general) |
| 269 | |
| 270 | def testBuildrootFormatMismatch(self): |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 271 | """Test CleanBuildRoot with no state information.""" |
Don Garrett | 125d4dc | 2017-04-25 16:26:03 -0700 | [diff] [blame] | 272 | self.populateBuildroot('0 master') |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 273 | self.mock_repo.branch = 'master' |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 274 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 275 | cbuildbot_launch.CleanBuildRoot( |
| 276 | self.root, self.mock_repo, self.metrics) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 277 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 278 | self.assertEqual(osutils.ReadFile(self.state), '2 master') |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 279 | self.assertNotExists(self.repo) |
| 280 | self.assertNotExists(self.chroot) |
| 281 | self.assertNotExists(self.general) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 282 | |
| 283 | def testBuildrootBranchChange(self): |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 284 | """Test CleanBuildRoot with a change in branches.""" |
| 285 | self.populateBuildroot('2 branchA') |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 286 | self.mock_repo.branch = 'branchB' |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 287 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 288 | cbuildbot_launch.CleanBuildRoot( |
| 289 | self.root, self.mock_repo, self.metrics) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 290 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 291 | self.assertEqual(osutils.ReadFile(self.state), '2 branchB') |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 292 | self.assertExists(self.repo) |
| 293 | self.assertNotExists(self.chroot) |
| 294 | self.assertExists(self.general) |
| 295 | |
| 296 | def testBuildrootBranchMatch(self): |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 297 | """Test CleanBuildRoot with no change in branch.""" |
| 298 | self.populateBuildroot('2 branchA') |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 299 | self.mock_repo.branch = 'branchA' |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 300 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 301 | cbuildbot_launch.CleanBuildRoot( |
| 302 | self.root, self.mock_repo, self.metrics) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 303 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 304 | self.assertEqual(osutils.ReadFile(self.state), '2 branchA') |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 305 | self.assertExists(self.repo) |
| 306 | self.assertExists(self.chroot) |
| 307 | self.assertExists(self.general) |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 308 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 309 | def testBuildrootRepoCleanFailure(self): |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 310 | """Test CleanBuildRoot with repo checkout failure.""" |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 311 | self.populateBuildroot('1 branchA') |
| 312 | self.mock_repo.branch = 'branchA' |
| 313 | self.mock_repo.BuildRootGitCleanup.side_effect = Exception |
| 314 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 315 | cbuildbot_launch.CleanBuildRoot( |
| 316 | self.root, self.mock_repo, self.metrics) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 317 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 318 | self.assertEqual(osutils.ReadFile(self.state), '2 branchA') |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 319 | self.assertNotExists(self.repo) |
| 320 | self.assertNotExists(self.chroot) |
| 321 | self.assertNotExists(self.general) |
| 322 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 323 | def testGetState(self): |
| 324 | """Test GetState.""" |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 325 | # No root dir. |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 326 | results = cbuildbot_launch.GetState(self.root) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 327 | self.assertEqual(results, (0, '')) |
| 328 | |
| 329 | # Empty root dir. |
| 330 | osutils.SafeMakedirs(self.root) |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 331 | results = cbuildbot_launch.GetState(self.root) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 332 | self.assertEqual(results, (0, '')) |
| 333 | |
| 334 | # Empty Contents |
| 335 | osutils.WriteFile(self.state, '') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 336 | results = cbuildbot_launch.GetState(self.root) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 337 | self.assertEqual(results, (0, '')) |
| 338 | |
| 339 | # Old Format Contents |
| 340 | osutils.WriteFile(self.state, 'happy-branch') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 341 | results = cbuildbot_launch.GetState(self.root) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 342 | self.assertEqual(results, (0, '')) |
| 343 | |
| 344 | # Expected Contents |
| 345 | osutils.WriteFile(self.state, '1 happy-branch') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 346 | results = cbuildbot_launch.GetState(self.root) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 347 | self.assertEqual(results, (1, 'happy-branch')) |
| 348 | |
| 349 | # Future Contents |
| 350 | osutils.WriteFile(self.state, '22 master') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 351 | results = cbuildbot_launch.GetState(self.root) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 352 | self.assertEqual(results, (22, 'master')) |
| 353 | |
| 354 | # Read Write |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 355 | cbuildbot_launch.SetState('happy-branch', self.root) |
| 356 | results = cbuildbot_launch.GetState(self.root) |
| 357 | self.assertEqual(results, (2, 'happy-branch')) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 358 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 359 | def testSetState(self): |
| 360 | """Test SetState.""" |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 361 | # Write out a state file. |
| 362 | osutils.SafeMakedirs(self.root) |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 363 | cbuildbot_launch.SetState('happy-branch', self.root) |
| 364 | self.assertEqual(osutils.ReadFile(self.state), '2 happy-branch') |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 365 | |
| 366 | # Change to a future version. |
| 367 | self.PatchObject(cbuildbot_launch, 'BUILDROOT_BUILDROOT_LAYOUT', 22) |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame^] | 368 | cbuildbot_launch.SetState('happy-branch', self.root) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 369 | self.assertEqual(osutils.ReadFile(self.state), '22 happy-branch') |