blob: 79bcbfb6d978c4ea6eabd8890f6d14af56fb409c [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 Garrett86881cb2017-02-15 15:41:55 -080010import mock
Don Garrett7ade05a2017-02-17 13:31:47 -080011import os
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -070012import time
Don Garrett86881cb2017-02-15 15:41:55 -080013
14from 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
Don Garrett597ddff2017-02-17 18:29:37 -080017from chromite.lib import cros_build_lib
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
Don Garrett0c54ed72017-03-03 11:18:57 -080021from chromite.scripts import cbuildbot_launch
Don Garrett86881cb2017-02-15 15:41:55 -080022
Don Garrett86881cb2017-02-15 15:41:55 -080023EXPECTED_MANIFEST_URL = 'https://chrome-internal-review.googlesource.com/chromeos/manifest-internal' # pylint: disable=line-too-long
Don Garrettc4114cc2016-11-01 20:04:06 -070024
25
Don Garrettacbb2392017-05-11 18:27:41 -070026# It's reasonable for unittests to look at internals.
27# pylint: disable=protected-access
28
29
Don Garrett8d314792017-05-18 13:11:42 -070030class FakeException(Exception):
31 """Test exception to raise during tests."""
32
33
Don Garrett0c54ed72017-03-03 11:18:57 -080034class CbuildbotLaunchTest(cros_test_lib.MockTestCase):
35 """Tests for cbuildbot_launch script."""
Don Garrettc4114cc2016-11-01 20:04:06 -070036
Don Garrett86881cb2017-02-15 15:41:55 -080037 def testPreParseArguments(self):
Don Garrettc4114cc2016-11-01 20:04:06 -070038 """Test that we can correctly extract branch values from cbuildbot args."""
Don Garrett597ddff2017-02-17 18:29:37 -080039 CASES = (
40 (['--buildroot', '/buildroot', 'daisy-incremental'],
41 (None, '/buildroot', None)),
42
43 (['--buildbot', '--buildroot', '/buildroot',
44 '--git-cache-dir', '/git-cache',
45 '-b', 'release-R57-9202.B',
46 'daisy-incremental'],
47 ('release-R57-9202.B', '/buildroot', '/git-cache')),
48
49 (['--debug', '--buildbot', '--notests',
50 '--buildroot', '/buildroot',
51 '--git-cache-dir', '/git-cache',
52 '--branch', 'release-R57-9202.B',
53 'daisy-incremental'],
54 ('release-R57-9202.B', '/buildroot', '/git-cache')),
Don Garrettc4114cc2016-11-01 20:04:06 -070055 )
56
Don Garrett597ddff2017-02-17 18:29:37 -080057 for cmd_args, expected in CASES:
58 expected_branch, expected_buildroot, expected_cache_dir = expected
59
Don Garrett0c54ed72017-03-03 11:18:57 -080060 options = cbuildbot_launch.PreParseArguments(cmd_args)
Don Garrett597ddff2017-02-17 18:29:37 -080061
62 self.assertEqual(options.branch, expected_branch)
63 self.assertEqual(options.buildroot, expected_buildroot)
64 self.assertEqual(options.git_cache_dir, expected_cache_dir)
Don Garrett86881cb2017-02-15 15:41:55 -080065
Don Garrettf324bc32017-05-23 14:00:53 -070066 def testInitialCheckout(self):
Don Garrett86881cb2017-02-15 15:41:55 -080067 """Test InitialCheckout with minimum settings."""
Don Garrettf324bc32017-05-23 14:00:53 -070068 mock_repo = mock.MagicMock()
69 mock_repo.branch = 'branch'
Don Garrett86881cb2017-02-15 15:41:55 -080070
Don Garrettf324bc32017-05-23 14:00:53 -070071 cbuildbot_launch.InitialCheckout(mock_repo)
Don Garrett86881cb2017-02-15 15:41:55 -080072
73 self.assertEqual(mock_repo.mock_calls, [
Don Garrettf324bc32017-05-23 14:00:53 -070074 mock.call.Sync(detach=True),
Don Garrett8d314792017-05-18 13:11:42 -070075 ])
76
Don Garrettf15d65b2017-04-12 12:39:55 -070077 def testConfigureGlobalEnvironment(self):
Don Garrett60967922017-04-12 18:51:44 -070078 """Ensure that we can setup our global runtime environment correctly."""
Don Garrett86fec482017-05-17 18:13:33 -070079
80 os.environ.pop('LANG', None)
81 os.environ['LC_MONETARY'] = 'bad'
82
Don Garrettf15d65b2017-04-12 12:39:55 -070083 cbuildbot_launch.ConfigureGlobalEnvironment()
84
Don Garrett86fec482017-05-17 18:13:33 -070085 # Verify umask is updated.
Don Garrettf15d65b2017-04-12 12:39:55 -070086 self.assertEqual(os.umask(0), 0o22)
87
Don Garrett86fec482017-05-17 18:13:33 -070088 # Verify ENVs are cleaned up.
89 self.assertEqual(os.environ['LANG'], 'en_US.UTF-8')
90 self.assertNotIn('LC_MONETARY', os.environ)
91
Don Garrettf15d65b2017-04-12 12:39:55 -070092
Benjamin Gordon121a2aa2018-05-04 16:24:45 -060093class RunTests(cros_test_lib.RunCommandTestCase):
Don Garrett0c54ed72017-03-03 11:18:57 -080094 """Tests for cbuildbot_launch script."""
Don Garrett597ddff2017-02-17 18:29:37 -080095
96 ARGS_BASE = ['--buildroot', '/buildroot']
Don Garrett5cd946b2017-07-20 13:42:20 -070097 EXPECTED_ARGS_BASE = ['--buildroot', '/cbuildbot_buildroot']
Don Garrett597ddff2017-02-17 18:29:37 -080098 ARGS_GIT_CACHE = ['--git-cache-dir', '/git-cache']
99 ARGS_CONFIG = ['config']
Don Garrettbf90cdf2017-05-19 15:54:02 -0700100 CMD = ['/cbuildbot_buildroot/chromite/bin/cbuildbot']
Don Garrett597ddff2017-02-17 18:29:37 -0800101
Don Garrett6e5c6b92018-04-06 17:58:49 -0700102 def verifyCbuildbot(self, args, expected_cmd, version):
Don Garrett86881cb2017-02-15 15:41:55 -0800103 """Ensure we invoke cbuildbot correctly."""
Don Garrett597ddff2017-02-17 18:29:37 -0800104 self.PatchObject(
105 cros_build_lib, 'GetTargetChromiteApiVersion', autospec=True,
106 return_value=version)
107
Don Garrett6e5c6b92018-04-06 17:58:49 -0700108 cbuildbot_launch.Cbuildbot('/cbuildbot_buildroot', '/depot_tools', args)
Don Garrett597ddff2017-02-17 18:29:37 -0800109
110 self.assertCommandCalled(
Don Garretta50bf492017-09-28 18:33:02 -0700111 expected_cmd, extra_env={'PATH': mock.ANY},
112 cwd='/cbuildbot_buildroot', error_code_ok=True)
Don Garrett597ddff2017-02-17 18:29:37 -0800113
Don Garrett6e5c6b92018-04-06 17:58:49 -0700114 def testCbuildbotSimple(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800115 """Ensure we invoke cbuildbot correctly."""
Don Garrett6e5c6b92018-04-06 17:58:49 -0700116 self.verifyCbuildbot(
Don Garrett597ddff2017-02-17 18:29:37 -0800117 self.ARGS_BASE + self.ARGS_CONFIG,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700118 self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE,
Don Garrett597ddff2017-02-17 18:29:37 -0800119 (0, 4))
120
Don Garrett6e5c6b92018-04-06 17:58:49 -0700121 def testCbuildbotNotFiltered(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 + self.ARGS_GIT_CACHE,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700125 (self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE +
126 self.ARGS_GIT_CACHE),
Don Garrett597ddff2017-02-17 18:29:37 -0800127 (0, 4))
128
Don Garrett6e5c6b92018-04-06 17:58:49 -0700129 def testCbuildbotFiltered(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800130 """Ensure we invoke cbuildbot correctly."""
Don Garrett6e5c6b92018-04-06 17:58:49 -0700131 self.verifyCbuildbot(
Don Garrett597ddff2017-02-17 18:29:37 -0800132 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700133 self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE,
Don Garrett597ddff2017-02-17 18:29:37 -0800134 (0, 2))
Don Garrettc4114cc2016-11-01 20:04:06 -0700135
Don Garrett86881cb2017-02-15 15:41:55 -0800136 def testMainMin(self):
137 """Test a minimal set of command line options."""
Don Garrett597ddff2017-02-17 18:29:37 -0800138 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
139 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
Don Garrett861e9182017-05-15 15:30:23 -0700140 autospec=True, return_value=(constants.REEXEC_API_MAJOR,
141 constants.REEXEC_API_MINOR))
Don Garrettf324bc32017-05-23 14:00:53 -0700142 mock_repo = mock.MagicMock()
143 mock_repo.branch = 'master'
Don Garrettbf90cdf2017-05-19 15:54:02 -0700144 mock_repo.directory = '/root/repository'
145
Don Garrettf324bc32017-05-23 14:00:53 -0700146 mock_repo_create = self.PatchObject(repository, 'RepoRepository',
147 autospec=True, return_value=mock_repo)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700148 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot',
Don Garrett0c54ed72017-03-03 11:18:57 -0800149 autospec=True)
150 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800151 autospec=True)
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700152 mock_cleanup_chroot = self.PatchObject(cbuildbot_launch, 'CleanupChroot',
153 autospec=True)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600154 mock_set_last_build_state = self.PatchObject(
155 cbuildbot_launch, 'SetLastBuildState', autospec=True)
156
157 expected_build_state = build_summary.BuildSummary(
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600158 build_number=0, master_build_id=0, status=mock.ANY,
159 buildroot_layout=2, branch='master')
Don Garrett7ade05a2017-02-17 13:31:47 -0800160
Dhanya Ganesh95c5c152018-10-08 16:48:29 -0600161 argv = ['-r', '/root', 'config']
162 options = cbuildbot_launch.PreParseArguments(argv)
163 cbuildbot_launch._main(options, argv)
Don Garrettc4114cc2016-11-01 20:04:06 -0700164
Don Garrettf324bc32017-05-23 14:00:53 -0700165 # Did we create the repo instance correctly?
166 self.assertEqual(mock_repo_create.mock_calls,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700167 [mock.call(EXPECTED_MANIFEST_URL, '/root/repository',
Don Garrett33872502018-08-03 22:30:40 +0000168 git_cache_dir=None, branch='master')])
Don Garrettf324bc32017-05-23 14:00:53 -0700169
Don Garrett7ade05a2017-02-17 13:31:47 -0800170 # Ensure we clean, as expected.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700171 self.assertEqual(mock_clean.mock_calls, [
Don Garrettd1d90dd2017-06-13 17:35:52 -0700172 mock.call('/root', mock_repo,
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600173 expected_build_state)])
Don Garrett7ade05a2017-02-17 13:31:47 -0800174
Don Garrett86881cb2017-02-15 15:41:55 -0800175 # Ensure we checkout, as expected.
176 self.assertEqual(mock_checkout.mock_calls,
Don Garrettf324bc32017-05-23 14:00:53 -0700177 [mock.call(mock_repo)])
Don Garrettc4114cc2016-11-01 20:04:06 -0700178
Don Garrett86881cb2017-02-15 15:41:55 -0800179 # Ensure we invoke cbuildbot, as expected.
180 self.assertCommandCalled(
Don Garrett5cd946b2017-07-20 13:42:20 -0700181 [
182 '/root/repository/chromite/bin/cbuildbot',
183 'config',
184 '-r', '/root/repository',
Don Garrettb497f552018-07-09 16:01:13 -0700185 '--workspace', '/root/workspace',
Don Garrett5cd946b2017-07-20 13:42:20 -0700186 '--ts-mon-task-num', '1',
187 ],
Don Garretta50bf492017-09-28 18:33:02 -0700188 extra_env={'PATH': mock.ANY},
Don Garrettbf90cdf2017-05-19 15:54:02 -0700189 cwd='/root/repository',
Don Garrettacbb2392017-05-11 18:27:41 -0700190 error_code_ok=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700191
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600192 # Ensure we saved the final state, as expected.
193 self.assertEqual(expected_build_state.status,
194 constants.BUILDER_STATUS_PASSED)
195 self.assertEqual(mock_set_last_build_state.mock_calls, [
196 mock.call('/root', expected_build_state)])
197
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700198 # Ensure we clean the chroot, as expected.
199 self.assertEqual(mock_cleanup_chroot.mock_calls, [
200 mock.call('/root/repository')])
201
Don Garrett86881cb2017-02-15 15:41:55 -0800202 def testMainMax(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800203 """Test a larger set of command line options."""
204 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
205 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
Don Garrett861e9182017-05-15 15:30:23 -0700206 autospec=True, return_value=(constants.REEXEC_API_MAJOR,
207 constants.REEXEC_API_MINOR))
Don Garrettf324bc32017-05-23 14:00:53 -0700208 mock_repo = mock.MagicMock()
209 mock_repo.branch = 'branch'
Don Garrettbf90cdf2017-05-19 15:54:02 -0700210 mock_repo.directory = '/root/repository'
211
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600212 mock_summary = build_summary.BuildSummary(
213 build_number=313,
214 master_build_id=123123123,
215 status=constants.BUILDER_STATUS_FAILED,
216 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
217 branch='branch')
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600218
219 mock_get_last_build_state = self.PatchObject(
220 cbuildbot_launch, 'GetLastBuildState', autospec=True,
221 return_value=mock_summary)
Don Garrettf324bc32017-05-23 14:00:53 -0700222 mock_repo_create = self.PatchObject(repository, 'RepoRepository',
223 autospec=True, return_value=mock_repo)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700224 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot',
Don Garrett0c54ed72017-03-03 11:18:57 -0800225 autospec=True)
226 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800227 autospec=True)
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700228 mock_cleanup_chroot = self.PatchObject(cbuildbot_launch, 'CleanupChroot',
229 autospec=True)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600230 mock_set_last_build_state = self.PatchObject(
231 cbuildbot_launch, 'SetLastBuildState', autospec=True)
Dhanya Ganesh95c5c152018-10-08 16:48:29 -0600232 argv = ['--buildroot', '/root',
233 '--branch', 'branch',
234 '--git-cache-dir', '/git-cache',
235 '--remote-trybot',
236 '--master-build-id', '123456789',
237 '--buildnumber', '314',
238 'config']
239 options = cbuildbot_launch.PreParseArguments(argv)
240 cbuildbot_launch._main(options, argv)
Don Garrettc4114cc2016-11-01 20:04:06 -0700241
Don Garrettf324bc32017-05-23 14:00:53 -0700242 # Did we create the repo instance correctly?
243 self.assertEqual(mock_repo_create.mock_calls,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700244 [mock.call(EXPECTED_MANIFEST_URL, '/root/repository',
Don Garrett33872502018-08-03 22:30:40 +0000245 git_cache_dir='/git-cache', branch='branch')])
Don Garrettf324bc32017-05-23 14:00:53 -0700246
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600247 # Ensure we look up the previous status.
248 self.assertEqual(mock_get_last_build_state.mock_calls, [
249 mock.call('/root')])
250
Don Garrett7ade05a2017-02-17 13:31:47 -0800251 # Ensure we clean, as expected.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700252 self.assertEqual(mock_clean.mock_calls, [
Don Garrettd1d90dd2017-06-13 17:35:52 -0700253 mock.call('/root',
254 mock_repo,
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600255 build_summary.BuildSummary(
256 build_number=314,
257 master_build_id=123456789,
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600258 status=mock.ANY,
259 branch='branch',
260 buildroot_layout=2
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600261 ))])
Don Garrett7ade05a2017-02-17 13:31:47 -0800262
Don Garrett86881cb2017-02-15 15:41:55 -0800263 # Ensure we checkout, as expected.
264 self.assertEqual(mock_checkout.mock_calls,
Don Garrettf324bc32017-05-23 14:00:53 -0700265 [mock.call(mock_repo)])
Don Garrett86881cb2017-02-15 15:41:55 -0800266
267 # Ensure we invoke cbuildbot, as expected.
268 self.assertCommandCalled(
Don Garrett5cd946b2017-07-20 13:42:20 -0700269 [
270 '/root/repository/chromite/bin/cbuildbot',
271 'config',
272 '--buildroot', '/root/repository',
273 '--branch', 'branch',
274 '--git-cache-dir', '/git-cache',
275 '--remote-trybot',
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600276 '--master-build-id', '123456789',
277 '--buildnumber', '314',
278 '--previous-build-state',
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600279 'eyJzdGF0dXMiOiAiZmFpbCIsICJtYXN0ZXJfYnVpbGRfaWQiOiAxMjMxMjMxMj'
280 'MsICJidWlsZF9udW1iZXIiOiAzMTMsICJidWlsZHJvb3RfbGF5b3V0IjogMiwg'
281 'ImJyYW5jaCI6ICJicmFuY2gifQ==',
Don Garrettb497f552018-07-09 16:01:13 -0700282 '--workspace', '/root/workspace',
Don Garrett5cd946b2017-07-20 13:42:20 -0700283 '--ts-mon-task-num', '1',
284 ],
Don Garretta50bf492017-09-28 18:33:02 -0700285 extra_env={'PATH': mock.ANY},
Don Garrettbf90cdf2017-05-19 15:54:02 -0700286 cwd='/root/repository',
Don Garrettacbb2392017-05-11 18:27:41 -0700287 error_code_ok=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800288
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600289 # Ensure we write the final build state, as expected.
290 final_state = build_summary.BuildSummary(
291 build_number=314,
292 master_build_id=123456789,
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600293 status=constants.BUILDER_STATUS_PASSED,
294 buildroot_layout=2,
295 branch='branch')
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600296 self.assertEqual(mock_set_last_build_state.mock_calls, [
297 mock.call('/root', final_state)])
298
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700299 # Ensure we clean the chroot, as expected.
300 self.assertEqual(mock_cleanup_chroot.mock_calls, [
301 mock.call('/root/repository')])
302
Don Garrett7ade05a2017-02-17 13:31:47 -0800303
Don Garrettbf90cdf2017-05-19 15:54:02 -0700304class CleanBuildRootTest(cros_test_lib.MockTempDirTestCase):
305 """Tests for CleanBuildRoot method."""
Don Garrett7ade05a2017-02-17 13:31:47 -0800306
307 def setUp(self):
308 """Create standard buildroot contents for cleanup."""
Don Garrettbf90cdf2017-05-19 15:54:02 -0700309 self.root = os.path.join(self.tempdir)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600310 self.previous_build_state = os.path.join(
311 self.root, '.cbuildbot_build_state.json')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700312 self.buildroot = os.path.join(self.root, 'buildroot')
313 self.repo = os.path.join(self.buildroot, '.repo/repo')
Don Garrett36650112018-06-28 15:54:34 -0700314 self.chroot = os.path.join(self.buildroot, 'chroot')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700315 self.general = os.path.join(self.buildroot, 'general/general')
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700316 self.cache = os.path.join(self.buildroot, '.cache')
317 self.distfiles = os.path.join(self.cache, 'distfiles')
Don Garrett7ade05a2017-02-17 13:31:47 -0800318
Don Garrettf324bc32017-05-23 14:00:53 -0700319 self.mock_repo = mock.MagicMock()
Don Garrettbf90cdf2017-05-19 15:54:02 -0700320 self.mock_repo.directory = self.buildroot
Don Garrettf324bc32017-05-23 14:00:53 -0700321
Benjamin Gordon8642bcc2018-05-01 13:49:56 -0600322 def populateBuildroot(self, previous_build_state=None):
Don Garrett7ade05a2017-02-17 13:31:47 -0800323 """Create standard buildroot contents for cleanup."""
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600324 if previous_build_state:
325 osutils.SafeMakedirs(self.root)
326 osutils.WriteFile(self.previous_build_state, previous_build_state)
327
Don Garrett7ade05a2017-02-17 13:31:47 -0800328 # Create files.
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700329 for f in (self.repo, self.chroot, self.general, self.distfiles):
Don Garrette17e1d92017-04-12 15:28:19 -0700330 osutils.Touch(f, makedirs=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800331
Don Garrette17e1d92017-04-12 15:28:19 -0700332 def testNoBuildroot(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700333 """Test CleanBuildRoot with no history."""
Don Garrettf324bc32017-05-23 14:00:53 -0700334 self.mock_repo.branch = 'master'
335
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600336 build_state = build_summary.BuildSummary(
337 status=constants.BUILDER_STATUS_INFLIGHT,
338 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
339 branch='master')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700340 cbuildbot_launch.CleanBuildRoot(
Dhanya Ganesh95c5c152018-10-08 16:48:29 -0600341 self.root, self.mock_repo, build_state)
Don Garrett7ade05a2017-02-17 13:31:47 -0800342
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600343 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600344 self.assertEqual(new_summary.buildroot_layout, 2)
345 self.assertEqual(new_summary.branch, 'master')
346 self.assertIsNotNone(new_summary.distfiles_ts)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600347 self.assertEqual(new_summary, build_state)
Don Garrett7ade05a2017-02-17 13:31:47 -0800348
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600349 self.assertExists(self.previous_build_state)
350
Don Garrett7ade05a2017-02-17 13:31:47 -0800351 def testBuildrootNoState(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700352 """Test CleanBuildRoot with no state information."""
Don Garrett7ade05a2017-02-17 13:31:47 -0800353 self.populateBuildroot()
Don Garrettf324bc32017-05-23 14:00:53 -0700354 self.mock_repo.branch = 'master'
Don Garrett7ade05a2017-02-17 13:31:47 -0800355
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600356 build_state = build_summary.BuildSummary(
357 status=constants.BUILDER_STATUS_INFLIGHT,
358 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
359 branch='master')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700360 cbuildbot_launch.CleanBuildRoot(
Dhanya Ganesh95c5c152018-10-08 16:48:29 -0600361 self.root, self.mock_repo, build_state)
Don Garrett7ade05a2017-02-17 13:31:47 -0800362
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600363 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600364 self.assertEqual(new_summary.buildroot_layout, 2)
365 self.assertEqual(new_summary.branch, 'master')
366 self.assertIsNotNone(new_summary.distfiles_ts)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600367 self.assertEqual(new_summary, build_state)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700368
Don Garrett60967922017-04-12 18:51:44 -0700369 self.assertNotExists(self.repo)
Don Garrett7ade05a2017-02-17 13:31:47 -0800370 self.assertNotExists(self.chroot)
Don Garrett60967922017-04-12 18:51:44 -0700371 self.assertNotExists(self.general)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700372 self.assertNotExists(self.distfiles)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600373 self.assertExists(self.previous_build_state)
Don Garrett7ade05a2017-02-17 13:31:47 -0800374
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600375 def testBuildrootFormatMismatch(self):
376 """Test CleanBuildRoot with buildroot layout mismatch."""
377 old_build_state = build_summary.BuildSummary(
378 status=constants.BUILDER_STATUS_PASSED,
379 buildroot_layout=1,
380 branch='master')
381 self.populateBuildroot(previous_build_state=old_build_state.to_json())
382 self.mock_repo.branch = 'master'
383
384 build_state = build_summary.BuildSummary(
385 status=constants.BUILDER_STATUS_INFLIGHT,
386 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
387 branch='master')
388 cbuildbot_launch.CleanBuildRoot(
Dhanya Ganesh95c5c152018-10-08 16:48:29 -0600389 self.root, self.mock_repo, build_state)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600390
391 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
392 self.assertEqual(new_summary.buildroot_layout, 2)
393 self.assertEqual(new_summary.branch, 'master')
394 self.assertIsNotNone(new_summary.distfiles_ts)
395 self.assertEqual(new_summary, build_state)
396
397 self.assertNotExists(self.repo)
398 self.assertNotExists(self.chroot)
399 self.assertNotExists(self.general)
400 self.assertNotExists(self.distfiles)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600401 self.assertExists(self.previous_build_state)
402
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600403 def testBuildrootBranchChange(self):
404 """Test CleanBuildRoot with a change in branches."""
405 old_build_state = build_summary.BuildSummary(
406 status=constants.BUILDER_STATUS_PASSED,
407 buildroot_layout=2,
408 branch='branchA')
409 self.populateBuildroot(previous_build_state=old_build_state.to_json())
410 self.mock_repo.branch = 'branchB'
Benjamin Gordon74645232018-05-04 17:40:42 -0600411 m = self.PatchObject(cros_sdk_lib, 'CleanupChrootMount')
Don Garrett7ade05a2017-02-17 13:31:47 -0800412
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600413 build_state = build_summary.BuildSummary(
414 status=constants.BUILDER_STATUS_INFLIGHT,
415 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
416 branch='branchB')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700417 cbuildbot_launch.CleanBuildRoot(
Dhanya Ganesh95c5c152018-10-08 16:48:29 -0600418 self.root, self.mock_repo, build_state)
Don Garrett7ade05a2017-02-17 13:31:47 -0800419
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600420 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600421 self.assertEqual(new_summary.buildroot_layout, 2)
422 self.assertEqual(new_summary.branch, 'branchB')
423 self.assertIsNotNone(new_summary.distfiles_ts)
424 self.assertEqual(new_summary, build_state)
425
426 self.assertExists(self.repo)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600427 self.assertExists(self.general)
428 self.assertNotExists(self.distfiles)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600429 self.assertExists(self.previous_build_state)
Don Garrett36650112018-06-28 15:54:34 -0700430 m.assert_called_with(self.chroot, delete=True)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600431
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600432 def testBuildrootBranchMatch(self):
433 """Test CleanBuildRoot with no change in branch."""
434 old_build_state = build_summary.BuildSummary(
435 status=constants.BUILDER_STATUS_PASSED,
436 buildroot_layout=2,
437 branch='branchA')
438 self.populateBuildroot(previous_build_state=old_build_state.to_json())
439 self.mock_repo.branch = 'branchA'
440
441 build_state = build_summary.BuildSummary(
442 status=constants.BUILDER_STATUS_INFLIGHT,
443 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
444 branch='branchA')
445 cbuildbot_launch.CleanBuildRoot(
Dhanya Ganesh95c5c152018-10-08 16:48:29 -0600446 self.root, self.mock_repo, build_state)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600447
448 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
449 self.assertEqual(new_summary.buildroot_layout, 2)
450 self.assertEqual(new_summary.branch, 'branchA')
451 self.assertIsNotNone(new_summary.distfiles_ts)
452 self.assertEqual(new_summary, build_state)
453
454 self.assertExists(self.repo)
455 self.assertExists(self.chroot)
456 self.assertExists(self.general)
457 self.assertExists(self.distfiles)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600458 self.assertExists(self.previous_build_state)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700459
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600460 def testBuildrootDistfilesRecentCache(self):
461 """Test CleanBuildRoot does not delete distfiles when cache is recent."""
462 seed_distfiles_ts = time.time() - 60
463 old_build_state = build_summary.BuildSummary(
464 status=constants.BUILDER_STATUS_PASSED,
465 buildroot_layout=2,
466 branch='branchA',
467 distfiles_ts=seed_distfiles_ts)
468 self.populateBuildroot(previous_build_state=old_build_state.to_json())
469 self.mock_repo.branch = 'branchA'
470
471 build_state = build_summary.BuildSummary(
472 status=constants.BUILDER_STATUS_INFLIGHT,
473 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
474 branch='branchA')
475 cbuildbot_launch.CleanBuildRoot(
Dhanya Ganesh95c5c152018-10-08 16:48:29 -0600476 self.root, self.mock_repo, build_state)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600477
478 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
479 self.assertEqual(new_summary.buildroot_layout, 2)
480 self.assertEqual(new_summary.branch, 'branchA')
481 # Same cache creation timestamp is rewritten to state.
482 self.assertEqual(new_summary.distfiles_ts, seed_distfiles_ts)
483 self.assertEqual(new_summary, build_state)
484
485 self.assertExists(self.repo)
486 self.assertExists(self.chroot)
487 self.assertExists(self.general)
488 self.assertExists(self.distfiles)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600489 self.assertExists(self.previous_build_state)
Don Garrette17e1d92017-04-12 15:28:19 -0700490
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600491 def testBuildrootDistfilesCacheExpired(self):
492 """Test CleanBuildRoot when the distfiles cache is too old."""
493 old_build_state = build_summary.BuildSummary(
494 status=constants.BUILDER_STATUS_PASSED,
495 buildroot_layout=2,
496 branch='branchA',
497 distfiles_ts=100.0)
498 self.populateBuildroot(previous_build_state=old_build_state.to_json())
499 self.mock_repo.branch = 'branchA'
500
501 build_state = build_summary.BuildSummary(
502 status=constants.BUILDER_STATUS_INFLIGHT,
503 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
504 branch='branchA')
505 cbuildbot_launch.CleanBuildRoot(
Dhanya Ganesh95c5c152018-10-08 16:48:29 -0600506 self.root, self.mock_repo, build_state)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600507
508 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
509 self.assertEqual(new_summary.buildroot_layout, 2)
510 self.assertEqual(new_summary.branch, 'branchA')
511 self.assertIsNotNone(new_summary.distfiles_ts)
512 self.assertEqual(new_summary, build_state)
513
514 self.assertExists(self.repo)
515 self.assertExists(self.chroot)
516 self.assertExists(self.general)
517 self.assertNotExists(self.distfiles)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600518 self.assertExists(self.previous_build_state)
519
520 def testBuildrootRepoCleanFailure(self):
521 """Test CleanBuildRoot with repo checkout failure."""
522 old_build_state = build_summary.BuildSummary(
523 status=constants.BUILDER_STATUS_PASSED,
524 buildroot_layout=1,
525 branch='branchA')
526 self.populateBuildroot(previous_build_state=old_build_state.to_json())
527 self.mock_repo.branch = 'branchA'
528 self.mock_repo.BuildRootGitCleanup.side_effect = Exception
529
530 build_state = build_summary.BuildSummary(
531 status=constants.BUILDER_STATUS_INFLIGHT,
532 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
533 branch='branchA')
534 cbuildbot_launch.CleanBuildRoot(
Dhanya Ganesh95c5c152018-10-08 16:48:29 -0600535 self.root, self.mock_repo, build_state)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600536
537 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
538 self.assertEqual(new_summary.buildroot_layout, 2)
539 self.assertEqual(new_summary.branch, 'branchA')
540 self.assertIsNotNone(new_summary.distfiles_ts)
541 self.assertEqual(new_summary, build_state)
542
543 self.assertNotExists(self.repo)
544 self.assertNotExists(self.chroot)
545 self.assertNotExists(self.general)
546 self.assertNotExists(self.distfiles)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600547 self.assertExists(self.previous_build_state)
Don Garrettf324bc32017-05-23 14:00:53 -0700548
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600549 def testGetCurrentBuildStateNoArgs(self):
550 """Tests GetCurrentBuildState without arguments."""
551 options = cbuildbot_launch.PreParseArguments([
552 '--buildroot', self.root, 'config'
553 ])
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600554 state = cbuildbot_launch.GetCurrentBuildState(options, 'master')
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600555
556 expected_state = build_summary.BuildSummary(
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600557 status=constants.BUILDER_STATUS_INFLIGHT,
558 buildroot_layout=2,
559 branch='master')
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600560 self.assertEqual(state, expected_state)
561
562 def testGetCurrentBuildStateHasArgs(self):
563 """Tests GetCurrentBuildState with arguments."""
564 options = cbuildbot_launch.PreParseArguments([
565 '--buildroot', self.root,
566 '--buildnumber', '20',
567 '--master-build-id', '50',
568 'config'
569 ])
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600570 state = cbuildbot_launch.GetCurrentBuildState(options, 'branchA')
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600571
572 expected_state = build_summary.BuildSummary(
573 build_number=20,
574 master_build_id=50,
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600575 status=constants.BUILDER_STATUS_INFLIGHT,
576 buildroot_layout=2,
577 branch='branchA')
578 self.assertEqual(state, expected_state)
579
580 def testGetCurrentBuildStateLayout(self):
581 """Test that GetCurrentBuildState uses the current buildroot layout."""
582 # Change to a future version.
583 self.PatchObject(cbuildbot_launch, 'BUILDROOT_BUILDROOT_LAYOUT', 22)
584
585 options = cbuildbot_launch.PreParseArguments([
586 '--buildroot', self.root, 'config'
587 ])
588 state = cbuildbot_launch.GetCurrentBuildState(options, 'branchA')
589
590 expected_state = build_summary.BuildSummary(
591 status=constants.BUILDER_STATUS_INFLIGHT,
592 buildroot_layout=22,
593 branch='branchA')
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600594 self.assertEqual(state, expected_state)
595
596 def testGetLastBuildStateNoFile(self):
597 """Tests GetLastBuildState if the file is missing."""
598 osutils.SafeMakedirs(self.root)
599 state = cbuildbot_launch.GetLastBuildState(self.root)
600 self.assertEqual(state, build_summary.BuildSummary())
601
602 def testGetLastBuildStateBadFile(self):
603 """Tests GetLastBuildState if the file contains invalid JSON."""
604 osutils.SafeMakedirs(self.root)
605 osutils.WriteFile(self.previous_build_state, '}}')
606 state = cbuildbot_launch.GetLastBuildState(self.root)
607 self.assertEqual(state, build_summary.BuildSummary())
608
609 def testGetLastBuildStateMissingBuildStatus(self):
610 """Tests GetLastBuildState if the file doesn't have a valid status."""
611 osutils.SafeMakedirs(self.root)
612 osutils.WriteFile(self.previous_build_state, '{"build_number": "3"}')
613 state = cbuildbot_launch.GetLastBuildState(self.root)
614 self.assertEqual(state, build_summary.BuildSummary())
615
616 def testGetLastBuildStateGoodFile(self):
617 """Tests GetLastBuildState on a good file."""
618 osutils.SafeMakedirs(self.root)
619 osutils.WriteFile(
620 self.previous_build_state,
621 '{"build_number": 1, "master_build_id": 3, "status": "pass"}')
622 state = cbuildbot_launch.GetLastBuildState(self.root)
623 self.assertEqual(
624 state,
625 build_summary.BuildSummary(
626 build_number=1, master_build_id=3, status='pass'))
627
628 def testSetLastBuildState(self):
629 """Verifies that SetLastBuildState writes to the expected file."""
630 osutils.SafeMakedirs(self.root)
631 old_state = build_summary.BuildSummary(
632 build_number=314,
633 master_build_id=2178,
634 status=constants.BUILDER_STATUS_PASSED)
635 cbuildbot_launch.SetLastBuildState(self.root, old_state)
636
637 saved_state = osutils.ReadFile(self.previous_build_state)
638 new_state = build_summary.BuildSummary()
639 new_state.from_json(saved_state)
640
641 self.assertEqual(old_state, new_state)