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