blob: eed51e0289728005d89cfc06f38f669e9ede6667 [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
Don Garrettc4114cc2016-11-01 20:04:06 -070018from chromite.lib import cros_build_lib_unittest
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
Don Garrett066e6f52017-09-28 19:14:01 -070093class RunDepotToolsEnsureBootstrap(cros_build_lib_unittest.RunCommandTestCase,
94 cros_test_lib.TempDirTestCase):
95 """Test the helper function DepotToolsEnsureBootstrap."""
96
97 def testEnsureBootstrap(self):
98 """Verify that the script is run if present."""
99 script = os.path.join(self.tempdir, 'ensure_bootstrap')
100 osutils.Touch(script, makedirs=True)
101
102 cbuildbot_launch.DepotToolsEnsureBootstrap(self.tempdir)
103 self.assertCommandCalled(
104 [script], extra_env={'PATH': mock.ANY}, cwd=self.tempdir)
105
106 def testEnsureBootstrapMissing(self):
107 """Verify that the script is NOT run if not present."""
108 cbuildbot_launch.DepotToolsEnsureBootstrap(self.tempdir)
109 self.assertEqual(self.rc.call_count, 0)
110
111
Don Garrett597ddff2017-02-17 18:29:37 -0800112class RunTests(cros_build_lib_unittest.RunCommandTestCase):
Don Garrett0c54ed72017-03-03 11:18:57 -0800113 """Tests for cbuildbot_launch script."""
Don Garrett597ddff2017-02-17 18:29:37 -0800114
115 ARGS_BASE = ['--buildroot', '/buildroot']
Don Garrett5cd946b2017-07-20 13:42:20 -0700116 EXPECTED_ARGS_BASE = ['--buildroot', '/cbuildbot_buildroot']
Don Garrett597ddff2017-02-17 18:29:37 -0800117 ARGS_GIT_CACHE = ['--git-cache-dir', '/git-cache']
118 ARGS_CONFIG = ['config']
Don Garrettbf90cdf2017-05-19 15:54:02 -0700119 CMD = ['/cbuildbot_buildroot/chromite/bin/cbuildbot']
Don Garrett597ddff2017-02-17 18:29:37 -0800120
Don Garrett6e5c6b92018-04-06 17:58:49 -0700121 def verifyCbuildbot(self, args, expected_cmd, version):
Don Garrett86881cb2017-02-15 15:41:55 -0800122 """Ensure we invoke cbuildbot correctly."""
Don Garrett597ddff2017-02-17 18:29:37 -0800123 self.PatchObject(
124 cros_build_lib, 'GetTargetChromiteApiVersion', autospec=True,
125 return_value=version)
126
Don Garrett6e5c6b92018-04-06 17:58:49 -0700127 cbuildbot_launch.Cbuildbot('/cbuildbot_buildroot', '/depot_tools', args)
Don Garrett597ddff2017-02-17 18:29:37 -0800128
129 self.assertCommandCalled(
Don Garretta50bf492017-09-28 18:33:02 -0700130 expected_cmd, extra_env={'PATH': mock.ANY},
131 cwd='/cbuildbot_buildroot', error_code_ok=True)
Don Garrett597ddff2017-02-17 18:29:37 -0800132
Don Garrett6e5c6b92018-04-06 17:58:49 -0700133 def testCbuildbotSimple(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800134 """Ensure we invoke cbuildbot correctly."""
Don Garrett6e5c6b92018-04-06 17:58:49 -0700135 self.verifyCbuildbot(
Don Garrett597ddff2017-02-17 18:29:37 -0800136 self.ARGS_BASE + self.ARGS_CONFIG,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700137 self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE,
Don Garrett597ddff2017-02-17 18:29:37 -0800138 (0, 4))
139
Don Garrett6e5c6b92018-04-06 17:58:49 -0700140 def testCbuildbotNotFiltered(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800141 """Ensure we invoke cbuildbot correctly."""
Don Garrett6e5c6b92018-04-06 17:58:49 -0700142 self.verifyCbuildbot(
Don Garrett597ddff2017-02-17 18:29:37 -0800143 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700144 (self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE +
145 self.ARGS_GIT_CACHE),
Don Garrett597ddff2017-02-17 18:29:37 -0800146 (0, 4))
147
Don Garrett6e5c6b92018-04-06 17:58:49 -0700148 def testCbuildbotFiltered(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800149 """Ensure we invoke cbuildbot correctly."""
Don Garrett6e5c6b92018-04-06 17:58:49 -0700150 self.verifyCbuildbot(
Don Garrett597ddff2017-02-17 18:29:37 -0800151 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700152 self.CMD + self.ARGS_CONFIG + self.EXPECTED_ARGS_BASE,
Don Garrett597ddff2017-02-17 18:29:37 -0800153 (0, 2))
Don Garrettc4114cc2016-11-01 20:04:06 -0700154
Don Garrett86881cb2017-02-15 15:41:55 -0800155 def testMainMin(self):
156 """Test a minimal set of command line options."""
Don Garrett597ddff2017-02-17 18:29:37 -0800157 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
158 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
Don Garrett861e9182017-05-15 15:30:23 -0700159 autospec=True, return_value=(constants.REEXEC_API_MAJOR,
160 constants.REEXEC_API_MINOR))
Don Garrettf324bc32017-05-23 14:00:53 -0700161 mock_repo = mock.MagicMock()
162 mock_repo.branch = 'master'
Don Garrettbf90cdf2017-05-19 15:54:02 -0700163 mock_repo.directory = '/root/repository'
164
Don Garrettf324bc32017-05-23 14:00:53 -0700165 mock_repo_create = self.PatchObject(repository, 'RepoRepository',
166 autospec=True, return_value=mock_repo)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700167 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot',
Don Garrett0c54ed72017-03-03 11:18:57 -0800168 autospec=True)
169 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800170 autospec=True)
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700171 mock_cleanup_chroot = self.PatchObject(cbuildbot_launch, 'CleanupChroot',
172 autospec=True)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600173 mock_set_last_build_state = self.PatchObject(
174 cbuildbot_launch, 'SetLastBuildState', autospec=True)
175
176 expected_build_state = build_summary.BuildSummary(
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600177 build_number=0, master_build_id=0, status=mock.ANY,
178 buildroot_layout=2, branch='master')
Don Garrett7ade05a2017-02-17 13:31:47 -0800179
Don Garrettbf90cdf2017-05-19 15:54:02 -0700180 cbuildbot_launch._main(['-r', '/root', 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700181
Don Garrettf324bc32017-05-23 14:00:53 -0700182 # Did we create the repo instance correctly?
183 self.assertEqual(mock_repo_create.mock_calls,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700184 [mock.call(EXPECTED_MANIFEST_URL, '/root/repository',
Don Garrettf324bc32017-05-23 14:00:53 -0700185 git_cache_dir=None, branch='master')])
186
Don Garrett7ade05a2017-02-17 13:31:47 -0800187 # Ensure we clean, as expected.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700188 self.assertEqual(mock_clean.mock_calls, [
Don Garrettd1d90dd2017-06-13 17:35:52 -0700189 mock.call('/root', mock_repo,
190 {
191 'branch_name': 'master',
192 'tryjob': False,
193 'build_config': 'config',
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600194 },
195 expected_build_state)])
Don Garrett7ade05a2017-02-17 13:31:47 -0800196
Don Garrett86881cb2017-02-15 15:41:55 -0800197 # Ensure we checkout, as expected.
198 self.assertEqual(mock_checkout.mock_calls,
Don Garrettf324bc32017-05-23 14:00:53 -0700199 [mock.call(mock_repo)])
Don Garrettc4114cc2016-11-01 20:04:06 -0700200
Don Garrett86881cb2017-02-15 15:41:55 -0800201 # Ensure we invoke cbuildbot, as expected.
202 self.assertCommandCalled(
Don Garrett5cd946b2017-07-20 13:42:20 -0700203 [
204 '/root/repository/chromite/bin/cbuildbot',
205 'config',
206 '-r', '/root/repository',
207 '--ts-mon-task-num', '1',
208 ],
Don Garretta50bf492017-09-28 18:33:02 -0700209 extra_env={'PATH': mock.ANY},
Don Garrettbf90cdf2017-05-19 15:54:02 -0700210 cwd='/root/repository',
Don Garrettacbb2392017-05-11 18:27:41 -0700211 error_code_ok=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700212
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600213 # Ensure we saved the final state, as expected.
214 self.assertEqual(expected_build_state.status,
215 constants.BUILDER_STATUS_PASSED)
216 self.assertEqual(mock_set_last_build_state.mock_calls, [
217 mock.call('/root', expected_build_state)])
218
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700219 # Ensure we clean the chroot, as expected.
220 self.assertEqual(mock_cleanup_chroot.mock_calls, [
221 mock.call('/root/repository')])
222
Don Garrett86881cb2017-02-15 15:41:55 -0800223 def testMainMax(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800224 """Test a larger set of command line options."""
225 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
226 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
Don Garrett861e9182017-05-15 15:30:23 -0700227 autospec=True, return_value=(constants.REEXEC_API_MAJOR,
228 constants.REEXEC_API_MINOR))
Don Garrettf324bc32017-05-23 14:00:53 -0700229 mock_repo = mock.MagicMock()
230 mock_repo.branch = 'branch'
Don Garrettbf90cdf2017-05-19 15:54:02 -0700231 mock_repo.directory = '/root/repository'
232
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600233 mock_summary = build_summary.BuildSummary(
234 build_number=313,
235 master_build_id=123123123,
236 status=constants.BUILDER_STATUS_FAILED,
237 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
238 branch='branch')
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600239
240 mock_get_last_build_state = self.PatchObject(
241 cbuildbot_launch, 'GetLastBuildState', autospec=True,
242 return_value=mock_summary)
Don Garrettf324bc32017-05-23 14:00:53 -0700243 mock_repo_create = self.PatchObject(repository, 'RepoRepository',
244 autospec=True, return_value=mock_repo)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700245 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildRoot',
Don Garrett0c54ed72017-03-03 11:18:57 -0800246 autospec=True)
247 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800248 autospec=True)
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700249 mock_cleanup_chroot = self.PatchObject(cbuildbot_launch, 'CleanupChroot',
250 autospec=True)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600251 mock_set_last_build_state = self.PatchObject(
252 cbuildbot_launch, 'SetLastBuildState', autospec=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700253
Don Garrettbf90cdf2017-05-19 15:54:02 -0700254 cbuildbot_launch._main(['--buildroot', '/root',
Don Garrettacbb2392017-05-11 18:27:41 -0700255 '--branch', 'branch',
256 '--git-cache-dir', '/git-cache',
Don Garrettd1d90dd2017-06-13 17:35:52 -0700257 '--remote-trybot',
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600258 '--master-build-id', '123456789',
259 '--buildnumber', '314',
Don Garrettacbb2392017-05-11 18:27:41 -0700260 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700261
Don Garrettf324bc32017-05-23 14:00:53 -0700262 # Did we create the repo instance correctly?
263 self.assertEqual(mock_repo_create.mock_calls,
Don Garrettbf90cdf2017-05-19 15:54:02 -0700264 [mock.call(EXPECTED_MANIFEST_URL, '/root/repository',
Don Garrettf324bc32017-05-23 14:00:53 -0700265 git_cache_dir='/git-cache', branch='branch')])
266
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600267 # Ensure we look up the previous status.
268 self.assertEqual(mock_get_last_build_state.mock_calls, [
269 mock.call('/root')])
270
Don Garrett7ade05a2017-02-17 13:31:47 -0800271 # Ensure we clean, as expected.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700272 self.assertEqual(mock_clean.mock_calls, [
Don Garrettd1d90dd2017-06-13 17:35:52 -0700273 mock.call('/root',
274 mock_repo,
275 {
276 'branch_name': 'branch',
277 'tryjob': True,
278 'build_config': 'config',
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600279 },
280 build_summary.BuildSummary(
281 build_number=314,
282 master_build_id=123456789,
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600283 status=mock.ANY,
284 branch='branch',
285 buildroot_layout=2
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600286 ))])
Don Garrett7ade05a2017-02-17 13:31:47 -0800287
Don Garrett86881cb2017-02-15 15:41:55 -0800288 # Ensure we checkout, as expected.
289 self.assertEqual(mock_checkout.mock_calls,
Don Garrettf324bc32017-05-23 14:00:53 -0700290 [mock.call(mock_repo)])
Don Garrett86881cb2017-02-15 15:41:55 -0800291
292 # Ensure we invoke cbuildbot, as expected.
293 self.assertCommandCalled(
Don Garrett5cd946b2017-07-20 13:42:20 -0700294 [
295 '/root/repository/chromite/bin/cbuildbot',
296 'config',
297 '--buildroot', '/root/repository',
298 '--branch', 'branch',
299 '--git-cache-dir', '/git-cache',
300 '--remote-trybot',
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600301 '--master-build-id', '123456789',
302 '--buildnumber', '314',
303 '--previous-build-state',
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600304 'eyJzdGF0dXMiOiAiZmFpbCIsICJtYXN0ZXJfYnVpbGRfaWQiOiAxMjMxMjMxMj'
305 'MsICJidWlsZF9udW1iZXIiOiAzMTMsICJidWlsZHJvb3RfbGF5b3V0IjogMiwg'
306 'ImJyYW5jaCI6ICJicmFuY2gifQ==',
Don Garrett5cd946b2017-07-20 13:42:20 -0700307 '--ts-mon-task-num', '1',
308 ],
Don Garretta50bf492017-09-28 18:33:02 -0700309 extra_env={'PATH': mock.ANY},
Don Garrettbf90cdf2017-05-19 15:54:02 -0700310 cwd='/root/repository',
Don Garrettacbb2392017-05-11 18:27:41 -0700311 error_code_ok=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800312
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600313 # Ensure we write the final build state, as expected.
314 final_state = build_summary.BuildSummary(
315 build_number=314,
316 master_build_id=123456789,
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600317 status=constants.BUILDER_STATUS_PASSED,
318 buildroot_layout=2,
319 branch='branch')
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600320 self.assertEqual(mock_set_last_build_state.mock_calls, [
321 mock.call('/root', final_state)])
322
Benjamin Gordonaee36b82018-02-05 14:25:26 -0700323 # Ensure we clean the chroot, as expected.
324 self.assertEqual(mock_cleanup_chroot.mock_calls, [
325 mock.call('/root/repository')])
326
Don Garrett7ade05a2017-02-17 13:31:47 -0800327
Don Garrettbf90cdf2017-05-19 15:54:02 -0700328class CleanBuildRootTest(cros_test_lib.MockTempDirTestCase):
329 """Tests for CleanBuildRoot method."""
Don Garrett7ade05a2017-02-17 13:31:47 -0800330
331 def setUp(self):
332 """Create standard buildroot contents for cleanup."""
Don Garrettbf90cdf2017-05-19 15:54:02 -0700333 self.root = os.path.join(self.tempdir)
Don Garrette17e1d92017-04-12 15:28:19 -0700334 self.state = os.path.join(self.root, '.cbuildbot_launch_state')
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600335 self.previous_build_state = os.path.join(
336 self.root, '.cbuildbot_build_state.json')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700337 self.buildroot = os.path.join(self.root, 'buildroot')
338 self.repo = os.path.join(self.buildroot, '.repo/repo')
339 self.chroot = os.path.join(self.buildroot, 'chroot/chroot')
340 self.general = os.path.join(self.buildroot, 'general/general')
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700341 self.cache = os.path.join(self.buildroot, '.cache')
342 self.distfiles = os.path.join(self.cache, 'distfiles')
Don Garrett7ade05a2017-02-17 13:31:47 -0800343
Don Garrettf324bc32017-05-23 14:00:53 -0700344 self.mock_repo = mock.MagicMock()
Don Garrettbf90cdf2017-05-19 15:54:02 -0700345 self.mock_repo.directory = self.buildroot
Don Garrettf324bc32017-05-23 14:00:53 -0700346
Don Garrettacbb2392017-05-11 18:27:41 -0700347 self.metrics = {}
348
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600349 def populateBuildroot(self, state=None, previous_build_state=None):
Don Garrett7ade05a2017-02-17 13:31:47 -0800350 """Create standard buildroot contents for cleanup."""
351 if state:
Don Garrettbf90cdf2017-05-19 15:54:02 -0700352 osutils.SafeMakedirs(self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800353 osutils.WriteFile(self.state, state)
354
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600355 if previous_build_state:
356 osutils.SafeMakedirs(self.root)
357 osutils.WriteFile(self.previous_build_state, previous_build_state)
358
Don Garrett7ade05a2017-02-17 13:31:47 -0800359 # Create files.
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700360 for f in (self.repo, self.chroot, self.general, self.distfiles):
Don Garrette17e1d92017-04-12 15:28:19 -0700361 osutils.Touch(f, makedirs=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800362
Don Garrette17e1d92017-04-12 15:28:19 -0700363 def testNoBuildroot(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700364 """Test CleanBuildRoot with no history."""
Don Garrettf324bc32017-05-23 14:00:53 -0700365 self.mock_repo.branch = 'master'
366
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600367 build_state = build_summary.BuildSummary(
368 status=constants.BUILDER_STATUS_INFLIGHT,
369 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
370 branch='master')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700371 cbuildbot_launch.CleanBuildRoot(
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600372 self.root, self.mock_repo, self.metrics, build_state)
Don Garrett7ade05a2017-02-17 13:31:47 -0800373
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600374 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600375 self.assertEqual(new_summary.buildroot_layout, 2)
376 self.assertEqual(new_summary.branch, 'master')
377 self.assertIsNotNone(new_summary.distfiles_ts)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600378 self.assertEqual(new_summary, build_state)
Don Garrett7ade05a2017-02-17 13:31:47 -0800379
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600380 self.assertNotExists(self.state)
381 self.assertExists(self.previous_build_state)
382
Don Garrett7ade05a2017-02-17 13:31:47 -0800383 def testBuildrootNoState(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700384 """Test CleanBuildRoot with no state information."""
Don Garrett7ade05a2017-02-17 13:31:47 -0800385 self.populateBuildroot()
Don Garrettf324bc32017-05-23 14:00:53 -0700386 self.mock_repo.branch = 'master'
Don Garrett7ade05a2017-02-17 13:31:47 -0800387
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600388 build_state = build_summary.BuildSummary(
389 status=constants.BUILDER_STATUS_INFLIGHT,
390 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
391 branch='master')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700392 cbuildbot_launch.CleanBuildRoot(
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600393 self.root, self.mock_repo, self.metrics, build_state)
Don Garrett7ade05a2017-02-17 13:31:47 -0800394
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600395 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600396 self.assertEqual(new_summary.buildroot_layout, 2)
397 self.assertEqual(new_summary.branch, 'master')
398 self.assertIsNotNone(new_summary.distfiles_ts)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600399 self.assertEqual(new_summary, build_state)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700400
Don Garrett60967922017-04-12 18:51:44 -0700401 self.assertNotExists(self.repo)
Don Garrett7ade05a2017-02-17 13:31:47 -0800402 self.assertNotExists(self.chroot)
Don Garrett60967922017-04-12 18:51:44 -0700403 self.assertNotExists(self.general)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700404 self.assertNotExists(self.distfiles)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600405 self.assertNotExists(self.state)
406 self.assertExists(self.previous_build_state)
Don Garrett60967922017-04-12 18:51:44 -0700407
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600408 def testBuildrootFormatMismatchMigration(self):
409 """Test CleanBuildRoot with format mismatch migrated from old data."""
Don Garrett125d4dc2017-04-25 16:26:03 -0700410 self.populateBuildroot('0 master')
Don Garrettf324bc32017-05-23 14:00:53 -0700411 self.mock_repo.branch = 'master'
Don Garrett60967922017-04-12 18:51:44 -0700412
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='master')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700417 cbuildbot_launch.CleanBuildRoot(
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600418 self.root, self.mock_repo, self.metrics, build_state)
Don Garrett60967922017-04-12 18:51:44 -0700419
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, 'master')
423 self.assertIsNotNone(new_summary.distfiles_ts)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600424 self.assertEqual(new_summary, build_state)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700425
Don Garrett60967922017-04-12 18:51:44 -0700426 self.assertNotExists(self.repo)
427 self.assertNotExists(self.chroot)
428 self.assertNotExists(self.general)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700429 self.assertNotExists(self.distfiles)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600430 self.assertNotExists(self.state)
431 self.assertExists(self.previous_build_state)
Don Garrett7ade05a2017-02-17 13:31:47 -0800432
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600433 def testBuildrootFormatMismatch(self):
434 """Test CleanBuildRoot with buildroot layout mismatch."""
435 old_build_state = build_summary.BuildSummary(
436 status=constants.BUILDER_STATUS_PASSED,
437 buildroot_layout=1,
438 branch='master')
439 self.populateBuildroot(previous_build_state=old_build_state.to_json())
440 self.mock_repo.branch = 'master'
441
442 build_state = build_summary.BuildSummary(
443 status=constants.BUILDER_STATUS_INFLIGHT,
444 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
445 branch='master')
446 cbuildbot_launch.CleanBuildRoot(
447 self.root, self.mock_repo, self.metrics, build_state)
448
449 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
450 self.assertEqual(new_summary.buildroot_layout, 2)
451 self.assertEqual(new_summary.branch, 'master')
452 self.assertIsNotNone(new_summary.distfiles_ts)
453 self.assertEqual(new_summary, build_state)
454
455 self.assertNotExists(self.repo)
456 self.assertNotExists(self.chroot)
457 self.assertNotExists(self.general)
458 self.assertNotExists(self.distfiles)
459 self.assertNotExists(self.state)
460 self.assertExists(self.previous_build_state)
461
462 def testBuildrootBranchChangeMigration(self):
463 """Test CleanBuildRoot with a change in branches migrated from old state."""
Don Garrettbf90cdf2017-05-19 15:54:02 -0700464 self.populateBuildroot('2 branchA')
Don Garrettf324bc32017-05-23 14:00:53 -0700465 self.mock_repo.branch = 'branchB'
Benjamin Gordon59ba2f82017-08-28 15:31:06 -0600466 m = self.PatchObject(cros_build_lib, 'CleanupChrootMount')
Don Garrett7ade05a2017-02-17 13:31:47 -0800467
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600468 build_state = build_summary.BuildSummary(
469 status=constants.BUILDER_STATUS_INFLIGHT,
470 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
471 branch='branchB')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700472 cbuildbot_launch.CleanBuildRoot(
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600473 self.root, self.mock_repo, self.metrics, build_state)
Don Garrett7ade05a2017-02-17 13:31:47 -0800474
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600475 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600476 self.assertEqual(new_summary.buildroot_layout, 2)
477 self.assertEqual(new_summary.branch, 'branchB')
478 self.assertIsNotNone(new_summary.distfiles_ts)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600479 self.assertEqual(new_summary, build_state)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700480
Don Garrett7ade05a2017-02-17 13:31:47 -0800481 self.assertExists(self.repo)
482 self.assertNotExists(self.chroot)
483 self.assertExists(self.general)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700484 self.assertNotExists(self.distfiles)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600485 self.assertNotExists(self.state)
486 self.assertExists(self.previous_build_state)
Benjamin Gordon59ba2f82017-08-28 15:31:06 -0600487 m.assert_called()
Don Garrett7ade05a2017-02-17 13:31:47 -0800488
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600489 def testBuildrootBranchChange(self):
490 """Test CleanBuildRoot with a change in branches."""
491 old_build_state = build_summary.BuildSummary(
492 status=constants.BUILDER_STATUS_PASSED,
493 buildroot_layout=2,
494 branch='branchA')
495 self.populateBuildroot(previous_build_state=old_build_state.to_json())
496 self.mock_repo.branch = 'branchB'
497 m = self.PatchObject(cros_build_lib, 'CleanupChrootMount')
Don Garrett7ade05a2017-02-17 13:31:47 -0800498
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600499 build_state = build_summary.BuildSummary(
500 status=constants.BUILDER_STATUS_INFLIGHT,
501 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
502 branch='branchB')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700503 cbuildbot_launch.CleanBuildRoot(
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600504 self.root, self.mock_repo, self.metrics, build_state)
Don Garrett7ade05a2017-02-17 13:31:47 -0800505
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600506 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600507 self.assertEqual(new_summary.buildroot_layout, 2)
508 self.assertEqual(new_summary.branch, 'branchB')
509 self.assertIsNotNone(new_summary.distfiles_ts)
510 self.assertEqual(new_summary, build_state)
511
512 self.assertExists(self.repo)
513 self.assertNotExists(self.chroot)
514 self.assertExists(self.general)
515 self.assertNotExists(self.distfiles)
516 self.assertNotExists(self.state)
517 self.assertExists(self.previous_build_state)
518 m.assert_called()
519
520 def testBuildrootBranchMatchMigration(self):
521 """Test CleanBuildRoot with no change in branch migrated from old state."""
522 self.populateBuildroot('2 branchA')
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.metrics, build_state)
531
532 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
533 self.assertEqual(new_summary.buildroot_layout, 2)
534 self.assertEqual(new_summary.branch, 'branchA')
535 self.assertIsNotNone(new_summary.distfiles_ts)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600536 self.assertEqual(new_summary, build_state)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700537
Don Garrett7ade05a2017-02-17 13:31:47 -0800538 self.assertExists(self.repo)
539 self.assertExists(self.chroot)
540 self.assertExists(self.general)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700541 self.assertExists(self.distfiles)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600542 self.assertNotExists(self.state)
543 self.assertExists(self.previous_build_state)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700544
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600545 def testBuildrootBranchMatch(self):
546 """Test CleanBuildRoot with no change in branch."""
547 old_build_state = build_summary.BuildSummary(
548 status=constants.BUILDER_STATUS_PASSED,
549 buildroot_layout=2,
550 branch='branchA')
551 self.populateBuildroot(previous_build_state=old_build_state.to_json())
552 self.mock_repo.branch = 'branchA'
553
554 build_state = build_summary.BuildSummary(
555 status=constants.BUILDER_STATUS_INFLIGHT,
556 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
557 branch='branchA')
558 cbuildbot_launch.CleanBuildRoot(
559 self.root, self.mock_repo, self.metrics, build_state)
560
561 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
562 self.assertEqual(new_summary.buildroot_layout, 2)
563 self.assertEqual(new_summary.branch, 'branchA')
564 self.assertIsNotNone(new_summary.distfiles_ts)
565 self.assertEqual(new_summary, build_state)
566
567 self.assertExists(self.repo)
568 self.assertExists(self.chroot)
569 self.assertExists(self.general)
570 self.assertExists(self.distfiles)
571 self.assertNotExists(self.state)
572 self.assertExists(self.previous_build_state)
573
574 def testBuildrootDistfilesRecentCacheMigration(self):
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700575 """Test CleanBuildRoot does not delete distfiles when cache is recent."""
576 seed_distfiles_ts = time.time() - 60
577 self.populateBuildroot('2 branchA %f' % seed_distfiles_ts)
578 self.mock_repo.branch = 'branchA'
579
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600580 build_state = build_summary.BuildSummary(
581 status=constants.BUILDER_STATUS_INFLIGHT,
582 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
583 branch='branchA')
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700584 cbuildbot_launch.CleanBuildRoot(
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600585 self.root, self.mock_repo, self.metrics, build_state)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700586
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600587 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600588 self.assertEqual(new_summary.buildroot_layout, 2)
589 self.assertEqual(new_summary.branch, 'branchA')
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700590 # Same cache creation timestamp is rewritten to state.
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600591 self.assertEqual(new_summary.distfiles_ts, seed_distfiles_ts)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600592 self.assertEqual(new_summary, build_state)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700593
594 self.assertExists(self.repo)
595 self.assertExists(self.chroot)
596 self.assertExists(self.general)
597 self.assertExists(self.distfiles)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600598 self.assertNotExists(self.state)
599 self.assertExists(self.previous_build_state)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700600
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600601 def testBuildrootDistfilesRecentCache(self):
602 """Test CleanBuildRoot does not delete distfiles when cache is recent."""
603 seed_distfiles_ts = time.time() - 60
604 old_build_state = build_summary.BuildSummary(
605 status=constants.BUILDER_STATUS_PASSED,
606 buildroot_layout=2,
607 branch='branchA',
608 distfiles_ts=seed_distfiles_ts)
609 self.populateBuildroot(previous_build_state=old_build_state.to_json())
610 self.mock_repo.branch = 'branchA'
611
612 build_state = build_summary.BuildSummary(
613 status=constants.BUILDER_STATUS_INFLIGHT,
614 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
615 branch='branchA')
616 cbuildbot_launch.CleanBuildRoot(
617 self.root, self.mock_repo, self.metrics, build_state)
618
619 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
620 self.assertEqual(new_summary.buildroot_layout, 2)
621 self.assertEqual(new_summary.branch, 'branchA')
622 # Same cache creation timestamp is rewritten to state.
623 self.assertEqual(new_summary.distfiles_ts, seed_distfiles_ts)
624 self.assertEqual(new_summary, build_state)
625
626 self.assertExists(self.repo)
627 self.assertExists(self.chroot)
628 self.assertExists(self.general)
629 self.assertExists(self.distfiles)
630 self.assertNotExists(self.state)
631 self.assertExists(self.previous_build_state)
632
633 def testBuildrootDistfilesCacheExpiredMigration(self):
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700634 """Test CleanBuildRoot when the distfiles cache is too old."""
635 self.populateBuildroot('2 branchA 100.000000')
636 self.mock_repo.branch = 'branchA'
637
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600638 build_state = build_summary.BuildSummary(
639 status=constants.BUILDER_STATUS_INFLIGHT,
640 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
641 branch='branchA')
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700642 cbuildbot_launch.CleanBuildRoot(
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600643 self.root, self.mock_repo, self.metrics, build_state)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700644
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600645 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600646 self.assertEqual(new_summary.buildroot_layout, 2)
647 self.assertEqual(new_summary.branch, 'branchA')
648 self.assertIsNotNone(new_summary.distfiles_ts)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600649 self.assertEqual(new_summary, build_state)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700650
651 self.assertExists(self.repo)
652 self.assertExists(self.chroot)
653 self.assertExists(self.general)
654 self.assertNotExists(self.distfiles)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600655 self.assertNotExists(self.state)
656 self.assertExists(self.previous_build_state)
Don Garrette17e1d92017-04-12 15:28:19 -0700657
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600658 def testBuildrootDistfilesCacheExpired(self):
659 """Test CleanBuildRoot when the distfiles cache is too old."""
660 old_build_state = build_summary.BuildSummary(
661 status=constants.BUILDER_STATUS_PASSED,
662 buildroot_layout=2,
663 branch='branchA',
664 distfiles_ts=100.0)
665 self.populateBuildroot(previous_build_state=old_build_state.to_json())
666 self.mock_repo.branch = 'branchA'
667
668 build_state = build_summary.BuildSummary(
669 status=constants.BUILDER_STATUS_INFLIGHT,
670 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
671 branch='branchA')
672 cbuildbot_launch.CleanBuildRoot(
673 self.root, self.mock_repo, self.metrics, build_state)
674
675 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
676 self.assertEqual(new_summary.buildroot_layout, 2)
677 self.assertEqual(new_summary.branch, 'branchA')
678 self.assertIsNotNone(new_summary.distfiles_ts)
679 self.assertEqual(new_summary, build_state)
680
681 self.assertExists(self.repo)
682 self.assertExists(self.chroot)
683 self.assertExists(self.general)
684 self.assertNotExists(self.distfiles)
685 self.assertNotExists(self.state)
686 self.assertExists(self.previous_build_state)
687
688 def testBuildrootRepoCleanFailureMigration(self):
Don Garrettbf90cdf2017-05-19 15:54:02 -0700689 """Test CleanBuildRoot with repo checkout failure."""
Don Garrettf324bc32017-05-23 14:00:53 -0700690 self.populateBuildroot('1 branchA')
691 self.mock_repo.branch = 'branchA'
692 self.mock_repo.BuildRootGitCleanup.side_effect = Exception
693
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600694 build_state = build_summary.BuildSummary(
695 status=constants.BUILDER_STATUS_INFLIGHT,
696 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
697 branch='branchA')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700698 cbuildbot_launch.CleanBuildRoot(
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600699 self.root, self.mock_repo, self.metrics, build_state)
Don Garrettf324bc32017-05-23 14:00:53 -0700700
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600701 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600702 self.assertEqual(new_summary.buildroot_layout, 2)
703 self.assertEqual(new_summary.branch, 'branchA')
704 self.assertIsNotNone(new_summary.distfiles_ts)
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600705 self.assertEqual(new_summary, build_state)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700706
Don Garrettf324bc32017-05-23 14:00:53 -0700707 self.assertNotExists(self.repo)
708 self.assertNotExists(self.chroot)
709 self.assertNotExists(self.general)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700710 self.assertNotExists(self.distfiles)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600711 self.assertNotExists(self.state)
712 self.assertExists(self.previous_build_state)
713
714 def testBuildrootRepoCleanFailure(self):
715 """Test CleanBuildRoot with repo checkout failure."""
716 old_build_state = build_summary.BuildSummary(
717 status=constants.BUILDER_STATUS_PASSED,
718 buildroot_layout=1,
719 branch='branchA')
720 self.populateBuildroot(previous_build_state=old_build_state.to_json())
721 self.mock_repo.branch = 'branchA'
722 self.mock_repo.BuildRootGitCleanup.side_effect = Exception
723
724 build_state = build_summary.BuildSummary(
725 status=constants.BUILDER_STATUS_INFLIGHT,
726 buildroot_layout=cbuildbot_launch.BUILDROOT_BUILDROOT_LAYOUT,
727 branch='branchA')
728 cbuildbot_launch.CleanBuildRoot(
729 self.root, self.mock_repo, self.metrics, build_state)
730
731 new_summary = cbuildbot_launch.GetLastBuildState(self.root)
732 self.assertEqual(new_summary.buildroot_layout, 2)
733 self.assertEqual(new_summary.branch, 'branchA')
734 self.assertIsNotNone(new_summary.distfiles_ts)
735 self.assertEqual(new_summary, build_state)
736
737 self.assertNotExists(self.repo)
738 self.assertNotExists(self.chroot)
739 self.assertNotExists(self.general)
740 self.assertNotExists(self.distfiles)
741 self.assertNotExists(self.state)
742 self.assertExists(self.previous_build_state)
Don Garrettf324bc32017-05-23 14:00:53 -0700743
Don Garrettbf90cdf2017-05-19 15:54:02 -0700744 def testGetState(self):
745 """Test GetState."""
Don Garrett60967922017-04-12 18:51:44 -0700746 # No root dir.
Don Garrettbf90cdf2017-05-19 15:54:02 -0700747 results = cbuildbot_launch.GetState(self.root)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700748 self.assertEqual(results, (0, '', None))
Don Garrett60967922017-04-12 18:51:44 -0700749
750 # Empty root dir.
751 osutils.SafeMakedirs(self.root)
Don Garrettbf90cdf2017-05-19 15:54:02 -0700752 results = cbuildbot_launch.GetState(self.root)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700753 self.assertEqual(results, (0, '', None))
Don Garrett60967922017-04-12 18:51:44 -0700754
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700755 # Empty contents
Don Garrett60967922017-04-12 18:51:44 -0700756 osutils.WriteFile(self.state, '')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700757 results = cbuildbot_launch.GetState(self.root)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700758 self.assertEqual(results, (0, '', None))
Don Garrett60967922017-04-12 18:51:44 -0700759
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700760 # Old format contents
Don Garrett60967922017-04-12 18:51:44 -0700761 osutils.WriteFile(self.state, 'happy-branch')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700762 results = cbuildbot_launch.GetState(self.root)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700763 self.assertEqual(results, (0, '', None))
Don Garrett60967922017-04-12 18:51:44 -0700764
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700765 # Expected contents, without distfiles timestamp
Don Garrett60967922017-04-12 18:51:44 -0700766 osutils.WriteFile(self.state, '1 happy-branch')
Don Garrettbf90cdf2017-05-19 15:54:02 -0700767 results = cbuildbot_launch.GetState(self.root)
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700768 self.assertEqual(results, (1, 'happy-branch', None))
Don Garrett60967922017-04-12 18:51:44 -0700769
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700770 # Expected contents
771 osutils.WriteFile(self.state, '1 happy-branch 1000.33')
772 version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root)
773 self.assertEqual(version, 1)
774 self.assertEqual(branch, 'happy-branch')
775 self.assertEqual(distfiles_ts, 1000.33)
Don Garrett60967922017-04-12 18:51:44 -0700776
Prathmesh Prabhuc41a0f52018-04-03 13:26:52 -0700777 # Future layout version contents
778 osutils.WriteFile(self.state, '22 happy-branch 222')
779 version, branch, distfiles_ts = cbuildbot_launch.GetState(self.root)
780 self.assertEqual(version, 22)
781 self.assertEqual(branch, 'happy-branch')
782 self.assertEqual(distfiles_ts, 222)
783
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600784 def testGetCurrentBuildStateNoArgs(self):
785 """Tests GetCurrentBuildState without arguments."""
786 options = cbuildbot_launch.PreParseArguments([
787 '--buildroot', self.root, 'config'
788 ])
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600789 state = cbuildbot_launch.GetCurrentBuildState(options, 'master')
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600790
791 expected_state = build_summary.BuildSummary(
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600792 status=constants.BUILDER_STATUS_INFLIGHT,
793 buildroot_layout=2,
794 branch='master')
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600795 self.assertEqual(state, expected_state)
796
797 def testGetCurrentBuildStateHasArgs(self):
798 """Tests GetCurrentBuildState with arguments."""
799 options = cbuildbot_launch.PreParseArguments([
800 '--buildroot', self.root,
801 '--buildnumber', '20',
802 '--master-build-id', '50',
803 'config'
804 ])
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600805 state = cbuildbot_launch.GetCurrentBuildState(options, 'branchA')
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600806
807 expected_state = build_summary.BuildSummary(
808 build_number=20,
809 master_build_id=50,
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600810 status=constants.BUILDER_STATUS_INFLIGHT,
811 buildroot_layout=2,
812 branch='branchA')
813 self.assertEqual(state, expected_state)
814
815 def testGetCurrentBuildStateLayout(self):
816 """Test that GetCurrentBuildState uses the current buildroot layout."""
817 # Change to a future version.
818 self.PatchObject(cbuildbot_launch, 'BUILDROOT_BUILDROOT_LAYOUT', 22)
819
820 options = cbuildbot_launch.PreParseArguments([
821 '--buildroot', self.root, 'config'
822 ])
823 state = cbuildbot_launch.GetCurrentBuildState(options, 'branchA')
824
825 expected_state = build_summary.BuildSummary(
826 status=constants.BUILDER_STATUS_INFLIGHT,
827 buildroot_layout=22,
828 branch='branchA')
Benjamin Gordon90b2dd92018-04-12 14:04:21 -0600829 self.assertEqual(state, expected_state)
830
831 def testGetLastBuildStateNoFile(self):
832 """Tests GetLastBuildState if the file is missing."""
833 osutils.SafeMakedirs(self.root)
834 state = cbuildbot_launch.GetLastBuildState(self.root)
835 self.assertEqual(state, build_summary.BuildSummary())
836
837 def testGetLastBuildStateBadFile(self):
838 """Tests GetLastBuildState if the file contains invalid JSON."""
839 osutils.SafeMakedirs(self.root)
840 osutils.WriteFile(self.previous_build_state, '}}')
841 state = cbuildbot_launch.GetLastBuildState(self.root)
842 self.assertEqual(state, build_summary.BuildSummary())
843
844 def testGetLastBuildStateMissingBuildStatus(self):
845 """Tests GetLastBuildState if the file doesn't have a valid status."""
846 osutils.SafeMakedirs(self.root)
847 osutils.WriteFile(self.previous_build_state, '{"build_number": "3"}')
848 state = cbuildbot_launch.GetLastBuildState(self.root)
849 self.assertEqual(state, build_summary.BuildSummary())
850
851 def testGetLastBuildStateGoodFile(self):
852 """Tests GetLastBuildState on a good file."""
853 osutils.SafeMakedirs(self.root)
854 osutils.WriteFile(
855 self.previous_build_state,
856 '{"build_number": 1, "master_build_id": 3, "status": "pass"}')
857 state = cbuildbot_launch.GetLastBuildState(self.root)
858 self.assertEqual(
859 state,
860 build_summary.BuildSummary(
861 build_number=1, master_build_id=3, status='pass'))
862
863 def testSetLastBuildState(self):
864 """Verifies that SetLastBuildState writes to the expected file."""
865 osutils.SafeMakedirs(self.root)
866 old_state = build_summary.BuildSummary(
867 build_number=314,
868 master_build_id=2178,
869 status=constants.BUILDER_STATUS_PASSED)
870 cbuildbot_launch.SetLastBuildState(self.root, old_state)
871
872 saved_state = osutils.ReadFile(self.previous_build_state)
873 new_state = build_summary.BuildSummary()
874 new_state.from_json(saved_state)
875
876 self.assertEqual(old_state, new_state)
Benjamin Gordon8b6d4122018-04-26 13:38:39 -0600877
878 def testSetLastBuildStateMigration(self):
879 """Verifies that SetLastBuildState deletes the old state file."""
880 osutils.SafeMakedirs(self.root)
881 osutils.WriteFile(self.state, '1 happy-branch')
882 state = build_summary.BuildSummary(
883 build_number=314,
884 master_build_id=2178,
885 status=constants.BUILDER_STATUS_PASSED)
886 cbuildbot_launch.SetLastBuildState(self.root, state)
887
888 self.assertExists(self.previous_build_state)
889 self.assertNotExists(self.state)