blob: 1f94e0ff29716a3a612221157372f92d8729c19e [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 Garrett0c54ed72017-03-03 11:18:57 -080023class CbuildbotLaunchTest(cros_test_lib.MockTestCase):
24 """Tests for cbuildbot_launch script."""
Don Garrettc4114cc2016-11-01 20:04:06 -070025
Don Garrett86881cb2017-02-15 15:41:55 -080026 def testPreParseArguments(self):
Don Garrettc4114cc2016-11-01 20:04:06 -070027 """Test that we can correctly extract branch values from cbuildbot args."""
Don Garrett597ddff2017-02-17 18:29:37 -080028 CASES = (
29 (['--buildroot', '/buildroot', 'daisy-incremental'],
30 (None, '/buildroot', None)),
31
32 (['--buildbot', '--buildroot', '/buildroot',
33 '--git-cache-dir', '/git-cache',
34 '-b', 'release-R57-9202.B',
35 'daisy-incremental'],
36 ('release-R57-9202.B', '/buildroot', '/git-cache')),
37
38 (['--debug', '--buildbot', '--notests',
39 '--buildroot', '/buildroot',
40 '--git-cache-dir', '/git-cache',
41 '--branch', 'release-R57-9202.B',
42 'daisy-incremental'],
43 ('release-R57-9202.B', '/buildroot', '/git-cache')),
Don Garrettc4114cc2016-11-01 20:04:06 -070044 )
45
Don Garrett597ddff2017-02-17 18:29:37 -080046 for cmd_args, expected in CASES:
47 expected_branch, expected_buildroot, expected_cache_dir = expected
48
Don Garrett0c54ed72017-03-03 11:18:57 -080049 options = cbuildbot_launch.PreParseArguments(cmd_args)
Don Garrett597ddff2017-02-17 18:29:37 -080050
51 self.assertEqual(options.branch, expected_branch)
52 self.assertEqual(options.buildroot, expected_buildroot)
53 self.assertEqual(options.git_cache_dir, expected_cache_dir)
Don Garrett86881cb2017-02-15 15:41:55 -080054
55 def testInitialCheckoutMin(self):
56 """Test InitialCheckout with minimum settings."""
57 mock_repo = self.PatchObject(repository, 'RepoRepository', autospec=True)
Don Garrett86881cb2017-02-15 15:41:55 -080058
Don Garrett0c54ed72017-03-03 11:18:57 -080059 cbuildbot_launch.InitialCheckout(None, '/buildroot', None)
Don Garrett86881cb2017-02-15 15:41:55 -080060
61 self.assertEqual(mock_repo.mock_calls, [
62 mock.call(EXPECTED_MANIFEST_URL, '/buildroot',
63 branch=None, git_cache_dir=None),
Don Garrett76496912017-05-11 16:59:11 -070064 mock.call().BuildRootGitCleanup(prune_all=True),
65 mock.call().Sync(detach=True),
Don Garrett86881cb2017-02-15 15:41:55 -080066 ])
67
68 def testInitialCheckoutMax(self):
69 """Test InitialCheckout with all settings."""
70 mock_repo = self.PatchObject(repository, 'RepoRepository', autospec=True)
Don Garrett86881cb2017-02-15 15:41:55 -080071
Don Garrett0c54ed72017-03-03 11:18:57 -080072 cbuildbot_launch.InitialCheckout(
73 'release-R56-9000.B', '/buildroot', '/git-cache')
Don Garrett86881cb2017-02-15 15:41:55 -080074
75 self.assertEqual(mock_repo.mock_calls, [
76 mock.call(EXPECTED_MANIFEST_URL, '/buildroot',
77 branch='release-R56-9000.B', git_cache_dir='/git-cache'),
Don Garrett76496912017-05-11 16:59:11 -070078 mock.call().BuildRootGitCleanup(prune_all=True),
79 mock.call().Sync(detach=True),
Don Garrett86881cb2017-02-15 15:41:55 -080080 ])
Don Garrettc4114cc2016-11-01 20:04:06 -070081
Don Garrettf15d65b2017-04-12 12:39:55 -070082 def testConfigureGlobalEnvironment(self):
Don Garrett60967922017-04-12 18:51:44 -070083 """Ensure that we can setup our global runtime environment correctly."""
Don Garrettf15d65b2017-04-12 12:39:55 -070084 cbuildbot_launch.ConfigureGlobalEnvironment()
85
Don Garrett60967922017-04-12 18:51:44 -070086 # So far, we only have to modify the umask to ensure safety.
Don Garrettf15d65b2017-04-12 12:39:55 -070087 self.assertEqual(os.umask(0), 0o22)
88
89
Don Garrett597ddff2017-02-17 18:29:37 -080090class RunTests(cros_build_lib_unittest.RunCommandTestCase):
Don Garrett0c54ed72017-03-03 11:18:57 -080091 """Tests for cbuildbot_launch script."""
Don Garrett597ddff2017-02-17 18:29:37 -080092
93 ARGS_BASE = ['--buildroot', '/buildroot']
94 ARGS_GIT_CACHE = ['--git-cache-dir', '/git-cache']
95 ARGS_CONFIG = ['config']
96 CMD = ['/buildroot/chromite/bin/cbuildbot']
97
98 def verifyRunCbuildbot(self, args, expected_cmd, version):
Don Garrett86881cb2017-02-15 15:41:55 -080099 """Ensure we invoke cbuildbot correctly."""
Don Garrett0c54ed72017-03-03 11:18:57 -0800100 options = cbuildbot_launch.PreParseArguments(args)
Don Garrett597ddff2017-02-17 18:29:37 -0800101
102 self.PatchObject(
103 cros_build_lib, 'GetTargetChromiteApiVersion', autospec=True,
104 return_value=version)
105
Don Garrett0c54ed72017-03-03 11:18:57 -0800106 cbuildbot_launch.RunCbuildbot(options)
Don Garrett597ddff2017-02-17 18:29:37 -0800107
108 self.assertCommandCalled(
Don Garrett125d4dc2017-04-25 16:26:03 -0700109 expected_cmd, cwd=options.buildroot)
Don Garrett597ddff2017-02-17 18:29:37 -0800110
111 def testRunCbuildbotSimple(self):
112 """Ensure we invoke cbuildbot correctly."""
113 self.verifyRunCbuildbot(
114 self.ARGS_BASE + self.ARGS_CONFIG,
115 self.CMD + self.ARGS_CONFIG + self.ARGS_BASE,
116 (0, 4))
117
118 def testRunCbuildbotNotFiltered(self):
119 """Ensure we invoke cbuildbot correctly."""
120 self.verifyRunCbuildbot(
121 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
122 self.CMD + self.ARGS_CONFIG + self.ARGS_BASE + self.ARGS_GIT_CACHE,
123 (0, 4))
124
125 def testRunCbuildbotFiltered(self):
126 """Ensure we invoke cbuildbot correctly."""
127 self.verifyRunCbuildbot(
128 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
129 self.CMD + self.ARGS_CONFIG + self.ARGS_BASE,
130 (0, 2))
Don Garrettc4114cc2016-11-01 20:04:06 -0700131
Don Garrett86881cb2017-02-15 15:41:55 -0800132 def testMainMin(self):
133 """Test a minimal set of command line options."""
Don Garrett597ddff2017-02-17 18:29:37 -0800134 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
135 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
136 autospec=True, return_value=(0, 4))
Don Garrett0c54ed72017-03-03 11:18:57 -0800137 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildroot',
138 autospec=True)
139 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800140 autospec=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800141
Don Garrett0c54ed72017-03-03 11:18:57 -0800142 cbuildbot_launch.main(['--buildroot', '/buildroot', 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700143
Don Garrett7ade05a2017-02-17 13:31:47 -0800144 # Ensure we clean, as expected.
145 self.assertEqual(mock_clean.mock_calls,
Don Garrett125d4dc2017-04-25 16:26:03 -0700146 [mock.call('master', '/buildroot')])
Don Garrett7ade05a2017-02-17 13:31:47 -0800147
Don Garrett86881cb2017-02-15 15:41:55 -0800148 # Ensure we checkout, as expected.
149 self.assertEqual(mock_checkout.mock_calls,
Don Garrett125d4dc2017-04-25 16:26:03 -0700150 [mock.call('master', '/buildroot', None)])
Don Garrettc4114cc2016-11-01 20:04:06 -0700151
Don Garrett86881cb2017-02-15 15:41:55 -0800152 # Ensure we invoke cbuildbot, as expected.
153 self.assertCommandCalled(
154 ['/buildroot/chromite/bin/cbuildbot',
Don Garrett597ddff2017-02-17 18:29:37 -0800155 'config', '--buildroot', '/buildroot'],
Don Garrett125d4dc2017-04-25 16:26:03 -0700156 cwd='/buildroot')
Don Garrettc4114cc2016-11-01 20:04:06 -0700157
Don Garrett86881cb2017-02-15 15:41:55 -0800158 def testMainMax(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800159 """Test a larger set of command line options."""
160 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 Garrettc4114cc2016-11-01 20:04:06 -0700167
Don Garrett0c54ed72017-03-03 11:18:57 -0800168 cbuildbot_launch.main(['--buildroot', '/buildroot',
Don Garrett125d4dc2017-04-25 16:26:03 -0700169 '--branch', 'branch',
Don Garrett0c54ed72017-03-03 11:18:57 -0800170 '--git-cache-dir', '/git-cache',
171 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700172
Don Garrett7ade05a2017-02-17 13:31:47 -0800173 # Ensure we clean, as expected.
174 self.assertEqual(mock_clean.mock_calls,
Don Garrett125d4dc2017-04-25 16:26:03 -0700175 [mock.call('branch', '/buildroot')])
Don Garrett7ade05a2017-02-17 13:31:47 -0800176
Don Garrett86881cb2017-02-15 15:41:55 -0800177 # Ensure we checkout, as expected.
178 self.assertEqual(mock_checkout.mock_calls,
Don Garrett125d4dc2017-04-25 16:26:03 -0700179 [mock.call('branch', '/buildroot', '/git-cache')])
Don Garrett86881cb2017-02-15 15:41:55 -0800180
181 # Ensure we invoke cbuildbot, as expected.
182 self.assertCommandCalled(
183 ['/buildroot/chromite/bin/cbuildbot',
Don Garrett597ddff2017-02-17 18:29:37 -0800184 'config',
185 '--buildroot', '/buildroot',
Don Garrett125d4dc2017-04-25 16:26:03 -0700186 '--branch', 'branch',
Don Garrett597ddff2017-02-17 18:29:37 -0800187 '--git-cache-dir', '/git-cache'],
Don Garrett125d4dc2017-04-25 16:26:03 -0700188 cwd='/buildroot')
Don Garrett7ade05a2017-02-17 13:31:47 -0800189
190
Don Garrett60967922017-04-12 18:51:44 -0700191class CleanBuildrootTest(cros_test_lib.MockTempDirTestCase):
Don Garrett7ade05a2017-02-17 13:31:47 -0800192 """Tests for CleanBuildroot method."""
193
194 def setUp(self):
195 """Create standard buildroot contents for cleanup."""
Don Garrette17e1d92017-04-12 15:28:19 -0700196 self.root = os.path.join(self.tempdir, 'buildroot')
197 self.state = os.path.join(self.root, '.cbuildbot_launch_state')
198 self.repo = os.path.join(self.root, '.repo/repo')
199 self.chroot = os.path.join(self.root, 'chroot/chroot')
200 self.general = os.path.join(self.root, 'general/general')
Don Garrett39963602017-02-27 14:41:58 -0800201 # TODO: Add .cache, and distfiles.
Don Garrett7ade05a2017-02-17 13:31:47 -0800202
Don Garrett60967922017-04-12 18:51:44 -0700203 def populateBuildroot(self, state=None):
Don Garrett7ade05a2017-02-17 13:31:47 -0800204 """Create standard buildroot contents for cleanup."""
Don Garrett60967922017-04-12 18:51:44 -0700205 osutils.SafeMakedirs(self.root)
Don Garrette17e1d92017-04-12 15:28:19 -0700206
Don Garrett7ade05a2017-02-17 13:31:47 -0800207 if state:
208 osutils.WriteFile(self.state, state)
209
210 # Create files.
211 for f in (self.repo, self.chroot, self.general):
Don Garrette17e1d92017-04-12 15:28:19 -0700212 osutils.Touch(f, makedirs=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800213
Don Garrette17e1d92017-04-12 15:28:19 -0700214 def testNoBuildroot(self):
Don Garrett7ade05a2017-02-17 13:31:47 -0800215 """Test CleanBuildroot with no history."""
Don Garrett125d4dc2017-04-25 16:26:03 -0700216 cbuildbot_launch.CleanBuildroot('master', self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800217
Don Garrett125d4dc2017-04-25 16:26:03 -0700218 self.assertEqual(osutils.ReadFile(self.state), '1 master')
Don Garrett7ade05a2017-02-17 13:31:47 -0800219
220 def testBuildrootNoState(self):
221 """Test CleanBuildroot with no state information."""
222 self.populateBuildroot()
223
Don Garrett125d4dc2017-04-25 16:26:03 -0700224 cbuildbot_launch.CleanBuildroot('master', self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800225
Don Garrett125d4dc2017-04-25 16:26:03 -0700226 self.assertEqual(osutils.ReadFile(self.state), '1 master')
Don Garrett60967922017-04-12 18:51:44 -0700227 self.assertNotExists(self.repo)
Don Garrett7ade05a2017-02-17 13:31:47 -0800228 self.assertNotExists(self.chroot)
Don Garrett60967922017-04-12 18:51:44 -0700229 self.assertNotExists(self.general)
230
231 def testBuildrootFormatMismatch(self):
232 """Test CleanBuildroot with no state information."""
Don Garrett125d4dc2017-04-25 16:26:03 -0700233 self.populateBuildroot('0 master')
Don Garrett60967922017-04-12 18:51:44 -0700234
Don Garrett125d4dc2017-04-25 16:26:03 -0700235 cbuildbot_launch.CleanBuildroot('master', self.root)
Don Garrett60967922017-04-12 18:51:44 -0700236
Don Garrett125d4dc2017-04-25 16:26:03 -0700237 self.assertEqual(osutils.ReadFile(self.state), '1 master')
Don Garrett60967922017-04-12 18:51:44 -0700238 self.assertNotExists(self.repo)
239 self.assertNotExists(self.chroot)
240 self.assertNotExists(self.general)
Don Garrett7ade05a2017-02-17 13:31:47 -0800241
242 def testBuildrootBranchChange(self):
243 """Test CleanBuildroot with a change in branches."""
Don Garrett60967922017-04-12 18:51:44 -0700244 self.populateBuildroot('1 branchA')
Don Garrett7ade05a2017-02-17 13:31:47 -0800245
Don Garrette17e1d92017-04-12 15:28:19 -0700246 cbuildbot_launch.CleanBuildroot('branchB', self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800247
Don Garrett60967922017-04-12 18:51:44 -0700248 self.assertEqual(osutils.ReadFile(self.state), '1 branchB')
Don Garrett7ade05a2017-02-17 13:31:47 -0800249 self.assertExists(self.repo)
250 self.assertNotExists(self.chroot)
251 self.assertExists(self.general)
252
253 def testBuildrootBranchMatch(self):
254 """Test CleanBuildroot with no change in branch."""
Don Garrett60967922017-04-12 18:51:44 -0700255 self.populateBuildroot('1 branchA')
Don Garrett7ade05a2017-02-17 13:31:47 -0800256
Don Garrette17e1d92017-04-12 15:28:19 -0700257 cbuildbot_launch.CleanBuildroot('branchA', self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800258
Don Garrett60967922017-04-12 18:51:44 -0700259 self.assertEqual(osutils.ReadFile(self.state), '1 branchA')
Don Garrett7ade05a2017-02-17 13:31:47 -0800260 self.assertExists(self.repo)
261 self.assertExists(self.chroot)
262 self.assertExists(self.general)
Don Garrette17e1d92017-04-12 15:28:19 -0700263
Don Garrett60967922017-04-12 18:51:44 -0700264 def testGetBuildrootState(self):
265 """Test GetBuildrootState."""
266 # No root dir.
267 results = cbuildbot_launch.GetBuildrootState(self.root)
268 self.assertEqual(results, (0, ''))
269
270 # Empty root dir.
271 osutils.SafeMakedirs(self.root)
272 results = cbuildbot_launch.GetBuildrootState(self.root)
273 self.assertEqual(results, (0, ''))
274
275 # Empty Contents
276 osutils.WriteFile(self.state, '')
277 results = cbuildbot_launch.GetBuildrootState(self.root)
278 self.assertEqual(results, (0, ''))
279
280 # Old Format Contents
281 osutils.WriteFile(self.state, 'happy-branch')
282 results = cbuildbot_launch.GetBuildrootState(self.root)
283 self.assertEqual(results, (0, ''))
284
285 # Expected Contents
286 osutils.WriteFile(self.state, '1 happy-branch')
287 results = cbuildbot_launch.GetBuildrootState(self.root)
288 self.assertEqual(results, (1, 'happy-branch'))
289
290 # Future Contents
291 osutils.WriteFile(self.state, '22 master')
292 results = cbuildbot_launch.GetBuildrootState(self.root)
293 self.assertEqual(results, (22, 'master'))
294
295 # Read Write
296 cbuildbot_launch.SetBuildrootState('happy-branch', self.root)
297 results = cbuildbot_launch.GetBuildrootState(self.root)
298 self.assertEqual(results, (1, 'happy-branch'))
299
300 def testSetBuildrootState(self):
301 """Test SetBuildrootState."""
302 # Write out a state file.
303 osutils.SafeMakedirs(self.root)
304 cbuildbot_launch.SetBuildrootState('happy-branch', self.root)
305 self.assertEqual(osutils.ReadFile(self.state), '1 happy-branch')
306
307 # Change to a future version.
308 self.PatchObject(cbuildbot_launch, 'BUILDROOT_BUILDROOT_LAYOUT', 22)
309 cbuildbot_launch.SetBuildrootState('happy-branch', self.root)
310 self.assertEqual(osutils.ReadFile(self.state), '22 happy-branch')