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