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