blob: 489d33f38c3450a92e9b71f72eb7cc291fc74a35 [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
Mike Nicholsd0acc7f2021-05-21 17:18:24 +00008import time
Mike Frysinger166fea02021-02-12 05:30:33 -05009from unittest import mock
Don Garrett86881cb2017-02-15 15:41:55 -080010
Mike Frysingerc263d092019-09-18 15:11:47 -040011from chromite.cbuildbot import commands
Don Garrett86881cb2017-02-15 15:41:55 -080012from chromite.cbuildbot import repository
Benjamin Gordon90b2dd92018-04-12 14:04:21 -060013from chromite.lib import build_summary
Don Garrett861e9182017-05-15 15:30:23 -070014from chromite.lib import constants
Benjamin Gordon74645232018-05-04 17:40:42 -060015from chromite.lib import cros_sdk_lib
Don Garrett7ade05a2017-02-17 13:31:47 -080016from chromite.lib import cros_test_lib
Don Garrett86881cb2017-02-15 15:41:55 -080017from chromite.lib import osutils
Mike Frysingerf0146252019-09-02 13:31:05 -040018from chromite.lib import timeout_util
Don Garrett0c54ed72017-03-03 11:18:57 -080019from chromite.scripts import cbuildbot_launch
Don Garrett86881cb2017-02-15 15:41:55 -080020
Mike Frysinger3c831a72020-02-19 02:47:04 -050021
Alex Klein1699fab2022-09-08 08:46:06 -060022EXPECTED_MANIFEST_URL = "https://chrome-internal-review.googlesource.com/chromeos/manifest-internal" # pylint: disable=line-too-long
Don Garrettc4114cc2016-11-01 20:04:06 -070023
24
Don Garrettacbb2392017-05-11 18:27:41 -070025# It's reasonable for unittests to look at internals.
26# pylint: disable=protected-access
27
28
Don Garrett8d314792017-05-18 13:11:42 -070029class FakeException(Exception):
Alex Klein1699fab2022-09-08 08:46:06 -060030 """Test exception to raise during tests."""
Don Garrett8d314792017-05-18 13:11:42 -070031
32
Don Garrett0c54ed72017-03-03 11:18:57 -080033class CbuildbotLaunchTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060034 """Tests for cbuildbot_launch script."""
Don Garrettc4114cc2016-11-01 20:04:06 -070035
Alex Klein1699fab2022-09-08 08:46:06 -060036 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 Garrett597ddff2017-02-17 18:29:37 -080072
Alex Klein1699fab2022-09-08 08:46:06 -060073 for cmd_args, expected in CASES:
74 expected_branch, expected_buildroot, expected_cache_dir = expected
Don Garrett597ddff2017-02-17 18:29:37 -080075
Alex Klein1699fab2022-09-08 08:46:06 -060076 options = cbuildbot_launch.PreParseArguments(cmd_args)
Don Garrettc4114cc2016-11-01 20:04:06 -070077
Alex Klein1699fab2022-09-08 08:46:06 -060078 self.assertEqual(options.branch, expected_branch)
79 self.assertEqual(options.buildroot, expected_buildroot)
80 self.assertEqual(options.git_cache_dir, expected_cache_dir)
Don Garrett597ddff2017-02-17 18:29:37 -080081
Alex Klein1699fab2022-09-08 08:46:06 -060082 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 Garrett597ddff2017-02-17 18:29:37 -080088
Alex Klein1699fab2022-09-08 08:46:06 -060089 cbuildbot_launch.InitialCheckout(mock_repo, options)
Don Garrett86881cb2017-02-15 15:41:55 -080090
Alex Klein1699fab2022-09-08 08:46:06 -060091 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 Garrett86881cb2017-02-15 15:41:55 -080098
Alex Klein1699fab2022-09-08 08:46:06 -060099 def testConfigureGlobalEnvironment(self):
100 """Ensure that we can setup our global runtime environment correctly."""
Don Garrett86881cb2017-02-15 15:41:55 -0800101
Alex Klein1699fab2022-09-08 08:46:06 -0600102 os.environ.pop("LANG", None)
103 os.environ["LC_MONETARY"] = "bad"
Don Garrett8d314792017-05-18 13:11:42 -0700104
Alex Klein1699fab2022-09-08 08:46:06 -0600105 cbuildbot_launch.ConfigureGlobalEnvironment()
Don Garrett86fec482017-05-17 18:13:33 -0700106
Alex Klein1699fab2022-09-08 08:46:06 -0600107 # Verify umask is updated.
108 self.assertEqual(os.umask(0), 0o22)
Don Garrett86fec482017-05-17 18:13:33 -0700109
Alex Klein1699fab2022-09-08 08:46:06 -0600110 # Verify ENVs are cleaned up.
111 self.assertEqual(os.environ["LANG"], "en_US.UTF-8")
112 self.assertNotIn("LC_MONETARY", os.environ)
Don Garrett86fec482017-05-17 18:13:33 -0700113
Don Garrettf15d65b2017-04-12 12:39:55 -0700114
Benjamin Gordon121a2aa2018-05-04 16:24:45 -0600115class RunTests(cros_test_lib.RunCommandTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600116 """Tests for cbuildbot_launch script."""
Don Garrett597ddff2017-02-17 18:29:37 -0800117
Alex Klein1699fab2022-09-08 08:46:06 -0600118 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 Garrett597ddff2017-02-17 18:29:37 -0800123
Alex Klein1699fab2022-09-08 08:46:06 -0600124 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 Garrett597ddff2017-02-17 18:29:37 -0800132
Alex Klein1699fab2022-09-08 08:46:06 -0600133 cbuildbot_launch.Cbuildbot("/cbuildbot_buildroot", "/depot_tools", args)
Don Garrett597ddff2017-02-17 18:29:37 -0800134
Alex Klein1699fab2022-09-08 08:46:06 -0600135 self.assertCommandCalled(
136 expected_cmd,
137 extra_env={"PATH": mock.ANY},
138 cwd="/cbuildbot_buildroot",
139 check=False,
140 )
Don Garrett597ddff2017-02-17 18:29:37 -0800141
Alex Klein1699fab2022-09-08 08:46:06 -0600142 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 Garrett597ddff2017-02-17 18:29:37 -0800149
Alex Klein1699fab2022-09-08 08:46:06 -0600150 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 Garrett597ddff2017-02-17 18:29:37 -0800162
Alex Klein1699fab2022-09-08 08:46:06 -0600163 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 Garrettc4114cc2016-11-01 20:04:06 -0700170
Alex Klein1699fab2022-09-08 08:46:06 -0600171 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 Garrettbf90cdf2017-05-19 15:54:02 -0700186
Alex Klein1699fab2022-09-08 08:46:06 -0600187 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 Gordon90b2dd92018-04-12 14:04:21 -0600202
Alex Klein1699fab2022-09-08 08:46:06 -0600203 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 Garrett7ade05a2017-02-17 13:31:47 -0800210
Alex Klein1699fab2022-09-08 08:46:06 -0600211 argv = ["-r", "/root", "config"]
212 options = cbuildbot_launch.PreParseArguments(argv)
213 cbuildbot_launch._main(options, argv)
Don Garrettc4114cc2016-11-01 20:04:06 -0700214
Alex Klein1699fab2022-09-08 08:46:06 -0600215 # 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 Garrettf324bc32017-05-23 14:00:53 -0700227
Alex Klein1699fab2022-09-08 08:46:06 -0600228 # 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000241
Alex Klein1699fab2022-09-08 08:46:06 -0600242 # Ensure we checkout, as expected.
243 self.assertEqual(
244 mock_checkout.mock_calls, [mock.call(mock_repo, options)]
245 )
Don Garrettc4114cc2016-11-01 20:04:06 -0700246
Alex Klein1699fab2022-09-08 08:46:06 -0600247 # 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 Garrettc4114cc2016-11-01 20:04:06 -0700266
Alex Klein1699fab2022-09-08 08:46:06 -0600267 # 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 Gordon90b2dd92018-04-12 14:04:21 -0600275
Alex Klein1699fab2022-09-08 08:46:06 -0600276 # Ensure we clean the chroot, as expected.
277 mock_cleanup_chroot.assert_called_once_with("/root/repository")
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700278
Alex Klein1699fab2022-09-08 08:46:06 -0600279 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 Garrettbf90cdf2017-05-19 15:54:02 -0700294
Alex Klein1699fab2022-09-08 08:46:06 -0600295 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 Gordon90b2dd92018-04-12 14:04:21 -0600302
Alex Klein1699fab2022-09-08 08:46:06 -0600303 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 Garrettc4114cc2016-11-01 20:04:06 -0700342
Alex Klein1699fab2022-09-08 08:46:06 -0600343 # 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 Garrettf324bc32017-05-23 14:00:53 -0700355
Alex Klein1699fab2022-09-08 08:46:06 -0600356 # Ensure we look up the previous status.
357 self.assertEqual(
358 mock_get_last_build_state.mock_calls, [mock.call("/root")]
359 )
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600360
Alex Klein1699fab2022-09-08 08:46:06 -0600361 # 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000380
Alex Klein1699fab2022-09-08 08:46:06 -0600381 # Ensure we checkout, as expected.
382 self.assertEqual(
383 mock_checkout.mock_calls, [mock.call(mock_repo, options)]
384 )
Don Garrett86881cb2017-02-15 15:41:55 -0800385
Alex Klein1699fab2022-09-08 08:46:06 -0600386 # 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 Garrett7ade05a2017-02-17 13:31:47 -0800417
Alex Klein1699fab2022-09-08 08:46:06 -0600418 # 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 Gordon90b2dd92018-04-12 14:04:21 -0600430
Alex Klein1699fab2022-09-08 08:46:06 -0600431 # Ensure we clean the chroot, as expected.
432 mock_cleanup_chroot.assert_called_once_with("/root/repository")
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700433
Don Garrett7ade05a2017-02-17 13:31:47 -0800434
Don Garrettbf90cdf2017-05-19 15:54:02 -0700435class CleanBuildRootTest(cros_test_lib.MockTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600436 """Tests for CleanBuildRoot method."""
Don Garrett7ade05a2017-02-17 13:31:47 -0800437
Alex Klein1699fab2022-09-08 08:46:06 -0600438 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 Garrett7ade05a2017-02-17 13:31:47 -0800450
Alex Klein1699fab2022-09-08 08:46:06 -0600451 self.mock_repo = mock.Mock(repository.RepoRepository)
452 self.mock_repo.directory = self.buildroot
Don Garrettf324bc32017-05-23 14:00:53 -0700453
Alex Klein1699fab2022-09-08 08:46:06 -0600454 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 Gordon90b2dd92018-04-12 14:04:21 -0600459
Alex Klein1699fab2022-09-08 08:46:06 -0600460 # Create files.
461 for f in (self.repo, self.chroot, self.general, self.distfiles):
462 osutils.Touch(f, makedirs=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800463
Alex Klein1699fab2022-09-08 08:46:06 -0600464 def testNoBuildroot(self):
465 """Test CleanBuildRoot with no history."""
466 self.mock_repo.branch = "main"
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000467
Alex Klein1699fab2022-09-08 08:46:06 -0600468 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000476
Alex Klein1699fab2022-09-08 08:46:06 -0600477 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000482
Alex Klein1699fab2022-09-08 08:46:06 -0600483 self.assertExists(self.previous_build_state)
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000484
Alex Klein1699fab2022-09-08 08:46:06 -0600485 def testBuildrootNoState(self):
486 """Test CleanBuildRoot with no state information."""
487 self.populateBuildroot()
488 self.mock_repo.branch = "main"
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000489
Alex Klein1699fab2022-09-08 08:46:06 -0600490 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000498
Alex Klein1699fab2022-09-08 08:46:06 -0600499 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000504
Alex Klein1699fab2022-09-08 08:46:06 -0600505 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000510
Alex Klein1699fab2022-09-08 08:46:06 -0600511 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000520
Alex Klein1699fab2022-09-08 08:46:06 -0600521 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000529
Alex Klein1699fab2022-09-08 08:46:06 -0600530 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000535
Alex Klein1699fab2022-09-08 08:46:06 -0600536 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000541
Alex Klein1699fab2022-09-08 08:46:06 -0600542 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000552
Alex Klein1699fab2022-09-08 08:46:06 -0600553 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000561
Alex Klein1699fab2022-09-08 08:46:06 -0600562 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000567
Alex Klein1699fab2022-09-08 08:46:06 -0600568 # 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000573
Alex Klein1699fab2022-09-08 08:46:06 -0600574 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000583
Alex Klein1699fab2022-09-08 08:46:06 -0600584 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000592
Alex Klein1699fab2022-09-08 08:46:06 -0600593 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000598
Alex Klein1699fab2022-09-08 08:46:06 -0600599 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000604
Alex Klein1699fab2022-09-08 08:46:06 -0600605 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000614
Alex Klein1699fab2022-09-08 08:46:06 -0600615 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000623
Alex Klein1699fab2022-09-08 08:46:06 -0600624 self.assertEqual(
625 self.mock_repo.mock_calls,
626 [
627 mock.call.PreLoad(),
628 mock.call.BuildRootGitCleanup(prune_all=True),
629 ],
630 )
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000631
Alex Klein1699fab2022-09-08 08:46:06 -0600632 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000641
Alex Klein1699fab2022-09-08 08:46:06 -0600642 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000650
Alex Klein1699fab2022-09-08 08:46:06 -0600651 self.assertEqual(
652 self.mock_repo.mock_calls,
653 [
654 mock.call.PreLoad(),
655 mock.call.BuildRootGitCleanup(prune_all=True),
656 ],
657 )
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000658
Alex Klein1699fab2022-09-08 08:46:06 -0600659 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000668
Alex Klein1699fab2022-09-08 08:46:06 -0600669 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000677
Alex Klein1699fab2022-09-08 08:46:06 -0600678 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000686
Alex Klein1699fab2022-09-08 08:46:06 -0600687 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000698
Alex Klein1699fab2022-09-08 08:46:06 -0600699 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000707
Alex Klein1699fab2022-09-08 08:46:06 -0600708 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000714
Alex Klein1699fab2022-09-08 08:46:06 -0600715 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000720
Alex Klein1699fab2022-09-08 08:46:06 -0600721 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000731
Alex Klein1699fab2022-09-08 08:46:06 -0600732 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000740
Alex Klein1699fab2022-09-08 08:46:06 -0600741 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000746
Alex Klein1699fab2022-09-08 08:46:06 -0600747 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000752
Alex Klein1699fab2022-09-08 08:46:06 -0600753 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000764
Alex Klein1699fab2022-09-08 08:46:06 -0600765 osutils.Chown(self.cache, "root", "root")
Mike Nicholsd0acc7f2021-05-21 17:18:24 +0000766
Alex Klein1699fab2022-09-08 08:46:06 -0600767 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000775
Alex Klein1699fab2022-09-08 08:46:06 -0600776 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000782
Alex Klein1699fab2022-09-08 08:46:06 -0600783 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000788
Alex Klein1699fab2022-09-08 08:46:06 -0600789 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000799
Alex Klein1699fab2022-09-08 08:46:06 -0600800 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000808
Alex Klein1699fab2022-09-08 08:46:06 -0600809 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000814
Alex Klein1699fab2022-09-08 08:46:06 -0600815 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 Nicholsd0acc7f2021-05-21 17:18:24 +0000820
Alex Klein1699fab2022-09-08 08:46:06 -0600821 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 Gordon90b2dd92018-04-12 14:04:21 -0600827
Alex Klein1699fab2022-09-08 08:46:06 -0600828 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 Gordon90b2dd92018-04-12 14:04:21 -0600834
Alex Klein1699fab2022-09-08 08:46:06 -0600835 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 Gordon90b2dd92018-04-12 14:04:21 -0600849
Alex Klein1699fab2022-09-08 08:46:06 -0600850 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 Gordon8b6d4122018-04-26 13:38:39 -0600858
Alex Klein1699fab2022-09-08 08:46:06 -0600859 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 Gordon8b6d4122018-04-26 13:38:39 -0600863
Alex Klein1699fab2022-09-08 08:46:06 -0600864 options = cbuildbot_launch.PreParseArguments(
865 ["--buildroot", self.root, "config"]
866 )
867 state = cbuildbot_launch.GetCurrentBuildState(options, "branchA")
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600868
Alex Klein1699fab2022-09-08 08:46:06 -0600869 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 Gordon90b2dd92018-04-12 14:04:21 -0600875
Alex Klein1699fab2022-09-08 08:46:06 -0600876 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 Gordon90b2dd92018-04-12 14:04:21 -0600881
Alex Klein1699fab2022-09-08 08:46:06 -0600882 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 Gordon90b2dd92018-04-12 14:04:21 -0600888
Alex Klein1699fab2022-09-08 08:46:06 -0600889 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 Gordon90b2dd92018-04-12 14:04:21 -0600895
Alex Klein1699fab2022-09-08 08:46:06 -0600896 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 Gordon90b2dd92018-04-12 14:04:21 -0600910
Alex Klein1699fab2022-09-08 08:46:06 -0600911 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 Gordon90b2dd92018-04-12 14:04:21 -0600920
Alex Klein1699fab2022-09-08 08:46:06 -0600921 saved_state = osutils.ReadFile(self.previous_build_state)
922 new_state = build_summary.BuildSummary()
923 new_state.from_json(saved_state)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600924
Alex Klein1699fab2022-09-08 08:46:06 -0600925 self.assertEqual(old_state, new_state)
Mike Frysingerf0146252019-09-02 13:31:05 -0400926
Alex Klein1699fab2022-09-08 08:46:06 -0600927 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 Frysingerf0146252019-09-02 13:31:05 -0400932
Alex Klein1699fab2022-09-08 08:46:06 -0600933 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 Frysingerf0146252019-09-02 13:31:05 -0400940
Alex Klein1699fab2022-09-08 08:46:06 -0600941 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)