blob: 873ed9aad2e56110ded2013b3d78dd75a202ce0e [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 Garrettf15d65b2017-04-12 12:39:55 -0700110 cbuildbot_launch.ConfigureGlobalEnvironment()
111
Don Garrett60967922017-04-12 18:51:44 -0700112 # So far, we only have to modify the umask to ensure safety.
Don Garrettf15d65b2017-04-12 12:39:55 -0700113 self.assertEqual(os.umask(0), 0o22)
114
115
Don Garrett597ddff2017-02-17 18:29:37 -0800116class RunTests(cros_build_lib_unittest.RunCommandTestCase):
Don Garrett0c54ed72017-03-03 11:18:57 -0800117 """Tests for cbuildbot_launch script."""
Don Garrett597ddff2017-02-17 18:29:37 -0800118
119 ARGS_BASE = ['--buildroot', '/buildroot']
120 ARGS_GIT_CACHE = ['--git-cache-dir', '/git-cache']
121 ARGS_CONFIG = ['config']
122 CMD = ['/buildroot/chromite/bin/cbuildbot']
123
124 def verifyRunCbuildbot(self, args, expected_cmd, version):
Don Garrett86881cb2017-02-15 15:41:55 -0800125 """Ensure we invoke cbuildbot correctly."""
Don Garrett0c54ed72017-03-03 11:18:57 -0800126 options = cbuildbot_launch.PreParseArguments(args)
Don Garrett597ddff2017-02-17 18:29:37 -0800127
128 self.PatchObject(
129 cros_build_lib, 'GetTargetChromiteApiVersion', autospec=True,
130 return_value=version)
131
Don Garrett0c54ed72017-03-03 11:18:57 -0800132 cbuildbot_launch.RunCbuildbot(options)
Don Garrett597ddff2017-02-17 18:29:37 -0800133
134 self.assertCommandCalled(
Don Garrett125d4dc2017-04-25 16:26:03 -0700135 expected_cmd, cwd=options.buildroot)
Don Garrett597ddff2017-02-17 18:29:37 -0800136
137 def testRunCbuildbotSimple(self):
138 """Ensure we invoke cbuildbot correctly."""
139 self.verifyRunCbuildbot(
140 self.ARGS_BASE + self.ARGS_CONFIG,
141 self.CMD + self.ARGS_CONFIG + self.ARGS_BASE,
142 (0, 4))
143
144 def testRunCbuildbotNotFiltered(self):
145 """Ensure we invoke cbuildbot correctly."""
146 self.verifyRunCbuildbot(
147 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
148 self.CMD + self.ARGS_CONFIG + self.ARGS_BASE + self.ARGS_GIT_CACHE,
149 (0, 4))
150
151 def testRunCbuildbotFiltered(self):
152 """Ensure we invoke cbuildbot correctly."""
153 self.verifyRunCbuildbot(
154 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
155 self.CMD + self.ARGS_CONFIG + self.ARGS_BASE,
156 (0, 2))
Don Garrettc4114cc2016-11-01 20:04:06 -0700157
Don Garrett86881cb2017-02-15 15:41:55 -0800158 def testMainMin(self):
159 """Test a minimal set of command line options."""
Don Garrett597ddff2017-02-17 18:29:37 -0800160 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
161 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
162 autospec=True, return_value=(0, 4))
Don Garrett0c54ed72017-03-03 11:18:57 -0800163 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildroot',
164 autospec=True)
165 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800166 autospec=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800167
Don Garrett0c54ed72017-03-03 11:18:57 -0800168 cbuildbot_launch.main(['--buildroot', '/buildroot', 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700169
Don Garrett7ade05a2017-02-17 13:31:47 -0800170 # Ensure we clean, as expected.
171 self.assertEqual(mock_clean.mock_calls,
Don Garrett125d4dc2017-04-25 16:26:03 -0700172 [mock.call('master', '/buildroot')])
Don Garrett7ade05a2017-02-17 13:31:47 -0800173
Don Garrett86881cb2017-02-15 15:41:55 -0800174 # Ensure we checkout, as expected.
175 self.assertEqual(mock_checkout.mock_calls,
Don Garrett125d4dc2017-04-25 16:26:03 -0700176 [mock.call('master', '/buildroot', None)])
Don Garrettc4114cc2016-11-01 20:04:06 -0700177
Don Garrett86881cb2017-02-15 15:41:55 -0800178 # Ensure we invoke cbuildbot, as expected.
179 self.assertCommandCalled(
180 ['/buildroot/chromite/bin/cbuildbot',
Don Garrett597ddff2017-02-17 18:29:37 -0800181 'config', '--buildroot', '/buildroot'],
Don Garrett125d4dc2017-04-25 16:26:03 -0700182 cwd='/buildroot')
Don Garrettc4114cc2016-11-01 20:04:06 -0700183
Don Garrett86881cb2017-02-15 15:41:55 -0800184 def testMainMax(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800185 """Test a larger set of command line options."""
186 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
187 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
188 autospec=True, return_value=(0, 4))
Don Garrett0c54ed72017-03-03 11:18:57 -0800189 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildroot',
190 autospec=True)
191 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800192 autospec=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700193
Don Garrett0c54ed72017-03-03 11:18:57 -0800194 cbuildbot_launch.main(['--buildroot', '/buildroot',
Don Garrett125d4dc2017-04-25 16:26:03 -0700195 '--branch', 'branch',
Don Garrett0c54ed72017-03-03 11:18:57 -0800196 '--git-cache-dir', '/git-cache',
197 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700198
Don Garrett7ade05a2017-02-17 13:31:47 -0800199 # Ensure we clean, as expected.
200 self.assertEqual(mock_clean.mock_calls,
Don Garrett125d4dc2017-04-25 16:26:03 -0700201 [mock.call('branch', '/buildroot')])
Don Garrett7ade05a2017-02-17 13:31:47 -0800202
Don Garrett86881cb2017-02-15 15:41:55 -0800203 # Ensure we checkout, as expected.
204 self.assertEqual(mock_checkout.mock_calls,
Don Garrett125d4dc2017-04-25 16:26:03 -0700205 [mock.call('branch', '/buildroot', '/git-cache')])
Don Garrett86881cb2017-02-15 15:41:55 -0800206
207 # Ensure we invoke cbuildbot, as expected.
208 self.assertCommandCalled(
209 ['/buildroot/chromite/bin/cbuildbot',
Don Garrett597ddff2017-02-17 18:29:37 -0800210 'config',
211 '--buildroot', '/buildroot',
Don Garrett125d4dc2017-04-25 16:26:03 -0700212 '--branch', 'branch',
Don Garrett597ddff2017-02-17 18:29:37 -0800213 '--git-cache-dir', '/git-cache'],
Don Garrett125d4dc2017-04-25 16:26:03 -0700214 cwd='/buildroot')
Don Garrett7ade05a2017-02-17 13:31:47 -0800215
216
Don Garrett60967922017-04-12 18:51:44 -0700217class CleanBuildrootTest(cros_test_lib.MockTempDirTestCase):
Don Garrett7ade05a2017-02-17 13:31:47 -0800218 """Tests for CleanBuildroot method."""
219
220 def setUp(self):
221 """Create standard buildroot contents for cleanup."""
Don Garrette17e1d92017-04-12 15:28:19 -0700222 self.root = os.path.join(self.tempdir, 'buildroot')
223 self.state = os.path.join(self.root, '.cbuildbot_launch_state')
224 self.repo = os.path.join(self.root, '.repo/repo')
225 self.chroot = os.path.join(self.root, 'chroot/chroot')
226 self.general = os.path.join(self.root, 'general/general')
Don Garrett39963602017-02-27 14:41:58 -0800227 # TODO: Add .cache, and distfiles.
Don Garrett7ade05a2017-02-17 13:31:47 -0800228
Don Garrett60967922017-04-12 18:51:44 -0700229 def populateBuildroot(self, state=None):
Don Garrett7ade05a2017-02-17 13:31:47 -0800230 """Create standard buildroot contents for cleanup."""
Don Garrett60967922017-04-12 18:51:44 -0700231 osutils.SafeMakedirs(self.root)
Don Garrette17e1d92017-04-12 15:28:19 -0700232
Don Garrett7ade05a2017-02-17 13:31:47 -0800233 if state:
234 osutils.WriteFile(self.state, state)
235
236 # Create files.
237 for f in (self.repo, self.chroot, self.general):
Don Garrette17e1d92017-04-12 15:28:19 -0700238 osutils.Touch(f, makedirs=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800239
Don Garrette17e1d92017-04-12 15:28:19 -0700240 def testNoBuildroot(self):
Don Garrett7ade05a2017-02-17 13:31:47 -0800241 """Test CleanBuildroot with no history."""
Don Garrett125d4dc2017-04-25 16:26:03 -0700242 cbuildbot_launch.CleanBuildroot('master', self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800243
Don Garrett125d4dc2017-04-25 16:26:03 -0700244 self.assertEqual(osutils.ReadFile(self.state), '1 master')
Don Garrett7ade05a2017-02-17 13:31:47 -0800245
246 def testBuildrootNoState(self):
247 """Test CleanBuildroot with no state information."""
248 self.populateBuildroot()
249
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 Garrett60967922017-04-12 18:51:44 -0700253 self.assertNotExists(self.repo)
Don Garrett7ade05a2017-02-17 13:31:47 -0800254 self.assertNotExists(self.chroot)
Don Garrett60967922017-04-12 18:51:44 -0700255 self.assertNotExists(self.general)
256
257 def testBuildrootFormatMismatch(self):
258 """Test CleanBuildroot with no state information."""
Don Garrett125d4dc2017-04-25 16:26:03 -0700259 self.populateBuildroot('0 master')
Don Garrett60967922017-04-12 18:51:44 -0700260
Don Garrett125d4dc2017-04-25 16:26:03 -0700261 cbuildbot_launch.CleanBuildroot('master', self.root)
Don Garrett60967922017-04-12 18:51:44 -0700262
Don Garrett125d4dc2017-04-25 16:26:03 -0700263 self.assertEqual(osutils.ReadFile(self.state), '1 master')
Don Garrett60967922017-04-12 18:51:44 -0700264 self.assertNotExists(self.repo)
265 self.assertNotExists(self.chroot)
266 self.assertNotExists(self.general)
Don Garrett7ade05a2017-02-17 13:31:47 -0800267
268 def testBuildrootBranchChange(self):
269 """Test CleanBuildroot with a change in branches."""
Don Garrett60967922017-04-12 18:51:44 -0700270 self.populateBuildroot('1 branchA')
Don Garrett7ade05a2017-02-17 13:31:47 -0800271
Don Garrette17e1d92017-04-12 15:28:19 -0700272 cbuildbot_launch.CleanBuildroot('branchB', self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800273
Don Garrett60967922017-04-12 18:51:44 -0700274 self.assertEqual(osutils.ReadFile(self.state), '1 branchB')
Don Garrett7ade05a2017-02-17 13:31:47 -0800275 self.assertExists(self.repo)
276 self.assertNotExists(self.chroot)
277 self.assertExists(self.general)
278
279 def testBuildrootBranchMatch(self):
280 """Test CleanBuildroot with no change in branch."""
Don Garrett60967922017-04-12 18:51:44 -0700281 self.populateBuildroot('1 branchA')
Don Garrett7ade05a2017-02-17 13:31:47 -0800282
Don Garrette17e1d92017-04-12 15:28:19 -0700283 cbuildbot_launch.CleanBuildroot('branchA', self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800284
Don Garrett60967922017-04-12 18:51:44 -0700285 self.assertEqual(osutils.ReadFile(self.state), '1 branchA')
Don Garrett7ade05a2017-02-17 13:31:47 -0800286 self.assertExists(self.repo)
287 self.assertExists(self.chroot)
288 self.assertExists(self.general)
Don Garrette17e1d92017-04-12 15:28:19 -0700289
Don Garrett60967922017-04-12 18:51:44 -0700290 def testGetBuildrootState(self):
291 """Test GetBuildrootState."""
292 # No root dir.
293 results = cbuildbot_launch.GetBuildrootState(self.root)
294 self.assertEqual(results, (0, ''))
295
296 # Empty root dir.
297 osutils.SafeMakedirs(self.root)
298 results = cbuildbot_launch.GetBuildrootState(self.root)
299 self.assertEqual(results, (0, ''))
300
301 # Empty Contents
302 osutils.WriteFile(self.state, '')
303 results = cbuildbot_launch.GetBuildrootState(self.root)
304 self.assertEqual(results, (0, ''))
305
306 # Old Format Contents
307 osutils.WriteFile(self.state, 'happy-branch')
308 results = cbuildbot_launch.GetBuildrootState(self.root)
309 self.assertEqual(results, (0, ''))
310
311 # Expected Contents
312 osutils.WriteFile(self.state, '1 happy-branch')
313 results = cbuildbot_launch.GetBuildrootState(self.root)
314 self.assertEqual(results, (1, 'happy-branch'))
315
316 # Future Contents
317 osutils.WriteFile(self.state, '22 master')
318 results = cbuildbot_launch.GetBuildrootState(self.root)
319 self.assertEqual(results, (22, 'master'))
320
321 # Read Write
322 cbuildbot_launch.SetBuildrootState('happy-branch', self.root)
323 results = cbuildbot_launch.GetBuildrootState(self.root)
324 self.assertEqual(results, (1, 'happy-branch'))
325
326 def testSetBuildrootState(self):
327 """Test SetBuildrootState."""
328 # Write out a state file.
329 osutils.SafeMakedirs(self.root)
330 cbuildbot_launch.SetBuildrootState('happy-branch', self.root)
331 self.assertEqual(osutils.ReadFile(self.state), '1 happy-branch')
332
333 # Change to a future version.
334 self.PatchObject(cbuildbot_launch, 'BUILDROOT_BUILDROOT_LAYOUT', 22)
335 cbuildbot_launch.SetBuildrootState('happy-branch', self.root)
336 self.assertEqual(osutils.ReadFile(self.state), '22 happy-branch')