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 | 5516acb | 2018-11-15 16:02:59 -0800 | [diff] [blame] | 74 | mock.call.PreLoad('/preload/chromeos'), |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 75 | mock.call.Sync(detach=True), |
Don Garrett | 8d31479 | 2017-05-18 13:11:42 -0700 | [diff] [blame] | 76 | ]) |
| 77 | |
Don Garrett | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 78 | def testConfigureGlobalEnvironment(self): |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 79 | """Ensure that we can setup our global runtime environment correctly.""" |
Don Garrett | 86fec48 | 2017-05-17 18:13:33 -0700 | [diff] [blame] | 80 | |
| 81 | os.environ.pop('LANG', None) |
| 82 | os.environ['LC_MONETARY'] = 'bad' |
| 83 | |
Don Garrett | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 84 | cbuildbot_launch.ConfigureGlobalEnvironment() |
| 85 | |
Don Garrett | 86fec48 | 2017-05-17 18:13:33 -0700 | [diff] [blame] | 86 | # Verify umask is updated. |
Don Garrett | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 87 | self.assertEqual(os.umask(0), 0o22) |
| 88 | |
Don Garrett | 86fec48 | 2017-05-17 18:13:33 -0700 | [diff] [blame] | 89 | # Verify ENVs are cleaned up. |
| 90 | self.assertEqual(os.environ['LANG'], 'en_US.UTF-8') |
| 91 | self.assertNotIn('LC_MONETARY', os.environ) |
| 92 | |
Don Garrett | f15d65b | 2017-04-12 12:39:55 -0700 | [diff] [blame] | 93 | |
Benjamin Gordon | 121a2aa | 2018-05-04 16:24:45 -0600 | [diff] [blame] | 94 | class RunTests(cros_test_lib.RunCommandTestCase): |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 95 | """Tests for cbuildbot_launch script.""" |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 96 | |
| 97 | ARGS_BASE = ['--buildroot', '/buildroot'] |
Don Garrett | 5cd946b | 2017-07-20 13:42:20 -0700 | [diff] [blame] | 98 | EXPECTED_ARGS_BASE = ['--buildroot', '/cbuildbot_buildroot'] |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 99 | ARGS_GIT_CACHE = ['--git-cache-dir', '/git-cache'] |
| 100 | ARGS_CONFIG = ['config'] |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 101 | CMD = ['/cbuildbot_buildroot/chromite/bin/cbuildbot'] |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 102 | |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 103 | def verifyCbuildbot(self, args, expected_cmd, version): |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 104 | """Ensure we invoke cbuildbot correctly.""" |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 105 | self.PatchObject( |
| 106 | cros_build_lib, 'GetTargetChromiteApiVersion', autospec=True, |
| 107 | return_value=version) |
| 108 | |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 109 | cbuildbot_launch.Cbuildbot('/cbuildbot_buildroot', '/depot_tools', args) |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 110 | |
| 111 | self.assertCommandCalled( |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 112 | expected_cmd, extra_env={'PATH': mock.ANY}, |
| 113 | cwd='/cbuildbot_buildroot', error_code_ok=True) |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 114 | |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 115 | def testCbuildbotSimple(self): |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 116 | """Ensure we invoke cbuildbot correctly.""" |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 117 | self.verifyCbuildbot( |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 118 | self.ARGS_BASE + self.ARGS_CONFIG, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 119 | self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE, |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 120 | (0, 4)) |
| 121 | |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 122 | def testCbuildbotNotFiltered(self): |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 123 | """Ensure we invoke cbuildbot correctly.""" |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 124 | self.verifyCbuildbot( |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 125 | self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 126 | (self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE + |
| 127 | self.ARGS_GIT_CACHE), |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 128 | (0, 4)) |
| 129 | |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 130 | def testCbuildbotFiltered(self): |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 131 | """Ensure we invoke cbuildbot correctly.""" |
Don Garrett | 6e5c6b9 | 2018-04-06 17:58:49 -0700 | [diff] [blame] | 132 | self.verifyCbuildbot( |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 133 | self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 134 | self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE, |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 135 | (0, 2)) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 136 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 137 | def testMainMin(self): |
| 138 | """Test a minimal set of command line options.""" |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 139 | self.PatchObject(osutils, 'SafeMakedirs', autospec=True) |
| 140 | self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion', |
Don Garrett | 861e918 | 2017-05-15 15:30:23 -0700 | [diff] [blame] | 141 | autospec=True, return_value=(constants.REEXEC_API_MAJOR, |
| 142 | constants.REEXEC_API_MINOR)) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 143 | mock_repo = mock.MagicMock() |
| 144 | mock_repo.branch = 'master' |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 145 | mock_repo.directory = '/root/repository' |
| 146 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 147 | mock_repo_create = self.PatchObject(repository, 'RepoRepository', |
| 148 | autospec=True, return_value=mock_repo) |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 149 | mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot', |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 150 | autospec=True) |
| 151 | mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout', |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 152 | autospec=True) |
Benjamin Gordon | aee36b8 | 2018-02-05 14:25:26 -0700 | [diff] [blame] | 153 | mock_cleanup_chroot = self.PatchObject(cbuildbot_launch, 'CleanupChroot', |
| 154 | autospec=True) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 155 | mock_set_last_build_state = self.PatchObject( |
| 156 | cbuildbot_launch, 'SetLastBuildState', autospec=True) |
| 157 | |
| 158 | expected_build_state = build_summary.BuildSummary( |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 159 | build_number=0, master_build_id=0, status=mock.ANY, |
| 160 | buildroot_layout=2, branch='master') |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 161 | |
Dhanya Ganesh | 95c5c15 | 2018-10-08 16:48:29 -0600 | [diff] [blame] | 162 | argv = ['-r', '/root', 'config'] |
| 163 | options = cbuildbot_launch.PreParseArguments(argv) |
| 164 | cbuildbot_launch._main(options, argv) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 165 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 166 | # Did we create the repo instance correctly? |
| 167 | self.assertEqual(mock_repo_create.mock_calls, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 168 | [mock.call(EXPECTED_MANIFEST_URL, '/root/repository', |
Don Garrett | 3387250 | 2018-08-03 22:30:40 +0000 | [diff] [blame] | 169 | git_cache_dir=None, branch='master')]) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 170 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 171 | # Ensure we clean, as expected. |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 172 | self.assertEqual(mock_clean.mock_calls, [ |
Don Garrett | 61ce1ee | 2019-02-26 16:20:25 -0800 | [diff] [blame] | 173 | mock.call('/root', mock_repo, '/root/repository/.cache', |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 174 | expected_build_state)]) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 175 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 176 | # Ensure we checkout, as expected. |
| 177 | self.assertEqual(mock_checkout.mock_calls, |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 178 | [mock.call(mock_repo)]) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 179 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 180 | # Ensure we invoke cbuildbot, as expected. |
| 181 | self.assertCommandCalled( |
Don Garrett | 5cd946b | 2017-07-20 13:42:20 -0700 | [diff] [blame] | 182 | [ |
| 183 | '/root/repository/chromite/bin/cbuildbot', |
| 184 | 'config', |
| 185 | '-r', '/root/repository', |
Don Garrett | b497f55 | 2018-07-09 16:01:13 -0700 | [diff] [blame] | 186 | '--workspace', '/root/workspace', |
Don Garrett | 61ce1ee | 2019-02-26 16:20:25 -0800 | [diff] [blame] | 187 | '--cache-dir', '/root/repository/.cache', |
| 188 | # The duplication is a bug, but not harmful. |
| 189 | '--cache-dir', '/root/repository/.cache', |
Don Garrett | 5cd946b | 2017-07-20 13:42:20 -0700 | [diff] [blame] | 190 | '--ts-mon-task-num', '1', |
| 191 | ], |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 192 | extra_env={'PATH': mock.ANY}, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 193 | cwd='/root/repository', |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 194 | error_code_ok=True) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 195 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 196 | # Ensure we saved the final state, as expected. |
| 197 | self.assertEqual(expected_build_state.status, |
| 198 | constants.BUILDER_STATUS_PASSED) |
| 199 | self.assertEqual(mock_set_last_build_state.mock_calls, [ |
| 200 | mock.call('/root', expected_build_state)]) |
| 201 | |
Benjamin Gordon | aee36b8 | 2018-02-05 14:25:26 -0700 | [diff] [blame] | 202 | # Ensure we clean the chroot, as expected. |
| 203 | self.assertEqual(mock_cleanup_chroot.mock_calls, [ |
| 204 | mock.call('/root/repository')]) |
| 205 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 206 | def testMainMax(self): |
Don Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame] | 207 | """Test a larger set of command line options.""" |
| 208 | self.PatchObject(osutils, 'SafeMakedirs', autospec=True) |
| 209 | self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion', |
Don Garrett | 861e918 | 2017-05-15 15:30:23 -0700 | [diff] [blame] | 210 | autospec=True, return_value=(constants.REEXEC_API_MAJOR, |
| 211 | constants.REEXEC_API_MINOR)) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 212 | mock_repo = mock.MagicMock() |
| 213 | mock_repo.branch = 'branch' |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 214 | mock_repo.directory = '/root/repository' |
| 215 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 216 | mock_summary = build_summary.BuildSummary( |
| 217 | build_number=313, |
| 218 | master_build_id=123123123, |
| 219 | status=constants.BUILDER_STATUS_FAILED, |
| 220 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 221 | branch='branch') |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 222 | |
| 223 | mock_get_last_build_state = self.PatchObject( |
| 224 | cbuildbot_launch, 'GetLastBuildState', autospec=True, |
| 225 | return_value=mock_summary) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 226 | mock_repo_create = self.PatchObject(repository, 'RepoRepository', |
| 227 | autospec=True, return_value=mock_repo) |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 228 | mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot', |
Don Garrett | 0c54ed7 | 2017-03-03 11:18:57 -0800 | [diff] [blame] | 229 | autospec=True) |
| 230 | mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout', |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 231 | autospec=True) |
Benjamin Gordon | aee36b8 | 2018-02-05 14:25:26 -0700 | [diff] [blame] | 232 | mock_cleanup_chroot = self.PatchObject(cbuildbot_launch, 'CleanupChroot', |
| 233 | autospec=True) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 234 | mock_set_last_build_state = self.PatchObject( |
| 235 | cbuildbot_launch, 'SetLastBuildState', autospec=True) |
Dhanya Ganesh | 95c5c15 | 2018-10-08 16:48:29 -0600 | [diff] [blame] | 236 | argv = ['--buildroot', '/root', |
| 237 | '--branch', 'branch', |
| 238 | '--git-cache-dir', '/git-cache', |
Don Garrett | 61ce1ee | 2019-02-26 16:20:25 -0800 | [diff] [blame] | 239 | '--cache-dir', '/cache', |
Dhanya Ganesh | 95c5c15 | 2018-10-08 16:48:29 -0600 | [diff] [blame] | 240 | '--remote-trybot', |
| 241 | '--master-build-id', '123456789', |
| 242 | '--buildnumber', '314', |
| 243 | 'config'] |
| 244 | options = cbuildbot_launch.PreParseArguments(argv) |
| 245 | cbuildbot_launch._main(options, argv) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 246 | |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 247 | # Did we create the repo instance correctly? |
| 248 | self.assertEqual(mock_repo_create.mock_calls, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 249 | [mock.call(EXPECTED_MANIFEST_URL, '/root/repository', |
Don Garrett | 3387250 | 2018-08-03 22:30:40 +0000 | [diff] [blame] | 250 | git_cache_dir='/git-cache', branch='branch')]) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 251 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 252 | # Ensure we look up the previous status. |
| 253 | self.assertEqual(mock_get_last_build_state.mock_calls, [ |
| 254 | mock.call('/root')]) |
| 255 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 256 | # Ensure we clean, as expected. |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 257 | self.assertEqual(mock_clean.mock_calls, [ |
Don Garrett | d1d90dd | 2017-06-13 17:35:52 -0700 | [diff] [blame] | 258 | mock.call('/root', |
| 259 | mock_repo, |
Don Garrett | 61ce1ee | 2019-02-26 16:20:25 -0800 | [diff] [blame] | 260 | '/cache', |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 261 | build_summary.BuildSummary( |
| 262 | build_number=314, |
| 263 | master_build_id=123456789, |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 264 | status=mock.ANY, |
| 265 | branch='branch', |
| 266 | buildroot_layout=2 |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 267 | ))]) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 268 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 269 | # Ensure we checkout, as expected. |
| 270 | self.assertEqual(mock_checkout.mock_calls, |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 271 | [mock.call(mock_repo)]) |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 272 | |
| 273 | # Ensure we invoke cbuildbot, as expected. |
| 274 | self.assertCommandCalled( |
Don Garrett | 5cd946b | 2017-07-20 13:42:20 -0700 | [diff] [blame] | 275 | [ |
| 276 | '/root/repository/chromite/bin/cbuildbot', |
| 277 | 'config', |
| 278 | '--buildroot', '/root/repository', |
| 279 | '--branch', 'branch', |
| 280 | '--git-cache-dir', '/git-cache', |
Don Garrett | 61ce1ee | 2019-02-26 16:20:25 -0800 | [diff] [blame] | 281 | '--cache-dir', '/cache', |
Don Garrett | 5cd946b | 2017-07-20 13:42:20 -0700 | [diff] [blame] | 282 | '--remote-trybot', |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 283 | '--master-build-id', '123456789', |
| 284 | '--buildnumber', '314', |
| 285 | '--previous-build-state', |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 286 | 'eyJzdGF0dXMiOiAiZmFpbCIsICJtYXN0ZXJfYnVpbGRfaWQiOiAxMjMxMjMxMj' |
| 287 | 'MsICJidWlsZF9udW1iZXIiOiAzMTMsICJidWlsZHJvb3RfbGF5b3V0IjogMiwg' |
| 288 | 'ImJyYW5jaCI6ICJicmFuY2gifQ==', |
Don Garrett | b497f55 | 2018-07-09 16:01:13 -0700 | [diff] [blame] | 289 | '--workspace', '/root/workspace', |
Don Garrett | 61ce1ee | 2019-02-26 16:20:25 -0800 | [diff] [blame] | 290 | '--cache-dir', '/cache', |
Don Garrett | 5cd946b | 2017-07-20 13:42:20 -0700 | [diff] [blame] | 291 | '--ts-mon-task-num', '1', |
| 292 | ], |
Don Garrett | a50bf49 | 2017-09-28 18:33:02 -0700 | [diff] [blame] | 293 | extra_env={'PATH': mock.ANY}, |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 294 | cwd='/root/repository', |
Don Garrett | acbb239 | 2017-05-11 18:27:41 -0700 | [diff] [blame] | 295 | error_code_ok=True) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 296 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 297 | # Ensure we write the final build state, as expected. |
| 298 | final_state = build_summary.BuildSummary( |
| 299 | build_number=314, |
| 300 | master_build_id=123456789, |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 301 | status=constants.BUILDER_STATUS_PASSED, |
| 302 | buildroot_layout=2, |
| 303 | branch='branch') |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 304 | self.assertEqual(mock_set_last_build_state.mock_calls, [ |
| 305 | mock.call('/root', final_state)]) |
| 306 | |
Benjamin Gordon | aee36b8 | 2018-02-05 14:25:26 -0700 | [diff] [blame] | 307 | # Ensure we clean the chroot, as expected. |
| 308 | self.assertEqual(mock_cleanup_chroot.mock_calls, [ |
| 309 | mock.call('/root/repository')]) |
| 310 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 311 | |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 312 | class CleanBuildRootTest(cros_test_lib.MockTempDirTestCase): |
| 313 | """Tests for CleanBuildRoot method.""" |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 314 | |
| 315 | def setUp(self): |
| 316 | """Create standard buildroot contents for cleanup.""" |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 317 | self.root = os.path.join(self.tempdir) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 318 | self.previous_build_state = os.path.join( |
| 319 | self.root, '.cbuildbot_build_state.json') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 320 | self.buildroot = os.path.join(self.root, 'buildroot') |
| 321 | self.repo = os.path.join(self.buildroot, '.repo/repo') |
Don Garrett | 3665011 | 2018-06-28 15:54:34 -0700 | [diff] [blame] | 322 | self.chroot = os.path.join(self.buildroot, 'chroot') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 323 | self.general = os.path.join(self.buildroot, 'general/general') |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 324 | self.cache = os.path.join(self.buildroot, '.cache') |
| 325 | self.distfiles = os.path.join(self.cache, 'distfiles') |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 326 | |
Don Garrett | 4166d18 | 2018-12-17 12:52:02 -0800 | [diff] [blame] | 327 | self.mock_repo = mock.Mock(repository.RepoRepository) |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 328 | self.mock_repo.directory = self.buildroot |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 329 | |
Benjamin Gordon | 8642bcc | 2018-05-01 13:49:56 -0600 | [diff] [blame] | 330 | def populateBuildroot(self, previous_build_state=None): |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 331 | """Create standard buildroot contents for cleanup.""" |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 332 | if previous_build_state: |
| 333 | osutils.SafeMakedirs(self.root) |
| 334 | osutils.WriteFile(self.previous_build_state, previous_build_state) |
| 335 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 336 | # Create files. |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 337 | for f in (self.repo, self.chroot, self.general, self.distfiles): |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 338 | osutils.Touch(f, makedirs=True) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 339 | |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 340 | def testNoBuildroot(self): |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 341 | """Test CleanBuildRoot with no history.""" |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 342 | self.mock_repo.branch = 'master' |
| 343 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 344 | build_state = build_summary.BuildSummary( |
| 345 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 346 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 347 | branch='master') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 348 | cbuildbot_launch.CleanBuildRoot( |
Don Garrett | 61ce1ee | 2019-02-26 16:20:25 -0800 | [diff] [blame] | 349 | self.root, self.mock_repo, self.cache, build_state) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 350 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 351 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 352 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 353 | self.assertEqual(new_summary.branch, 'master') |
| 354 | self.assertIsNotNone(new_summary.distfiles_ts) |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 355 | self.assertEqual(new_summary, build_state) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 356 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 357 | self.assertExists(self.previous_build_state) |
| 358 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 359 | def testBuildrootNoState(self): |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 360 | """Test CleanBuildRoot with no state information.""" |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 361 | self.populateBuildroot() |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 362 | self.mock_repo.branch = 'master' |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 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( |
Don Garrett | 61ce1ee | 2019-02-26 16:20:25 -0800 | [diff] [blame] | 369 | self.root, self.mock_repo, self.cache, 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) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 376 | |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 377 | self.assertNotExists(self.repo) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 378 | self.assertNotExists(self.chroot) |
Don Garrett | 6096792 | 2017-04-12 18:51:44 -0700 | [diff] [blame] | 379 | self.assertNotExists(self.general) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 380 | self.assertNotExists(self.distfiles) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 381 | self.assertExists(self.previous_build_state) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 382 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 383 | def testBuildrootFormatMismatch(self): |
| 384 | """Test CleanBuildRoot with buildroot layout mismatch.""" |
| 385 | old_build_state = build_summary.BuildSummary( |
| 386 | status=constants.BUILDER_STATUS_PASSED, |
| 387 | buildroot_layout=1, |
| 388 | branch='master') |
| 389 | self.populateBuildroot(previous_build_state=old_build_state.to_json()) |
| 390 | self.mock_repo.branch = 'master' |
| 391 | |
| 392 | build_state = build_summary.BuildSummary( |
| 393 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 394 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 395 | branch='master') |
| 396 | cbuildbot_launch.CleanBuildRoot( |
Don Garrett | 61ce1ee | 2019-02-26 16:20:25 -0800 | [diff] [blame] | 397 | self.root, self.mock_repo, self.cache, build_state) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 398 | |
| 399 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
| 400 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 401 | self.assertEqual(new_summary.branch, 'master') |
| 402 | self.assertIsNotNone(new_summary.distfiles_ts) |
| 403 | self.assertEqual(new_summary, build_state) |
| 404 | |
| 405 | self.assertNotExists(self.repo) |
| 406 | self.assertNotExists(self.chroot) |
| 407 | self.assertNotExists(self.general) |
| 408 | self.assertNotExists(self.distfiles) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 409 | self.assertExists(self.previous_build_state) |
| 410 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 411 | def testBuildrootBranchChange(self): |
| 412 | """Test CleanBuildRoot with a change in branches.""" |
| 413 | old_build_state = build_summary.BuildSummary( |
| 414 | status=constants.BUILDER_STATUS_PASSED, |
| 415 | buildroot_layout=2, |
| 416 | branch='branchA') |
| 417 | self.populateBuildroot(previous_build_state=old_build_state.to_json()) |
| 418 | self.mock_repo.branch = 'branchB' |
Benjamin Gordon | 7464523 | 2018-05-04 17:40:42 -0600 | [diff] [blame] | 419 | m = self.PatchObject(cros_sdk_lib, 'CleanupChrootMount') |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 420 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 421 | build_state = build_summary.BuildSummary( |
| 422 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 423 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 424 | branch='branchB') |
Don Garrett | bf90cdf | 2017-05-19 15:54:02 -0700 | [diff] [blame] | 425 | cbuildbot_launch.CleanBuildRoot( |
Don Garrett | 61ce1ee | 2019-02-26 16:20:25 -0800 | [diff] [blame] | 426 | self.root, self.mock_repo, self.cache, build_state) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame] | 427 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 428 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 429 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 430 | self.assertEqual(new_summary.branch, 'branchB') |
| 431 | self.assertIsNotNone(new_summary.distfiles_ts) |
| 432 | self.assertEqual(new_summary, build_state) |
| 433 | |
Don Garrett | 4166d18 | 2018-12-17 12:52:02 -0800 | [diff] [blame] | 434 | # self.assertExists(self.repo) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 435 | self.assertExists(self.general) |
| 436 | self.assertNotExists(self.distfiles) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 437 | self.assertExists(self.previous_build_state) |
Don Garrett | 3665011 | 2018-06-28 15:54:34 -0700 | [diff] [blame] | 438 | m.assert_called_with(self.chroot, delete=True) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 439 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 440 | def testBuildrootBranchMatch(self): |
| 441 | """Test CleanBuildRoot with no change in branch.""" |
| 442 | old_build_state = build_summary.BuildSummary( |
| 443 | status=constants.BUILDER_STATUS_PASSED, |
| 444 | buildroot_layout=2, |
| 445 | branch='branchA') |
| 446 | self.populateBuildroot(previous_build_state=old_build_state.to_json()) |
| 447 | self.mock_repo.branch = 'branchA' |
| 448 | |
| 449 | build_state = build_summary.BuildSummary( |
| 450 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 451 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 452 | branch='branchA') |
| 453 | cbuildbot_launch.CleanBuildRoot( |
Don Garrett | 61ce1ee | 2019-02-26 16:20:25 -0800 | [diff] [blame] | 454 | self.root, self.mock_repo, self.cache, build_state) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 455 | |
| 456 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
| 457 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 458 | self.assertEqual(new_summary.branch, 'branchA') |
| 459 | self.assertIsNotNone(new_summary.distfiles_ts) |
| 460 | self.assertEqual(new_summary, build_state) |
| 461 | |
| 462 | self.assertExists(self.repo) |
| 463 | self.assertExists(self.chroot) |
| 464 | self.assertExists(self.general) |
| 465 | self.assertExists(self.distfiles) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 466 | self.assertExists(self.previous_build_state) |
Prathmesh Prabhu | c41a0f5 | 2018-04-03 13:26:52 -0700 | [diff] [blame] | 467 | |
Don Garrett | 4166d18 | 2018-12-17 12:52:02 -0800 | [diff] [blame] | 468 | def testBuildrootGitLocksPrevPass(self): |
| 469 | """Verify not CleanStaleLocks, if previous build was in passed.""" |
| 470 | old_build_state = build_summary.BuildSummary( |
| 471 | status=constants.BUILDER_STATUS_PASSED, |
| 472 | buildroot_layout=2, |
| 473 | branch='branchA') |
| 474 | self.populateBuildroot(previous_build_state=old_build_state.to_json()) |
| 475 | self.mock_repo.branch = 'branchA' |
| 476 | |
| 477 | build_state = build_summary.BuildSummary( |
| 478 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 479 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 480 | branch='branchA') |
| 481 | cbuildbot_launch.CleanBuildRoot( |
Don Garrett | 61ce1ee | 2019-02-26 16:20:25 -0800 | [diff] [blame] | 482 | self.root, self.mock_repo, self.cache, build_state) |
Don Garrett | 4166d18 | 2018-12-17 12:52:02 -0800 | [diff] [blame] | 483 | |
| 484 | self.assertEqual( |
| 485 | self.mock_repo.mock_calls, [ |
| 486 | mock.call.PreLoad(), |
| 487 | mock.call.BuildRootGitCleanup(prune_all=True), |
| 488 | ]) |
| 489 | |
| 490 | def testBuildrootGitLocksPrevFail(self): |
| 491 | """Verify not CleanStaleLocks, if previous build was in failed.""" |
| 492 | old_build_state = build_summary.BuildSummary( |
| 493 | status=constants.BUILDER_STATUS_FAILED, |
| 494 | buildroot_layout=2, |
| 495 | branch='branchA') |
| 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( |
Don Garrett | 61ce1ee | 2019-02-26 16:20:25 -0800 | [diff] [blame] | 504 | self.root, self.mock_repo, self.cache, build_state) |
Don Garrett | 4166d18 | 2018-12-17 12:52:02 -0800 | [diff] [blame] | 505 | |
| 506 | self.assertEqual( |
| 507 | self.mock_repo.mock_calls, [ |
| 508 | mock.call.PreLoad(), |
| 509 | mock.call.BuildRootGitCleanup(prune_all=True), |
| 510 | ]) |
| 511 | |
| 512 | def testBuildrootGitLocksPrevInFlight(self): |
| 513 | """Verify CleanStaleLocks, if previous build was in flight.""" |
| 514 | old_build_state = build_summary.BuildSummary( |
| 515 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 516 | buildroot_layout=2, |
| 517 | branch='branchA') |
| 518 | self.populateBuildroot(previous_build_state=old_build_state.to_json()) |
| 519 | self.mock_repo.branch = 'branchA' |
| 520 | |
| 521 | build_state = build_summary.BuildSummary( |
| 522 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 523 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 524 | branch='branchA') |
| 525 | cbuildbot_launch.CleanBuildRoot( |
Don Garrett | 61ce1ee | 2019-02-26 16:20:25 -0800 | [diff] [blame] | 526 | self.root, self.mock_repo, self.cache, build_state) |
Don Garrett | 4166d18 | 2018-12-17 12:52:02 -0800 | [diff] [blame] | 527 | |
| 528 | |
| 529 | self.assertEqual( |
| 530 | self.mock_repo.method_calls, [ |
| 531 | mock.call.PreLoad(), |
| 532 | mock.call.CleanStaleLocks(), |
| 533 | mock.call.BuildRootGitCleanup(prune_all=True), |
| 534 | ]) |
| 535 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 536 | def testBuildrootDistfilesRecentCache(self): |
| 537 | """Test CleanBuildRoot does not delete distfiles when cache is recent.""" |
| 538 | seed_distfiles_ts = time.time() - 60 |
| 539 | old_build_state = build_summary.BuildSummary( |
| 540 | status=constants.BUILDER_STATUS_PASSED, |
| 541 | buildroot_layout=2, |
| 542 | branch='branchA', |
| 543 | distfiles_ts=seed_distfiles_ts) |
| 544 | self.populateBuildroot(previous_build_state=old_build_state.to_json()) |
| 545 | self.mock_repo.branch = 'branchA' |
| 546 | |
| 547 | build_state = build_summary.BuildSummary( |
| 548 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 549 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 550 | branch='branchA') |
| 551 | cbuildbot_launch.CleanBuildRoot( |
Don Garrett | 61ce1ee | 2019-02-26 16:20:25 -0800 | [diff] [blame] | 552 | self.root, self.mock_repo, self.cache, build_state) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 553 | |
| 554 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
| 555 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 556 | self.assertEqual(new_summary.branch, 'branchA') |
| 557 | # Same cache creation timestamp is rewritten to state. |
| 558 | self.assertEqual(new_summary.distfiles_ts, seed_distfiles_ts) |
| 559 | self.assertEqual(new_summary, build_state) |
| 560 | |
| 561 | self.assertExists(self.repo) |
| 562 | self.assertExists(self.chroot) |
| 563 | self.assertExists(self.general) |
| 564 | self.assertExists(self.distfiles) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 565 | self.assertExists(self.previous_build_state) |
Don Garrett | e17e1d9 | 2017-04-12 15:28:19 -0700 | [diff] [blame] | 566 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 567 | def testBuildrootDistfilesCacheExpired(self): |
| 568 | """Test CleanBuildRoot when the distfiles cache is too old.""" |
| 569 | old_build_state = build_summary.BuildSummary( |
| 570 | status=constants.BUILDER_STATUS_PASSED, |
| 571 | buildroot_layout=2, |
| 572 | branch='branchA', |
| 573 | distfiles_ts=100.0) |
| 574 | self.populateBuildroot(previous_build_state=old_build_state.to_json()) |
| 575 | self.mock_repo.branch = 'branchA' |
| 576 | |
| 577 | build_state = build_summary.BuildSummary( |
| 578 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 579 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 580 | branch='branchA') |
| 581 | cbuildbot_launch.CleanBuildRoot( |
Don Garrett | 61ce1ee | 2019-02-26 16:20:25 -0800 | [diff] [blame] | 582 | self.root, self.mock_repo, self.cache, build_state) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 583 | |
| 584 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
| 585 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 586 | self.assertEqual(new_summary.branch, 'branchA') |
| 587 | self.assertIsNotNone(new_summary.distfiles_ts) |
| 588 | self.assertEqual(new_summary, build_state) |
| 589 | |
| 590 | self.assertExists(self.repo) |
| 591 | self.assertExists(self.chroot) |
| 592 | self.assertExists(self.general) |
| 593 | self.assertNotExists(self.distfiles) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 594 | self.assertExists(self.previous_build_state) |
| 595 | |
Don Garrett | 61ce1ee | 2019-02-26 16:20:25 -0800 | [diff] [blame] | 596 | def testRootOwnedCache(self): |
| 597 | """Test CleanBuildRoot with no history.""" |
| 598 | seed_distfiles_ts = time.time() - 60 |
| 599 | old_build_state = build_summary.BuildSummary( |
| 600 | status=constants.BUILDER_STATUS_PASSED, |
| 601 | buildroot_layout=2, |
| 602 | branch='branchA', |
| 603 | distfiles_ts=seed_distfiles_ts) |
| 604 | self.populateBuildroot(previous_build_state=old_build_state.to_json()) |
| 605 | self.mock_repo.branch = 'branchA' |
| 606 | |
| 607 | osutils.Chown(self.cache, 'root', 'root') |
| 608 | |
| 609 | build_state = build_summary.BuildSummary( |
| 610 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 611 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 612 | branch='branchA') |
| 613 | cbuildbot_launch.CleanBuildRoot( |
| 614 | self.root, self.mock_repo, self.cache, build_state) |
| 615 | |
| 616 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
| 617 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 618 | self.assertEqual(new_summary.branch, 'branchA') |
| 619 | # Same cache creation timestamp is rewritten to state. |
| 620 | self.assertEqual(new_summary.distfiles_ts, seed_distfiles_ts) |
| 621 | self.assertEqual(new_summary, build_state) |
| 622 | |
| 623 | self.assertExists(self.repo) |
| 624 | self.assertExists(self.chroot) |
| 625 | self.assertExists(self.general) |
| 626 | self.assertNotExists(self.distfiles) |
| 627 | self.assertExists(self.previous_build_state) |
| 628 | |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 629 | def testBuildrootRepoCleanFailure(self): |
| 630 | """Test CleanBuildRoot with repo checkout failure.""" |
| 631 | old_build_state = build_summary.BuildSummary( |
| 632 | status=constants.BUILDER_STATUS_PASSED, |
| 633 | buildroot_layout=1, |
| 634 | branch='branchA') |
| 635 | self.populateBuildroot(previous_build_state=old_build_state.to_json()) |
| 636 | self.mock_repo.branch = 'branchA' |
| 637 | self.mock_repo.BuildRootGitCleanup.side_effect = Exception |
| 638 | |
| 639 | build_state = build_summary.BuildSummary( |
| 640 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 641 | buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT, |
| 642 | branch='branchA') |
| 643 | cbuildbot_launch.CleanBuildRoot( |
Don Garrett | 61ce1ee | 2019-02-26 16:20:25 -0800 | [diff] [blame] | 644 | self.root, self.mock_repo, self.cache, build_state) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 645 | |
| 646 | new_summary = cbuildbot_launch.GetLastBuildState(self.root) |
| 647 | self.assertEqual(new_summary.buildroot_layout, 2) |
| 648 | self.assertEqual(new_summary.branch, 'branchA') |
| 649 | self.assertIsNotNone(new_summary.distfiles_ts) |
| 650 | self.assertEqual(new_summary, build_state) |
| 651 | |
| 652 | self.assertNotExists(self.repo) |
| 653 | self.assertNotExists(self.chroot) |
| 654 | self.assertNotExists(self.general) |
| 655 | self.assertNotExists(self.distfiles) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 656 | self.assertExists(self.previous_build_state) |
Don Garrett | f324bc3 | 2017-05-23 14:00:53 -0700 | [diff] [blame] | 657 | |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 658 | def testGetCurrentBuildStateNoArgs(self): |
| 659 | """Tests GetCurrentBuildState without arguments.""" |
| 660 | options = cbuildbot_launch.PreParseArguments([ |
| 661 | '--buildroot', self.root, 'config' |
| 662 | ]) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 663 | state = cbuildbot_launch.GetCurrentBuildState(options, 'master') |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 664 | |
| 665 | expected_state = build_summary.BuildSummary( |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 666 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 667 | buildroot_layout=2, |
| 668 | branch='master') |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 669 | self.assertEqual(state, expected_state) |
| 670 | |
| 671 | def testGetCurrentBuildStateHasArgs(self): |
| 672 | """Tests GetCurrentBuildState with arguments.""" |
| 673 | options = cbuildbot_launch.PreParseArguments([ |
| 674 | '--buildroot', self.root, |
| 675 | '--buildnumber', '20', |
| 676 | '--master-build-id', '50', |
| 677 | 'config' |
| 678 | ]) |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 679 | state = cbuildbot_launch.GetCurrentBuildState(options, 'branchA') |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 680 | |
| 681 | expected_state = build_summary.BuildSummary( |
| 682 | build_number=20, |
| 683 | master_build_id=50, |
Benjamin Gordon | 8b6d412 | 2018-04-26 13:38:39 -0600 | [diff] [blame] | 684 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 685 | buildroot_layout=2, |
| 686 | branch='branchA') |
| 687 | self.assertEqual(state, expected_state) |
| 688 | |
| 689 | def testGetCurrentBuildStateLayout(self): |
| 690 | """Test that GetCurrentBuildState uses the current buildroot layout.""" |
| 691 | # Change to a future version. |
| 692 | self.PatchObject(cbuildbot_launch, 'BUILDROOT_BUILDROOT_LAYOUT', 22) |
| 693 | |
| 694 | options = cbuildbot_launch.PreParseArguments([ |
| 695 | '--buildroot', self.root, 'config' |
| 696 | ]) |
| 697 | state = cbuildbot_launch.GetCurrentBuildState(options, 'branchA') |
| 698 | |
| 699 | expected_state = build_summary.BuildSummary( |
| 700 | status=constants.BUILDER_STATUS_INFLIGHT, |
| 701 | buildroot_layout=22, |
| 702 | branch='branchA') |
Benjamin Gordon | 90b2dd9 | 2018-04-12 14:04:21 -0600 | [diff] [blame] | 703 | self.assertEqual(state, expected_state) |
| 704 | |
| 705 | def testGetLastBuildStateNoFile(self): |
| 706 | """Tests GetLastBuildState if the file is missing.""" |
| 707 | osutils.SafeMakedirs(self.root) |
| 708 | state = cbuildbot_launch.GetLastBuildState(self.root) |
| 709 | self.assertEqual(state, build_summary.BuildSummary()) |
| 710 | |
| 711 | def testGetLastBuildStateBadFile(self): |
| 712 | """Tests GetLastBuildState if the file contains invalid JSON.""" |
| 713 | osutils.SafeMakedirs(self.root) |
| 714 | osutils.WriteFile(self.previous_build_state, '}}') |
| 715 | state = cbuildbot_launch.GetLastBuildState(self.root) |
| 716 | self.assertEqual(state, build_summary.BuildSummary()) |
| 717 | |
| 718 | def testGetLastBuildStateMissingBuildStatus(self): |
| 719 | """Tests GetLastBuildState if the file doesn't have a valid status.""" |
| 720 | osutils.SafeMakedirs(self.root) |
| 721 | osutils.WriteFile(self.previous_build_state, '{"build_number": "3"}') |
| 722 | state = cbuildbot_launch.GetLastBuildState(self.root) |
| 723 | self.assertEqual(state, build_summary.BuildSummary()) |
| 724 | |
| 725 | def testGetLastBuildStateGoodFile(self): |
| 726 | """Tests GetLastBuildState on a good file.""" |
| 727 | osutils.SafeMakedirs(self.root) |
| 728 | osutils.WriteFile( |
| 729 | self.previous_build_state, |
| 730 | '{"build_number": 1, "master_build_id": 3, "status": "pass"}') |
| 731 | state = cbuildbot_launch.GetLastBuildState(self.root) |
| 732 | self.assertEqual( |
| 733 | state, |
| 734 | build_summary.BuildSummary( |
| 735 | build_number=1, master_build_id=3, status='pass')) |
| 736 | |
| 737 | def testSetLastBuildState(self): |
| 738 | """Verifies that SetLastBuildState writes to the expected file.""" |
| 739 | osutils.SafeMakedirs(self.root) |
| 740 | old_state = build_summary.BuildSummary( |
| 741 | build_number=314, |
| 742 | master_build_id=2178, |
| 743 | status=constants.BUILDER_STATUS_PASSED) |
| 744 | cbuildbot_launch.SetLastBuildState(self.root, old_state) |
| 745 | |
| 746 | saved_state = osutils.ReadFile(self.previous_build_state) |
| 747 | new_state = build_summary.BuildSummary() |
| 748 | new_state.from_json(saved_state) |
| 749 | |
| 750 | self.assertEqual(old_state, new_state) |