blob: edfc283382145814f7cb562d2169d92116c56160 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2016 The ChromiumOS Authors
Don Garrettc4114cc2016-11-01 20:04:06 -07002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -07005"""Unit tests for chromite.scripts.cbuildbot_launch."""
Don Garrettc4114cc2016-11-01 20:04:06 -07006
Don Garrett7ade05a2017-02-17 13:31:47 -08007import os
Brian Norris872684f2023-08-22 15:20:20 -07008from pathlib import Path
Mike Nicholsd0acc7f2021-05-21 17:18:24 +00009import time
Mike Frysinger166fea02021-02-12 05:30:33 -050010from unittest import mock
Don Garrett86881cb2017-02-15 15:41:55 -080011
Mike Frysingerc263d092019-09-18 15:11:47 -040012from chromite.cbuildbot import commands
Don Garrett86881cb2017-02-15 15:41:55 -080013from chromite.cbuildbot import repository
Benjamin Gordon90b2dd92018-04-12 14:04:21 -060014from chromite.lib import build_summary
Brian Norris872684f2023-08-22 15:20:20 -070015from chromite.lib import chroot_lib
Don Garrett861e9182017-05-15 15:30:23 -070016from chromite.lib import constants
Benjamin Gordon74645232018-05-04 17:40:42 -060017from chromite.lib import cros_sdk_lib
Don Garrett7ade05a2017-02-17 13:31:47 -080018from chromite.lib import cros_test_lib
Don Garrett86881cb2017-02-15 15:41:55 -080019from chromite.lib import osutils
Mike Frysingerf0146252019-09-02 13:31:05 -040020from chromite.lib import timeout_util
Don Garrett0c54ed72017-03-03 11:18:57 -080021from chromite.scripts import cbuildbot_launch
Don Garrett86881cb2017-02-15 15:41:55 -080022
Mike Frysinger3c831a72020-02-19 02:47:04 -050023
Alex Klein1699fab2022-09-08 08:46:06 -060024EXPECTED_MANIFEST_URL = "https://chrome-internal-review.googlesource.com/chromeos/manifest-internal" # pylint: disable=line-too-long
Don Garrettc4114cc2016-11-01 20:04:06 -070025
26
Don Garrettacbb2392017-05-11 18:27:41 -070027# It's reasonable for unittests to look at internals.
28# pylint: disable=protected-access
29
30
Don Garrett8d314792017-05-18 13:11:42 -070031class FakeException(Exception):
Alex Klein1699fab2022-09-08 08:46:06 -060032 """Test exception to raise during tests."""
Don Garrett8d314792017-05-18 13:11:42 -070033
34
Don Garrett0c54ed72017-03-03 11:18:57 -080035class CbuildbotLaunchTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060036 """Tests for cbuildbot_launch script."""
Don Garrettc4114cc2016-11-01 20:04:06 -070037
Alex Klein1699fab2022-09-08 08:46:06 -060038 def testPreParseArguments(self):
Alex Klein9e7b29e2023-04-11 16:10:31 -060039 """Test we can correctly extract branch values from cbuildbot args."""
Alex Klein1699fab2022-09-08 08:46:06 -060040 CASES = (
41 (
42 ["--buildroot", "/buildroot", "daisy-incremental"],
43 (None, "/buildroot", None),
44 ),
45 (
46 [
47 "--buildbot",
48 "--buildroot",
49 "/buildroot",
50 "--git-cache-dir",
51 "/git-cache",
52 "-b",
53 "release-R57-9202.B",
54 "daisy-incremental",
55 ],
56 ("release-R57-9202.B", "/buildroot", "/git-cache"),
57 ),
58 (
59 [
60 "--debug",
61 "--buildbot",
62 "--notests",
63 "--buildroot",
64 "/buildroot",
65 "--git-cache-dir",
66 "/git-cache",
67 "--branch",
68 "release-R57-9202.B",
69 "daisy-incremental",
70 ],
71 ("release-R57-9202.B", "/buildroot", "/git-cache"),
72 ),
73 )
Don Garrett597ddff2017-02-17 18:29:37 -080074
Alex Klein1699fab2022-09-08 08:46:06 -060075 for cmd_args, expected in CASES:
76 expected_branch, expected_buildroot, expected_cache_dir = expected
Don Garrett597ddff2017-02-17 18:29:37 -080077
Alex Klein1699fab2022-09-08 08:46:06 -060078 options = cbuildbot_launch.PreParseArguments(cmd_args)
Don Garrettc4114cc2016-11-01 20:04:06 -070079
Alex Klein1699fab2022-09-08 08:46:06 -060080 self.assertEqual(options.branch, expected_branch)
81 self.assertEqual(options.buildroot, expected_buildroot)
82 self.assertEqual(options.git_cache_dir, expected_cache_dir)
Don Garrett597ddff2017-02-17 18:29:37 -080083
Alex Klein1699fab2022-09-08 08:46:06 -060084 def testInitialCheckout(self):
85 """Test InitialCheckout with minimum settings."""
86 mock_repo = mock.MagicMock()
87 mock_repo.branch = "branch"
88 argv = ["-r", "/root", "config"]
89 options = cbuildbot_launch.PreParseArguments(argv)
Don Garrett597ddff2017-02-17 18:29:37 -080090
Alex Klein1699fab2022-09-08 08:46:06 -060091 cbuildbot_launch.InitialCheckout(mock_repo, options)
Don Garrett86881cb2017-02-15 15:41:55 -080092
Alex Klein1699fab2022-09-08 08:46:06 -060093 self.assertEqual(
94 mock_repo.mock_calls,
95 [
96 mock.call.PreLoad("/preload/chromeos"),
97 mock.call.Sync(jobs=32, detach=True, downgrade_repo=False),
98 ],
99 )
Don Garrett86881cb2017-02-15 15:41:55 -0800100
Alex Klein1699fab2022-09-08 08:46:06 -0600101 def testConfigureGlobalEnvironment(self):
102 """Ensure that we can setup our global runtime environment correctly."""
Don Garrett86881cb2017-02-15 15:41:55 -0800103
Alex Klein1699fab2022-09-08 08:46:06 -0600104 os.environ.pop("LANG", None)
105 os.environ["LC_MONETARY"] = "bad"
Don Garrett8d314792017-05-18 13:11:42 -0700106
Alex Klein1699fab2022-09-08 08:46:06 -0600107 cbuildbot_launch.ConfigureGlobalEnvironment()
Don Garrett86fec482017-05-17 18:13:33 -0700108
Alex Klein1699fab2022-09-08 08:46:06 -0600109 # Verify umask is updated.
110 self.assertEqual(os.umask(0), 0o22)
Don Garrett86fec482017-05-17 18:13:33 -0700111
Alex Klein1699fab2022-09-08 08:46:06 -0600112 # Verify ENVs are cleaned up.
113 self.assertEqual(os.environ["LANG"], "en_US.UTF-8")
114 self.assertNotIn("LC_MONETARY", os.environ)
Don Garrett86fec482017-05-17 18:13:33 -0700115
Don Garrettf15d65b2017-04-12 12:39:55 -0700116
Benjamin Gordon121a2aa2018-05-04 16:24:45 -0600117class RunTests(cros_test_lib.RunCommandTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600118 """Tests for cbuildbot_launch script."""
Don Garrett597ddff2017-02-17 18:29:37 -0800119
Alex Klein1699fab2022-09-08 08:46:06 -0600120 ARGS_BASE = ["--buildroot", "/buildroot"]
121 EXPECTED_ARGS_BASE = ["--buildroot", "/cbuildbot_buildroot"]
122 ARGS_GIT_CACHE = ["--git-cache-dir", "/git-cache"]
123 ARGS_CONFIG = ["config"]
124 CMD = ["/cbuildbot_buildroot/chromite/bin/cbuildbot"]
Don Garrett597ddff2017-02-17 18:29:37 -0800125
Alex Klein1699fab2022-09-08 08:46:06 -0600126 def verifyCbuildbot(self, args, expected_cmd, version):
127 """Ensure we invoke cbuildbot correctly."""
128 self.PatchObject(
129 commands,
130 "GetTargetChromiteApiVersion",
131 autospec=True,
132 return_value=version,
133 )
Don Garrett597ddff2017-02-17 18:29:37 -0800134
Alex Klein1699fab2022-09-08 08:46:06 -0600135 cbuildbot_launch.Cbuildbot("/cbuildbot_buildroot", "/depot_tools", args)
Don Garrett597ddff2017-02-17 18:29:37 -0800136
Alex Klein1699fab2022-09-08 08:46:06 -0600137 self.assertCommandCalled(
138 expected_cmd,
139 extra_env={"PATH": mock.ANY},
140 cwd="/cbuildbot_buildroot",
141 check=False,
142 )
Don Garrett597ddff2017-02-17 18:29:37 -0800143
Alex Klein1699fab2022-09-08 08:46:06 -0600144 def testCbuildbotSimple(self):
145 """Ensure we invoke cbuildbot correctly."""
146 self.verifyCbuildbot(
147 self.ARGS_BASE + self.ARGS_CONFIG,
148 self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE,
149 (0, 4),
150 )
Don Garrett597ddff2017-02-17 18:29:37 -0800151
Alex Klein1699fab2022-09-08 08:46:06 -0600152 def testCbuildbotNotFiltered(self):
153 """Ensure we invoke cbuildbot correctly."""
154 self.verifyCbuildbot(
155 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
156 (
157 self.CMD
158 + self.ARGS_CONFIG
159 + self.EXPECTED_ARGS_BASE
160 + self.ARGS_GIT_CACHE
161 ),
162 (0, 4),
163 )
Don Garrett597ddff2017-02-17 18:29:37 -0800164
Alex Klein1699fab2022-09-08 08:46:06 -0600165 def testCbuildbotFiltered(self):
166 """Ensure we invoke cbuildbot correctly."""
167 self.verifyCbuildbot(
168 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
169 self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE,
170 (0, 2),
171 )
Don Garrettc4114cc2016-11-01 20:04:06 -0700172
Alex Klein1699fab2022-09-08 08:46:06 -0600173 def testMainMin(self):
174 """Test a minimal set of command line options."""
175 self.PatchObject(osutils, "SafeMakedirs", autospec=True)
176 self.PatchObject(
177 commands,
178 "GetTargetChromiteApiVersion",
179 autospec=True,
180 return_value=(
181 constants.REEXEC_API_MAJOR,
182 constants.REEXEC_API_MINOR,
183 ),
184 )
185 mock_repo = mock.MagicMock()
186 mock_repo.branch = "main"
187 mock_repo.directory = "/root/repository"
Don Garrettbf90cdf2017-05-19 15:54:02 -0700188
Alex Klein1699fab2022-09-08 08:46:06 -0600189 mock_repo_create = self.PatchObject(
190 repository, "RepoRepository", autospec=True, return_value=mock_repo
191 )
192 mock_clean = self.PatchObject(
193 cbuildbot_launch, "CleanBuildRoot", autospec=True
194 )
195 mock_checkout = self.PatchObject(
196 cbuildbot_launch, "InitialCheckout", autospec=True
197 )
198 mock_cleanup_chroot = self.PatchObject(
199 cbuildbot_launch, "CleanupChroot", autospec=True
200 )
201 mock_set_last_build_state = self.PatchObject(
202 cbuildbot_launch, "SetLastBuildState", autospec=True
203 )
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600204
Alex Klein1699fab2022-09-08 08:46:06 -0600205 expected_build_state = build_summary.BuildSummary(
206 build_number=0,
207 master_build_id=0,
208 status=mock.ANY,
209 buildroot_layout=2,
210 branch="main",
211 )
Don Garrett7ade05a2017-02-17 13:31:47 -0800212
Alex Klein1699fab2022-09-08 08:46:06 -0600213 argv = ["-r", "/root", "config"]
214 options = cbuildbot_launch.PreParseArguments(argv)
215 cbuildbot_launch._main(options, argv)
Don Garrettc4114cc2016-11-01 20:04:06 -0700216
Alex Klein1699fab2022-09-08 08:46:06 -0600217 # Did we create the repo instance correctly?
218 self.assertEqual(
219 mock_repo_create.mock_calls,
220 [
221 mock.call(
222 EXPECTED_MANIFEST_URL,
223 "/root/repository",
224 git_cache_dir=None,
225 branch="main",
226 )
227 ],
228 )
Don Garrettf324bc32017-05-23 14:00:53 -0700229
Alex Klein1699fab2022-09-08 08:46:06 -0600230 # Ensure we clean, as expected.
231 self.assertEqual(
232 mock_clean.mock_calls,
233 [
234 mock.call(
235 "/root",
236 mock_repo,
237 "/root/repository/.cache",
238 expected_build_state,
239 False,
240 )
241 ],
242 )
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000243
Alex Klein1699fab2022-09-08 08:46:06 -0600244 # Ensure we checkout, as expected.
245 self.assertEqual(
246 mock_checkout.mock_calls, [mock.call(mock_repo, options)]
247 )
Don Garrettc4114cc2016-11-01 20:04:06 -0700248
Alex Klein1699fab2022-09-08 08:46:06 -0600249 # Ensure we invoke cbuildbot, as expected.
250 self.assertCommandCalled(
251 [
252 "/root/repository/chromite/bin/cbuildbot",
253 "config",
254 "-r",
255 "/root/repository",
256 "--workspace",
257 "/root/workspace",
258 "--cache-dir",
259 "/root/repository/.cache",
260 # The duplication is a bug, but not harmful.
261 "--cache-dir",
262 "/root/repository/.cache",
263 ],
264 extra_env={"PATH": mock.ANY},
265 cwd="/root/repository",
266 check=False,
267 )
Don Garrettc4114cc2016-11-01 20:04:06 -0700268
Alex Klein1699fab2022-09-08 08:46:06 -0600269 # Ensure we saved the final state, as expected.
270 self.assertEqual(
271 expected_build_state.status, constants.BUILDER_STATUS_PASSED
272 )
273 self.assertEqual(
274 mock_set_last_build_state.mock_calls,
275 [mock.call("/root", expected_build_state)],
276 )
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600277
Alex Klein1699fab2022-09-08 08:46:06 -0600278 # Ensure we clean the chroot, as expected.
279 mock_cleanup_chroot.assert_called_once_with("/root/repository")
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700280
Alex Klein1699fab2022-09-08 08:46:06 -0600281 def testMainMax(self):
282 """Test a larger set of command line options."""
283 self.PatchObject(osutils, "SafeMakedirs", autospec=True)
284 self.PatchObject(
285 commands,
286 "GetTargetChromiteApiVersion",
287 autospec=True,
288 return_value=(
289 constants.REEXEC_API_MAJOR,
290 constants.REEXEC_API_MINOR,
291 ),
292 )
293 mock_repo = mock.MagicMock()
294 mock_repo.branch = "branch"
295 mock_repo.directory = "/root/repository"
Don Garrettbf90cdf2017-05-19 15:54:02 -0700296
Alex Klein1699fab2022-09-08 08:46:06 -0600297 mock_summary = build_summary.BuildSummary(
298 build_number=313,
299 master_build_id=123123123,
300 status=constants.BUILDER_STATUS_FAILED,
301 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
302 branch="branch",
303 )
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600304
Alex Klein1699fab2022-09-08 08:46:06 -0600305 mock_get_last_build_state = self.PatchObject(
306 cbuildbot_launch,
307 "GetLastBuildState",
308 autospec=True,
309 return_value=mock_summary,
310 )
311 mock_repo_create = self.PatchObject(
312 repository, "RepoRepository", autospec=True, return_value=mock_repo
313 )
314 mock_clean = self.PatchObject(
315 cbuildbot_launch, "CleanBuildRoot", autospec=True
316 )
317 mock_checkout = self.PatchObject(
318 cbuildbot_launch, "InitialCheckout", autospec=True
319 )
320 mock_cleanup_chroot = self.PatchObject(
321 cbuildbot_launch, "CleanupChroot", autospec=True
322 )
323 mock_set_last_build_state = self.PatchObject(
324 cbuildbot_launch, "SetLastBuildState", autospec=True
325 )
326 argv = [
327 "--buildroot",
328 "/root",
329 "--branch",
330 "branch",
331 "--git-cache-dir",
332 "/git-cache",
333 "--cache-dir",
334 "/cache",
335 "--remote-trybot",
336 "--master-build-id",
337 "123456789",
338 "--buildnumber",
339 "314",
340 "config",
341 ]
342 options = cbuildbot_launch.PreParseArguments(argv)
343 cbuildbot_launch._main(options, argv)
Don Garrettc4114cc2016-11-01 20:04:06 -0700344
Alex Klein1699fab2022-09-08 08:46:06 -0600345 # Did we create the repo instance correctly?
346 self.assertEqual(
347 mock_repo_create.mock_calls,
348 [
349 mock.call(
350 EXPECTED_MANIFEST_URL,
351 "/root/repository",
352 git_cache_dir="/git-cache",
353 branch="branch",
354 )
355 ],
356 )
Don Garrettf324bc32017-05-23 14:00:53 -0700357
Alex Klein1699fab2022-09-08 08:46:06 -0600358 # Ensure we look up the previous status.
359 self.assertEqual(
360 mock_get_last_build_state.mock_calls, [mock.call("/root")]
361 )
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600362
Alex Klein1699fab2022-09-08 08:46:06 -0600363 # Ensure we clean, as expected.
364 self.assertEqual(
365 mock_clean.mock_calls,
366 [
367 mock.call(
368 "/root",
369 mock_repo,
370 "/cache",
371 build_summary.BuildSummary(
372 build_number=314,
373 master_build_id=123456789,
374 status=mock.ANY,
375 branch="branch",
376 buildroot_layout=2,
377 ),
378 False,
379 )
380 ],
381 )
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000382
Alex Klein1699fab2022-09-08 08:46:06 -0600383 # Ensure we checkout, as expected.
384 self.assertEqual(
385 mock_checkout.mock_calls, [mock.call(mock_repo, options)]
386 )
Don Garrett86881cb2017-02-15 15:41:55 -0800387
Alex Klein1699fab2022-09-08 08:46:06 -0600388 # Ensure we invoke cbuildbot, as expected.
389 self.assertCommandCalled(
390 [
391 "/root/repository/chromite/bin/cbuildbot",
392 "config",
393 "--buildroot",
394 "/root/repository",
395 "--branch",
396 "branch",
397 "--git-cache-dir",
398 "/git-cache",
399 "--cache-dir",
400 "/cache",
401 "--remote-trybot",
402 "--master-build-id",
403 "123456789",
404 "--buildnumber",
405 "314",
406 "--previous-build-state",
Alex Klein9e7b29e2023-04-11 16:10:31 -0600407 "eyJicmFuY2giOiJicmFuY2giLCJidWlsZF9udW1iZXIiOjMxMywiYnVpbGRy"
408 "b290X2xheW91dCI6MiwibWFzdGVyX2J1aWxkX2lkIjoxMjMxMjMxMjMsInN0"
409 "YXR1cyI6ImZhaWwifQ==",
Alex Klein1699fab2022-09-08 08:46:06 -0600410 "--workspace",
411 "/root/workspace",
412 "--cache-dir",
413 "/cache",
414 ],
415 extra_env={"PATH": mock.ANY},
416 cwd="/root/repository",
417 check=False,
418 )
Don Garrett7ade05a2017-02-17 13:31:47 -0800419
Alex Klein1699fab2022-09-08 08:46:06 -0600420 # Ensure we write the final build state, as expected.
421 final_state = build_summary.BuildSummary(
422 build_number=314,
423 master_build_id=123456789,
424 status=constants.BUILDER_STATUS_PASSED,
425 buildroot_layout=2,
426 branch="branch",
427 )
428 self.assertEqual(
429 mock_set_last_build_state.mock_calls,
430 [mock.call("/root", final_state)],
431 )
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600432
Alex Klein1699fab2022-09-08 08:46:06 -0600433 # Ensure we clean the chroot, as expected.
434 mock_cleanup_chroot.assert_called_once_with("/root/repository")
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700435
Don Garrett7ade05a2017-02-17 13:31:47 -0800436
Don Garrettbf90cdf2017-05-19 15:54:02 -0700437class CleanBuildRootTest(cros_test_lib.MockTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600438 """Tests for CleanBuildRoot method."""
Don Garrett7ade05a2017-02-17 13:31:47 -0800439
Alex Klein1699fab2022-09-08 08:46:06 -0600440 def setUp(self):
441 """Create standard buildroot contents for cleanup."""
442 self.root = os.path.join(self.tempdir)
443 self.previous_build_state = os.path.join(
444 self.root, ".cbuildbot_build_state.json"
445 )
446 self.buildroot = os.path.join(self.root, "buildroot")
447 self.repo = os.path.join(self.buildroot, ".repo/repo")
448 self.chroot = os.path.join(self.buildroot, "chroot")
449 self.general = os.path.join(self.buildroot, "general/general")
450 self.cache = os.path.join(self.buildroot, ".cache")
451 self.distfiles = os.path.join(self.cache, "distfiles")
Don Garrett7ade05a2017-02-17 13:31:47 -0800452
Alex Klein1699fab2022-09-08 08:46:06 -0600453 self.mock_repo = mock.Mock(repository.RepoRepository)
454 self.mock_repo.directory = self.buildroot
Don Garrettf324bc32017-05-23 14:00:53 -0700455
Alex Klein1699fab2022-09-08 08:46:06 -0600456 def populateBuildroot(self, previous_build_state=None):
457 """Create standard buildroot contents for cleanup."""
458 if previous_build_state:
459 osutils.SafeMakedirs(self.root)
460 osutils.WriteFile(self.previous_build_state, previous_build_state)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600461
Alex Klein1699fab2022-09-08 08:46:06 -0600462 # Create files.
463 for f in (self.repo, self.chroot, self.general, self.distfiles):
464 osutils.Touch(f, makedirs=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800465
Alex Klein1699fab2022-09-08 08:46:06 -0600466 def testNoBuildroot(self):
467 """Test CleanBuildRoot with no history."""
468 self.mock_repo.branch = "main"
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000469
Alex Klein1699fab2022-09-08 08:46:06 -0600470 build_state = build_summary.BuildSummary(
471 status=constants.BUILDER_STATUS_INFLIGHT,
472 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
473 branch="main",
474 )
475 cbuildbot_launch.CleanBuildRoot(
476 self.root, self.mock_repo, self.cache, build_state
477 )
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000478
Alex Klein1699fab2022-09-08 08:46:06 -0600479 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
480 self.assertEqual(new_summary.buildroot_layout, 2)
481 self.assertEqual(new_summary.branch, "main")
482 self.assertIsNotNone(new_summary.distfiles_ts)
483 self.assertEqual(new_summary, build_state)
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000484
Alex Klein1699fab2022-09-08 08:46:06 -0600485 self.assertExists(self.previous_build_state)
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000486
Alex Klein1699fab2022-09-08 08:46:06 -0600487 def testBuildrootNoState(self):
488 """Test CleanBuildRoot with no state information."""
489 self.populateBuildroot()
490 self.mock_repo.branch = "main"
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000491
Alex Klein1699fab2022-09-08 08:46:06 -0600492 build_state = build_summary.BuildSummary(
493 status=constants.BUILDER_STATUS_INFLIGHT,
494 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
495 branch="main",
496 )
497 cbuildbot_launch.CleanBuildRoot(
498 self.root, self.mock_repo, self.cache, build_state
499 )
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000500
Alex Klein1699fab2022-09-08 08:46:06 -0600501 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
502 self.assertEqual(new_summary.buildroot_layout, 2)
503 self.assertEqual(new_summary.branch, "main")
504 self.assertIsNotNone(new_summary.distfiles_ts)
505 self.assertEqual(new_summary, build_state)
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000506
Alex Klein1699fab2022-09-08 08:46:06 -0600507 self.assertNotExists(self.repo)
508 self.assertNotExists(self.chroot)
509 self.assertNotExists(self.general)
510 self.assertNotExists(self.distfiles)
511 self.assertExists(self.previous_build_state)
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000512
Alex Klein1699fab2022-09-08 08:46:06 -0600513 def testBuildrootFormatMismatch(self):
514 """Test CleanBuildRoot with buildroot layout mismatch."""
515 old_build_state = build_summary.BuildSummary(
516 status=constants.BUILDER_STATUS_PASSED,
517 buildroot_layout=1,
518 branch="main",
519 )
520 self.populateBuildroot(previous_build_state=old_build_state.to_json())
521 self.mock_repo.branch = "main"
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000522
Alex Klein1699fab2022-09-08 08:46:06 -0600523 build_state = build_summary.BuildSummary(
524 status=constants.BUILDER_STATUS_INFLIGHT,
525 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
526 branch="main",
527 )
528 cbuildbot_launch.CleanBuildRoot(
529 self.root, self.mock_repo, self.cache, build_state
530 )
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000531
Alex Klein1699fab2022-09-08 08:46:06 -0600532 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
533 self.assertEqual(new_summary.buildroot_layout, 2)
534 self.assertEqual(new_summary.branch, "main")
535 self.assertIsNotNone(new_summary.distfiles_ts)
536 self.assertEqual(new_summary, build_state)
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000537
Alex Klein1699fab2022-09-08 08:46:06 -0600538 self.assertNotExists(self.repo)
539 self.assertNotExists(self.chroot)
540 self.assertNotExists(self.general)
541 self.assertNotExists(self.distfiles)
542 self.assertExists(self.previous_build_state)
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000543
Alex Klein1699fab2022-09-08 08:46:06 -0600544 def testBuildrootBranchChange(self):
545 """Test CleanBuildRoot with a change in branches."""
546 old_build_state = build_summary.BuildSummary(
547 status=constants.BUILDER_STATUS_PASSED,
548 buildroot_layout=2,
549 branch="branchA",
550 )
551 self.populateBuildroot(previous_build_state=old_build_state.to_json())
552 self.mock_repo.branch = "branchB"
553 m = self.PatchObject(cros_sdk_lib, "CleanupChrootMount")
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000554
Alex Klein1699fab2022-09-08 08:46:06 -0600555 build_state = build_summary.BuildSummary(
556 status=constants.BUILDER_STATUS_INFLIGHT,
557 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
558 branch="branchB",
559 )
560 cbuildbot_launch.CleanBuildRoot(
561 self.root, self.mock_repo, self.cache, build_state
562 )
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000563
Alex Klein1699fab2022-09-08 08:46:06 -0600564 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
565 self.assertEqual(new_summary.buildroot_layout, 2)
566 self.assertEqual(new_summary.branch, "branchB")
567 self.assertIsNotNone(new_summary.distfiles_ts)
568 self.assertEqual(new_summary, build_state)
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000569
Alex Klein1699fab2022-09-08 08:46:06 -0600570 # self.assertExists(self.repo)
571 self.assertExists(self.general)
572 self.assertNotExists(self.distfiles)
573 self.assertExists(self.previous_build_state)
Brian Norris872684f2023-08-22 15:20:20 -0700574 m.assert_called_with(
575 chroot_lib.Chroot(
576 path=self.buildroot / Path(constants.DEFAULT_CHROOT_DIR),
577 out_path=self.buildroot / constants.DEFAULT_OUT_DIR,
578 ),
579 delete=True,
580 )
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000581
Alex Klein1699fab2022-09-08 08:46:06 -0600582 def testBuildrootBranchMatch(self):
583 """Test CleanBuildRoot with no change in branch."""
584 old_build_state = build_summary.BuildSummary(
585 status=constants.BUILDER_STATUS_PASSED,
586 buildroot_layout=2,
587 branch="branchA",
588 )
589 self.populateBuildroot(previous_build_state=old_build_state.to_json())
590 self.mock_repo.branch = "branchA"
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000591
Alex Klein1699fab2022-09-08 08:46:06 -0600592 build_state = build_summary.BuildSummary(
593 status=constants.BUILDER_STATUS_INFLIGHT,
594 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
595 branch="branchA",
596 )
597 cbuildbot_launch.CleanBuildRoot(
598 self.root, self.mock_repo, self.cache, build_state
599 )
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000600
Alex Klein1699fab2022-09-08 08:46:06 -0600601 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
602 self.assertEqual(new_summary.buildroot_layout, 2)
603 self.assertEqual(new_summary.branch, "branchA")
604 self.assertIsNotNone(new_summary.distfiles_ts)
605 self.assertEqual(new_summary, build_state)
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000606
Alex Klein1699fab2022-09-08 08:46:06 -0600607 self.assertExists(self.repo)
608 self.assertExists(self.chroot)
609 self.assertExists(self.general)
610 self.assertExists(self.distfiles)
611 self.assertExists(self.previous_build_state)
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000612
Alex Klein1699fab2022-09-08 08:46:06 -0600613 def testBuildrootGitLocksPrevPass(self):
614 """Verify not CleanStaleLocks, if previous build was in passed."""
615 old_build_state = build_summary.BuildSummary(
616 status=constants.BUILDER_STATUS_PASSED,
617 buildroot_layout=2,
618 branch="branchA",
619 )
620 self.populateBuildroot(previous_build_state=old_build_state.to_json())
621 self.mock_repo.branch = "branchA"
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000622
Alex Klein1699fab2022-09-08 08:46:06 -0600623 build_state = build_summary.BuildSummary(
624 status=constants.BUILDER_STATUS_INFLIGHT,
625 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
626 branch="branchA",
627 )
628 cbuildbot_launch.CleanBuildRoot(
629 self.root, self.mock_repo, self.cache, build_state
630 )
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000631
Alex Klein1699fab2022-09-08 08:46:06 -0600632 self.assertEqual(
633 self.mock_repo.mock_calls,
634 [
635 mock.call.PreLoad(),
636 mock.call.BuildRootGitCleanup(prune_all=True),
637 ],
638 )
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000639
Alex Klein1699fab2022-09-08 08:46:06 -0600640 def testBuildrootGitLocksPrevFail(self):
641 """Verify not CleanStaleLocks, if previous build was in failed."""
642 old_build_state = build_summary.BuildSummary(
643 status=constants.BUILDER_STATUS_FAILED,
644 buildroot_layout=2,
645 branch="branchA",
646 )
647 self.populateBuildroot(previous_build_state=old_build_state.to_json())
648 self.mock_repo.branch = "branchA"
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000649
Alex Klein1699fab2022-09-08 08:46:06 -0600650 build_state = build_summary.BuildSummary(
651 status=constants.BUILDER_STATUS_INFLIGHT,
652 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
653 branch="branchA",
654 )
655 cbuildbot_launch.CleanBuildRoot(
656 self.root, self.mock_repo, self.cache, build_state
657 )
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000658
Alex Klein1699fab2022-09-08 08:46:06 -0600659 self.assertEqual(
660 self.mock_repo.mock_calls,
661 [
662 mock.call.PreLoad(),
663 mock.call.BuildRootGitCleanup(prune_all=True),
664 ],
665 )
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000666
Alex Klein1699fab2022-09-08 08:46:06 -0600667 def testBuildrootGitLocksPrevInFlight(self):
668 """Verify CleanStaleLocks, if previous build was in flight."""
669 old_build_state = build_summary.BuildSummary(
670 status=constants.BUILDER_STATUS_INFLIGHT,
671 buildroot_layout=2,
672 branch="branchA",
673 )
674 self.populateBuildroot(previous_build_state=old_build_state.to_json())
675 self.mock_repo.branch = "branchA"
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000676
Alex Klein1699fab2022-09-08 08:46:06 -0600677 build_state = build_summary.BuildSummary(
678 status=constants.BUILDER_STATUS_INFLIGHT,
679 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
680 branch="branchA",
681 )
682 cbuildbot_launch.CleanBuildRoot(
683 self.root, self.mock_repo, self.cache, build_state
684 )
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000685
Alex Klein1699fab2022-09-08 08:46:06 -0600686 self.assertEqual(
687 self.mock_repo.method_calls,
688 [
689 mock.call.PreLoad(),
690 mock.call.CleanStaleLocks(),
691 mock.call.BuildRootGitCleanup(prune_all=True),
692 ],
693 )
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000694
Alex Klein1699fab2022-09-08 08:46:06 -0600695 def testBuildrootDistfilesRecentCache(self):
Alex Klein9e7b29e2023-04-11 16:10:31 -0600696 """Test CleanBuildRoot skips distfiles when cache is recent."""
Alex Klein1699fab2022-09-08 08:46:06 -0600697 seed_distfiles_ts = time.time() - 60
698 old_build_state = build_summary.BuildSummary(
699 status=constants.BUILDER_STATUS_PASSED,
700 buildroot_layout=2,
701 branch="branchA",
702 distfiles_ts=seed_distfiles_ts,
703 )
704 self.populateBuildroot(previous_build_state=old_build_state.to_json())
705 self.mock_repo.branch = "branchA"
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000706
Alex Klein1699fab2022-09-08 08:46:06 -0600707 build_state = build_summary.BuildSummary(
708 status=constants.BUILDER_STATUS_INFLIGHT,
709 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
710 branch="branchA",
711 )
712 cbuildbot_launch.CleanBuildRoot(
713 self.root, self.mock_repo, self.cache, build_state
714 )
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000715
Alex Klein1699fab2022-09-08 08:46:06 -0600716 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
717 self.assertEqual(new_summary.buildroot_layout, 2)
718 self.assertEqual(new_summary.branch, "branchA")
719 # Same cache creation timestamp is rewritten to state.
720 self.assertEqual(new_summary.distfiles_ts, seed_distfiles_ts)
721 self.assertEqual(new_summary, build_state)
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000722
Alex Klein1699fab2022-09-08 08:46:06 -0600723 self.assertExists(self.repo)
724 self.assertExists(self.chroot)
725 self.assertExists(self.general)
726 self.assertExists(self.distfiles)
727 self.assertExists(self.previous_build_state)
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000728
Alex Klein1699fab2022-09-08 08:46:06 -0600729 def testBuildrootDistfilesCacheExpired(self):
730 """Test CleanBuildRoot when the distfiles cache is too old."""
731 old_build_state = build_summary.BuildSummary(
732 status=constants.BUILDER_STATUS_PASSED,
733 buildroot_layout=2,
734 branch="branchA",
735 distfiles_ts=100.0,
736 )
737 self.populateBuildroot(previous_build_state=old_build_state.to_json())
738 self.mock_repo.branch = "branchA"
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000739
Alex Klein1699fab2022-09-08 08:46:06 -0600740 build_state = build_summary.BuildSummary(
741 status=constants.BUILDER_STATUS_INFLIGHT,
742 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
743 branch="branchA",
744 )
745 cbuildbot_launch.CleanBuildRoot(
746 self.root, self.mock_repo, self.cache, build_state
747 )
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000748
Alex Klein1699fab2022-09-08 08:46:06 -0600749 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
750 self.assertEqual(new_summary.buildroot_layout, 2)
751 self.assertEqual(new_summary.branch, "branchA")
752 self.assertIsNotNone(new_summary.distfiles_ts)
753 self.assertEqual(new_summary, build_state)
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000754
Alex Klein1699fab2022-09-08 08:46:06 -0600755 self.assertExists(self.repo)
756 self.assertExists(self.chroot)
757 self.assertExists(self.general)
758 self.assertNotExists(self.distfiles)
759 self.assertExists(self.previous_build_state)
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000760
Alex Klein1699fab2022-09-08 08:46:06 -0600761 def testRootOwnedCache(self):
762 """Test CleanBuildRoot with no history."""
763 seed_distfiles_ts = time.time() - 60
764 old_build_state = build_summary.BuildSummary(
765 status=constants.BUILDER_STATUS_PASSED,
766 buildroot_layout=2,
767 branch="branchA",
768 distfiles_ts=seed_distfiles_ts,
769 )
770 self.populateBuildroot(previous_build_state=old_build_state.to_json())
771 self.mock_repo.branch = "branchA"
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000772
Alex Klein1699fab2022-09-08 08:46:06 -0600773 osutils.Chown(self.cache, "root", "root")
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000774
Alex Klein1699fab2022-09-08 08:46:06 -0600775 build_state = build_summary.BuildSummary(
776 status=constants.BUILDER_STATUS_INFLIGHT,
777 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
778 branch="branchA",
779 )
780 cbuildbot_launch.CleanBuildRoot(
781 self.root, self.mock_repo, self.cache, build_state
782 )
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000783
Alex Klein1699fab2022-09-08 08:46:06 -0600784 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
785 self.assertEqual(new_summary.buildroot_layout, 2)
786 self.assertEqual(new_summary.branch, "branchA")
787 # Same cache creation timestamp is rewritten to state.
788 self.assertEqual(new_summary.distfiles_ts, seed_distfiles_ts)
789 self.assertEqual(new_summary, build_state)
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000790
Alex Klein1699fab2022-09-08 08:46:06 -0600791 self.assertExists(self.repo)
792 self.assertExists(self.chroot)
793 self.assertExists(self.general)
794 self.assertNotExists(self.distfiles)
795 self.assertExists(self.previous_build_state)
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000796
Alex Klein1699fab2022-09-08 08:46:06 -0600797 def testBuildrootRepoCleanFailure(self):
798 """Test CleanBuildRoot with repo checkout failure."""
799 old_build_state = build_summary.BuildSummary(
800 status=constants.BUILDER_STATUS_PASSED,
801 buildroot_layout=1,
802 branch="branchA",
803 )
804 self.populateBuildroot(previous_build_state=old_build_state.to_json())
805 self.mock_repo.branch = "branchA"
806 self.mock_repo.BuildRootGitCleanup.side_effect = Exception
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000807
Alex Klein1699fab2022-09-08 08:46:06 -0600808 build_state = build_summary.BuildSummary(
809 status=constants.BUILDER_STATUS_INFLIGHT,
810 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
811 branch="branchA",
812 )
813 cbuildbot_launch.CleanBuildRoot(
814 self.root, self.mock_repo, self.cache, build_state
815 )
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000816
Alex Klein1699fab2022-09-08 08:46:06 -0600817 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
818 self.assertEqual(new_summary.buildroot_layout, 2)
819 self.assertEqual(new_summary.branch, "branchA")
820 self.assertIsNotNone(new_summary.distfiles_ts)
821 self.assertEqual(new_summary, build_state)
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000822
Alex Klein1699fab2022-09-08 08:46:06 -0600823 self.assertNotExists(self.repo)
824 self.assertNotExists(self.chroot)
825 self.assertNotExists(self.general)
826 self.assertNotExists(self.distfiles)
827 self.assertExists(self.previous_build_state)
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000828
Alex Klein1699fab2022-09-08 08:46:06 -0600829 def testGetCurrentBuildStateNoArgs(self):
830 """Tests GetCurrentBuildState without arguments."""
831 options = cbuildbot_launch.PreParseArguments(
832 ["--buildroot", self.root, "config"]
833 )
834 state = cbuildbot_launch.GetCurrentBuildState(options, "main")
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600835
Alex Klein1699fab2022-09-08 08:46:06 -0600836 expected_state = build_summary.BuildSummary(
837 status=constants.BUILDER_STATUS_INFLIGHT,
838 buildroot_layout=2,
839 branch="main",
840 )
841 self.assertEqual(state, expected_state)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600842
Alex Klein1699fab2022-09-08 08:46:06 -0600843 def testGetCurrentBuildStateHasArgs(self):
844 """Tests GetCurrentBuildState with arguments."""
845 options = cbuildbot_launch.PreParseArguments(
846 [
847 "--buildroot",
848 self.root,
849 "--buildnumber",
850 "20",
851 "--master-build-id",
852 "50",
853 "config",
854 ]
855 )
856 state = cbuildbot_launch.GetCurrentBuildState(options, "branchA")
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600857
Alex Klein1699fab2022-09-08 08:46:06 -0600858 expected_state = build_summary.BuildSummary(
859 build_number=20,
860 master_build_id=50,
861 status=constants.BUILDER_STATUS_INFLIGHT,
862 buildroot_layout=2,
863 branch="branchA",
864 )
865 self.assertEqual(state, expected_state)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600866
Alex Klein1699fab2022-09-08 08:46:06 -0600867 def testGetCurrentBuildStateLayout(self):
868 """Test that GetCurrentBuildState uses the current buildroot layout."""
869 # Change to a future version.
870 self.PatchObject(cbuildbot_launch, "BUILDROOT_BUILDROOT_LAYOUT", 22)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600871
Alex Klein1699fab2022-09-08 08:46:06 -0600872 options = cbuildbot_launch.PreParseArguments(
873 ["--buildroot", self.root, "config"]
874 )
875 state = cbuildbot_launch.GetCurrentBuildState(options, "branchA")
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600876
Alex Klein1699fab2022-09-08 08:46:06 -0600877 expected_state = build_summary.BuildSummary(
878 status=constants.BUILDER_STATUS_INFLIGHT,
879 buildroot_layout=22,
880 branch="branchA",
881 )
882 self.assertEqual(state, expected_state)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600883
Alex Klein1699fab2022-09-08 08:46:06 -0600884 def testGetLastBuildStateNoFile(self):
885 """Tests GetLastBuildState if the file is missing."""
886 osutils.SafeMakedirs(self.root)
887 state = cbuildbot_launch.GetLastBuildState(self.root)
888 self.assertEqual(state, build_summary.BuildSummary())
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600889
Alex Klein1699fab2022-09-08 08:46:06 -0600890 def testGetLastBuildStateBadFile(self):
891 """Tests GetLastBuildState if the file contains invalid JSON."""
892 osutils.SafeMakedirs(self.root)
893 osutils.WriteFile(self.previous_build_state, "}}")
894 state = cbuildbot_launch.GetLastBuildState(self.root)
895 self.assertEqual(state, build_summary.BuildSummary())
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600896
Alex Klein1699fab2022-09-08 08:46:06 -0600897 def testGetLastBuildStateMissingBuildStatus(self):
898 """Tests GetLastBuildState if the file doesn't have a valid status."""
899 osutils.SafeMakedirs(self.root)
900 osutils.WriteFile(self.previous_build_state, '{"build_number": "3"}')
901 state = cbuildbot_launch.GetLastBuildState(self.root)
902 self.assertEqual(state, build_summary.BuildSummary())
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600903
Alex Klein1699fab2022-09-08 08:46:06 -0600904 def testGetLastBuildStateGoodFile(self):
905 """Tests GetLastBuildState on a good file."""
906 osutils.SafeMakedirs(self.root)
907 osutils.WriteFile(
908 self.previous_build_state,
909 '{"build_number": 1, "master_build_id": 3, "status": "pass"}',
910 )
911 state = cbuildbot_launch.GetLastBuildState(self.root)
912 self.assertEqual(
913 state,
914 build_summary.BuildSummary(
915 build_number=1, master_build_id=3, status="pass"
916 ),
917 )
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600918
Alex Klein1699fab2022-09-08 08:46:06 -0600919 def testSetLastBuildState(self):
920 """Verifies that SetLastBuildState writes to the expected file."""
921 osutils.SafeMakedirs(self.root)
922 old_state = build_summary.BuildSummary(
923 build_number=314,
924 master_build_id=2178,
925 status=constants.BUILDER_STATUS_PASSED,
926 )
927 cbuildbot_launch.SetLastBuildState(self.root, old_state)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600928
Alex Klein1699fab2022-09-08 08:46:06 -0600929 saved_state = osutils.ReadFile(self.previous_build_state)
930 new_state = build_summary.BuildSummary()
931 new_state.from_json(saved_state)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600932
Alex Klein1699fab2022-09-08 08:46:06 -0600933 self.assertEqual(old_state, new_state)
Mike Frysingerf0146252019-09-02 13:31:05 -0400934
Alex Klein1699fab2022-09-08 08:46:06 -0600935 def testCleanupChrootNoChroot(self):
936 """Check CleanupChroot without a chroot."""
937 self.StartPatcher(cros_test_lib.RunCommandMock())
938 with mock.patch.object(cros_sdk_lib, "CleanupChrootMount"):
939 cbuildbot_launch.CleanupChroot(self.buildroot)
Mike Frysingerf0146252019-09-02 13:31:05 -0400940
Alex Klein1699fab2022-09-08 08:46:06 -0600941 def testCleanupChrootNormal(self):
942 """Check normal CleanupChroot."""
943 osutils.SafeMakedirs(self.chroot)
944 osutils.Touch(self.chroot + ".img")
945 self.StartPatcher(cros_test_lib.RunCommandMock())
946 with mock.patch.object(cros_sdk_lib, "CleanupChrootMount"):
947 cbuildbot_launch.CleanupChroot(self.buildroot)
Mike Frysingerf0146252019-09-02 13:31:05 -0400948
Alex Klein1699fab2022-09-08 08:46:06 -0600949 def testCleanupChrootTimeout(self):
950 """Check timeouts in CleanupChroot."""
951 osutils.SafeMakedirs(self.chroot)
952 osutils.Touch(self.chroot + ".img")
953 rc_mock = self.StartPatcher(cros_test_lib.RunCommandMock())
954 rc_mock.SetDefaultCmdResult()
955 with mock.patch.object(
956 cros_sdk_lib,
957 "CleanupChrootMount",
958 side_effect=timeout_util.TimeoutError,
959 ):
960 cbuildbot_launch.CleanupChroot(self.buildroot)