blob: 24e6f31305d81c31c3864a5f41a3b9561e5e7825 [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
Don Garrett86881cb2017-02-15 15:41:55 -080012
Mike Frysingerc263d092019-09-18 15:11:47 -040013from chromite.cbuildbot import commands
Don Garrett86881cb2017-02-15 15:41:55 -080014from chromite.cbuildbot import repository
Benjamin Gordon90b2dd92018-04-12 14:04:21 -060015from chromite.lib import build_summary
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
Mike Frysinger40ffb532021-02-12 07:36:08 -050022from chromite.third_party import mock
Don Garrett86881cb2017-02-15 15:41:55 -080023
Mike Frysinger3c831a72020-02-19 02:47:04 -050024
25assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
26
27
Don Garrett86881cb2017-02-15 15:41:55 -080028EXPECTED_MANIFEST_URL = 'https://chrome-internal-review.googlesource.com/chromeos/manifest-internal' # pylint: disable=line-too-long
Don Garrettc4114cc2016-11-01 20:04:06 -070029
30
Don Garrettacbb2392017-05-11 18:27:41 -070031# It's reasonable for unittests to look at internals.
32# pylint: disable=protected-access
33
34
Don Garrett8d314792017-05-18 13:11:42 -070035class FakeException(Exception):
36 """Test exception to raise during tests."""
37
38
Don Garrett0c54ed72017-03-03 11:18:57 -080039class CbuildbotLaunchTest(cros_test_lib.MockTestCase):
40 """Tests for cbuildbot_launch script."""
Don Garrettc4114cc2016-11-01 20:04:06 -070041
Don Garrett86881cb2017-02-15 15:41:55 -080042 def testPreParseArguments(self):
Don Garrettc4114cc2016-11-01 20:04:06 -070043 """Test that we can correctly extract branch values from cbuildbot args."""
Don Garrett597ddff2017-02-17 18:29:37 -080044 CASES = (
45 (['--buildroot', '/buildroot', 'daisy-incremental'],
46 (None, '/buildroot', None)),
47
48 (['--buildbot', '--buildroot', '/buildroot',
49 '--git-cache-dir', '/git-cache',
50 '-b', 'release-R57-9202.B',
51 'daisy-incremental'],
52 ('release-R57-9202.B', '/buildroot', '/git-cache')),
53
54 (['--debug', '--buildbot', '--notests',
55 '--buildroot', '/buildroot',
56 '--git-cache-dir', '/git-cache',
57 '--branch', 'release-R57-9202.B',
58 'daisy-incremental'],
59 ('release-R57-9202.B', '/buildroot', '/git-cache')),
Don Garrettc4114cc2016-11-01 20:04:06 -070060 )
61
Don Garrett597ddff2017-02-17 18:29:37 -080062 for cmd_args, expected in CASES:
63 expected_branch, expected_buildroot, expected_cache_dir = expected
64
Don Garrett0c54ed72017-03-03 11:18:57 -080065 options = cbuildbot_launch.PreParseArguments(cmd_args)
Don Garrett597ddff2017-02-17 18:29:37 -080066
67 self.assertEqual(options.branch, expected_branch)
68 self.assertEqual(options.buildroot, expected_buildroot)
69 self.assertEqual(options.git_cache_dir, expected_cache_dir)
Don Garrett86881cb2017-02-15 15:41:55 -080070
Don Garrettf324bc32017-05-23 14:00:53 -070071 def testInitialCheckout(self):
Don Garrett86881cb2017-02-15 15:41:55 -080072 """Test InitialCheckout with minimum settings."""
Don Garrettf324bc32017-05-23 14:00:53 -070073 mock_repo = mock.MagicMock()
74 mock_repo.branch = 'branch'
Mike Nichols9fb48832021-01-29 14:47:15 -070075 argv = ['-r', '/root', 'config']
76 options = cbuildbot_launch.PreParseArguments(argv)
Don Garrett86881cb2017-02-15 15:41:55 -080077
Mike Nichols9fb48832021-01-29 14:47:15 -070078 cbuildbot_launch.InitialCheckout(mock_repo, options)
Don Garrett86881cb2017-02-15 15:41:55 -080079
80 self.assertEqual(mock_repo.mock_calls, [
Mike Nichols207e2a12021-05-20 14:00:29 -060081 mock.call.Sync(jobs=20, detach=True, downgrade_repo=False),
Don Garrett8d314792017-05-18 13:11:42 -070082 ])
83
Don Garrettf15d65b2017-04-12 12:39:55 -070084 def testConfigureGlobalEnvironment(self):
Don Garrett60967922017-04-12 18:51:44 -070085 """Ensure that we can setup our global runtime environment correctly."""
Don Garrett86fec482017-05-17 18:13:33 -070086
87 os.environ.pop('LANG', None)
88 os.environ['LC_MONETARY'] = 'bad'
89
Don Garrettf15d65b2017-04-12 12:39:55 -070090 cbuildbot_launch.ConfigureGlobalEnvironment()
91
Don Garrett86fec482017-05-17 18:13:33 -070092 # Verify umask is updated.
Don Garrettf15d65b2017-04-12 12:39:55 -070093 self.assertEqual(os.umask(0), 0o22)
94
Don Garrett86fec482017-05-17 18:13:33 -070095 # Verify ENVs are cleaned up.
96 self.assertEqual(os.environ['LANG'], 'en_US.UTF-8')
97 self.assertNotIn('LC_MONETARY', os.environ)
98
Don Garrettf15d65b2017-04-12 12:39:55 -070099
Benjamin Gordon121a2aa2018-05-04 16:24:45 -0600100class RunTests(cros_test_lib.RunCommandTestCase):
Don Garrett0c54ed72017-03-03 11:18:57 -0800101 """Tests for cbuildbot_launch script."""
Don Garrett597ddff2017-02-17 18:29:37 -0800102
103 ARGS_BASE = ['--buildroot', '/buildroot']
Don Garrett5cd946b2017-07-20 13:42:20 -0700104 EXPECTED_ARGS_BASE = ['--buildroot', '/cbuildbot_buildroot']
Don Garrett597ddff2017-02-17 18:29:37 -0800105 ARGS_GIT_CACHE = ['--git-cache-dir', '/git-cache']
106 ARGS_CONFIG = ['config']
Don Garrettbf90cdf2017-05-19 15:54:02 -0700107 CMD = ['/cbuildbot_buildroot/chromite/bin/cbuildbot']
Don Garrett597ddff2017-02-17 18:29:37 -0800108
Don Garrett6e5c6b92018-04-06 17:58:49 -0700109 def verifyCbuildbot(self, args, expected_cmd, version):
Don Garrett86881cb2017-02-15 15:41:55 -0800110 """Ensure we invoke cbuildbot correctly."""
Don Garrett597ddff2017-02-17 18:29:37 -0800111 self.PatchObject(
Mike Frysingerc263d092019-09-18 15:11:47 -0400112 commands, 'GetTargetChromiteApiVersion', autospec=True,
Don Garrett597ddff2017-02-17 18:29:37 -0800113 return_value=version)
114
Don Garrett6e5c6b92018-04-06 17:58:49 -0700115 cbuildbot_launch.Cbuildbot('/cbuildbot_buildroot', '/depot_tools', args)
Don Garrett597ddff2017-02-17 18:29:37 -0800116
117 self.assertCommandCalled(
Don Garretta50bf492017-09-28 18:33:02 -0700118 expected_cmd, extra_env={'PATH': mock.ANY},
Mike Frysingerf5a3b2d2019-12-12 14:36:17 -0500119 cwd='/cbuildbot_buildroot', check=False)
Don Garrett597ddff2017-02-17 18:29:37 -0800120
Don Garrett6e5c6b92018-04-06 17:58:49 -0700121 def testCbuildbotSimple(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800122 """Ensure we invoke cbuildbot correctly."""
Don Garrett6e5c6b92018-04-06 17:58:49 -0700123 self.verifyCbuildbot(
Don Garrett597ddff2017-02-17 18:29:37 -0800124 self.ARGS_BASE + self.ARGS_CONFIG,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700125 self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE,
Don Garrett597ddff2017-02-17 18:29:37 -0800126 (0, 4))
127
Don Garrett6e5c6b92018-04-06 17:58:49 -0700128 def testCbuildbotNotFiltered(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800129 """Ensure we invoke cbuildbot correctly."""
Don Garrett6e5c6b92018-04-06 17:58:49 -0700130 self.verifyCbuildbot(
Don Garrett597ddff2017-02-17 18:29:37 -0800131 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700132 (self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE +
133 self.ARGS_GIT_CACHE),
Don Garrett597ddff2017-02-17 18:29:37 -0800134 (0, 4))
135
Don Garrett6e5c6b92018-04-06 17:58:49 -0700136 def testCbuildbotFiltered(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800137 """Ensure we invoke cbuildbot correctly."""
Don Garrett6e5c6b92018-04-06 17:58:49 -0700138 self.verifyCbuildbot(
Don Garrett597ddff2017-02-17 18:29:37 -0800139 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700140 self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE,
Don Garrett597ddff2017-02-17 18:29:37 -0800141 (0, 2))
Don Garrettc4114cc2016-11-01 20:04:06 -0700142
Don Garrett86881cb2017-02-15 15:41:55 -0800143 def testMainMin(self):
144 """Test a minimal set of command line options."""
Don Garrett597ddff2017-02-17 18:29:37 -0800145 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
Mike Frysingerc263d092019-09-18 15:11:47 -0400146 self.PatchObject(commands, 'GetTargetChromiteApiVersion',
Don Garrett861e9182017-05-15 15:30:23 -0700147 autospec=True, return_value=(constants.REEXEC_API_MAJOR,
148 constants.REEXEC_API_MINOR))
Don Garrettf324bc32017-05-23 14:00:53 -0700149 mock_repo = mock.MagicMock()
Julio Hurtado9265c7e2021-04-19 23:04:44 +0000150 mock_repo.branch = 'main'
Mike Nichols207e2a12021-05-20 14:00:29 -0600151 mock_repo.directory = '/root'
Don Garrettbf90cdf2017-05-19 15:54:02 -0700152
Don Garrettf324bc32017-05-23 14:00:53 -0700153 mock_repo_create = self.PatchObject(repository, 'RepoRepository',
154 autospec=True, return_value=mock_repo)
Don Garrett0c54ed72017-03-03 11:18:57 -0800155 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800156 autospec=True)
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700157 mock_cleanup_chroot = self.PatchObject(cbuildbot_launch, 'CleanupChroot',
158 autospec=True)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600159 mock_set_last_build_state = self.PatchObject(
160 cbuildbot_launch, 'SetLastBuildState', autospec=True)
161
162 expected_build_state = build_summary.BuildSummary(
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600163 build_number=0, master_build_id=0, status=mock.ANY,
Julio Hurtado9265c7e2021-04-19 23:04:44 +0000164 buildroot_layout=2, branch='main')
Don Garrett7ade05a2017-02-17 13:31:47 -0800165
Mike Nichols207e2a12021-05-20 14:00:29 -0600166 argv = ['-r', '/root', 'config',
167 '--workspace', '/root/workspace']
Dhanya Ganesh95c5c152018-10-08 16:48:29 -0600168 options = cbuildbot_launch.PreParseArguments(argv)
169 cbuildbot_launch._main(options, argv)
Don Garrettc4114cc2016-11-01 20:04:06 -0700170
Don Garrettf324bc32017-05-23 14:00:53 -0700171 # Did we create the repo instance correctly?
172 self.assertEqual(mock_repo_create.mock_calls,
Mike Nichols207e2a12021-05-20 14:00:29 -0600173 [mock.call(EXPECTED_MANIFEST_URL, '/root',
Julio Hurtado9265c7e2021-04-19 23:04:44 +0000174 git_cache_dir=None, branch='main')])
Don Garrettf324bc32017-05-23 14:00:53 -0700175
Don Garrett86881cb2017-02-15 15:41:55 -0800176 # Ensure we checkout, as expected.
177 self.assertEqual(mock_checkout.mock_calls,
Mike Nichols9fb48832021-01-29 14:47:15 -0700178 [mock.call(mock_repo, options)])
Don Garrettc4114cc2016-11-01 20:04:06 -0700179
Don Garrett86881cb2017-02-15 15:41:55 -0800180 # Ensure we invoke cbuildbot, as expected.
181 self.assertCommandCalled(
Don Garrett5cd946b2017-07-20 13:42:20 -0700182 [
Mike Nichols207e2a12021-05-20 14:00:29 -0600183 '/root/chromite/bin/cbuildbot',
Don Garrett5cd946b2017-07-20 13:42:20 -0700184 'config',
Mike Nichols207e2a12021-05-20 14:00:29 -0600185 '-r', '/root',
Don Garrettb497f552018-07-09 16:01:13 -0700186 '--workspace', '/root/workspace',
Mike Nichols207e2a12021-05-20 14:00:29 -0600187 '--workspace', '/root/workspace',
188 '--cache-dir', '/root/.cache',
Don Garrett61ce1ee2019-02-26 16:20:25 -0800189 # The duplication is a bug, but not harmful.
Mike Nichols207e2a12021-05-20 14:00:29 -0600190 '--cache-dir', '/root/.cache',
Don Garrett5cd946b2017-07-20 13:42:20 -0700191 ],
Don Garretta50bf492017-09-28 18:33:02 -0700192 extra_env={'PATH': mock.ANY},
Mike Nichols207e2a12021-05-20 14:00:29 -0600193 cwd='/root',
Mike Frysingerf5a3b2d2019-12-12 14:36:17 -0500194 check=False)
Don Garrettc4114cc2016-11-01 20:04:06 -0700195
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600196 # Ensure we saved the final state, as expected.
197 self.assertEqual(expected_build_state.status,
198 constants.BUILDER_STATUS_PASSED)
199 self.assertEqual(mock_set_last_build_state.mock_calls, [
200 mock.call('/root', expected_build_state)])
201
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700202 # Ensure we clean the chroot, as expected.
Mike Nichols207e2a12021-05-20 14:00:29 -0600203 mock_cleanup_chroot.assert_called_once_with('/root')
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700204
Don Garrett86881cb2017-02-15 15:41:55 -0800205 def testMainMax(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800206 """Test a larger set of command line options."""
207 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
Mike Frysingerc263d092019-09-18 15:11:47 -0400208 self.PatchObject(commands, 'GetTargetChromiteApiVersion',
Don Garrett861e9182017-05-15 15:30:23 -0700209 autospec=True, return_value=(constants.REEXEC_API_MAJOR,
210 constants.REEXEC_API_MINOR))
Don Garrettf324bc32017-05-23 14:00:53 -0700211 mock_repo = mock.MagicMock()
212 mock_repo.branch = 'branch'
Mike Nichols207e2a12021-05-20 14:00:29 -0600213 mock_repo.directory = '/root'
Don Garrettbf90cdf2017-05-19 15:54:02 -0700214
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600215 mock_summary = build_summary.BuildSummary(
216 build_number=313,
217 master_build_id=123123123,
218 status=constants.BUILDER_STATUS_FAILED,
219 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
220 branch='branch')
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600221
222 mock_get_last_build_state = self.PatchObject(
223 cbuildbot_launch, 'GetLastBuildState', autospec=True,
224 return_value=mock_summary)
Don Garrettf324bc32017-05-23 14:00:53 -0700225 mock_repo_create = self.PatchObject(repository, 'RepoRepository',
226 autospec=True, return_value=mock_repo)
Don Garrett0c54ed72017-03-03 11:18:57 -0800227 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800228 autospec=True)
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700229 mock_cleanup_chroot = self.PatchObject(cbuildbot_launch, 'CleanupChroot',
230 autospec=True)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600231 mock_set_last_build_state = self.PatchObject(
232 cbuildbot_launch, 'SetLastBuildState', autospec=True)
Dhanya Ganesh95c5c152018-10-08 16:48:29 -0600233 argv = ['--buildroot', '/root',
234 '--branch', 'branch',
235 '--git-cache-dir', '/git-cache',
Don Garrett61ce1ee2019-02-26 16:20:25 -0800236 '--cache-dir', '/cache',
Mike Nichols207e2a12021-05-20 14:00:29 -0600237 '--workspace', '/root/workspace',
Dhanya Ganesh95c5c152018-10-08 16:48:29 -0600238 '--remote-trybot',
239 '--master-build-id', '123456789',
240 '--buildnumber', '314',
241 'config']
242 options = cbuildbot_launch.PreParseArguments(argv)
243 cbuildbot_launch._main(options, argv)
Don Garrettc4114cc2016-11-01 20:04:06 -0700244
Don Garrettf324bc32017-05-23 14:00:53 -0700245 # Did we create the repo instance correctly?
246 self.assertEqual(mock_repo_create.mock_calls,
Mike Nichols207e2a12021-05-20 14:00:29 -0600247 [mock.call(EXPECTED_MANIFEST_URL, '/root',
Don Garrett33872502018-08-03 22:30:40 +0000248 git_cache_dir='/git-cache', branch='branch')])
Don Garrettf324bc32017-05-23 14:00:53 -0700249
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600250 # Ensure we look up the previous status.
251 self.assertEqual(mock_get_last_build_state.mock_calls, [
252 mock.call('/root')])
253
Don Garrett86881cb2017-02-15 15:41:55 -0800254 # Ensure we checkout, as expected.
255 self.assertEqual(mock_checkout.mock_calls,
Mike Nichols9fb48832021-01-29 14:47:15 -0700256 [mock.call(mock_repo, options)])
Don Garrett86881cb2017-02-15 15:41:55 -0800257
258 # Ensure we invoke cbuildbot, as expected.
259 self.assertCommandCalled(
Don Garrett5cd946b2017-07-20 13:42:20 -0700260 [
Mike Nichols207e2a12021-05-20 14:00:29 -0600261 '/root/chromite/bin/cbuildbot',
Don Garrett5cd946b2017-07-20 13:42:20 -0700262 'config',
Mike Nichols207e2a12021-05-20 14:00:29 -0600263 '--buildroot', '/root',
Don Garrett5cd946b2017-07-20 13:42:20 -0700264 '--branch', 'branch',
265 '--git-cache-dir', '/git-cache',
Don Garrett61ce1ee2019-02-26 16:20:25 -0800266 '--cache-dir', '/cache',
Mike Nichols207e2a12021-05-20 14:00:29 -0600267 '--workspace', '/root/workspace',
Don Garrett5cd946b2017-07-20 13:42:20 -0700268 '--remote-trybot',
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600269 '--master-build-id', '123456789',
270 '--buildnumber', '314',
271 '--previous-build-state',
Mike Frysingerd0960812020-06-09 01:53:32 -0400272 'eyJicmFuY2giOiJicmFuY2giLCJidWlsZF9udW1iZXIiOjMxMywiYnVpbGRyb290X'
273 '2xheW91dCI6MiwibWFzdGVyX2J1aWxkX2lkIjoxMjMxMjMxMjMsInN0YXR1cyI6Im'
274 'ZhaWwifQ==',
Don Garrettb497f552018-07-09 16:01:13 -0700275 '--workspace', '/root/workspace',
Don Garrett61ce1ee2019-02-26 16:20:25 -0800276 '--cache-dir', '/cache',
Don Garrett5cd946b2017-07-20 13:42:20 -0700277 ],
Don Garretta50bf492017-09-28 18:33:02 -0700278 extra_env={'PATH': mock.ANY},
Mike Nichols207e2a12021-05-20 14:00:29 -0600279 cwd='/root',
Mike Frysingerf5a3b2d2019-12-12 14:36:17 -0500280 check=False)
Don Garrett7ade05a2017-02-17 13:31:47 -0800281
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600282 # Ensure we write the final build state, as expected.
283 final_state = build_summary.BuildSummary(
284 build_number=314,
285 master_build_id=123456789,
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600286 status=constants.BUILDER_STATUS_PASSED,
287 buildroot_layout=2,
288 branch='branch')
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600289 self.assertEqual(mock_set_last_build_state.mock_calls, [
290 mock.call('/root', final_state)])
291
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700292 # Ensure we clean the chroot, as expected.
Mike Nichols207e2a12021-05-20 14:00:29 -0600293 mock_cleanup_chroot.assert_called_once_with('/root')
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700294
Don Garrett7ade05a2017-02-17 13:31:47 -0800295
Don Garrettbf90cdf2017-05-19 15:54:02 -0700296class CleanBuildRootTest(cros_test_lib.MockTempDirTestCase):
297 """Tests for CleanBuildRoot method."""
Don Garrett7ade05a2017-02-17 13:31:47 -0800298
299 def setUp(self):
300 """Create standard buildroot contents for cleanup."""
Don Garrettbf90cdf2017-05-19 15:54:02 -0700301 self.root = os.path.join(self.tempdir)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600302 self.previous_build_state = os.path.join(
303 self.root, '.cbuildbot_build_state.json')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700304 self.buildroot = os.path.join(self.root, 'buildroot')
305 self.repo = os.path.join(self.buildroot, '.repo/repo')
Don Garrett36650112018-06-28 15:54:34 -0700306 self.chroot = os.path.join(self.buildroot, 'chroot')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700307 self.general = os.path.join(self.buildroot, 'general/general')
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700308 self.cache = os.path.join(self.buildroot, '.cache')
309 self.distfiles = os.path.join(self.cache, 'distfiles')
Don Garrett7ade05a2017-02-17 13:31:47 -0800310
Don Garrett4166d182018-12-17 12:52:02 -0800311 self.mock_repo = mock.Mock(repository.RepoRepository)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700312 self.mock_repo.directory = self.buildroot
Don Garrettf324bc32017-05-23 14:00:53 -0700313
Benjamin Gordon8642bcc2018-05-01 13:49:56 -0600314 def populateBuildroot(self, previous_build_state=None):
Don Garrett7ade05a2017-02-17 13:31:47 -0800315 """Create standard buildroot contents for cleanup."""
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600316 if previous_build_state:
317 osutils.SafeMakedirs(self.root)
318 osutils.WriteFile(self.previous_build_state, previous_build_state)
319
Don Garrett7ade05a2017-02-17 13:31:47 -0800320 # Create files.
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700321 for f in (self.repo, self.chroot, self.general, self.distfiles):
Don Garrette17e1d92017-04-12 15:28:19 -0700322 osutils.Touch(f, makedirs=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800323
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600324 def testGetCurrentBuildStateNoArgs(self):
325 """Tests GetCurrentBuildState without arguments."""
326 options = cbuildbot_launch.PreParseArguments([
327 '--buildroot', self.root, 'config'
328 ])
Julio Hurtado9265c7e2021-04-19 23:04:44 +0000329 state = cbuildbot_launch.GetCurrentBuildState(options, 'main')
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600330
331 expected_state = build_summary.BuildSummary(
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600332 status=constants.BUILDER_STATUS_INFLIGHT,
333 buildroot_layout=2,
Julio Hurtado9265c7e2021-04-19 23:04:44 +0000334 branch='main')
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600335 self.assertEqual(state, expected_state)
336
337 def testGetCurrentBuildStateHasArgs(self):
338 """Tests GetCurrentBuildState with arguments."""
339 options = cbuildbot_launch.PreParseArguments([
340 '--buildroot', self.root,
341 '--buildnumber', '20',
342 '--master-build-id', '50',
343 'config'
344 ])
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600345 state = cbuildbot_launch.GetCurrentBuildState(options, 'branchA')
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600346
347 expected_state = build_summary.BuildSummary(
348 build_number=20,
349 master_build_id=50,
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600350 status=constants.BUILDER_STATUS_INFLIGHT,
351 buildroot_layout=2,
352 branch='branchA')
353 self.assertEqual(state, expected_state)
354
355 def testGetCurrentBuildStateLayout(self):
356 """Test that GetCurrentBuildState uses the current buildroot layout."""
357 # Change to a future version.
358 self.PatchObject(cbuildbot_launch, 'BUILDROOT_BUILDROOT_LAYOUT', 22)
359
360 options = cbuildbot_launch.PreParseArguments([
361 '--buildroot', self.root, 'config'
362 ])
363 state = cbuildbot_launch.GetCurrentBuildState(options, 'branchA')
364
365 expected_state = build_summary.BuildSummary(
366 status=constants.BUILDER_STATUS_INFLIGHT,
367 buildroot_layout=22,
368 branch='branchA')
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600369 self.assertEqual(state, expected_state)
370
371 def testGetLastBuildStateNoFile(self):
372 """Tests GetLastBuildState if the file is missing."""
373 osutils.SafeMakedirs(self.root)
374 state = cbuildbot_launch.GetLastBuildState(self.root)
375 self.assertEqual(state, build_summary.BuildSummary())
376
377 def testGetLastBuildStateBadFile(self):
378 """Tests GetLastBuildState if the file contains invalid JSON."""
379 osutils.SafeMakedirs(self.root)
380 osutils.WriteFile(self.previous_build_state, '}}')
381 state = cbuildbot_launch.GetLastBuildState(self.root)
382 self.assertEqual(state, build_summary.BuildSummary())
383
384 def testGetLastBuildStateMissingBuildStatus(self):
385 """Tests GetLastBuildState if the file doesn't have a valid status."""
386 osutils.SafeMakedirs(self.root)
387 osutils.WriteFile(self.previous_build_state, '{"build_number": "3"}')
388 state = cbuildbot_launch.GetLastBuildState(self.root)
389 self.assertEqual(state, build_summary.BuildSummary())
390
391 def testGetLastBuildStateGoodFile(self):
392 """Tests GetLastBuildState on a good file."""
393 osutils.SafeMakedirs(self.root)
394 osutils.WriteFile(
395 self.previous_build_state,
396 '{"build_number": 1, "master_build_id": 3, "status": "pass"}')
397 state = cbuildbot_launch.GetLastBuildState(self.root)
398 self.assertEqual(
399 state,
400 build_summary.BuildSummary(
401 build_number=1, master_build_id=3, status='pass'))
402
403 def testSetLastBuildState(self):
404 """Verifies that SetLastBuildState writes to the expected file."""
405 osutils.SafeMakedirs(self.root)
406 old_state = build_summary.BuildSummary(
407 build_number=314,
408 master_build_id=2178,
409 status=constants.BUILDER_STATUS_PASSED)
410 cbuildbot_launch.SetLastBuildState(self.root, old_state)
411
412 saved_state = osutils.ReadFile(self.previous_build_state)
413 new_state = build_summary.BuildSummary()
414 new_state.from_json(saved_state)
415
416 self.assertEqual(old_state, new_state)
Mike Frysingerf0146252019-09-02 13:31:05 -0400417
418 def testCleanupChrootNoChroot(self):
419 """Check CleanupChroot without a chroot."""
420 self.StartPatcher(cros_test_lib.RunCommandMock())
421 with mock.patch.object(cros_sdk_lib, 'CleanupChrootMount'):
Mike Frysinger7a63ee72019-09-03 14:24:06 -0400422 cbuildbot_launch.CleanupChroot(self.buildroot)
Mike Frysingerf0146252019-09-02 13:31:05 -0400423
424 def testCleanupChrootNormal(self):
425 """Check normal CleanupChroot."""
426 osutils.SafeMakedirs(self.chroot)
427 osutils.Touch(self.chroot + '.img')
428 self.StartPatcher(cros_test_lib.RunCommandMock())
429 with mock.patch.object(cros_sdk_lib, 'CleanupChrootMount'):
Mike Frysinger7a63ee72019-09-03 14:24:06 -0400430 cbuildbot_launch.CleanupChroot(self.buildroot)
Mike Frysingerf0146252019-09-02 13:31:05 -0400431
432 def testCleanupChrootTimeout(self):
433 """Check timeouts in CleanupChroot."""
434 osutils.SafeMakedirs(self.chroot)
435 osutils.Touch(self.chroot + '.img')
436 rc_mock = self.StartPatcher(cros_test_lib.RunCommandMock())
437 rc_mock.SetDefaultCmdResult()
438 with mock.patch.object(cros_sdk_lib, 'CleanupChrootMount',
439 side_effect=timeout_util.TimeoutError):
Mike Frysinger7a63ee72019-09-03 14:24:06 -0400440 cbuildbot_launch.CleanupChroot(self.buildroot)