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 |
Benjamin Gordon | 7464523 | 2018-05-04 17:40:42 -0600 | [diff] [blame] | 18 | from chromite.lib import cros_sdk_lib |
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 | |
Benjamin Gordon | 121a2aa | 2018-05-04 16:24:45 -0600 | [diff] [blame] | 93 | class RunDepotToolsEnsureBootstrap(cros_test_lib.RunCommandTestCase, |
Don Garrett | 066e6f5 | 2017-09-28 19:14:01 -0700 | [diff] [blame] | 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 | |
Benjamin Gordon | 121a2aa | 2018-05-04 16:24:45 -0600 | [diff] [blame] | 112 | class RunTests(cros_test_lib.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( |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 177 | build_number=0, master_build_id=0, status=mock.ANY, |
| 178 | buildroot_layout=2, branch='master') |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 179 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 180 | cbuildbot_launch._main(['-r', '/root', 'config']) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 181 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 182 | # Did we create the repo instance correctly? |
| 183 | self.assertEqual(mock_repo_create.mock_calls, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 184 | [mock.call(EXPECTED_MANIFEST_URL, '/root/repository', |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 185 | git_cache_dir=None, branch='master')]) |
| 186 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 187 | # Ensure we clean, as expected. |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 188 | self.assertEqual(mock_clean.mock_calls, [ |
Don Garrett | d1d90dd | 2017-06-13 17:35:52 -0700 | [diff] [blame] | 189 | mock.call('/root', mock_repo, |
| 190 | { |
| 191 | 'branch_name': 'master', |
| 192 | 'tryjob': False, |
| 193 | 'build_config': 'config', |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 194 | }, |
| 195 | expected_build_state)]) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 196 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 197 | # Ensure we checkout, as expected. |
| 198 | self.assertEqual(mock_checkout.mock_calls, |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 199 | [mock.call(mock_repo)]) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 200 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 201 | # Ensure we invoke cbuildbot, as expected. |
| 202 | self.assertCommandCalled( |
Don Garrett | 5cd946b | 2017-07-20 13:42:20 -0700 | [diff] [blame] | 203 | [ |
| 204 | '/root/repository/chromite/bin/cbuildbot', |
| 205 | 'config', |
| 206 | '-r', '/root/repository', |
Don Garrett | b497f55 | 2018-07-09 16:01:13 -0700 | [diff] [blame^] | 207 | '--workspace', '/root/workspace', |
Don Garrett | 5cd946b | 2017-07-20 13:42:20 -0700 | [diff] [blame] | 208 | '--ts-mon-task-num', '1', |
| 209 | ], |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 210 | extra_env={'PATH': mock.ANY}, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 211 | cwd='/root/repository', |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 212 | error_code_ok=True) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 213 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 214 | # Ensure we saved the final state, as expected. |
| 215 | self.assertEqual(expected_build_state.status, |
| 216 | constants.BUILDER_STATUS_PASSED) |
| 217 | self.assertEqual(mock_set_last_build_state.mock_calls, [ |
| 218 | mock.call('/root', expected_build_state)]) |
| 219 | |
Benjamin Gordon | aee36b8 | 2018-02-05 14:25:26 -0700 | [diff] [blame] | 220 | # Ensure we clean the chroot, as expected. |
| 221 | self.assertEqual(mock_cleanup_chroot.mock_calls, [ |
| 222 | mock.call('/root/repository')]) |
| 223 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 224 | def testMainMax(self): |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 225 | """Test a larger set of command line options.""" |
| 226 | self.PatchObject(osutils, 'SafeMakedirs', autospec=True) |
| 227 | self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion', |
Don Garrett | 861e918 | 2017-05-15 15:30:23 -0700 | [diff] [blame] | 228 | autospec=True, return_value=(constants.REEXEC_API_MAJOR, |
| 229 | constants.REEXEC_API_MINOR)) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 230 | mock_repo = mock.MagicMock() |
| 231 | mock_repo.branch = 'branch' |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 232 | mock_repo.directory = '/root/repository' |
| 233 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 234 | mock_summary = build_summary.BuildSummary( |
| 235 | build_number=313, |
| 236 | master_build_id=123123123, |
| 237 | status=constants.BUILDER_STATUS_FAILED, |
| 238 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 239 | branch='branch') |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 240 | |
| 241 | mock_get_last_build_state = self.PatchObject( |
| 242 | cbuildbot_launch, 'GetLastBuildState', autospec=True, |
| 243 | return_value=mock_summary) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 244 | mock_repo_create = self.PatchObject(repository, 'RepoRepository', |
| 245 | autospec=True, return_value=mock_repo) |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 246 | mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot', |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 247 | autospec=True) |
| 248 | mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout', |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 249 | autospec=True) |
Benjamin Gordon | aee36b8 | 2018-02-05 14:25:26 -0700 | [diff] [blame] | 250 | mock_cleanup_chroot = self.PatchObject(cbuildbot_launch, 'CleanupChroot', |
| 251 | autospec=True) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 252 | mock_set_last_build_state = self.PatchObject( |
| 253 | cbuildbot_launch, 'SetLastBuildState', autospec=True) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 254 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 255 | cbuildbot_launch._main(['--buildroot', '/root', |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 256 | '--branch', 'branch', |
| 257 | '--git-cache-dir', '/git-cache', |
Don Garrett | d1d90dd | 2017-06-13 17:35:52 -0700 | [diff] [blame] | 258 | '--remote-trybot', |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 259 | '--master-build-id', '123456789', |
| 260 | '--buildnumber', '314', |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 261 | 'config']) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 262 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 263 | # Did we create the repo instance correctly? |
| 264 | self.assertEqual(mock_repo_create.mock_calls, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 265 | [mock.call(EXPECTED_MANIFEST_URL, '/root/repository', |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 266 | git_cache_dir='/git-cache', branch='branch')]) |
| 267 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 268 | # Ensure we look up the previous status. |
| 269 | self.assertEqual(mock_get_last_build_state.mock_calls, [ |
| 270 | mock.call('/root')]) |
| 271 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 272 | # Ensure we clean, as expected. |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 273 | self.assertEqual(mock_clean.mock_calls, [ |
Don Garrett | d1d90dd | 2017-06-13 17:35:52 -0700 | [diff] [blame] | 274 | mock.call('/root', |
| 275 | mock_repo, |
| 276 | { |
| 277 | 'branch_name': 'branch', |
| 278 | 'tryjob': True, |
| 279 | 'build_config': 'config', |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 280 | }, |
| 281 | build_summary.BuildSummary( |
| 282 | build_number=314, |
| 283 | master_build_id=123456789, |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 284 | status=mock.ANY, |
| 285 | branch='branch', |
| 286 | buildroot_layout=2 |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 287 | ))]) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 288 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 289 | # Ensure we checkout, as expected. |
| 290 | self.assertEqual(mock_checkout.mock_calls, |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 291 | [mock.call(mock_repo)]) |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 292 | |
| 293 | # Ensure we invoke cbuildbot, as expected. |
| 294 | self.assertCommandCalled( |
Don Garrett | 5cd946b | 2017-07-20 13:42:20 -0700 | [diff] [blame] | 295 | [ |
| 296 | '/root/repository/chromite/bin/cbuildbot', |
| 297 | 'config', |
| 298 | '--buildroot', '/root/repository', |
| 299 | '--branch', 'branch', |
| 300 | '--git-cache-dir', '/git-cache', |
| 301 | '--remote-trybot', |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 302 | '--master-build-id', '123456789', |
| 303 | '--buildnumber', '314', |
| 304 | '--previous-build-state', |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 305 | 'eyJzdGF0dXMiOiAiZmFpbCIsICJtYXN0ZXJfYnVpbGRfaWQiOiAxMjMxMjMxMj' |
| 306 | 'MsICJidWlsZF9udW1iZXIiOiAzMTMsICJidWlsZHJvb3RfbGF5b3V0IjogMiwg' |
| 307 | 'ImJyYW5jaCI6ICJicmFuY2gifQ==', |
Don Garrett | b497f55 | 2018-07-09 16:01:13 -0700 | [diff] [blame^] | 308 | '--workspace', '/root/workspace', |
Don Garrett | 5cd946b | 2017-07-20 13:42:20 -0700 | [diff] [blame] | 309 | '--ts-mon-task-num', '1', |
| 310 | ], |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 311 | extra_env={'PATH': mock.ANY}, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 312 | cwd='/root/repository', |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 313 | error_code_ok=True) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 314 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 315 | # Ensure we write the final build state, as expected. |
| 316 | final_state = build_summary.BuildSummary( |
| 317 | build_number=314, |
| 318 | master_build_id=123456789, |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 319 | status=constants.BUILDER_STATUS_PASSED, |
| 320 | buildroot_layout=2, |
| 321 | branch='branch') |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 322 | self.assertEqual(mock_set_last_build_state.mock_calls, [ |
| 323 | mock.call('/root', final_state)]) |
| 324 | |
Benjamin Gordon | aee36b8 | 2018-02-05 14:25:26 -0700 | [diff] [blame] | 325 | # Ensure we clean the chroot, as expected. |
| 326 | self.assertEqual(mock_cleanup_chroot.mock_calls, [ |
| 327 | mock.call('/root/repository')]) |
| 328 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 329 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 330 | class CleanBuildRootTest(cros_test_lib.MockTempDirTestCase): |
| 331 | """Tests for CleanBuildRoot method.""" |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 332 | |
| 333 | def setUp(self): |
| 334 | """Create standard buildroot contents for cleanup.""" |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 335 | self.root = os.path.join(self.tempdir) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 336 | self.previous_build_state = os.path.join( |
| 337 | self.root, '.cbuildbot_build_state.json') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 338 | self.buildroot = os.path.join(self.root, 'buildroot') |
| 339 | self.repo = os.path.join(self.buildroot, '.repo/repo') |
Don Garrett | 3665011 | 2018-06-28 15:54:34 -0700 | [diff] [blame] | 340 | self.chroot = os.path.join(self.buildroot, 'chroot') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 341 | self.general = os.path.join(self.buildroot, 'general/general') |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 342 | self.cache = os.path.join(self.buildroot, '.cache') |
| 343 | self.distfiles = os.path.join(self.cache, 'distfiles') |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 344 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 345 | self.mock_repo = mock.MagicMock() |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 346 | self.mock_repo.directory = self.buildroot |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 347 | |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 348 | self.metrics = {} |
| 349 | |
Benjamin Gordon | 8642bcc | 2018-05-01 13:49:56 -0600 | [diff] [blame] | 350 | def populateBuildroot(self, previous_build_state=None): |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 351 | """Create standard buildroot contents for cleanup.""" |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 352 | if previous_build_state: |
| 353 | osutils.SafeMakedirs(self.root) |
| 354 | osutils.WriteFile(self.previous_build_state, previous_build_state) |
| 355 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 356 | # Create files. |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 357 | for f in (self.repo, self.chroot, self.general, self.distfiles): |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 358 | osutils.Touch(f, makedirs=True) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 359 | |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 360 | def testNoBuildroot(self): |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 361 | """Test CleanBuildRoot with no history.""" |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 362 | self.mock_repo.branch = 'master' |
| 363 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 364 | build_state = build_summary.BuildSummary( |
| 365 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 366 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 367 | branch='master') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 368 | cbuildbot_launch.CleanBuildRoot( |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 369 | self.root, self.mock_repo, self.metrics, build_state) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 370 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 371 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 372 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 373 | self.assertEqual(new_summary.branch, 'master') |
| 374 | self.assertIsNotNone(new_summary.distfiles_ts) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 375 | self.assertEqual(new_summary, build_state) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 376 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 377 | self.assertExists(self.previous_build_state) |
| 378 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 379 | def testBuildrootNoState(self): |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 380 | """Test CleanBuildRoot with no state information.""" |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 381 | self.populateBuildroot() |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 382 | self.mock_repo.branch = 'master' |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 383 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 384 | build_state = build_summary.BuildSummary( |
| 385 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 386 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 387 | branch='master') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 388 | cbuildbot_launch.CleanBuildRoot( |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 389 | self.root, self.mock_repo, self.metrics, build_state) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 390 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 391 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 392 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 393 | self.assertEqual(new_summary.branch, 'master') |
| 394 | self.assertIsNotNone(new_summary.distfiles_ts) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 395 | self.assertEqual(new_summary, build_state) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 396 | |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 397 | self.assertNotExists(self.repo) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 398 | self.assertNotExists(self.chroot) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 399 | self.assertNotExists(self.general) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 400 | self.assertNotExists(self.distfiles) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 401 | self.assertExists(self.previous_build_state) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 402 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 403 | def testBuildrootFormatMismatch(self): |
| 404 | """Test CleanBuildRoot with buildroot layout mismatch.""" |
| 405 | old_build_state = build_summary.BuildSummary( |
| 406 | status=constants.BUILDER_STATUS_PASSED, |
| 407 | buildroot_layout=1, |
| 408 | branch='master') |
| 409 | self.populateBuildroot(previous_build_state=old_build_state.to_json()) |
| 410 | self.mock_repo.branch = 'master' |
| 411 | |
| 412 | build_state = build_summary.BuildSummary( |
| 413 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 414 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 415 | branch='master') |
| 416 | cbuildbot_launch.CleanBuildRoot( |
| 417 | self.root, self.mock_repo, self.metrics, build_state) |
| 418 | |
| 419 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
| 420 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 421 | self.assertEqual(new_summary.branch, 'master') |
| 422 | self.assertIsNotNone(new_summary.distfiles_ts) |
| 423 | self.assertEqual(new_summary, build_state) |
| 424 | |
| 425 | self.assertNotExists(self.repo) |
| 426 | self.assertNotExists(self.chroot) |
| 427 | self.assertNotExists(self.general) |
| 428 | self.assertNotExists(self.distfiles) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 429 | self.assertExists(self.previous_build_state) |
| 430 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 431 | def testBuildrootBranchChange(self): |
| 432 | """Test CleanBuildRoot with a change in branches.""" |
| 433 | old_build_state = build_summary.BuildSummary( |
| 434 | status=constants.BUILDER_STATUS_PASSED, |
| 435 | buildroot_layout=2, |
| 436 | branch='branchA') |
| 437 | self.populateBuildroot(previous_build_state=old_build_state.to_json()) |
| 438 | self.mock_repo.branch = 'branchB' |
Benjamin Gordon | 7464523 | 2018-05-04 17:40:42 -0600 | [diff] [blame] | 439 | m = self.PatchObject(cros_sdk_lib, 'CleanupChrootMount') |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 440 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 441 | build_state = build_summary.BuildSummary( |
| 442 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 443 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 444 | branch='branchB') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 445 | cbuildbot_launch.CleanBuildRoot( |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 446 | self.root, self.mock_repo, self.metrics, build_state) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 447 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 448 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 449 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 450 | self.assertEqual(new_summary.branch, 'branchB') |
| 451 | self.assertIsNotNone(new_summary.distfiles_ts) |
| 452 | self.assertEqual(new_summary, build_state) |
| 453 | |
| 454 | self.assertExists(self.repo) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 455 | self.assertExists(self.general) |
| 456 | self.assertNotExists(self.distfiles) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 457 | self.assertExists(self.previous_build_state) |
Don Garrett | 3665011 | 2018-06-28 15:54:34 -0700 | [diff] [blame] | 458 | m.assert_called_with(self.chroot, delete=True) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 459 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 460 | def testBuildrootBranchMatch(self): |
| 461 | """Test CleanBuildRoot with no change in branch.""" |
| 462 | old_build_state = build_summary.BuildSummary( |
| 463 | status=constants.BUILDER_STATUS_PASSED, |
| 464 | buildroot_layout=2, |
| 465 | branch='branchA') |
| 466 | self.populateBuildroot(previous_build_state=old_build_state.to_json()) |
| 467 | self.mock_repo.branch = 'branchA' |
| 468 | |
| 469 | build_state = build_summary.BuildSummary( |
| 470 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 471 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 472 | branch='branchA') |
| 473 | cbuildbot_launch.CleanBuildRoot( |
| 474 | self.root, self.mock_repo, self.metrics, build_state) |
| 475 | |
| 476 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
| 477 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 478 | self.assertEqual(new_summary.branch, 'branchA') |
| 479 | self.assertIsNotNone(new_summary.distfiles_ts) |
| 480 | self.assertEqual(new_summary, build_state) |
| 481 | |
| 482 | self.assertExists(self.repo) |
| 483 | self.assertExists(self.chroot) |
| 484 | self.assertExists(self.general) |
| 485 | self.assertExists(self.distfiles) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 486 | self.assertExists(self.previous_build_state) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 487 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 488 | def testBuildrootDistfilesRecentCache(self): |
| 489 | """Test CleanBuildRoot does not delete distfiles when cache is recent.""" |
| 490 | seed_distfiles_ts = time.time() - 60 |
| 491 | old_build_state = build_summary.BuildSummary( |
| 492 | status=constants.BUILDER_STATUS_PASSED, |
| 493 | buildroot_layout=2, |
| 494 | branch='branchA', |
| 495 | distfiles_ts=seed_distfiles_ts) |
| 496 | self.populateBuildroot(previous_build_state=old_build_state.to_json()) |
| 497 | self.mock_repo.branch = 'branchA' |
| 498 | |
| 499 | build_state = build_summary.BuildSummary( |
| 500 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 501 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 502 | branch='branchA') |
| 503 | cbuildbot_launch.CleanBuildRoot( |
| 504 | self.root, self.mock_repo, self.metrics, build_state) |
| 505 | |
| 506 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
| 507 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 508 | self.assertEqual(new_summary.branch, 'branchA') |
| 509 | # Same cache creation timestamp is rewritten to state. |
| 510 | self.assertEqual(new_summary.distfiles_ts, seed_distfiles_ts) |
| 511 | self.assertEqual(new_summary, build_state) |
| 512 | |
| 513 | self.assertExists(self.repo) |
| 514 | self.assertExists(self.chroot) |
| 515 | self.assertExists(self.general) |
| 516 | self.assertExists(self.distfiles) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 517 | self.assertExists(self.previous_build_state) |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 518 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 519 | def testBuildrootDistfilesCacheExpired(self): |
| 520 | """Test CleanBuildRoot when the distfiles cache is too old.""" |
| 521 | old_build_state = build_summary.BuildSummary( |
| 522 | status=constants.BUILDER_STATUS_PASSED, |
| 523 | buildroot_layout=2, |
| 524 | branch='branchA', |
| 525 | distfiles_ts=100.0) |
| 526 | self.populateBuildroot(previous_build_state=old_build_state.to_json()) |
| 527 | self.mock_repo.branch = 'branchA' |
| 528 | |
| 529 | build_state = build_summary.BuildSummary( |
| 530 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 531 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 532 | branch='branchA') |
| 533 | cbuildbot_launch.CleanBuildRoot( |
| 534 | self.root, self.mock_repo, self.metrics, build_state) |
| 535 | |
| 536 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
| 537 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 538 | self.assertEqual(new_summary.branch, 'branchA') |
| 539 | self.assertIsNotNone(new_summary.distfiles_ts) |
| 540 | self.assertEqual(new_summary, build_state) |
| 541 | |
| 542 | self.assertExists(self.repo) |
| 543 | self.assertExists(self.chroot) |
| 544 | self.assertExists(self.general) |
| 545 | self.assertNotExists(self.distfiles) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 546 | self.assertExists(self.previous_build_state) |
| 547 | |
| 548 | def testBuildrootRepoCleanFailure(self): |
| 549 | """Test CleanBuildRoot with repo checkout failure.""" |
| 550 | old_build_state = build_summary.BuildSummary( |
| 551 | status=constants.BUILDER_STATUS_PASSED, |
| 552 | buildroot_layout=1, |
| 553 | branch='branchA') |
| 554 | self.populateBuildroot(previous_build_state=old_build_state.to_json()) |
| 555 | self.mock_repo.branch = 'branchA' |
| 556 | self.mock_repo.BuildRootGitCleanup.side_effect = Exception |
| 557 | |
| 558 | build_state = build_summary.BuildSummary( |
| 559 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 560 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 561 | branch='branchA') |
| 562 | cbuildbot_launch.CleanBuildRoot( |
| 563 | self.root, self.mock_repo, self.metrics, build_state) |
| 564 | |
| 565 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
| 566 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 567 | self.assertEqual(new_summary.branch, 'branchA') |
| 568 | self.assertIsNotNone(new_summary.distfiles_ts) |
| 569 | self.assertEqual(new_summary, build_state) |
| 570 | |
| 571 | self.assertNotExists(self.repo) |
| 572 | self.assertNotExists(self.chroot) |
| 573 | self.assertNotExists(self.general) |
| 574 | self.assertNotExists(self.distfiles) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 575 | self.assertExists(self.previous_build_state) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 576 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 577 | def testGetCurrentBuildStateNoArgs(self): |
| 578 | """Tests GetCurrentBuildState without arguments.""" |
| 579 | options = cbuildbot_launch.PreParseArguments([ |
| 580 | '--buildroot', self.root, 'config' |
| 581 | ]) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 582 | state = cbuildbot_launch.GetCurrentBuildState(options, 'master') |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 583 | |
| 584 | expected_state = build_summary.BuildSummary( |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 585 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 586 | buildroot_layout=2, |
| 587 | branch='master') |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 588 | self.assertEqual(state, expected_state) |
| 589 | |
| 590 | def testGetCurrentBuildStateHasArgs(self): |
| 591 | """Tests GetCurrentBuildState with arguments.""" |
| 592 | options = cbuildbot_launch.PreParseArguments([ |
| 593 | '--buildroot', self.root, |
| 594 | '--buildnumber', '20', |
| 595 | '--master-build-id', '50', |
| 596 | 'config' |
| 597 | ]) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 598 | state = cbuildbot_launch.GetCurrentBuildState(options, 'branchA') |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 599 | |
| 600 | expected_state = build_summary.BuildSummary( |
| 601 | build_number=20, |
| 602 | master_build_id=50, |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 603 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 604 | buildroot_layout=2, |
| 605 | branch='branchA') |
| 606 | self.assertEqual(state, expected_state) |
| 607 | |
| 608 | def testGetCurrentBuildStateLayout(self): |
| 609 | """Test that GetCurrentBuildState uses the current buildroot layout.""" |
| 610 | # Change to a future version. |
| 611 | self.PatchObject(cbuildbot_launch, 'BUILDROOT_BUILDROOT_LAYOUT', 22) |
| 612 | |
| 613 | options = cbuildbot_launch.PreParseArguments([ |
| 614 | '--buildroot', self.root, 'config' |
| 615 | ]) |
| 616 | state = cbuildbot_launch.GetCurrentBuildState(options, 'branchA') |
| 617 | |
| 618 | expected_state = build_summary.BuildSummary( |
| 619 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 620 | buildroot_layout=22, |
| 621 | branch='branchA') |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 622 | self.assertEqual(state, expected_state) |
| 623 | |
| 624 | def testGetLastBuildStateNoFile(self): |
| 625 | """Tests GetLastBuildState if the file is missing.""" |
| 626 | osutils.SafeMakedirs(self.root) |
| 627 | state = cbuildbot_launch.GetLastBuildState(self.root) |
| 628 | self.assertEqual(state, build_summary.BuildSummary()) |
| 629 | |
| 630 | def testGetLastBuildStateBadFile(self): |
| 631 | """Tests GetLastBuildState if the file contains invalid JSON.""" |
| 632 | osutils.SafeMakedirs(self.root) |
| 633 | osutils.WriteFile(self.previous_build_state, '}}') |
| 634 | state = cbuildbot_launch.GetLastBuildState(self.root) |
| 635 | self.assertEqual(state, build_summary.BuildSummary()) |
| 636 | |
| 637 | def testGetLastBuildStateMissingBuildStatus(self): |
| 638 | """Tests GetLastBuildState if the file doesn't have a valid status.""" |
| 639 | osutils.SafeMakedirs(self.root) |
| 640 | osutils.WriteFile(self.previous_build_state, '{"build_number": "3"}') |
| 641 | state = cbuildbot_launch.GetLastBuildState(self.root) |
| 642 | self.assertEqual(state, build_summary.BuildSummary()) |
| 643 | |
| 644 | def testGetLastBuildStateGoodFile(self): |
| 645 | """Tests GetLastBuildState on a good file.""" |
| 646 | osutils.SafeMakedirs(self.root) |
| 647 | osutils.WriteFile( |
| 648 | self.previous_build_state, |
| 649 | '{"build_number": 1, "master_build_id": 3, "status": "pass"}') |
| 650 | state = cbuildbot_launch.GetLastBuildState(self.root) |
| 651 | self.assertEqual( |
| 652 | state, |
| 653 | build_summary.BuildSummary( |
| 654 | build_number=1, master_build_id=3, status='pass')) |
| 655 | |
| 656 | def testSetLastBuildState(self): |
| 657 | """Verifies that SetLastBuildState writes to the expected file.""" |
| 658 | osutils.SafeMakedirs(self.root) |
| 659 | old_state = build_summary.BuildSummary( |
| 660 | build_number=314, |
| 661 | master_build_id=2178, |
| 662 | status=constants.BUILDER_STATUS_PASSED) |
| 663 | cbuildbot_launch.SetLastBuildState(self.root, old_state) |
| 664 | |
| 665 | saved_state = osutils.ReadFile(self.previous_build_state) |
| 666 | new_state = build_summary.BuildSummary() |
| 667 | new_state.from_json(saved_state) |
| 668 | |
| 669 | self.assertEqual(old_state, new_state) |