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