blob: 8a9a6ff4226eb4709827309ea98ba18aac522059 [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
Don Garrettc4114cc2016-11-01 20:04:06 -07002# Copyright 2016 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -07006"""Unit tests for chromite.scripts.cbuildbot_launch."""
Don Garrettc4114cc2016-11-01 20:04:06 -07007
8from __future__ import print_function
9
Don Garrett7ade05a2017-02-17 13:31:47 -080010import os
Mike Frysinger3c831a72020-02-19 02:47:04 -050011import sys
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -070012import time
Don Garrett86881cb2017-02-15 15:41:55 -080013
Mike Frysingerc263d092019-09-18 15:11:47 -040014from chromite.cbuildbot import commands
Don Garrett86881cb2017-02-15 15:41:55 -080015from chromite.cbuildbot import repository
Benjamin Gordon90b2dd92018-04-12 14:04:21 -060016from chromite.lib import build_summary
Don Garrett861e9182017-05-15 15:30:23 -070017from chromite.lib import constants
Benjamin Gordon74645232018-05-04 17:40:42 -060018from chromite.lib import cros_sdk_lib
Don Garrett7ade05a2017-02-17 13:31:47 -080019from chromite.lib import cros_test_lib
Don Garrett86881cb2017-02-15 15:41:55 -080020from chromite.lib import osutils
Mike Frysingerf0146252019-09-02 13:31:05 -040021from chromite.lib import timeout_util
Don Garrett0c54ed72017-03-03 11:18:57 -080022from chromite.scripts import cbuildbot_launch
Mike Frysinger40ffb532021-02-12 07:36:08 -050023from chromite.third_party import mock
Don Garrett86881cb2017-02-15 15:41:55 -080024
Mike Frysinger3c831a72020-02-19 02:47:04 -050025
26assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
27
28
Don Garrett86881cb2017-02-15 15:41:55 -080029EXPECTED_MANIFEST_URL = 'https://chrome-internal-review.googlesource.com/chromeos/manifest-internal' # pylint: disable=line-too-long
Don Garrettc4114cc2016-11-01 20:04:06 -070030
31
Don Garrettacbb2392017-05-11 18:27:41 -070032# It's reasonable for unittests to look at internals.
33# pylint: disable=protected-access
34
35
Don Garrett8d314792017-05-18 13:11:42 -070036class FakeException(Exception):
37 """Test exception to raise during tests."""
38
39
Don Garrett0c54ed72017-03-03 11:18:57 -080040class CbuildbotLaunchTest(cros_test_lib.MockTestCase):
41 """Tests for cbuildbot_launch script."""
Don Garrettc4114cc2016-11-01 20:04:06 -070042
Don Garrett86881cb2017-02-15 15:41:55 -080043 def testPreParseArguments(self):
Don Garrettc4114cc2016-11-01 20:04:06 -070044 """Test that we can correctly extract branch values from cbuildbot args."""
Don Garrett597ddff2017-02-17 18:29:37 -080045 CASES = (
46 (['--buildroot', '/buildroot', 'daisy-incremental'],
47 (None, '/buildroot', None)),
48
49 (['--buildbot', '--buildroot', '/buildroot',
50 '--git-cache-dir', '/git-cache',
51 '-b', 'release-R57-9202.B',
52 'daisy-incremental'],
53 ('release-R57-9202.B', '/buildroot', '/git-cache')),
54
55 (['--debug', '--buildbot', '--notests',
56 '--buildroot', '/buildroot',
57 '--git-cache-dir', '/git-cache',
58 '--branch', 'release-R57-9202.B',
59 'daisy-incremental'],
60 ('release-R57-9202.B', '/buildroot', '/git-cache')),
Don Garrettc4114cc2016-11-01 20:04:06 -070061 )
62
Don Garrett597ddff2017-02-17 18:29:37 -080063 for cmd_args, expected in CASES:
64 expected_branch, expected_buildroot, expected_cache_dir = expected
65
Don Garrett0c54ed72017-03-03 11:18:57 -080066 options = cbuildbot_launch.PreParseArguments(cmd_args)
Don Garrett597ddff2017-02-17 18:29:37 -080067
68 self.assertEqual(options.branch, expected_branch)
69 self.assertEqual(options.buildroot, expected_buildroot)
70 self.assertEqual(options.git_cache_dir, expected_cache_dir)
Don Garrett86881cb2017-02-15 15:41:55 -080071
Don Garrettf324bc32017-05-23 14:00:53 -070072 def testInitialCheckout(self):
Don Garrett86881cb2017-02-15 15:41:55 -080073 """Test InitialCheckout with minimum settings."""
Don Garrettf324bc32017-05-23 14:00:53 -070074 mock_repo = mock.MagicMock()
75 mock_repo.branch = 'branch'
Mike Nichols9fb48832021-01-29 14:47:15 -070076 argv = ['-r', '/root', 'config']
77 options = cbuildbot_launch.PreParseArguments(argv)
Don Garrett86881cb2017-02-15 15:41:55 -080078
Mike Nichols9fb48832021-01-29 14:47:15 -070079 cbuildbot_launch.InitialCheckout(mock_repo, options)
Don Garrett86881cb2017-02-15 15:41:55 -080080
81 self.assertEqual(mock_repo.mock_calls, [
Mike Nicholsf6e6c7e2021-05-20 00:09:02 +000082 mock.call.PreLoad('/preload/chromeos'),
83 mock.call.Sync(detach=True, downgrade_repo=False),
Don Garrett8d314792017-05-18 13:11:42 -070084 ])
85
Don Garrettf15d65b2017-04-12 12:39:55 -070086 def testConfigureGlobalEnvironment(self):
Don Garrett60967922017-04-12 18:51:44 -070087 """Ensure that we can setup our global runtime environment correctly."""
Don Garrett86fec482017-05-17 18:13:33 -070088
89 os.environ.pop('LANG', None)
90 os.environ['LC_MONETARY'] = 'bad'
91
Don Garrettf15d65b2017-04-12 12:39:55 -070092 cbuildbot_launch.ConfigureGlobalEnvironment()
93
Don Garrett86fec482017-05-17 18:13:33 -070094 # Verify umask is updated.
Don Garrettf15d65b2017-04-12 12:39:55 -070095 self.assertEqual(os.umask(0), 0o22)
96
Don Garrett86fec482017-05-17 18:13:33 -070097 # Verify ENVs are cleaned up.
98 self.assertEqual(os.environ['LANG'], 'en_US.UTF-8')
99 self.assertNotIn('LC_MONETARY', os.environ)
100
Don Garrettf15d65b2017-04-12 12:39:55 -0700101
Benjamin Gordon121a2aa2018-05-04 16:24:45 -0600102class RunTests(cros_test_lib.RunCommandTestCase):
Don Garrett0c54ed72017-03-03 11:18:57 -0800103 """Tests for cbuildbot_launch script."""
Don Garrett597ddff2017-02-17 18:29:37 -0800104
105 ARGS_BASE = ['--buildroot', '/buildroot']
Don Garrett5cd946b2017-07-20 13:42:20 -0700106 EXPECTED_ARGS_BASE = ['--buildroot', '/cbuildbot_buildroot']
Don Garrett597ddff2017-02-17 18:29:37 -0800107 ARGS_GIT_CACHE = ['--git-cache-dir', '/git-cache']
108 ARGS_CONFIG = ['config']
Don Garrettbf90cdf2017-05-19 15:54:02 -0700109 CMD = ['/cbuildbot_buildroot/chromite/bin/cbuildbot']
Don Garrett597ddff2017-02-17 18:29:37 -0800110
Don Garrett6e5c6b92018-04-06 17:58:49 -0700111 def verifyCbuildbot(self, args, expected_cmd, version):
Don Garrett86881cb2017-02-15 15:41:55 -0800112 """Ensure we invoke cbuildbot correctly."""
Don Garrett597ddff2017-02-17 18:29:37 -0800113 self.PatchObject(
Mike Frysingerc263d092019-09-18 15:11:47 -0400114 commands, 'GetTargetChromiteApiVersion', autospec=True,
Don Garrett597ddff2017-02-17 18:29:37 -0800115 return_value=version)
116
Don Garrett6e5c6b92018-04-06 17:58:49 -0700117 cbuildbot_launch.Cbuildbot('/cbuildbot_buildroot', '/depot_tools', args)
Don Garrett597ddff2017-02-17 18:29:37 -0800118
119 self.assertCommandCalled(
Don Garretta50bf492017-09-28 18:33:02 -0700120 expected_cmd, extra_env={'PATH': mock.ANY},
Mike Frysingerf5a3b2d2019-12-12 14:36:17 -0500121 cwd='/cbuildbot_buildroot', check=False)
Don Garrett597ddff2017-02-17 18:29:37 -0800122
Don Garrett6e5c6b92018-04-06 17:58:49 -0700123 def testCbuildbotSimple(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800124 """Ensure we invoke cbuildbot correctly."""
Don Garrett6e5c6b92018-04-06 17:58:49 -0700125 self.verifyCbuildbot(
Don Garrett597ddff2017-02-17 18:29:37 -0800126 self.ARGS_BASE + self.ARGS_CONFIG,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700127 self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE,
Don Garrett597ddff2017-02-17 18:29:37 -0800128 (0, 4))
129
Don Garrett6e5c6b92018-04-06 17:58:49 -0700130 def testCbuildbotNotFiltered(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800131 """Ensure we invoke cbuildbot correctly."""
Don Garrett6e5c6b92018-04-06 17:58:49 -0700132 self.verifyCbuildbot(
Don Garrett597ddff2017-02-17 18:29:37 -0800133 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700134 (self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE +
135 self.ARGS_GIT_CACHE),
Don Garrett597ddff2017-02-17 18:29:37 -0800136 (0, 4))
137
Don Garrett6e5c6b92018-04-06 17:58:49 -0700138 def testCbuildbotFiltered(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800139 """Ensure we invoke cbuildbot correctly."""
Don Garrett6e5c6b92018-04-06 17:58:49 -0700140 self.verifyCbuildbot(
Don Garrett597ddff2017-02-17 18:29:37 -0800141 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700142 self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE,
Don Garrett597ddff2017-02-17 18:29:37 -0800143 (0, 2))
Don Garrettc4114cc2016-11-01 20:04:06 -0700144
Don Garrett86881cb2017-02-15 15:41:55 -0800145 def testMainMin(self):
146 """Test a minimal set of command line options."""
Don Garrett597ddff2017-02-17 18:29:37 -0800147 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
Mike Frysingerc263d092019-09-18 15:11:47 -0400148 self.PatchObject(commands, 'GetTargetChromiteApiVersion',
Don Garrett861e9182017-05-15 15:30:23 -0700149 autospec=True, return_value=(constants.REEXEC_API_MAJOR,
150 constants.REEXEC_API_MINOR))
Don Garrettf324bc32017-05-23 14:00:53 -0700151 mock_repo = mock.MagicMock()
Julio Hurtado9265c7e2021-04-19 23:04:44 +0000152 mock_repo.branch = 'main'
Mike Nicholsf6e6c7e2021-05-20 00:09:02 +0000153 mock_repo.directory = '/root/repository'
Don Garrettbf90cdf2017-05-19 15:54:02 -0700154
Don Garrettf324bc32017-05-23 14:00:53 -0700155 mock_repo_create = self.PatchObject(repository, 'RepoRepository',
156 autospec=True, return_value=mock_repo)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700157 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot',
Don Garrett0c54ed72017-03-03 11:18:57 -0800158 autospec=True)
159 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800160 autospec=True)
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700161 mock_cleanup_chroot = self.PatchObject(cbuildbot_launch, 'CleanupChroot',
162 autospec=True)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600163 mock_set_last_build_state = self.PatchObject(
164 cbuildbot_launch, 'SetLastBuildState', autospec=True)
165
166 expected_build_state = build_summary.BuildSummary(
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600167 build_number=0, master_build_id=0, status=mock.ANY,
Julio Hurtado9265c7e2021-04-19 23:04:44 +0000168 buildroot_layout=2, branch='main')
Don Garrett7ade05a2017-02-17 13:31:47 -0800169
Mike Nicholsf6e6c7e2021-05-20 00:09:02 +0000170 argv = ['-r', '/root', 'config']
Dhanya Ganesh95c5c152018-10-08 16:48:29 -0600171 options = cbuildbot_launch.PreParseArguments(argv)
172 cbuildbot_launch._main(options, argv)
Don Garrettc4114cc2016-11-01 20:04:06 -0700173
Don Garrettf324bc32017-05-23 14:00:53 -0700174 # Did we create the repo instance correctly?
175 self.assertEqual(mock_repo_create.mock_calls,
Mike Nicholsf6e6c7e2021-05-20 00:09:02 +0000176 [mock.call(EXPECTED_MANIFEST_URL, '/root/repository',
Julio Hurtado9265c7e2021-04-19 23:04:44 +0000177 git_cache_dir=None, branch='main')])
Don Garrettf324bc32017-05-23 14:00:53 -0700178
Don Garrett7ade05a2017-02-17 13:31:47 -0800179 # Ensure we clean, as expected.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700180 self.assertEqual(mock_clean.mock_calls, [
Mike Nicholsf6e6c7e2021-05-20 00:09:02 +0000181 mock.call('/root', mock_repo, '/root/repository/.cache',
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600182 expected_build_state)])
Don Garrett7ade05a2017-02-17 13:31:47 -0800183
Don Garrett86881cb2017-02-15 15:41:55 -0800184 # Ensure we checkout, as expected.
185 self.assertEqual(mock_checkout.mock_calls,
Mike Nichols9fb48832021-01-29 14:47:15 -0700186 [mock.call(mock_repo, options)])
Don Garrettc4114cc2016-11-01 20:04:06 -0700187
Don Garrett86881cb2017-02-15 15:41:55 -0800188 # Ensure we invoke cbuildbot, as expected.
189 self.assertCommandCalled(
Don Garrett5cd946b2017-07-20 13:42:20 -0700190 [
Mike Nicholsf6e6c7e2021-05-20 00:09:02 +0000191 '/root/repository/chromite/bin/cbuildbot',
Don Garrett5cd946b2017-07-20 13:42:20 -0700192 'config',
Mike Nicholsf6e6c7e2021-05-20 00:09:02 +0000193 '-r', '/root/repository',
Don Garrettb497f552018-07-09 16:01:13 -0700194 '--workspace', '/root/workspace',
Mike Nicholsf6e6c7e2021-05-20 00:09:02 +0000195 '--cache-dir', '/root/repository/.cache',
Don Garrett61ce1ee2019-02-26 16:20:25 -0800196 # The duplication is a bug, but not harmful.
Mike Nicholsf6e6c7e2021-05-20 00:09:02 +0000197 '--cache-dir', '/root/repository/.cache',
Don Garrett5cd946b2017-07-20 13:42:20 -0700198 ],
Don Garretta50bf492017-09-28 18:33:02 -0700199 extra_env={'PATH': mock.ANY},
Mike Nicholsf6e6c7e2021-05-20 00:09:02 +0000200 cwd='/root/repository',
Mike Frysingerf5a3b2d2019-12-12 14:36:17 -0500201 check=False)
Don Garrettc4114cc2016-11-01 20:04:06 -0700202
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600203 # Ensure we saved the final state, as expected.
204 self.assertEqual(expected_build_state.status,
205 constants.BUILDER_STATUS_PASSED)
206 self.assertEqual(mock_set_last_build_state.mock_calls, [
207 mock.call('/root', expected_build_state)])
208
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700209 # Ensure we clean the chroot, as expected.
Mike Nicholsf6e6c7e2021-05-20 00:09:02 +0000210 mock_cleanup_chroot.assert_called_once_with('/root/repository')
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700211
Don Garrett86881cb2017-02-15 15:41:55 -0800212 def testMainMax(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800213 """Test a larger set of command line options."""
214 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
Mike Frysingerc263d092019-09-18 15:11:47 -0400215 self.PatchObject(commands, 'GetTargetChromiteApiVersion',
Don Garrett861e9182017-05-15 15:30:23 -0700216 autospec=True, return_value=(constants.REEXEC_API_MAJOR,
217 constants.REEXEC_API_MINOR))
Don Garrettf324bc32017-05-23 14:00:53 -0700218 mock_repo = mock.MagicMock()
219 mock_repo.branch = 'branch'
Mike Nicholsf6e6c7e2021-05-20 00:09:02 +0000220 mock_repo.directory = '/root/repository'
Don Garrettbf90cdf2017-05-19 15:54:02 -0700221
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600222 mock_summary = build_summary.BuildSummary(
223 build_number=313,
224 master_build_id=123123123,
225 status=constants.BUILDER_STATUS_FAILED,
226 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
227 branch='branch')
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600228
229 mock_get_last_build_state = self.PatchObject(
230 cbuildbot_launch, 'GetLastBuildState', autospec=True,
231 return_value=mock_summary)
Don Garrettf324bc32017-05-23 14:00:53 -0700232 mock_repo_create = self.PatchObject(repository, 'RepoRepository',
233 autospec=True, return_value=mock_repo)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700234 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot',
Don Garrett0c54ed72017-03-03 11:18:57 -0800235 autospec=True)
236 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800237 autospec=True)
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700238 mock_cleanup_chroot = self.PatchObject(cbuildbot_launch, 'CleanupChroot',
239 autospec=True)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600240 mock_set_last_build_state = self.PatchObject(
241 cbuildbot_launch, 'SetLastBuildState', autospec=True)
Dhanya Ganesh95c5c152018-10-08 16:48:29 -0600242 argv = ['--buildroot', '/root',
243 '--branch', 'branch',
244 '--git-cache-dir', '/git-cache',
Don Garrett61ce1ee2019-02-26 16:20:25 -0800245 '--cache-dir', '/cache',
Dhanya Ganesh95c5c152018-10-08 16:48:29 -0600246 '--remote-trybot',
247 '--master-build-id', '123456789',
248 '--buildnumber', '314',
249 'config']
250 options = cbuildbot_launch.PreParseArguments(argv)
251 cbuildbot_launch._main(options, argv)
Don Garrettc4114cc2016-11-01 20:04:06 -0700252
Don Garrettf324bc32017-05-23 14:00:53 -0700253 # Did we create the repo instance correctly?
254 self.assertEqual(mock_repo_create.mock_calls,
Mike Nicholsf6e6c7e2021-05-20 00:09:02 +0000255 [mock.call(EXPECTED_MANIFEST_URL, '/root/repository',
Don Garrett33872502018-08-03 22:30:40 +0000256 git_cache_dir='/git-cache', branch='branch')])
Don Garrettf324bc32017-05-23 14:00:53 -0700257
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600258 # Ensure we look up the previous status.
259 self.assertEqual(mock_get_last_build_state.mock_calls, [
260 mock.call('/root')])
261
Don Garrett7ade05a2017-02-17 13:31:47 -0800262 # Ensure we clean, as expected.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700263 self.assertEqual(mock_clean.mock_calls, [
Don Garrettd1d90dd2017-06-13 17:35:52 -0700264 mock.call('/root',
265 mock_repo,
Don Garrett61ce1ee2019-02-26 16:20:25 -0800266 '/cache',
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600267 build_summary.BuildSummary(
268 build_number=314,
269 master_build_id=123456789,
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600270 status=mock.ANY,
271 branch='branch',
272 buildroot_layout=2
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600273 ))])
Don Garrett7ade05a2017-02-17 13:31:47 -0800274
Don Garrett86881cb2017-02-15 15:41:55 -0800275 # Ensure we checkout, as expected.
276 self.assertEqual(mock_checkout.mock_calls,
Mike Nichols9fb48832021-01-29 14:47:15 -0700277 [mock.call(mock_repo, options)])
Don Garrett86881cb2017-02-15 15:41:55 -0800278
279 # Ensure we invoke cbuildbot, as expected.
280 self.assertCommandCalled(
Don Garrett5cd946b2017-07-20 13:42:20 -0700281 [
Mike Nicholsf6e6c7e2021-05-20 00:09:02 +0000282 '/root/repository/chromite/bin/cbuildbot',
Don Garrett5cd946b2017-07-20 13:42:20 -0700283 'config',
Mike Nicholsf6e6c7e2021-05-20 00:09:02 +0000284 '--buildroot', '/root/repository',
Don Garrett5cd946b2017-07-20 13:42:20 -0700285 '--branch', 'branch',
286 '--git-cache-dir', '/git-cache',
Don Garrett61ce1ee2019-02-26 16:20:25 -0800287 '--cache-dir', '/cache',
Don Garrett5cd946b2017-07-20 13:42:20 -0700288 '--remote-trybot',
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600289 '--master-build-id', '123456789',
290 '--buildnumber', '314',
291 '--previous-build-state',
Mike Frysingerd0960812020-06-09 01:53:32 -0400292 'eyJicmFuY2giOiJicmFuY2giLCJidWlsZF9udW1iZXIiOjMxMywiYnVpbGRyb290X'
293 '2xheW91dCI6MiwibWFzdGVyX2J1aWxkX2lkIjoxMjMxMjMxMjMsInN0YXR1cyI6Im'
294 'ZhaWwifQ==',
Don Garrettb497f552018-07-09 16:01:13 -0700295 '--workspace', '/root/workspace',
Don Garrett61ce1ee2019-02-26 16:20:25 -0800296 '--cache-dir', '/cache',
Don Garrett5cd946b2017-07-20 13:42:20 -0700297 ],
Don Garretta50bf492017-09-28 18:33:02 -0700298 extra_env={'PATH': mock.ANY},
Mike Nicholsf6e6c7e2021-05-20 00:09:02 +0000299 cwd='/root/repository',
Mike Frysingerf5a3b2d2019-12-12 14:36:17 -0500300 check=False)
Don Garrett7ade05a2017-02-17 13:31:47 -0800301
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600302 # Ensure we write the final build state, as expected.
303 final_state = build_summary.BuildSummary(
304 build_number=314,
305 master_build_id=123456789,
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600306 status=constants.BUILDER_STATUS_PASSED,
307 buildroot_layout=2,
308 branch='branch')
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600309 self.assertEqual(mock_set_last_build_state.mock_calls, [
310 mock.call('/root', final_state)])
311
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700312 # Ensure we clean the chroot, as expected.
Mike Nicholsf6e6c7e2021-05-20 00:09:02 +0000313 mock_cleanup_chroot.assert_called_once_with('/root/repository')
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700314
Don Garrett7ade05a2017-02-17 13:31:47 -0800315
Don Garrettbf90cdf2017-05-19 15:54:02 -0700316class CleanBuildRootTest(cros_test_lib.MockTempDirTestCase):
317 """Tests for CleanBuildRoot method."""
Don Garrett7ade05a2017-02-17 13:31:47 -0800318
319 def setUp(self):
320 """Create standard buildroot contents for cleanup."""
Don Garrettbf90cdf2017-05-19 15:54:02 -0700321 self.root = os.path.join(self.tempdir)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600322 self.previous_build_state = os.path.join(
323 self.root, '.cbuildbot_build_state.json')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700324 self.buildroot = os.path.join(self.root, 'buildroot')
325 self.repo = os.path.join(self.buildroot, '.repo/repo')
Don Garrett36650112018-06-28 15:54:34 -0700326 self.chroot = os.path.join(self.buildroot, 'chroot')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700327 self.general = os.path.join(self.buildroot, 'general/general')
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700328 self.cache = os.path.join(self.buildroot, '.cache')
329 self.distfiles = os.path.join(self.cache, 'distfiles')
Don Garrett7ade05a2017-02-17 13:31:47 -0800330
Don Garrett4166d182018-12-17 12:52:02 -0800331 self.mock_repo = mock.Mock(repository.RepoRepository)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700332 self.mock_repo.directory = self.buildroot
Don Garrettf324bc32017-05-23 14:00:53 -0700333
Benjamin Gordon8642bcc2018-05-01 13:49:56 -0600334 def populateBuildroot(self, previous_build_state=None):
Don Garrett7ade05a2017-02-17 13:31:47 -0800335 """Create standard buildroot contents for cleanup."""
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600336 if previous_build_state:
337 osutils.SafeMakedirs(self.root)
338 osutils.WriteFile(self.previous_build_state, previous_build_state)
339
Don Garrett7ade05a2017-02-17 13:31:47 -0800340 # Create files.
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700341 for f in (self.repo, self.chroot, self.general, self.distfiles):
Don Garrette17e1d92017-04-12 15:28:19 -0700342 osutils.Touch(f, makedirs=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800343
Don Garrette17e1d92017-04-12 15:28:19 -0700344 def testNoBuildroot(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700345 """Test CleanBuildRoot with no history."""
Julio Hurtado9265c7e2021-04-19 23:04:44 +0000346 self.mock_repo.branch = 'main'
Don Garrettf324bc32017-05-23 14:00:53 -0700347
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600348 build_state = build_summary.BuildSummary(
349 status=constants.BUILDER_STATUS_INFLIGHT,
350 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
Julio Hurtado9265c7e2021-04-19 23:04:44 +0000351 branch='main')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700352 cbuildbot_launch.CleanBuildRoot(
Don Garrett61ce1ee2019-02-26 16:20:25 -0800353 self.root, self.mock_repo, self.cache, build_state)
Don Garrett7ade05a2017-02-17 13:31:47 -0800354
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600355 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600356 self.assertEqual(new_summary.buildroot_layout, 2)
Julio Hurtado9265c7e2021-04-19 23:04:44 +0000357 self.assertEqual(new_summary.branch, 'main')
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600358 self.assertIsNotNone(new_summary.distfiles_ts)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600359 self.assertEqual(new_summary, build_state)
Don Garrett7ade05a2017-02-17 13:31:47 -0800360
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600361 self.assertExists(self.previous_build_state)
362
Mike Nicholsf6e6c7e2021-05-20 00:09:02 +0000363 def testBuildrootNoState(self):
364 """Test CleanBuildRoot with no state information."""
365 self.populateBuildroot()
366 self.mock_repo.branch = 'main'
367
368 build_state = build_summary.BuildSummary(
369 status=constants.BUILDER_STATUS_INFLIGHT,
370 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
371 branch='main')
372 cbuildbot_launch.CleanBuildRoot(
373 self.root, self.mock_repo, self.cache, build_state)
374
375 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
376 self.assertEqual(new_summary.buildroot_layout, 2)
377 self.assertEqual(new_summary.branch, 'main')
378 self.assertIsNotNone(new_summary.distfiles_ts)
379 self.assertEqual(new_summary, build_state)
380
381 self.assertNotExists(self.repo)
382 self.assertNotExists(self.chroot)
383 self.assertNotExists(self.general)
384 self.assertNotExists(self.distfiles)
385 self.assertExists(self.previous_build_state)
386
387 def testBuildrootFormatMismatch(self):
388 """Test CleanBuildRoot with buildroot layout mismatch."""
389 old_build_state = build_summary.BuildSummary(
390 status=constants.BUILDER_STATUS_PASSED,
391 buildroot_layout=1,
392 branch='main')
393 self.populateBuildroot(previous_build_state=old_build_state.to_json())
394 self.mock_repo.branch = 'main'
395
396 build_state = build_summary.BuildSummary(
397 status=constants.BUILDER_STATUS_INFLIGHT,
398 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
399 branch='main')
400 cbuildbot_launch.CleanBuildRoot(
401 self.root, self.mock_repo, self.cache, build_state)
402
403 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
404 self.assertEqual(new_summary.buildroot_layout, 2)
405 self.assertEqual(new_summary.branch, 'main')
406 self.assertIsNotNone(new_summary.distfiles_ts)
407 self.assertEqual(new_summary, build_state)
408
409 self.assertNotExists(self.repo)
410 self.assertNotExists(self.chroot)
411 self.assertNotExists(self.general)
412 self.assertNotExists(self.distfiles)
413 self.assertExists(self.previous_build_state)
414
415 def testBuildrootBranchChange(self):
416 """Test CleanBuildRoot with a change in branches."""
417 old_build_state = build_summary.BuildSummary(
418 status=constants.BUILDER_STATUS_PASSED,
419 buildroot_layout=2,
420 branch='branchA')
421 self.populateBuildroot(previous_build_state=old_build_state.to_json())
422 self.mock_repo.branch = 'branchB'
423 m = self.PatchObject(cros_sdk_lib, 'CleanupChrootMount')
424
425 build_state = build_summary.BuildSummary(
426 status=constants.BUILDER_STATUS_INFLIGHT,
427 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
428 branch='branchB')
429 cbuildbot_launch.CleanBuildRoot(
430 self.root, self.mock_repo, self.cache, build_state)
431
432 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
433 self.assertEqual(new_summary.buildroot_layout, 2)
434 self.assertEqual(new_summary.branch, 'branchB')
435 self.assertIsNotNone(new_summary.distfiles_ts)
436 self.assertEqual(new_summary, build_state)
437
438 # self.assertExists(self.repo)
439 self.assertExists(self.general)
440 self.assertNotExists(self.distfiles)
441 self.assertExists(self.previous_build_state)
442 m.assert_called_with(self.chroot, delete=True)
443
444 def testBuildrootBranchMatch(self):
445 """Test CleanBuildRoot with no change in branch."""
446 old_build_state = build_summary.BuildSummary(
447 status=constants.BUILDER_STATUS_PASSED,
448 buildroot_layout=2,
449 branch='branchA')
450 self.populateBuildroot(previous_build_state=old_build_state.to_json())
451 self.mock_repo.branch = 'branchA'
452
453 build_state = build_summary.BuildSummary(
454 status=constants.BUILDER_STATUS_INFLIGHT,
455 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
456 branch='branchA')
457 cbuildbot_launch.CleanBuildRoot(
458 self.root, self.mock_repo, self.cache, build_state)
459
460 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
461 self.assertEqual(new_summary.buildroot_layout, 2)
462 self.assertEqual(new_summary.branch, 'branchA')
463 self.assertIsNotNone(new_summary.distfiles_ts)
464 self.assertEqual(new_summary, build_state)
465
466 self.assertExists(self.repo)
467 self.assertExists(self.chroot)
468 self.assertExists(self.general)
469 self.assertExists(self.distfiles)
470 self.assertExists(self.previous_build_state)
471
472 def testBuildrootGitLocksPrevPass(self):
473 """Verify not CleanStaleLocks, if previous build was in passed."""
474 old_build_state = build_summary.BuildSummary(
475 status=constants.BUILDER_STATUS_PASSED,
476 buildroot_layout=2,
477 branch='branchA')
478 self.populateBuildroot(previous_build_state=old_build_state.to_json())
479 self.mock_repo.branch = 'branchA'
480
481 build_state = build_summary.BuildSummary(
482 status=constants.BUILDER_STATUS_INFLIGHT,
483 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
484 branch='branchA')
485 cbuildbot_launch.CleanBuildRoot(
486 self.root, self.mock_repo, self.cache, build_state)
487
488 self.assertEqual(
489 self.mock_repo.mock_calls, [
490 mock.call.PreLoad(),
491 mock.call.BuildRootGitCleanup(prune_all=True),
492 ])
493
494 def testBuildrootGitLocksPrevFail(self):
495 """Verify not CleanStaleLocks, if previous build was in failed."""
496 old_build_state = build_summary.BuildSummary(
497 status=constants.BUILDER_STATUS_FAILED,
498 buildroot_layout=2,
499 branch='branchA')
500 self.populateBuildroot(previous_build_state=old_build_state.to_json())
501 self.mock_repo.branch = 'branchA'
502
503 build_state = build_summary.BuildSummary(
504 status=constants.BUILDER_STATUS_INFLIGHT,
505 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
506 branch='branchA')
507 cbuildbot_launch.CleanBuildRoot(
508 self.root, self.mock_repo, self.cache, build_state)
509
510 self.assertEqual(
511 self.mock_repo.mock_calls, [
512 mock.call.PreLoad(),
513 mock.call.BuildRootGitCleanup(prune_all=True),
514 ])
515
516 def testBuildrootGitLocksPrevInFlight(self):
517 """Verify CleanStaleLocks, if previous build was in flight."""
518 old_build_state = build_summary.BuildSummary(
519 status=constants.BUILDER_STATUS_INFLIGHT,
520 buildroot_layout=2,
521 branch='branchA')
522 self.populateBuildroot(previous_build_state=old_build_state.to_json())
523 self.mock_repo.branch = 'branchA'
524
525 build_state = build_summary.BuildSummary(
526 status=constants.BUILDER_STATUS_INFLIGHT,
527 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
528 branch='branchA')
529 cbuildbot_launch.CleanBuildRoot(
530 self.root, self.mock_repo, self.cache, build_state)
531
532
533 self.assertEqual(
534 self.mock_repo.method_calls, [
535 mock.call.PreLoad(),
536 mock.call.CleanStaleLocks(),
537 mock.call.BuildRootGitCleanup(prune_all=True),
538 ])
539
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600540 def testBuildrootDistfilesRecentCache(self):
541 """Test CleanBuildRoot does not delete distfiles when cache is recent."""
542 seed_distfiles_ts = time.time() - 60
543 old_build_state = build_summary.BuildSummary(
544 status=constants.BUILDER_STATUS_PASSED,
545 buildroot_layout=2,
546 branch='branchA',
547 distfiles_ts=seed_distfiles_ts)
548 self.populateBuildroot(previous_build_state=old_build_state.to_json())
549 self.mock_repo.branch = 'branchA'
550
551 build_state = build_summary.BuildSummary(
552 status=constants.BUILDER_STATUS_INFLIGHT,
553 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
554 branch='branchA')
555 cbuildbot_launch.CleanBuildRoot(
Don Garrett61ce1ee2019-02-26 16:20:25 -0800556 self.root, self.mock_repo, self.cache, build_state)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600557
558 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
559 self.assertEqual(new_summary.buildroot_layout, 2)
560 self.assertEqual(new_summary.branch, 'branchA')
561 # Same cache creation timestamp is rewritten to state.
562 self.assertEqual(new_summary.distfiles_ts, seed_distfiles_ts)
563 self.assertEqual(new_summary, build_state)
564
565 self.assertExists(self.repo)
566 self.assertExists(self.chroot)
567 self.assertExists(self.general)
568 self.assertExists(self.distfiles)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600569 self.assertExists(self.previous_build_state)
Don Garrette17e1d92017-04-12 15:28:19 -0700570
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600571 def testBuildrootDistfilesCacheExpired(self):
572 """Test CleanBuildRoot when the distfiles cache is too old."""
573 old_build_state = build_summary.BuildSummary(
574 status=constants.BUILDER_STATUS_PASSED,
575 buildroot_layout=2,
576 branch='branchA',
577 distfiles_ts=100.0)
578 self.populateBuildroot(previous_build_state=old_build_state.to_json())
579 self.mock_repo.branch = 'branchA'
580
581 build_state = build_summary.BuildSummary(
582 status=constants.BUILDER_STATUS_INFLIGHT,
583 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
584 branch='branchA')
585 cbuildbot_launch.CleanBuildRoot(
Don Garrett61ce1ee2019-02-26 16:20:25 -0800586 self.root, self.mock_repo, self.cache, build_state)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600587
588 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
589 self.assertEqual(new_summary.buildroot_layout, 2)
590 self.assertEqual(new_summary.branch, 'branchA')
591 self.assertIsNotNone(new_summary.distfiles_ts)
592 self.assertEqual(new_summary, build_state)
593
594 self.assertExists(self.repo)
595 self.assertExists(self.chroot)
596 self.assertExists(self.general)
597 self.assertNotExists(self.distfiles)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600598 self.assertExists(self.previous_build_state)
599
Don Garrett61ce1ee2019-02-26 16:20:25 -0800600 def testRootOwnedCache(self):
601 """Test CleanBuildRoot with no history."""
602 seed_distfiles_ts = time.time() - 60
603 old_build_state = build_summary.BuildSummary(
604 status=constants.BUILDER_STATUS_PASSED,
605 buildroot_layout=2,
606 branch='branchA',
607 distfiles_ts=seed_distfiles_ts)
608 self.populateBuildroot(previous_build_state=old_build_state.to_json())
609 self.mock_repo.branch = 'branchA'
610
611 osutils.Chown(self.cache, 'root', 'root')
612
613 build_state = build_summary.BuildSummary(
614 status=constants.BUILDER_STATUS_INFLIGHT,
615 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
616 branch='branchA')
617 cbuildbot_launch.CleanBuildRoot(
618 self.root, self.mock_repo, self.cache, build_state)
619
620 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
621 self.assertEqual(new_summary.buildroot_layout, 2)
622 self.assertEqual(new_summary.branch, 'branchA')
623 # Same cache creation timestamp is rewritten to state.
624 self.assertEqual(new_summary.distfiles_ts, seed_distfiles_ts)
625 self.assertEqual(new_summary, build_state)
626
627 self.assertExists(self.repo)
628 self.assertExists(self.chroot)
629 self.assertExists(self.general)
630 self.assertNotExists(self.distfiles)
631 self.assertExists(self.previous_build_state)
632
Mike Nicholsf6e6c7e2021-05-20 00:09:02 +0000633 def testBuildrootRepoCleanFailure(self):
634 """Test CleanBuildRoot with repo checkout failure."""
635 old_build_state = build_summary.BuildSummary(
636 status=constants.BUILDER_STATUS_PASSED,
637 buildroot_layout=1,
638 branch='branchA')
639 self.populateBuildroot(previous_build_state=old_build_state.to_json())
640 self.mock_repo.branch = 'branchA'
641 self.mock_repo.BuildRootGitCleanup.side_effect = Exception
642
643 build_state = build_summary.BuildSummary(
644 status=constants.BUILDER_STATUS_INFLIGHT,
645 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
646 branch='branchA')
647 cbuildbot_launch.CleanBuildRoot(
648 self.root, self.mock_repo, self.cache, build_state)
649
650 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
651 self.assertEqual(new_summary.buildroot_layout, 2)
652 self.assertEqual(new_summary.branch, 'branchA')
653 self.assertIsNotNone(new_summary.distfiles_ts)
654 self.assertEqual(new_summary, build_state)
655
656 self.assertNotExists(self.repo)
657 self.assertNotExists(self.chroot)
658 self.assertNotExists(self.general)
659 self.assertNotExists(self.distfiles)
660 self.assertExists(self.previous_build_state)
661
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600662 def testGetCurrentBuildStateNoArgs(self):
663 """Tests GetCurrentBuildState without arguments."""
664 options = cbuildbot_launch.PreParseArguments([
665 '--buildroot', self.root, 'config'
666 ])
Julio Hurtado9265c7e2021-04-19 23:04:44 +0000667 state = cbuildbot_launch.GetCurrentBuildState(options, 'main')
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600668
669 expected_state = build_summary.BuildSummary(
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600670 status=constants.BUILDER_STATUS_INFLIGHT,
671 buildroot_layout=2,
Julio Hurtado9265c7e2021-04-19 23:04:44 +0000672 branch='main')
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600673 self.assertEqual(state, expected_state)
674
675 def testGetCurrentBuildStateHasArgs(self):
676 """Tests GetCurrentBuildState with arguments."""
677 options = cbuildbot_launch.PreParseArguments([
678 '--buildroot', self.root,
679 '--buildnumber', '20',
680 '--master-build-id', '50',
681 'config'
682 ])
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600683 state = cbuildbot_launch.GetCurrentBuildState(options, 'branchA')
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600684
685 expected_state = build_summary.BuildSummary(
686 build_number=20,
687 master_build_id=50,
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600688 status=constants.BUILDER_STATUS_INFLIGHT,
689 buildroot_layout=2,
690 branch='branchA')
691 self.assertEqual(state, expected_state)
692
693 def testGetCurrentBuildStateLayout(self):
694 """Test that GetCurrentBuildState uses the current buildroot layout."""
695 # Change to a future version.
696 self.PatchObject(cbuildbot_launch, 'BUILDROOT_BUILDROOT_LAYOUT', 22)
697
698 options = cbuildbot_launch.PreParseArguments([
699 '--buildroot', self.root, 'config'
700 ])
701 state = cbuildbot_launch.GetCurrentBuildState(options, 'branchA')
702
703 expected_state = build_summary.BuildSummary(
704 status=constants.BUILDER_STATUS_INFLIGHT,
705 buildroot_layout=22,
706 branch='branchA')
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600707 self.assertEqual(state, expected_state)
708
709 def testGetLastBuildStateNoFile(self):
710 """Tests GetLastBuildState if the file is missing."""
711 osutils.SafeMakedirs(self.root)
712 state = cbuildbot_launch.GetLastBuildState(self.root)
713 self.assertEqual(state, build_summary.BuildSummary())
714
715 def testGetLastBuildStateBadFile(self):
716 """Tests GetLastBuildState if the file contains invalid JSON."""
717 osutils.SafeMakedirs(self.root)
718 osutils.WriteFile(self.previous_build_state, '}}')
719 state = cbuildbot_launch.GetLastBuildState(self.root)
720 self.assertEqual(state, build_summary.BuildSummary())
721
722 def testGetLastBuildStateMissingBuildStatus(self):
723 """Tests GetLastBuildState if the file doesn't have a valid status."""
724 osutils.SafeMakedirs(self.root)
725 osutils.WriteFile(self.previous_build_state, '{"build_number": "3"}')
726 state = cbuildbot_launch.GetLastBuildState(self.root)
727 self.assertEqual(state, build_summary.BuildSummary())
728
729 def testGetLastBuildStateGoodFile(self):
730 """Tests GetLastBuildState on a good file."""
731 osutils.SafeMakedirs(self.root)
732 osutils.WriteFile(
733 self.previous_build_state,
734 '{"build_number": 1, "master_build_id": 3, "status": "pass"}')
735 state = cbuildbot_launch.GetLastBuildState(self.root)
736 self.assertEqual(
737 state,
738 build_summary.BuildSummary(
739 build_number=1, master_build_id=3, status='pass'))
740
741 def testSetLastBuildState(self):
742 """Verifies that SetLastBuildState writes to the expected file."""
743 osutils.SafeMakedirs(self.root)
744 old_state = build_summary.BuildSummary(
745 build_number=314,
746 master_build_id=2178,
747 status=constants.BUILDER_STATUS_PASSED)
748 cbuildbot_launch.SetLastBuildState(self.root, old_state)
749
750 saved_state = osutils.ReadFile(self.previous_build_state)
751 new_state = build_summary.BuildSummary()
752 new_state.from_json(saved_state)
753
754 self.assertEqual(old_state, new_state)
Mike Frysingerf0146252019-09-02 13:31:05 -0400755
756 def testCleanupChrootNoChroot(self):
757 """Check CleanupChroot without a chroot."""
758 self.StartPatcher(cros_test_lib.RunCommandMock())
759 with mock.patch.object(cros_sdk_lib, 'CleanupChrootMount'):
Mike Frysinger7a63ee72019-09-03 14:24:06 -0400760 cbuildbot_launch.CleanupChroot(self.buildroot)
Mike Frysingerf0146252019-09-02 13:31:05 -0400761
762 def testCleanupChrootNormal(self):
763 """Check normal CleanupChroot."""
764 osutils.SafeMakedirs(self.chroot)
765 osutils.Touch(self.chroot + '.img')
766 self.StartPatcher(cros_test_lib.RunCommandMock())
767 with mock.patch.object(cros_sdk_lib, 'CleanupChrootMount'):
Mike Frysinger7a63ee72019-09-03 14:24:06 -0400768 cbuildbot_launch.CleanupChroot(self.buildroot)
Mike Frysingerf0146252019-09-02 13:31:05 -0400769
770 def testCleanupChrootTimeout(self):
771 """Check timeouts in CleanupChroot."""
772 osutils.SafeMakedirs(self.chroot)
773 osutils.Touch(self.chroot + '.img')
774 rc_mock = self.StartPatcher(cros_test_lib.RunCommandMock())
775 rc_mock.SetDefaultCmdResult()
776 with mock.patch.object(cros_sdk_lib, 'CleanupChrootMount',
777 side_effect=timeout_util.TimeoutError):
Mike Frysinger7a63ee72019-09-03 14:24:06 -0400778 cbuildbot_launch.CleanupChroot(self.buildroot)