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