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 | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 11 | import unittest |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 12 | |
| 13 | from chromite.cbuildbot import repository |
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 | |
| 20 | # pylint |
| 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 | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 24 | class CbuildbotLaunchTest(cros_test_lib.MockTestCase): |
| 25 | """Tests for cbuildbot_launch script.""" |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 26 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 27 | def testPreParseArguments(self): |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 28 | """Test that we can correctly extract branch values from cbuildbot args.""" |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 29 | CASES = ( |
| 30 | (['--buildroot', '/buildroot', 'daisy-incremental'], |
| 31 | (None, '/buildroot', None)), |
| 32 | |
| 33 | (['--buildbot', '--buildroot', '/buildroot', |
| 34 | '--git-cache-dir', '/git-cache', |
| 35 | '-b', 'release-R57-9202.B', |
| 36 | 'daisy-incremental'], |
| 37 | ('release-R57-9202.B', '/buildroot', '/git-cache')), |
| 38 | |
| 39 | (['--debug', '--buildbot', '--notests', |
| 40 | '--buildroot', '/buildroot', |
| 41 | '--git-cache-dir', '/git-cache', |
| 42 | '--branch', 'release-R57-9202.B', |
| 43 | 'daisy-incremental'], |
| 44 | ('release-R57-9202.B', '/buildroot', '/git-cache')), |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 45 | ) |
| 46 | |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 47 | for cmd_args, expected in CASES: |
| 48 | expected_branch, expected_buildroot, expected_cache_dir = expected |
| 49 | |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 50 | options = cbuildbot_launch.PreParseArguments(cmd_args) |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 51 | |
| 52 | self.assertEqual(options.branch, expected_branch) |
| 53 | self.assertEqual(options.buildroot, expected_buildroot) |
| 54 | self.assertEqual(options.git_cache_dir, expected_cache_dir) |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 55 | |
| 56 | def testInitialCheckoutMin(self): |
| 57 | """Test InitialCheckout with minimum settings.""" |
| 58 | mock_repo = self.PatchObject(repository, 'RepoRepository', autospec=True) |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 59 | |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 60 | cbuildbot_launch.InitialCheckout(None, '/buildroot', None) |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 61 | |
| 62 | self.assertEqual(mock_repo.mock_calls, [ |
| 63 | mock.call(EXPECTED_MANIFEST_URL, '/buildroot', |
| 64 | branch=None, git_cache_dir=None), |
| 65 | mock.call().Sync() |
| 66 | ]) |
| 67 | |
| 68 | def testInitialCheckoutMax(self): |
| 69 | """Test InitialCheckout with all settings.""" |
| 70 | mock_repo = self.PatchObject(repository, 'RepoRepository', autospec=True) |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 71 | |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 72 | cbuildbot_launch.InitialCheckout( |
| 73 | 'release-R56-9000.B', '/buildroot', '/git-cache') |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 74 | |
| 75 | self.assertEqual(mock_repo.mock_calls, [ |
| 76 | mock.call(EXPECTED_MANIFEST_URL, '/buildroot', |
| 77 | branch='release-R56-9000.B', git_cache_dir='/git-cache'), |
| 78 | mock.call().Sync() |
| 79 | ]) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 80 | |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 81 | |
Don Garrett | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 82 | class CbuildbotLaunchGlobalTest(cros_test_lib.TestCase): |
| 83 | """Validate our global setup function.""" |
| 84 | def setUp(self): |
Don Garrett | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 85 | self.originalUmask = os.umask(0) # Have to set it to read it, make it bogus |
| 86 | |
| 87 | def teardown(self): |
Don Garrett | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 88 | os.umask(self.originalUmask) |
| 89 | |
| 90 | @unittest.skip("Global side effects break other tests. Run serially?") |
| 91 | def testConfigureGlobalEnvironment(self): |
Don Garrett | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 92 | cbuildbot_launch.ConfigureGlobalEnvironment() |
| 93 | |
Don Garrett | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 94 | self.assertEqual(os.umask(0), 0o22) |
| 95 | |
| 96 | |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 97 | class RunTests(cros_build_lib_unittest.RunCommandTestCase): |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 98 | """Tests for cbuildbot_launch script.""" |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 99 | |
| 100 | ARGS_BASE = ['--buildroot', '/buildroot'] |
| 101 | ARGS_GIT_CACHE = ['--git-cache-dir', '/git-cache'] |
| 102 | ARGS_CONFIG = ['config'] |
| 103 | CMD = ['/buildroot/chromite/bin/cbuildbot'] |
| 104 | |
| 105 | def verifyRunCbuildbot(self, args, expected_cmd, version): |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 106 | """Ensure we invoke cbuildbot correctly.""" |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 107 | options = cbuildbot_launch.PreParseArguments(args) |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 108 | |
| 109 | self.PatchObject( |
| 110 | cros_build_lib, 'GetTargetChromiteApiVersion', autospec=True, |
| 111 | return_value=version) |
| 112 | |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 113 | cbuildbot_launch.RunCbuildbot(options) |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 114 | |
| 115 | self.assertCommandCalled( |
| 116 | expected_cmd, cwd=options.buildroot, error_code_ok=True) |
| 117 | |
| 118 | def testRunCbuildbotSimple(self): |
| 119 | """Ensure we invoke cbuildbot correctly.""" |
| 120 | self.verifyRunCbuildbot( |
| 121 | self.ARGS_BASE + self.ARGS_CONFIG, |
| 122 | self.CMD + self.ARGS_CONFIG + self.ARGS_BASE, |
| 123 | (0, 4)) |
| 124 | |
| 125 | def testRunCbuildbotNotFiltered(self): |
| 126 | """Ensure we invoke cbuildbot correctly.""" |
| 127 | self.verifyRunCbuildbot( |
| 128 | self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE, |
| 129 | self.CMD + self.ARGS_CONFIG + self.ARGS_BASE + self.ARGS_GIT_CACHE, |
| 130 | (0, 4)) |
| 131 | |
| 132 | def testRunCbuildbotFiltered(self): |
| 133 | """Ensure we invoke cbuildbot correctly.""" |
| 134 | self.verifyRunCbuildbot( |
| 135 | self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE, |
| 136 | self.CMD + self.ARGS_CONFIG + self.ARGS_BASE, |
| 137 | (0, 2)) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 138 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 139 | def testMainMin(self): |
| 140 | """Test a minimal set of command line options.""" |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 141 | self.PatchObject(osutils, 'SafeMakedirs', autospec=True) |
| 142 | self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion', |
| 143 | autospec=True, return_value=(0, 4)) |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 144 | mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildroot', |
| 145 | autospec=True) |
| 146 | mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout', |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 147 | autospec=True) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 148 | |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 149 | cbuildbot_launch.main(['--buildroot', '/buildroot', 'config']) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 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, |
| 153 | [mock.call(None, '/buildroot')]) |
| 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, |
| 157 | [mock.call(None, '/buildroot', None)]) |
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 | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 163 | cwd='/buildroot', error_code_ok=True) |
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', |
| 169 | autospec=True, return_value=(0, 4)) |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 170 | mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildroot', |
| 171 | autospec=True) |
| 172 | mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout', |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 173 | autospec=True) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 174 | |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 175 | cbuildbot_launch.main(['--buildroot', '/buildroot', |
| 176 | '--git-cache-dir', '/git-cache', |
| 177 | 'config']) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 178 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 179 | # Ensure we clean, as expected. |
| 180 | self.assertEqual(mock_clean.mock_calls, |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 181 | [mock.call(None, '/buildroot')]) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 182 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 183 | # Ensure we checkout, as expected. |
| 184 | self.assertEqual(mock_checkout.mock_calls, |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 185 | [mock.call(None, '/buildroot', '/git-cache')]) |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 186 | |
| 187 | # Ensure we invoke cbuildbot, as expected. |
| 188 | self.assertCommandCalled( |
| 189 | ['/buildroot/chromite/bin/cbuildbot', |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 190 | 'config', |
| 191 | '--buildroot', '/buildroot', |
| 192 | '--git-cache-dir', '/git-cache'], |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 193 | cwd='/buildroot', error_code_ok=True) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 194 | |
| 195 | |
| 196 | class CleanBuildrootTest(cros_test_lib.TempDirTestCase): |
| 197 | """Tests for CleanBuildroot method.""" |
| 198 | |
| 199 | def setUp(self): |
| 200 | """Create standard buildroot contents for cleanup.""" |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 201 | self.root = os.path.join(self.tempdir, 'buildroot') |
| 202 | self.state = os.path.join(self.root, '.cbuildbot_launch_state') |
| 203 | self.repo = os.path.join(self.root, '.repo/repo') |
| 204 | self.chroot = os.path.join(self.root, 'chroot/chroot') |
| 205 | self.general = os.path.join(self.root, 'general/general') |
Don Garrett | 3996360 | 2017-02-27 14:41:58 -0800 | [diff] [blame] | 206 | # TODO: Add .cache, and distfiles. |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 207 | |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 208 | def populateBuildroot(self, state=None, group_readable=True): |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 209 | """Create standard buildroot contents for cleanup.""" |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 210 | buildroot_mode = 0o775 if group_readable else 0o700 |
| 211 | osutils.SafeMakedirs(self.root, mode=buildroot_mode) |
| 212 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 213 | if state: |
| 214 | osutils.WriteFile(self.state, state) |
| 215 | |
| 216 | # Create files. |
| 217 | for f in (self.repo, self.chroot, self.general): |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 218 | osutils.Touch(f, makedirs=True) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 219 | |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 220 | def testNoBuildroot(self): |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 221 | """Test CleanBuildroot with no history.""" |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 222 | cbuildbot_launch.CleanBuildroot(None, self.root) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 223 | |
| 224 | self.assertEqual(osutils.ReadFile(self.state), 'TOT') |
| 225 | |
| 226 | def testBuildrootNoState(self): |
| 227 | """Test CleanBuildroot with no state information.""" |
| 228 | self.populateBuildroot() |
| 229 | |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 230 | cbuildbot_launch.CleanBuildroot(None, self.root) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 231 | |
| 232 | self.assertEqual(osutils.ReadFile(self.state), 'TOT') |
| 233 | self.assertExists(self.repo) |
| 234 | self.assertNotExists(self.chroot) |
| 235 | self.assertExists(self.general) |
| 236 | |
| 237 | def testBuildrootBranchChange(self): |
| 238 | """Test CleanBuildroot with a change in branches.""" |
| 239 | self.populateBuildroot('branchA') |
| 240 | |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 241 | cbuildbot_launch.CleanBuildroot('branchB', self.root) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 242 | |
| 243 | self.assertEqual(osutils.ReadFile(self.state), 'branchB') |
| 244 | self.assertExists(self.repo) |
| 245 | self.assertNotExists(self.chroot) |
| 246 | self.assertExists(self.general) |
| 247 | |
| 248 | def testBuildrootBranchMatch(self): |
| 249 | """Test CleanBuildroot with no change in branch.""" |
| 250 | self.populateBuildroot('branchA') |
| 251 | |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 252 | cbuildbot_launch.CleanBuildroot('branchA', self.root) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 253 | |
| 254 | self.assertEqual(osutils.ReadFile(self.state), 'branchA') |
| 255 | self.assertExists(self.repo) |
| 256 | self.assertExists(self.chroot) |
| 257 | self.assertExists(self.general) |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 258 | |
| 259 | |
| 260 | def testBuildrootNotGroupReadable(self): |
| 261 | """Test CleanBuildroot with no state information.""" |
| 262 | self.populateBuildroot(group_readable=False) |
| 263 | |
| 264 | cbuildbot_launch.CleanBuildroot(None, self.root) |
| 265 | |
| 266 | self.assertEqual(osutils.ReadFile(self.state), 'TOT') |
| 267 | self.assertNotExists(self.repo) |
| 268 | self.assertNotExists(self.chroot) |
| 269 | self.assertNotExists(self.general) |