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