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 | |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 6 | """Unit tests for chromite.scripts.cbuildbot_launch.""" |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 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 |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 12 | import time |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 13 | |
| 14 | from chromite.cbuildbot import repository |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 15 | from chromite.lib import build_summary |
Don Garrett | 861e918 | 2017-05-15 15:30:23 -0700 | [diff] [blame] | 16 | from chromite.lib import constants |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 17 | from chromite.lib import cros_build_lib |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 18 | from chromite.lib import cros_build_lib_unittest |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 19 | from chromite.lib import cros_test_lib |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 20 | from chromite.lib import osutils |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 21 | from chromite.scripts import cbuildbot_launch |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 22 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 23 | 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] | 24 | |
| 25 | |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 26 | # It's reasonable for unittests to look at internals. |
| 27 | # pylint: disable=protected-access |
| 28 | |
| 29 | |
Don Garrett | 8d31479 | 2017-05-18 13:11:42 -0700 | [diff] [blame] | 30 | class FakeException(Exception): |
| 31 | """Test exception to raise during tests.""" |
| 32 | |
| 33 | |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 34 | class CbuildbotLaunchTest(cros_test_lib.MockTestCase): |
| 35 | """Tests for cbuildbot_launch script.""" |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 36 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 37 | def testPreParseArguments(self): |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 38 | """Test that we can correctly extract branch values from cbuildbot args.""" |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 39 | CASES = ( |
| 40 | (['--buildroot', '/buildroot', 'daisy-incremental'], |
| 41 | (None, '/buildroot', None)), |
| 42 | |
| 43 | (['--buildbot', '--buildroot', '/buildroot', |
| 44 | '--git-cache-dir', '/git-cache', |
| 45 | '-b', 'release-R57-9202.B', |
| 46 | 'daisy-incremental'], |
| 47 | ('release-R57-9202.B', '/buildroot', '/git-cache')), |
| 48 | |
| 49 | (['--debug', '--buildbot', '--notests', |
| 50 | '--buildroot', '/buildroot', |
| 51 | '--git-cache-dir', '/git-cache', |
| 52 | '--branch', 'release-R57-9202.B', |
| 53 | 'daisy-incremental'], |
| 54 | ('release-R57-9202.B', '/buildroot', '/git-cache')), |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 55 | ) |
| 56 | |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 57 | for cmd_args, expected in CASES: |
| 58 | expected_branch, expected_buildroot, expected_cache_dir = expected |
| 59 | |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 60 | options = cbuildbot_launch.PreParseArguments(cmd_args) |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 61 | |
| 62 | self.assertEqual(options.branch, expected_branch) |
| 63 | self.assertEqual(options.buildroot, expected_buildroot) |
| 64 | self.assertEqual(options.git_cache_dir, expected_cache_dir) |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 65 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 66 | def testInitialCheckout(self): |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 67 | """Test InitialCheckout with minimum settings.""" |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 68 | mock_repo = mock.MagicMock() |
| 69 | mock_repo.branch = 'branch' |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 70 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 71 | cbuildbot_launch.InitialCheckout(mock_repo) |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 72 | |
| 73 | self.assertEqual(mock_repo.mock_calls, [ |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 74 | mock.call.Sync(detach=True), |
Don Garrett | 8d31479 | 2017-05-18 13:11:42 -0700 | [diff] [blame] | 75 | ]) |
| 76 | |
Don Garrett | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 77 | def testConfigureGlobalEnvironment(self): |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 78 | """Ensure that we can setup our global runtime environment correctly.""" |
Don Garrett | 86fec48 | 2017-05-17 18:13:33 -0700 | [diff] [blame] | 79 | |
| 80 | os.environ.pop('LANG', None) |
| 81 | os.environ['LC_MONETARY'] = 'bad' |
| 82 | |
Don Garrett | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 83 | cbuildbot_launch.ConfigureGlobalEnvironment() |
| 84 | |
Don Garrett | 86fec48 | 2017-05-17 18:13:33 -0700 | [diff] [blame] | 85 | # Verify umask is updated. |
Don Garrett | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 86 | self.assertEqual(os.umask(0), 0o22) |
| 87 | |
Don Garrett | 86fec48 | 2017-05-17 18:13:33 -0700 | [diff] [blame] | 88 | # Verify ENVs are cleaned up. |
| 89 | self.assertEqual(os.environ['LANG'], 'en_US.UTF-8') |
| 90 | self.assertNotIn('LC_MONETARY', os.environ) |
| 91 | |
Don Garrett | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 92 | |
Don Garrett | 066e6f5 | 2017-09-28 19:14:01 -0700 | [diff] [blame] | 93 | class RunDepotToolsEnsureBootstrap(cros_build_lib_unittest.RunCommandTestCase, |
| 94 | cros_test_lib.TempDirTestCase): |
| 95 | """Test the helper function DepotToolsEnsureBootstrap.""" |
| 96 | |
| 97 | def testEnsureBootstrap(self): |
| 98 | """Verify that the script is run if present.""" |
| 99 | script = os.path.join(self.tempdir, 'ensure_bootstrap') |
| 100 | osutils.Touch(script, makedirs=True) |
| 101 | |
| 102 | cbuildbot_launch.DepotToolsEnsureBootstrap(self.tempdir) |
| 103 | self.assertCommandCalled( |
| 104 | [script], extra_env={'PATH': mock.ANY}, cwd=self.tempdir) |
| 105 | |
| 106 | def testEnsureBootstrapMissing(self): |
| 107 | """Verify that the script is NOT run if not present.""" |
| 108 | cbuildbot_launch.DepotToolsEnsureBootstrap(self.tempdir) |
| 109 | self.assertEqual(self.rc.call_count, 0) |
| 110 | |
| 111 | |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 112 | class RunTests(cros_build_lib_unittest.RunCommandTestCase): |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 113 | """Tests for cbuildbot_launch script.""" |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 114 | |
| 115 | ARGS_BASE = ['--buildroot', '/buildroot'] |
Don Garrett | 5cd946b | 2017-07-20 13:42:20 -0700 | [diff] [blame] | 116 | EXPECTED_ARGS_BASE = ['--buildroot', '/cbuildbot_buildroot'] |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 117 | ARGS_GIT_CACHE = ['--git-cache-dir', '/git-cache'] |
| 118 | ARGS_CONFIG = ['config'] |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 119 | CMD = ['/cbuildbot_buildroot/chromite/bin/cbuildbot'] |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 120 | |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 121 | def verifyCbuildbot(self, args, expected_cmd, version): |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 122 | """Ensure we invoke cbuildbot correctly.""" |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 123 | self.PatchObject( |
| 124 | cros_build_lib, 'GetTargetChromiteApiVersion', autospec=True, |
| 125 | return_value=version) |
| 126 | |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 127 | cbuildbot_launch.Cbuildbot('/cbuildbot_buildroot', '/depot_tools', args) |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 128 | |
| 129 | self.assertCommandCalled( |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 130 | expected_cmd, extra_env={'PATH': mock.ANY}, |
| 131 | cwd='/cbuildbot_buildroot', error_code_ok=True) |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 132 | |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 133 | def testCbuildbotSimple(self): |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 134 | """Ensure we invoke cbuildbot correctly.""" |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 135 | self.verifyCbuildbot( |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 136 | self.ARGS_BASE + self.ARGS_CONFIG, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 137 | self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE, |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 138 | (0, 4)) |
| 139 | |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 140 | def testCbuildbotNotFiltered(self): |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 141 | """Ensure we invoke cbuildbot correctly.""" |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 142 | self.verifyCbuildbot( |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 143 | self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 144 | (self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE + |
| 145 | self.ARGS_GIT_CACHE), |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 146 | (0, 4)) |
| 147 | |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 148 | def testCbuildbotFiltered(self): |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 149 | """Ensure we invoke cbuildbot correctly.""" |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 150 | self.verifyCbuildbot( |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 151 | self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 152 | self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE, |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 153 | (0, 2)) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 154 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 155 | def testMainMin(self): |
| 156 | """Test a minimal set of command line options.""" |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 157 | self.PatchObject(osutils, 'SafeMakedirs', autospec=True) |
| 158 | self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion', |
Don Garrett | 861e918 | 2017-05-15 15:30:23 -0700 | [diff] [blame] | 159 | autospec=True, return_value=(constants.REEXEC_API_MAJOR, |
| 160 | constants.REEXEC_API_MINOR)) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 161 | mock_repo = mock.MagicMock() |
| 162 | mock_repo.branch = 'master' |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 163 | mock_repo.directory = '/root/repository' |
| 164 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 165 | mock_repo_create = self.PatchObject(repository, 'RepoRepository', |
| 166 | autospec=True, return_value=mock_repo) |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 167 | mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot', |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 168 | autospec=True) |
| 169 | mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout', |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 170 | autospec=True) |
Benjamin Gordon | aee36b8 | 2018-02-05 14:25:26 -0700 | [diff] [blame] | 171 | mock_cleanup_chroot = self.PatchObject(cbuildbot_launch, 'CleanupChroot', |
| 172 | autospec=True) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 173 | mock_set_last_build_state = self.PatchObject( |
| 174 | cbuildbot_launch, 'SetLastBuildState', autospec=True) |
| 175 | |
| 176 | expected_build_state = build_summary.BuildSummary( |
| 177 | build_number=0, master_build_id=0, status=mock.ANY) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 178 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 179 | cbuildbot_launch._main(['-r', '/root', 'config']) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 180 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 181 | # Did we create the repo instance correctly? |
| 182 | self.assertEqual(mock_repo_create.mock_calls, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 183 | [mock.call(EXPECTED_MANIFEST_URL, '/root/repository', |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 184 | git_cache_dir=None, branch='master')]) |
| 185 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 186 | # Ensure we clean, as expected. |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 187 | self.assertEqual(mock_clean.mock_calls, [ |
Don Garrett | d1d90dd | 2017-06-13 17:35:52 -0700 | [diff] [blame] | 188 | mock.call('/root', mock_repo, |
| 189 | { |
| 190 | 'branch_name': 'master', |
| 191 | 'tryjob': False, |
| 192 | 'build_config': 'config', |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 193 | }, |
| 194 | expected_build_state)]) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 195 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 196 | # Ensure we checkout, as expected. |
| 197 | self.assertEqual(mock_checkout.mock_calls, |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 198 | [mock.call(mock_repo)]) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 199 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 200 | # Ensure we invoke cbuildbot, as expected. |
| 201 | self.assertCommandCalled( |
Don Garrett | 5cd946b | 2017-07-20 13:42:20 -0700 | [diff] [blame] | 202 | [ |
| 203 | '/root/repository/chromite/bin/cbuildbot', |
| 204 | 'config', |
| 205 | '-r', '/root/repository', |
| 206 | '--ts-mon-task-num', '1', |
| 207 | ], |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 208 | extra_env={'PATH': mock.ANY}, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 209 | cwd='/root/repository', |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 210 | error_code_ok=True) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 211 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 212 | # Ensure we saved the final state, as expected. |
| 213 | self.assertEqual(expected_build_state.status, |
| 214 | constants.BUILDER_STATUS_PASSED) |
| 215 | self.assertEqual(mock_set_last_build_state.mock_calls, [ |
| 216 | mock.call('/root', expected_build_state)]) |
| 217 | |
Benjamin Gordon | aee36b8 | 2018-02-05 14:25:26 -0700 | [diff] [blame] | 218 | # Ensure we clean the chroot, as expected. |
| 219 | self.assertEqual(mock_cleanup_chroot.mock_calls, [ |
| 220 | mock.call('/root/repository')]) |
| 221 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 222 | def testMainMax(self): |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 223 | """Test a larger set of command line options.""" |
| 224 | self.PatchObject(osutils, 'SafeMakedirs', autospec=True) |
| 225 | self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion', |
Don Garrett | 861e918 | 2017-05-15 15:30:23 -0700 | [diff] [blame] | 226 | autospec=True, return_value=(constants.REEXEC_API_MAJOR, |
| 227 | constants.REEXEC_API_MINOR)) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 228 | mock_repo = mock.MagicMock() |
| 229 | mock_repo.branch = 'branch' |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 230 | mock_repo.directory = '/root/repository' |
| 231 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 232 | mock_summary = build_summary.BuildSummary() |
| 233 | mock_summary.build_number = 313 |
| 234 | mock_summary.master_build_id = 123123123 |
| 235 | mock_summary.status = constants.BUILDER_STATUS_FAILED |
| 236 | |
| 237 | mock_get_last_build_state = self.PatchObject( |
| 238 | cbuildbot_launch, 'GetLastBuildState', autospec=True, |
| 239 | return_value=mock_summary) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 240 | mock_repo_create = self.PatchObject(repository, 'RepoRepository', |
| 241 | autospec=True, return_value=mock_repo) |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 242 | mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot', |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 243 | autospec=True) |
| 244 | mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout', |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 245 | autospec=True) |
Benjamin Gordon | aee36b8 | 2018-02-05 14:25:26 -0700 | [diff] [blame] | 246 | mock_cleanup_chroot = self.PatchObject(cbuildbot_launch, 'CleanupChroot', |
| 247 | autospec=True) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 248 | mock_set_last_build_state = self.PatchObject( |
| 249 | cbuildbot_launch, 'SetLastBuildState', autospec=True) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 250 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 251 | cbuildbot_launch._main(['--buildroot', '/root', |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 252 | '--branch', 'branch', |
| 253 | '--git-cache-dir', '/git-cache', |
Don Garrett | d1d90dd | 2017-06-13 17:35:52 -0700 | [diff] [blame] | 254 | '--remote-trybot', |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 255 | '--master-build-id', '123456789', |
| 256 | '--buildnumber', '314', |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 257 | 'config']) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 258 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 259 | # Did we create the repo instance correctly? |
| 260 | self.assertEqual(mock_repo_create.mock_calls, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 261 | [mock.call(EXPECTED_MANIFEST_URL, '/root/repository', |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 262 | git_cache_dir='/git-cache', branch='branch')]) |
| 263 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 264 | # Ensure we look up the previous status. |
| 265 | self.assertEqual(mock_get_last_build_state.mock_calls, [ |
| 266 | mock.call('/root')]) |
| 267 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 268 | # Ensure we clean, as expected. |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 269 | self.assertEqual(mock_clean.mock_calls, [ |
Don Garrett | d1d90dd | 2017-06-13 17:35:52 -0700 | [diff] [blame] | 270 | mock.call('/root', |
| 271 | mock_repo, |
| 272 | { |
| 273 | 'branch_name': 'branch', |
| 274 | 'tryjob': True, |
| 275 | 'build_config': 'config', |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 276 | }, |
| 277 | build_summary.BuildSummary( |
| 278 | build_number=314, |
| 279 | master_build_id=123456789, |
| 280 | status=mock.ANY |
| 281 | ))]) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 282 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 283 | # Ensure we checkout, as expected. |
| 284 | self.assertEqual(mock_checkout.mock_calls, |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 285 | [mock.call(mock_repo)]) |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 286 | |
| 287 | # Ensure we invoke cbuildbot, as expected. |
| 288 | self.assertCommandCalled( |
Don Garrett | 5cd946b | 2017-07-20 13:42:20 -0700 | [diff] [blame] | 289 | [ |
| 290 | '/root/repository/chromite/bin/cbuildbot', |
| 291 | 'config', |
| 292 | '--buildroot', '/root/repository', |
| 293 | '--branch', 'branch', |
| 294 | '--git-cache-dir', '/git-cache', |
| 295 | '--remote-trybot', |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 296 | '--master-build-id', '123456789', |
| 297 | '--buildnumber', '314', |
| 298 | '--previous-build-state', |
| 299 | 'eyJzdGF0dXMiOiAiZmFpbCIsICJtYXN0ZXJfYnVpbGRfaWQiOiAxM' |
| 300 | 'jMxMjMxMjMsICJidWlsZF9udW1iZXIiOiAzMTN9', |
Don Garrett | 5cd946b | 2017-07-20 13:42:20 -0700 | [diff] [blame] | 301 | '--ts-mon-task-num', '1', |
| 302 | ], |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 303 | extra_env={'PATH': mock.ANY}, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 304 | cwd='/root/repository', |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 305 | error_code_ok=True) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 306 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 307 | # Ensure we write the final build state, as expected. |
| 308 | final_state = build_summary.BuildSummary( |
| 309 | build_number=314, |
| 310 | master_build_id=123456789, |
| 311 | status=constants.BUILDER_STATUS_PASSED) |
| 312 | self.assertEqual(mock_set_last_build_state.mock_calls, [ |
| 313 | mock.call('/root', final_state)]) |
| 314 | |
Benjamin Gordon | aee36b8 | 2018-02-05 14:25:26 -0700 | [diff] [blame] | 315 | # Ensure we clean the chroot, as expected. |
| 316 | self.assertEqual(mock_cleanup_chroot.mock_calls, [ |
| 317 | mock.call('/root/repository')]) |
| 318 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 319 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 320 | class CleanBuildRootTest(cros_test_lib.MockTempDirTestCase): |
| 321 | """Tests for CleanBuildRoot method.""" |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 322 | |
| 323 | def setUp(self): |
| 324 | """Create standard buildroot contents for cleanup.""" |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 325 | self.root = os.path.join(self.tempdir) |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 326 | self.state = os.path.join(self.root, '.cbuildbot_launch_state') |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 327 | self.previous_build_state = os.path.join( |
| 328 | self.root, '.cbuildbot_build_state.json') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 329 | self.buildroot = os.path.join(self.root, 'buildroot') |
| 330 | self.repo = os.path.join(self.buildroot, '.repo/repo') |
| 331 | self.chroot = os.path.join(self.buildroot, 'chroot/chroot') |
| 332 | self.general = os.path.join(self.buildroot, 'general/general') |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 333 | self.cache = os.path.join(self.buildroot, '.cache') |
| 334 | self.distfiles = os.path.join(self.cache, 'distfiles') |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 335 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 336 | self.mock_repo = mock.MagicMock() |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 337 | self.mock_repo.directory = self.buildroot |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 338 | |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 339 | self.metrics = {} |
| 340 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 341 | def populateBuildroot(self, state=None, previous_build_state=None): |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 342 | """Create standard buildroot contents for cleanup.""" |
| 343 | if state: |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 344 | osutils.SafeMakedirs(self.root) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 345 | osutils.WriteFile(self.state, state) |
| 346 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 347 | if previous_build_state: |
| 348 | osutils.SafeMakedirs(self.root) |
| 349 | osutils.WriteFile(self.previous_build_state, previous_build_state) |
| 350 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 351 | # Create files. |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 352 | for f in (self.repo, self.chroot, self.general, self.distfiles): |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 353 | osutils.Touch(f, makedirs=True) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 354 | |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 355 | def testNoBuildroot(self): |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 356 | """Test CleanBuildRoot with no history.""" |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 357 | self.mock_repo.branch = 'master' |
| 358 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 359 | build_state = build_summary.BuildSummary() |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 360 | cbuildbot_launch.CleanBuildRoot( |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 361 | self.root, self.mock_repo, self.metrics, build_state) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 362 | |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 363 | version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 364 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 365 | self.assertEqual(version, 2) |
| 366 | self.assertEqual(branch, 'master') |
| 367 | self.assertIsNotNone(distfiles_ts) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 368 | self.assertEqual(new_summary, build_state) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 369 | |
| 370 | def testBuildrootNoState(self): |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 371 | """Test CleanBuildRoot with no state information.""" |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 372 | self.populateBuildroot() |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 373 | self.mock_repo.branch = 'master' |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 374 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 375 | build_state = build_summary.BuildSummary() |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 376 | cbuildbot_launch.CleanBuildRoot( |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 377 | self.root, self.mock_repo, self.metrics, build_state) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 378 | |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 379 | version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 380 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 381 | self.assertEqual(version, 2) |
| 382 | self.assertEqual(branch, 'master') |
| 383 | self.assertIsNotNone(distfiles_ts) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 384 | self.assertEqual(new_summary, build_state) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 385 | |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 386 | self.assertNotExists(self.repo) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 387 | self.assertNotExists(self.chroot) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 388 | self.assertNotExists(self.general) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 389 | self.assertNotExists(self.distfiles) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 390 | |
| 391 | def testBuildrootFormatMismatch(self): |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 392 | """Test CleanBuildRoot with no state information.""" |
Don Garrett | 125d4dc | 2017-04-25 16:26:03 -0700 | [diff] [blame] | 393 | self.populateBuildroot('0 master') |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 394 | self.mock_repo.branch = 'master' |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 395 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 396 | build_state = build_summary.BuildSummary() |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 397 | cbuildbot_launch.CleanBuildRoot( |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 398 | self.root, self.mock_repo, self.metrics, build_state) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 399 | |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 400 | version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 401 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 402 | self.assertEqual(version, 2) |
| 403 | self.assertEqual(branch, 'master') |
| 404 | self.assertIsNotNone(distfiles_ts) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 405 | self.assertEqual(new_summary, build_state) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 406 | |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 407 | self.assertNotExists(self.repo) |
| 408 | self.assertNotExists(self.chroot) |
| 409 | self.assertNotExists(self.general) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 410 | self.assertNotExists(self.distfiles) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 411 | |
| 412 | def testBuildrootBranchChange(self): |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 413 | """Test CleanBuildRoot with a change in branches.""" |
| 414 | self.populateBuildroot('2 branchA') |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 415 | self.mock_repo.branch = 'branchB' |
Benjamin Gordon | 59ba2f8 | 2017-08-28 15:31:06 -0600 | [diff] [blame] | 416 | m = self.PatchObject(cros_build_lib, 'CleanupChrootMount') |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 417 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 418 | build_state = build_summary.BuildSummary() |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 419 | cbuildbot_launch.CleanBuildRoot( |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 420 | self.root, self.mock_repo, self.metrics, build_state) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 421 | |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 422 | version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 423 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 424 | self.assertEqual(version, 2) |
| 425 | self.assertEqual(branch, 'branchB') |
| 426 | self.assertIsNotNone(distfiles_ts) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 427 | self.assertEqual(new_summary, build_state) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 428 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 429 | self.assertExists(self.repo) |
| 430 | self.assertNotExists(self.chroot) |
| 431 | self.assertExists(self.general) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 432 | self.assertNotExists(self.distfiles) |
Benjamin Gordon | 59ba2f8 | 2017-08-28 15:31:06 -0600 | [diff] [blame] | 433 | m.assert_called() |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 434 | |
| 435 | def testBuildrootBranchMatch(self): |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 436 | """Test CleanBuildRoot with no change in branch.""" |
| 437 | self.populateBuildroot('2 branchA') |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 438 | self.mock_repo.branch = 'branchA' |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 439 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 440 | build_state = build_summary.BuildSummary() |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 441 | cbuildbot_launch.CleanBuildRoot( |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 442 | self.root, self.mock_repo, self.metrics, build_state) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 443 | |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 444 | version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 445 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 446 | self.assertEqual(version, 2) |
| 447 | self.assertEqual(branch, 'branchA') |
| 448 | self.assertIsNotNone(distfiles_ts) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 449 | self.assertEqual(new_summary, build_state) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 450 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 451 | self.assertExists(self.repo) |
| 452 | self.assertExists(self.chroot) |
| 453 | self.assertExists(self.general) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 454 | self.assertExists(self.distfiles) |
| 455 | |
| 456 | def testBuildrootDistfilesRecentCache(self): |
| 457 | """Test CleanBuildRoot does not delete distfiles when cache is recent.""" |
| 458 | seed_distfiles_ts = time.time() - 60 |
| 459 | self.populateBuildroot('2 branchA %f' % seed_distfiles_ts) |
| 460 | self.mock_repo.branch = 'branchA' |
| 461 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 462 | build_state = build_summary.BuildSummary() |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 463 | cbuildbot_launch.CleanBuildRoot( |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 464 | self.root, self.mock_repo, self.metrics, build_state) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 465 | |
| 466 | version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 467 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 468 | self.assertEqual(version, 2) |
| 469 | self.assertEqual(branch, 'branchA') |
| 470 | # Same cache creation timestamp is rewritten to state. |
| 471 | self.assertEqual(distfiles_ts, seed_distfiles_ts) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 472 | self.assertEqual(new_summary, build_state) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 473 | |
| 474 | self.assertExists(self.repo) |
| 475 | self.assertExists(self.chroot) |
| 476 | self.assertExists(self.general) |
| 477 | self.assertExists(self.distfiles) |
| 478 | |
| 479 | def testBuildrootDistfilesCacheExpired(self): |
| 480 | """Test CleanBuildRoot when the distfiles cache is too old.""" |
| 481 | self.populateBuildroot('2 branchA 100.000000') |
| 482 | self.mock_repo.branch = 'branchA' |
| 483 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 484 | build_state = build_summary.BuildSummary() |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 485 | cbuildbot_launch.CleanBuildRoot( |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 486 | self.root, self.mock_repo, self.metrics, build_state) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 487 | |
| 488 | version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 489 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 490 | self.assertEqual(version, 2) |
| 491 | self.assertEqual(branch, 'branchA') |
| 492 | self.assertIsNotNone(distfiles_ts) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 493 | self.assertEqual(new_summary, build_state) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 494 | |
| 495 | self.assertExists(self.repo) |
| 496 | self.assertExists(self.chroot) |
| 497 | self.assertExists(self.general) |
| 498 | self.assertNotExists(self.distfiles) |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 499 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 500 | def testBuildrootRepoCleanFailure(self): |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 501 | """Test CleanBuildRoot with repo checkout failure.""" |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 502 | self.populateBuildroot('1 branchA') |
| 503 | self.mock_repo.branch = 'branchA' |
| 504 | self.mock_repo.BuildRootGitCleanup.side_effect = Exception |
| 505 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 506 | build_state = build_summary.BuildSummary() |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 507 | cbuildbot_launch.CleanBuildRoot( |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 508 | self.root, self.mock_repo, self.metrics, build_state) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 509 | |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 510 | version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 511 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 512 | self.assertEqual(version, 2) |
| 513 | self.assertEqual(branch, 'branchA') |
| 514 | self.assertIsNotNone(distfiles_ts) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 515 | self.assertEqual(new_summary, build_state) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 516 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 517 | self.assertNotExists(self.repo) |
| 518 | self.assertNotExists(self.chroot) |
| 519 | self.assertNotExists(self.general) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 520 | self.assertNotExists(self.distfiles) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 521 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 522 | def testGetState(self): |
| 523 | """Test GetState.""" |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 524 | # No root dir. |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 525 | results = cbuildbot_launch.GetState(self.root) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 526 | self.assertEqual(results, (0, '', None)) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 527 | |
| 528 | # Empty root dir. |
| 529 | osutils.SafeMakedirs(self.root) |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 530 | results = cbuildbot_launch.GetState(self.root) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 531 | self.assertEqual(results, (0, '', None)) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 532 | |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 533 | # Empty contents |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 534 | osutils.WriteFile(self.state, '') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 535 | results = cbuildbot_launch.GetState(self.root) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 536 | self.assertEqual(results, (0, '', None)) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 537 | |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 538 | # Old format contents |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 539 | osutils.WriteFile(self.state, 'happy-branch') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 540 | results = cbuildbot_launch.GetState(self.root) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 541 | self.assertEqual(results, (0, '', None)) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 542 | |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 543 | # Expected contents, without distfiles timestamp |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 544 | osutils.WriteFile(self.state, '1 happy-branch') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 545 | results = cbuildbot_launch.GetState(self.root) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 546 | self.assertEqual(results, (1, 'happy-branch', None)) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 547 | |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 548 | # Expected contents |
| 549 | osutils.WriteFile(self.state, '1 happy-branch 1000.33') |
| 550 | version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root) |
| 551 | self.assertEqual(version, 1) |
| 552 | self.assertEqual(branch, 'happy-branch') |
| 553 | self.assertEqual(distfiles_ts, 1000.33) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 554 | |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 555 | # Future layout version contents |
| 556 | osutils.WriteFile(self.state, '22 happy-branch 222') |
| 557 | version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root) |
| 558 | self.assertEqual(version, 22) |
| 559 | self.assertEqual(branch, 'happy-branch') |
| 560 | self.assertEqual(distfiles_ts, 222) |
| 561 | |
| 562 | # Read write |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 563 | cbuildbot_launch.SetState('happy-branch', self.root) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 564 | version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root) |
| 565 | self.assertEqual(version, 2) |
| 566 | self.assertEqual(branch, 'happy-branch') |
| 567 | self.assertIsNotNone(distfiles_ts) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 568 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 569 | def testSetState(self): |
| 570 | """Test SetState.""" |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 571 | # Write out a state file. |
| 572 | osutils.SafeMakedirs(self.root) |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 573 | cbuildbot_launch.SetState('happy-branch', self.root) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 574 | state_file_parts = osutils.ReadFile(self.state).split() |
| 575 | self.assertEqual(state_file_parts[:2], ['2', 'happy-branch']) |
| 576 | # Will flake if this test takes > 1 hour to run. |
| 577 | self.assertGreater(float(state_file_parts[2]), time.time() - 3600) |
| 578 | |
| 579 | # Explicitly provide a timestamp |
| 580 | cbuildbot_launch.SetState('happy-branch', self.root, |
| 581 | 333.33) |
| 582 | state_file_parts = osutils.ReadFile(self.state).split() |
| 583 | self.assertEqual(state_file_parts, |
| 584 | ['2', 'happy-branch', '333.330000']) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 585 | |
| 586 | # Change to a future version. |
| 587 | self.PatchObject(cbuildbot_launch, 'BUILDROOT_BUILDROOT_LAYOUT', 22) |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 588 | cbuildbot_launch.SetState('happy-branch', self.root) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 589 | state_file_parts = osutils.ReadFile(self.state).split() |
| 590 | self.assertEqual(state_file_parts[:2], ['22', 'happy-branch']) |
| 591 | # Will flake if this test takes > 1 hour to run. |
| 592 | self.assertGreater(float(state_file_parts[2]), time.time() - 3600) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 593 | |
| 594 | def testGetCurrentBuildStateNoArgs(self): |
| 595 | """Tests GetCurrentBuildState without arguments.""" |
| 596 | options = cbuildbot_launch.PreParseArguments([ |
| 597 | '--buildroot', self.root, 'config' |
| 598 | ]) |
| 599 | state = cbuildbot_launch.GetCurrentBuildState(options) |
| 600 | |
| 601 | expected_state = build_summary.BuildSummary( |
| 602 | status=constants.BUILDER_STATUS_INFLIGHT) |
| 603 | self.assertEqual(state, expected_state) |
| 604 | |
| 605 | def testGetCurrentBuildStateHasArgs(self): |
| 606 | """Tests GetCurrentBuildState with arguments.""" |
| 607 | options = cbuildbot_launch.PreParseArguments([ |
| 608 | '--buildroot', self.root, |
| 609 | '--buildnumber', '20', |
| 610 | '--master-build-id', '50', |
| 611 | 'config' |
| 612 | ]) |
| 613 | state = cbuildbot_launch.GetCurrentBuildState(options) |
| 614 | |
| 615 | expected_state = build_summary.BuildSummary( |
| 616 | build_number=20, |
| 617 | master_build_id=50, |
| 618 | status=constants.BUILDER_STATUS_INFLIGHT) |
| 619 | self.assertEqual(state, expected_state) |
| 620 | |
| 621 | def testGetLastBuildStateNoFile(self): |
| 622 | """Tests GetLastBuildState if the file is missing.""" |
| 623 | osutils.SafeMakedirs(self.root) |
| 624 | state = cbuildbot_launch.GetLastBuildState(self.root) |
| 625 | self.assertEqual(state, build_summary.BuildSummary()) |
| 626 | |
| 627 | def testGetLastBuildStateBadFile(self): |
| 628 | """Tests GetLastBuildState if the file contains invalid JSON.""" |
| 629 | osutils.SafeMakedirs(self.root) |
| 630 | osutils.WriteFile(self.previous_build_state, '}}') |
| 631 | state = cbuildbot_launch.GetLastBuildState(self.root) |
| 632 | self.assertEqual(state, build_summary.BuildSummary()) |
| 633 | |
| 634 | def testGetLastBuildStateMissingBuildStatus(self): |
| 635 | """Tests GetLastBuildState if the file doesn't have a valid status.""" |
| 636 | osutils.SafeMakedirs(self.root) |
| 637 | osutils.WriteFile(self.previous_build_state, '{"build_number": "3"}') |
| 638 | state = cbuildbot_launch.GetLastBuildState(self.root) |
| 639 | self.assertEqual(state, build_summary.BuildSummary()) |
| 640 | |
| 641 | def testGetLastBuildStateGoodFile(self): |
| 642 | """Tests GetLastBuildState on a good file.""" |
| 643 | osutils.SafeMakedirs(self.root) |
| 644 | osutils.WriteFile( |
| 645 | self.previous_build_state, |
| 646 | '{"build_number": 1, "master_build_id": 3, "status": "pass"}') |
| 647 | state = cbuildbot_launch.GetLastBuildState(self.root) |
| 648 | self.assertEqual( |
| 649 | state, |
| 650 | build_summary.BuildSummary( |
| 651 | build_number=1, master_build_id=3, status='pass')) |
| 652 | |
| 653 | def testSetLastBuildState(self): |
| 654 | """Verifies that SetLastBuildState writes to the expected file.""" |
| 655 | osutils.SafeMakedirs(self.root) |
| 656 | old_state = build_summary.BuildSummary( |
| 657 | build_number=314, |
| 658 | master_build_id=2178, |
| 659 | status=constants.BUILDER_STATUS_PASSED) |
| 660 | cbuildbot_launch.SetLastBuildState(self.root, old_state) |
| 661 | |
| 662 | saved_state = osutils.ReadFile(self.previous_build_state) |
| 663 | new_state = build_summary.BuildSummary() |
| 664 | new_state.from_json(saved_state) |
| 665 | |
| 666 | self.assertEqual(old_state, new_state) |