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