blob: e9b46c2ec18d73bd2db9e1c91d50d3d13503f69c [file] [log] [blame]
Don Garrettc4114cc2016-11-01 20:04:06 -07001# Copyright 2016 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Unit tests for chromite.lib.git and helpers for testing that module."""
6
7from __future__ import print_function
8
Don Garrett86881cb2017-02-15 15:41:55 -08009import mock
Don Garrett7ade05a2017-02-17 13:31:47 -080010import os
Don Garrett86881cb2017-02-15 15:41:55 -080011
12from chromite.cbuildbot import repository
Don Garrett597ddff2017-02-17 18:29:37 -080013from chromite.lib import cros_build_lib
Don Garrettc4114cc2016-11-01 20:04:06 -070014from chromite.lib import cros_build_lib_unittest
Don Garrett7ade05a2017-02-17 13:31:47 -080015from chromite.lib import cros_test_lib
Don Garrett86881cb2017-02-15 15:41:55 -080016from chromite.lib import osutils
Don Garrett0c54ed72017-03-03 11:18:57 -080017from chromite.scripts import cbuildbot_launch
Don Garrett86881cb2017-02-15 15:41:55 -080018
Don Garrett60967922017-04-12 18:51:44 -070019
Don Garrett86881cb2017-02-15 15:41:55 -080020EXPECTED_MANIFEST_URL = 'https://chrome-internal-review.googlesource.com/chromeos/manifest-internal' # pylint: disable=line-too-long
Don Garrettc4114cc2016-11-01 20:04:06 -070021
22
Don Garrett8d314792017-05-18 13:11:42 -070023class FakeException(Exception):
24 """Test exception to raise during tests."""
25
26
Don Garrett0c54ed72017-03-03 11:18:57 -080027class CbuildbotLaunchTest(cros_test_lib.MockTestCase):
28 """Tests for cbuildbot_launch script."""
Don Garrettc4114cc2016-11-01 20:04:06 -070029
Don Garrett86881cb2017-02-15 15:41:55 -080030 def testPreParseArguments(self):
Don Garrettc4114cc2016-11-01 20:04:06 -070031 """Test that we can correctly extract branch values from cbuildbot args."""
Don Garrett597ddff2017-02-17 18:29:37 -080032 CASES = (
33 (['--buildroot', '/buildroot', 'daisy-incremental'],
34 (None, '/buildroot', None)),
35
36 (['--buildbot', '--buildroot', '/buildroot',
37 '--git-cache-dir', '/git-cache',
38 '-b', 'release-R57-9202.B',
39 'daisy-incremental'],
40 ('release-R57-9202.B', '/buildroot', '/git-cache')),
41
42 (['--debug', '--buildbot', '--notests',
43 '--buildroot', '/buildroot',
44 '--git-cache-dir', '/git-cache',
45 '--branch', 'release-R57-9202.B',
46 'daisy-incremental'],
47 ('release-R57-9202.B', '/buildroot', '/git-cache')),
Don Garrettc4114cc2016-11-01 20:04:06 -070048 )
49
Don Garrett597ddff2017-02-17 18:29:37 -080050 for cmd_args, expected in CASES:
51 expected_branch, expected_buildroot, expected_cache_dir = expected
52
Don Garrett0c54ed72017-03-03 11:18:57 -080053 options = cbuildbot_launch.PreParseArguments(cmd_args)
Don Garrett597ddff2017-02-17 18:29:37 -080054
55 self.assertEqual(options.branch, expected_branch)
56 self.assertEqual(options.buildroot, expected_buildroot)
57 self.assertEqual(options.git_cache_dir, expected_cache_dir)
Don Garrett86881cb2017-02-15 15:41:55 -080058
59 def testInitialCheckoutMin(self):
60 """Test InitialCheckout with minimum settings."""
61 mock_repo = self.PatchObject(repository, 'RepoRepository', autospec=True)
Don Garrett86881cb2017-02-15 15:41:55 -080062
Don Garrett0c54ed72017-03-03 11:18:57 -080063 cbuildbot_launch.InitialCheckout(None, '/buildroot', None)
Don Garrett86881cb2017-02-15 15:41:55 -080064
65 self.assertEqual(mock_repo.mock_calls, [
66 mock.call(EXPECTED_MANIFEST_URL, '/buildroot',
67 branch=None, git_cache_dir=None),
Don Garrett76496912017-05-11 16:59:11 -070068 mock.call().BuildRootGitCleanup(prune_all=True),
69 mock.call().Sync(detach=True),
Don Garrett86881cb2017-02-15 15:41:55 -080070 ])
71
72 def testInitialCheckoutMax(self):
73 """Test InitialCheckout with all settings."""
74 mock_repo = self.PatchObject(repository, 'RepoRepository', autospec=True)
Don Garrett86881cb2017-02-15 15:41:55 -080075
Don Garrett0c54ed72017-03-03 11:18:57 -080076 cbuildbot_launch.InitialCheckout(
77 'release-R56-9000.B', '/buildroot', '/git-cache')
Don Garrett86881cb2017-02-15 15:41:55 -080078
79 self.assertEqual(mock_repo.mock_calls, [
80 mock.call(EXPECTED_MANIFEST_URL, '/buildroot',
81 branch='release-R56-9000.B', git_cache_dir='/git-cache'),
Don Garrett76496912017-05-11 16:59:11 -070082 mock.call().BuildRootGitCleanup(prune_all=True),
83 mock.call().Sync(detach=True),
Don Garrett86881cb2017-02-15 15:41:55 -080084 ])
Don Garrettc4114cc2016-11-01 20:04:06 -070085
Don Garrett8d314792017-05-18 13:11:42 -070086 def testInitialCheckoutCleanupError(self):
87 """Test we wipe buildroot when cleanup fails."""
88 mock_clean = self.PatchObject(
89 repository.RepoRepository, 'BuildRootGitCleanup', autospec=True,
90 side_effect=FakeException)
91 mock_sync = self.PatchObject(
92 repository.RepoRepository, 'Sync', autospec=True)
93 mock_remove = self.PatchObject(
94 repository, 'ClearBuildRoot', autospec=True)
95
96 cbuildbot_launch.InitialCheckout('master', '/buildroot', None)
97
98 self.assertEqual(mock_clean.mock_calls, [
99 mock.call(mock.ANY, prune_all=True),
100 ])
101 self.assertEqual(mock_sync.mock_calls, [
102 mock.call(mock.ANY, detach=True),
103 ])
104 self.assertEqual(mock_remove.mock_calls, [
105 mock.call('/buildroot'),
106 ])
107
Don Garrettf15d65b2017-04-12 12:39:55 -0700108 def testConfigureGlobalEnvironment(self):
Don Garrett60967922017-04-12 18:51:44 -0700109 """Ensure that we can setup our global runtime environment correctly."""
Don Garrett86fec482017-05-17 18:13:33 -0700110
111 os.environ.pop('LANG', None)
112 os.environ['LC_MONETARY'] = 'bad'
113
Don Garrettf15d65b2017-04-12 12:39:55 -0700114 cbuildbot_launch.ConfigureGlobalEnvironment()
115
Don Garrett86fec482017-05-17 18:13:33 -0700116 # Verify umask is updated.
Don Garrettf15d65b2017-04-12 12:39:55 -0700117 self.assertEqual(os.umask(0), 0o22)
118
Don Garrett86fec482017-05-17 18:13:33 -0700119 # Verify ENVs are cleaned up.
120 self.assertEqual(os.environ['LANG'], 'en_US.UTF-8')
121 self.assertNotIn('LC_MONETARY', os.environ)
122
Don Garrettf15d65b2017-04-12 12:39:55 -0700123
Don Garrett597ddff2017-02-17 18:29:37 -0800124class RunTests(cros_build_lib_unittest.RunCommandTestCase):
Don Garrett0c54ed72017-03-03 11:18:57 -0800125 """Tests for cbuildbot_launch script."""
Don Garrett597ddff2017-02-17 18:29:37 -0800126
127 ARGS_BASE = ['--buildroot', '/buildroot']
128 ARGS_GIT_CACHE = ['--git-cache-dir', '/git-cache']
129 ARGS_CONFIG = ['config']
130 CMD = ['/buildroot/chromite/bin/cbuildbot']
131
132 def verifyRunCbuildbot(self, args, expected_cmd, version):
Don Garrett86881cb2017-02-15 15:41:55 -0800133 """Ensure we invoke cbuildbot correctly."""
Don Garrett0c54ed72017-03-03 11:18:57 -0800134 options = cbuildbot_launch.PreParseArguments(args)
Don Garrett597ddff2017-02-17 18:29:37 -0800135
136 self.PatchObject(
137 cros_build_lib, 'GetTargetChromiteApiVersion', autospec=True,
138 return_value=version)
139
Don Garrett0c54ed72017-03-03 11:18:57 -0800140 cbuildbot_launch.RunCbuildbot(options)
Don Garrett597ddff2017-02-17 18:29:37 -0800141
142 self.assertCommandCalled(
Don Garrett125d4dc2017-04-25 16:26:03 -0700143 expected_cmd, cwd=options.buildroot)
Don Garrett597ddff2017-02-17 18:29:37 -0800144
145 def testRunCbuildbotSimple(self):
146 """Ensure we invoke cbuildbot correctly."""
147 self.verifyRunCbuildbot(
148 self.ARGS_BASE + self.ARGS_CONFIG,
149 self.CMD + self.ARGS_CONFIG + self.ARGS_BASE,
150 (0, 4))
151
152 def testRunCbuildbotNotFiltered(self):
153 """Ensure we invoke cbuildbot correctly."""
154 self.verifyRunCbuildbot(
155 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
156 self.CMD + self.ARGS_CONFIG + self.ARGS_BASE + self.ARGS_GIT_CACHE,
157 (0, 4))
158
159 def testRunCbuildbotFiltered(self):
160 """Ensure we invoke cbuildbot correctly."""
161 self.verifyRunCbuildbot(
162 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
163 self.CMD + self.ARGS_CONFIG + self.ARGS_BASE,
164 (0, 2))
Don Garrettc4114cc2016-11-01 20:04:06 -0700165
Don Garrett86881cb2017-02-15 15:41:55 -0800166 def testMainMin(self):
167 """Test a minimal set of command line options."""
Don Garrett597ddff2017-02-17 18:29:37 -0800168 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
169 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
170 autospec=True, return_value=(0, 4))
Don Garrett0c54ed72017-03-03 11:18:57 -0800171 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildroot',
172 autospec=True)
173 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800174 autospec=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800175
Don Garrett0c54ed72017-03-03 11:18:57 -0800176 cbuildbot_launch.main(['--buildroot', '/buildroot', 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700177
Don Garrett7ade05a2017-02-17 13:31:47 -0800178 # Ensure we clean, as expected.
179 self.assertEqual(mock_clean.mock_calls,
Don Garrett125d4dc2017-04-25 16:26:03 -0700180 [mock.call('master', '/buildroot')])
Don Garrett7ade05a2017-02-17 13:31:47 -0800181
Don Garrett86881cb2017-02-15 15:41:55 -0800182 # Ensure we checkout, as expected.
183 self.assertEqual(mock_checkout.mock_calls,
Don Garrett125d4dc2017-04-25 16:26:03 -0700184 [mock.call('master', '/buildroot', None)])
Don Garrettc4114cc2016-11-01 20:04:06 -0700185
Don Garrett86881cb2017-02-15 15:41:55 -0800186 # Ensure we invoke cbuildbot, as expected.
187 self.assertCommandCalled(
188 ['/buildroot/chromite/bin/cbuildbot',
Don Garrett597ddff2017-02-17 18:29:37 -0800189 'config', '--buildroot', '/buildroot'],
Don Garrett125d4dc2017-04-25 16:26:03 -0700190 cwd='/buildroot')
Don Garrettc4114cc2016-11-01 20:04:06 -0700191
Don Garrett86881cb2017-02-15 15:41:55 -0800192 def testMainMax(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800193 """Test a larger set of command line options."""
194 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
195 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
196 autospec=True, return_value=(0, 4))
Don Garrett0c54ed72017-03-03 11:18:57 -0800197 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildroot',
198 autospec=True)
199 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800200 autospec=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700201
Don Garrett0c54ed72017-03-03 11:18:57 -0800202 cbuildbot_launch.main(['--buildroot', '/buildroot',
Don Garrett125d4dc2017-04-25 16:26:03 -0700203 '--branch', 'branch',
Don Garrett0c54ed72017-03-03 11:18:57 -0800204 '--git-cache-dir', '/git-cache',
205 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700206
Don Garrett7ade05a2017-02-17 13:31:47 -0800207 # Ensure we clean, as expected.
208 self.assertEqual(mock_clean.mock_calls,
Don Garrett125d4dc2017-04-25 16:26:03 -0700209 [mock.call('branch', '/buildroot')])
Don Garrett7ade05a2017-02-17 13:31:47 -0800210
Don Garrett86881cb2017-02-15 15:41:55 -0800211 # Ensure we checkout, as expected.
212 self.assertEqual(mock_checkout.mock_calls,
Don Garrett125d4dc2017-04-25 16:26:03 -0700213 [mock.call('branch', '/buildroot', '/git-cache')])
Don Garrett86881cb2017-02-15 15:41:55 -0800214
215 # Ensure we invoke cbuildbot, as expected.
216 self.assertCommandCalled(
217 ['/buildroot/chromite/bin/cbuildbot',
Don Garrett597ddff2017-02-17 18:29:37 -0800218 'config',
219 '--buildroot', '/buildroot',
Don Garrett125d4dc2017-04-25 16:26:03 -0700220 '--branch', 'branch',
Don Garrett597ddff2017-02-17 18:29:37 -0800221 '--git-cache-dir', '/git-cache'],
Don Garrett125d4dc2017-04-25 16:26:03 -0700222 cwd='/buildroot')
Don Garrett7ade05a2017-02-17 13:31:47 -0800223
224
Don Garrett60967922017-04-12 18:51:44 -0700225class CleanBuildrootTest(cros_test_lib.MockTempDirTestCase):
Don Garrett7ade05a2017-02-17 13:31:47 -0800226 """Tests for CleanBuildroot method."""
227
228 def setUp(self):
229 """Create standard buildroot contents for cleanup."""
Don Garrette17e1d92017-04-12 15:28:19 -0700230 self.root = os.path.join(self.tempdir, 'buildroot')
231 self.state = os.path.join(self.root, '.cbuildbot_launch_state')
232 self.repo = os.path.join(self.root, '.repo/repo')
233 self.chroot = os.path.join(self.root, 'chroot/chroot')
234 self.general = os.path.join(self.root, 'general/general')
Don Garrett39963602017-02-27 14:41:58 -0800235 # TODO: Add .cache, and distfiles.
Don Garrett7ade05a2017-02-17 13:31:47 -0800236
Don Garrett60967922017-04-12 18:51:44 -0700237 def populateBuildroot(self, state=None):
Don Garrett7ade05a2017-02-17 13:31:47 -0800238 """Create standard buildroot contents for cleanup."""
Don Garrett60967922017-04-12 18:51:44 -0700239 osutils.SafeMakedirs(self.root)
Don Garrette17e1d92017-04-12 15:28:19 -0700240
Don Garrett7ade05a2017-02-17 13:31:47 -0800241 if state:
242 osutils.WriteFile(self.state, state)
243
244 # Create files.
245 for f in (self.repo, self.chroot, self.general):
Don Garrette17e1d92017-04-12 15:28:19 -0700246 osutils.Touch(f, makedirs=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800247
Don Garrette17e1d92017-04-12 15:28:19 -0700248 def testNoBuildroot(self):
Don Garrett7ade05a2017-02-17 13:31:47 -0800249 """Test CleanBuildroot with no history."""
Don Garrett125d4dc2017-04-25 16:26:03 -0700250 cbuildbot_launch.CleanBuildroot('master', self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800251
Don Garrett125d4dc2017-04-25 16:26:03 -0700252 self.assertEqual(osutils.ReadFile(self.state), '1 master')
Don Garrett7ade05a2017-02-17 13:31:47 -0800253
254 def testBuildrootNoState(self):
255 """Test CleanBuildroot with no state information."""
256 self.populateBuildroot()
257
Don Garrett125d4dc2017-04-25 16:26:03 -0700258 cbuildbot_launch.CleanBuildroot('master', self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800259
Don Garrett125d4dc2017-04-25 16:26:03 -0700260 self.assertEqual(osutils.ReadFile(self.state), '1 master')
Don Garrett60967922017-04-12 18:51:44 -0700261 self.assertNotExists(self.repo)
Don Garrett7ade05a2017-02-17 13:31:47 -0800262 self.assertNotExists(self.chroot)
Don Garrett60967922017-04-12 18:51:44 -0700263 self.assertNotExists(self.general)
264
265 def testBuildrootFormatMismatch(self):
266 """Test CleanBuildroot with no state information."""
Don Garrett125d4dc2017-04-25 16:26:03 -0700267 self.populateBuildroot('0 master')
Don Garrett60967922017-04-12 18:51:44 -0700268
Don Garrett125d4dc2017-04-25 16:26:03 -0700269 cbuildbot_launch.CleanBuildroot('master', self.root)
Don Garrett60967922017-04-12 18:51:44 -0700270
Don Garrett125d4dc2017-04-25 16:26:03 -0700271 self.assertEqual(osutils.ReadFile(self.state), '1 master')
Don Garrett60967922017-04-12 18:51:44 -0700272 self.assertNotExists(self.repo)
273 self.assertNotExists(self.chroot)
274 self.assertNotExists(self.general)
Don Garrett7ade05a2017-02-17 13:31:47 -0800275
276 def testBuildrootBranchChange(self):
277 """Test CleanBuildroot with a change in branches."""
Don Garrett60967922017-04-12 18:51:44 -0700278 self.populateBuildroot('1 branchA')
Don Garrett7ade05a2017-02-17 13:31:47 -0800279
Don Garrette17e1d92017-04-12 15:28:19 -0700280 cbuildbot_launch.CleanBuildroot('branchB', self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800281
Don Garrett60967922017-04-12 18:51:44 -0700282 self.assertEqual(osutils.ReadFile(self.state), '1 branchB')
Don Garrett7ade05a2017-02-17 13:31:47 -0800283 self.assertExists(self.repo)
284 self.assertNotExists(self.chroot)
285 self.assertExists(self.general)
286
287 def testBuildrootBranchMatch(self):
288 """Test CleanBuildroot with no change in branch."""
Don Garrett60967922017-04-12 18:51:44 -0700289 self.populateBuildroot('1 branchA')
Don Garrett7ade05a2017-02-17 13:31:47 -0800290
Don Garrette17e1d92017-04-12 15:28:19 -0700291 cbuildbot_launch.CleanBuildroot('branchA', self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800292
Don Garrett60967922017-04-12 18:51:44 -0700293 self.assertEqual(osutils.ReadFile(self.state), '1 branchA')
Don Garrett7ade05a2017-02-17 13:31:47 -0800294 self.assertExists(self.repo)
295 self.assertExists(self.chroot)
296 self.assertExists(self.general)
Don Garrette17e1d92017-04-12 15:28:19 -0700297
Don Garrett60967922017-04-12 18:51:44 -0700298 def testGetBuildrootState(self):
299 """Test GetBuildrootState."""
300 # No root dir.
301 results = cbuildbot_launch.GetBuildrootState(self.root)
302 self.assertEqual(results, (0, ''))
303
304 # Empty root dir.
305 osutils.SafeMakedirs(self.root)
306 results = cbuildbot_launch.GetBuildrootState(self.root)
307 self.assertEqual(results, (0, ''))
308
309 # Empty Contents
310 osutils.WriteFile(self.state, '')
311 results = cbuildbot_launch.GetBuildrootState(self.root)
312 self.assertEqual(results, (0, ''))
313
314 # Old Format Contents
315 osutils.WriteFile(self.state, 'happy-branch')
316 results = cbuildbot_launch.GetBuildrootState(self.root)
317 self.assertEqual(results, (0, ''))
318
319 # Expected Contents
320 osutils.WriteFile(self.state, '1 happy-branch')
321 results = cbuildbot_launch.GetBuildrootState(self.root)
322 self.assertEqual(results, (1, 'happy-branch'))
323
324 # Future Contents
325 osutils.WriteFile(self.state, '22 master')
326 results = cbuildbot_launch.GetBuildrootState(self.root)
327 self.assertEqual(results, (22, 'master'))
328
329 # Read Write
330 cbuildbot_launch.SetBuildrootState('happy-branch', self.root)
331 results = cbuildbot_launch.GetBuildrootState(self.root)
332 self.assertEqual(results, (1, 'happy-branch'))
333
334 def testSetBuildrootState(self):
335 """Test SetBuildrootState."""
336 # Write out a state file.
337 osutils.SafeMakedirs(self.root)
338 cbuildbot_launch.SetBuildrootState('happy-branch', self.root)
339 self.assertEqual(osutils.ReadFile(self.state), '1 happy-branch')
340
341 # Change to a future version.
342 self.PatchObject(cbuildbot_launch, 'BUILDROOT_BUILDROOT_LAYOUT', 22)
343 cbuildbot_launch.SetBuildrootState('happy-branch', self.root)
344 self.assertEqual(osutils.ReadFile(self.state), '22 happy-branch')