blob: d042c27b347bcd106ba4570bf3bbe01a79651e61 [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),
64 mock.call().Sync()
65 ])
66
67 def testInitialCheckoutMax(self):
68 """Test InitialCheckout with all settings."""
69 mock_repo = self.PatchObject(repository, 'RepoRepository', autospec=True)
Don Garrett86881cb2017-02-15 15:41:55 -080070
Don Garrett0c54ed72017-03-03 11:18:57 -080071 cbuildbot_launch.InitialCheckout(
72 'release-R56-9000.B', '/buildroot', '/git-cache')
Don Garrett86881cb2017-02-15 15:41:55 -080073
74 self.assertEqual(mock_repo.mock_calls, [
75 mock.call(EXPECTED_MANIFEST_URL, '/buildroot',
76 branch='release-R56-9000.B', git_cache_dir='/git-cache'),
77 mock.call().Sync()
78 ])
Don Garrettc4114cc2016-11-01 20:04:06 -070079
Don Garrettf15d65b2017-04-12 12:39:55 -070080 def testConfigureGlobalEnvironment(self):
Don Garrett60967922017-04-12 18:51:44 -070081 """Ensure that we can setup our global runtime environment correctly."""
Don Garrettf15d65b2017-04-12 12:39:55 -070082 cbuildbot_launch.ConfigureGlobalEnvironment()
83
Don Garrett60967922017-04-12 18:51:44 -070084 # So far, we only have to modify the umask to ensure safety.
Don Garrettf15d65b2017-04-12 12:39:55 -070085 self.assertEqual(os.umask(0), 0o22)
86
87
Don Garrett597ddff2017-02-17 18:29:37 -080088class RunTests(cros_build_lib_unittest.RunCommandTestCase):
Don Garrett0c54ed72017-03-03 11:18:57 -080089 """Tests for cbuildbot_launch script."""
Don Garrett597ddff2017-02-17 18:29:37 -080090
91 ARGS_BASE = ['--buildroot', '/buildroot']
92 ARGS_GIT_CACHE = ['--git-cache-dir', '/git-cache']
93 ARGS_CONFIG = ['config']
94 CMD = ['/buildroot/chromite/bin/cbuildbot']
95
96 def verifyRunCbuildbot(self, args, expected_cmd, version):
Don Garrett86881cb2017-02-15 15:41:55 -080097 """Ensure we invoke cbuildbot correctly."""
Don Garrett0c54ed72017-03-03 11:18:57 -080098 options = cbuildbot_launch.PreParseArguments(args)
Don Garrett597ddff2017-02-17 18:29:37 -080099
100 self.PatchObject(
101 cros_build_lib, 'GetTargetChromiteApiVersion', autospec=True,
102 return_value=version)
103
Don Garrett0c54ed72017-03-03 11:18:57 -0800104 cbuildbot_launch.RunCbuildbot(options)
Don Garrett597ddff2017-02-17 18:29:37 -0800105
106 self.assertCommandCalled(
Don Garrett125d4dc2017-04-25 16:26:03 -0700107 expected_cmd, cwd=options.buildroot)
Don Garrett597ddff2017-02-17 18:29:37 -0800108
109 def testRunCbuildbotSimple(self):
110 """Ensure we invoke cbuildbot correctly."""
111 self.verifyRunCbuildbot(
112 self.ARGS_BASE + self.ARGS_CONFIG,
113 self.CMD + self.ARGS_CONFIG + self.ARGS_BASE,
114 (0, 4))
115
116 def testRunCbuildbotNotFiltered(self):
117 """Ensure we invoke cbuildbot correctly."""
118 self.verifyRunCbuildbot(
119 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
120 self.CMD + self.ARGS_CONFIG + self.ARGS_BASE + self.ARGS_GIT_CACHE,
121 (0, 4))
122
123 def testRunCbuildbotFiltered(self):
124 """Ensure we invoke cbuildbot correctly."""
125 self.verifyRunCbuildbot(
126 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
127 self.CMD + self.ARGS_CONFIG + self.ARGS_BASE,
128 (0, 2))
Don Garrettc4114cc2016-11-01 20:04:06 -0700129
Don Garrett86881cb2017-02-15 15:41:55 -0800130 def testMainMin(self):
131 """Test a minimal set of command line options."""
Don Garrett597ddff2017-02-17 18:29:37 -0800132 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
133 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
134 autospec=True, return_value=(0, 4))
Don Garrett0c54ed72017-03-03 11:18:57 -0800135 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildroot',
136 autospec=True)
137 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800138 autospec=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800139
Don Garrett0c54ed72017-03-03 11:18:57 -0800140 cbuildbot_launch.main(['--buildroot', '/buildroot', 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700141
Don Garrett7ade05a2017-02-17 13:31:47 -0800142 # Ensure we clean, as expected.
143 self.assertEqual(mock_clean.mock_calls,
Don Garrett125d4dc2017-04-25 16:26:03 -0700144 [mock.call('master', '/buildroot')])
Don Garrett7ade05a2017-02-17 13:31:47 -0800145
Don Garrett86881cb2017-02-15 15:41:55 -0800146 # Ensure we checkout, as expected.
147 self.assertEqual(mock_checkout.mock_calls,
Don Garrett125d4dc2017-04-25 16:26:03 -0700148 [mock.call('master', '/buildroot', None)])
Don Garrettc4114cc2016-11-01 20:04:06 -0700149
Don Garrett86881cb2017-02-15 15:41:55 -0800150 # Ensure we invoke cbuildbot, as expected.
151 self.assertCommandCalled(
152 ['/buildroot/chromite/bin/cbuildbot',
Don Garrett597ddff2017-02-17 18:29:37 -0800153 'config', '--buildroot', '/buildroot'],
Don Garrett125d4dc2017-04-25 16:26:03 -0700154 cwd='/buildroot')
Don Garrettc4114cc2016-11-01 20:04:06 -0700155
Don Garrett86881cb2017-02-15 15:41:55 -0800156 def testMainMax(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800157 """Test a larger set of command line options."""
158 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
159 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
160 autospec=True, return_value=(0, 4))
Don Garrett0c54ed72017-03-03 11:18:57 -0800161 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildroot',
162 autospec=True)
163 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800164 autospec=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700165
Don Garrett0c54ed72017-03-03 11:18:57 -0800166 cbuildbot_launch.main(['--buildroot', '/buildroot',
Don Garrett125d4dc2017-04-25 16:26:03 -0700167 '--branch', 'branch',
Don Garrett0c54ed72017-03-03 11:18:57 -0800168 '--git-cache-dir', '/git-cache',
169 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700170
Don Garrett7ade05a2017-02-17 13:31:47 -0800171 # Ensure we clean, as expected.
172 self.assertEqual(mock_clean.mock_calls,
Don Garrett125d4dc2017-04-25 16:26:03 -0700173 [mock.call('branch', '/buildroot')])
Don Garrett7ade05a2017-02-17 13:31:47 -0800174
Don Garrett86881cb2017-02-15 15:41:55 -0800175 # Ensure we checkout, as expected.
176 self.assertEqual(mock_checkout.mock_calls,
Don Garrett125d4dc2017-04-25 16:26:03 -0700177 [mock.call('branch', '/buildroot', '/git-cache')])
Don Garrett86881cb2017-02-15 15:41:55 -0800178
179 # Ensure we invoke cbuildbot, as expected.
180 self.assertCommandCalled(
181 ['/buildroot/chromite/bin/cbuildbot',
Don Garrett597ddff2017-02-17 18:29:37 -0800182 'config',
183 '--buildroot', '/buildroot',
Don Garrett125d4dc2017-04-25 16:26:03 -0700184 '--branch', 'branch',
Don Garrett597ddff2017-02-17 18:29:37 -0800185 '--git-cache-dir', '/git-cache'],
Don Garrett125d4dc2017-04-25 16:26:03 -0700186 cwd='/buildroot')
Don Garrett7ade05a2017-02-17 13:31:47 -0800187
188
Don Garrett60967922017-04-12 18:51:44 -0700189class CleanBuildrootTest(cros_test_lib.MockTempDirTestCase):
Don Garrett7ade05a2017-02-17 13:31:47 -0800190 """Tests for CleanBuildroot method."""
191
192 def setUp(self):
193 """Create standard buildroot contents for cleanup."""
Don Garrette17e1d92017-04-12 15:28:19 -0700194 self.root = os.path.join(self.tempdir, 'buildroot')
195 self.state = os.path.join(self.root, '.cbuildbot_launch_state')
196 self.repo = os.path.join(self.root, '.repo/repo')
197 self.chroot = os.path.join(self.root, 'chroot/chroot')
198 self.general = os.path.join(self.root, 'general/general')
Don Garrett39963602017-02-27 14:41:58 -0800199 # TODO: Add .cache, and distfiles.
Don Garrett7ade05a2017-02-17 13:31:47 -0800200
Don Garrett60967922017-04-12 18:51:44 -0700201 def populateBuildroot(self, state=None):
Don Garrett7ade05a2017-02-17 13:31:47 -0800202 """Create standard buildroot contents for cleanup."""
Don Garrett60967922017-04-12 18:51:44 -0700203 osutils.SafeMakedirs(self.root)
Don Garrette17e1d92017-04-12 15:28:19 -0700204
Don Garrett7ade05a2017-02-17 13:31:47 -0800205 if state:
206 osutils.WriteFile(self.state, state)
207
208 # Create files.
209 for f in (self.repo, self.chroot, self.general):
Don Garrette17e1d92017-04-12 15:28:19 -0700210 osutils.Touch(f, makedirs=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800211
Don Garrette17e1d92017-04-12 15:28:19 -0700212 def testNoBuildroot(self):
Don Garrett7ade05a2017-02-17 13:31:47 -0800213 """Test CleanBuildroot with no history."""
Don Garrett125d4dc2017-04-25 16:26:03 -0700214 cbuildbot_launch.CleanBuildroot('master', self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800215
Don Garrett125d4dc2017-04-25 16:26:03 -0700216 self.assertEqual(osutils.ReadFile(self.state), '1 master')
Don Garrett7ade05a2017-02-17 13:31:47 -0800217
218 def testBuildrootNoState(self):
219 """Test CleanBuildroot with no state information."""
220 self.populateBuildroot()
221
Don Garrett125d4dc2017-04-25 16:26:03 -0700222 cbuildbot_launch.CleanBuildroot('master', self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800223
Don Garrett125d4dc2017-04-25 16:26:03 -0700224 self.assertEqual(osutils.ReadFile(self.state), '1 master')
Don Garrett60967922017-04-12 18:51:44 -0700225 self.assertNotExists(self.repo)
Don Garrett7ade05a2017-02-17 13:31:47 -0800226 self.assertNotExists(self.chroot)
Don Garrett60967922017-04-12 18:51:44 -0700227 self.assertNotExists(self.general)
228
229 def testBuildrootFormatMismatch(self):
230 """Test CleanBuildroot with no state information."""
Don Garrett125d4dc2017-04-25 16:26:03 -0700231 self.populateBuildroot('0 master')
Don Garrett60967922017-04-12 18:51:44 -0700232
Don Garrett125d4dc2017-04-25 16:26:03 -0700233 cbuildbot_launch.CleanBuildroot('master', self.root)
Don Garrett60967922017-04-12 18:51:44 -0700234
Don Garrett125d4dc2017-04-25 16:26:03 -0700235 self.assertEqual(osutils.ReadFile(self.state), '1 master')
Don Garrett60967922017-04-12 18:51:44 -0700236 self.assertNotExists(self.repo)
237 self.assertNotExists(self.chroot)
238 self.assertNotExists(self.general)
Don Garrett7ade05a2017-02-17 13:31:47 -0800239
240 def testBuildrootBranchChange(self):
241 """Test CleanBuildroot with a change in branches."""
Don Garrett60967922017-04-12 18:51:44 -0700242 self.populateBuildroot('1 branchA')
Don Garrett7ade05a2017-02-17 13:31:47 -0800243
Don Garrette17e1d92017-04-12 15:28:19 -0700244 cbuildbot_launch.CleanBuildroot('branchB', self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800245
Don Garrett60967922017-04-12 18:51:44 -0700246 self.assertEqual(osutils.ReadFile(self.state), '1 branchB')
Don Garrett7ade05a2017-02-17 13:31:47 -0800247 self.assertExists(self.repo)
248 self.assertNotExists(self.chroot)
249 self.assertExists(self.general)
250
251 def testBuildrootBranchMatch(self):
252 """Test CleanBuildroot with no change in branch."""
Don Garrett60967922017-04-12 18:51:44 -0700253 self.populateBuildroot('1 branchA')
Don Garrett7ade05a2017-02-17 13:31:47 -0800254
Don Garrette17e1d92017-04-12 15:28:19 -0700255 cbuildbot_launch.CleanBuildroot('branchA', self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800256
Don Garrett60967922017-04-12 18:51:44 -0700257 self.assertEqual(osutils.ReadFile(self.state), '1 branchA')
Don Garrett7ade05a2017-02-17 13:31:47 -0800258 self.assertExists(self.repo)
259 self.assertExists(self.chroot)
260 self.assertExists(self.general)
Don Garrette17e1d92017-04-12 15:28:19 -0700261
Don Garrett60967922017-04-12 18:51:44 -0700262 def testGetBuildrootState(self):
263 """Test GetBuildrootState."""
264 # No root dir.
265 results = cbuildbot_launch.GetBuildrootState(self.root)
266 self.assertEqual(results, (0, ''))
267
268 # Empty root dir.
269 osutils.SafeMakedirs(self.root)
270 results = cbuildbot_launch.GetBuildrootState(self.root)
271 self.assertEqual(results, (0, ''))
272
273 # Empty Contents
274 osutils.WriteFile(self.state, '')
275 results = cbuildbot_launch.GetBuildrootState(self.root)
276 self.assertEqual(results, (0, ''))
277
278 # Old Format Contents
279 osutils.WriteFile(self.state, 'happy-branch')
280 results = cbuildbot_launch.GetBuildrootState(self.root)
281 self.assertEqual(results, (0, ''))
282
283 # Expected Contents
284 osutils.WriteFile(self.state, '1 happy-branch')
285 results = cbuildbot_launch.GetBuildrootState(self.root)
286 self.assertEqual(results, (1, 'happy-branch'))
287
288 # Future Contents
289 osutils.WriteFile(self.state, '22 master')
290 results = cbuildbot_launch.GetBuildrootState(self.root)
291 self.assertEqual(results, (22, 'master'))
292
293 # Read Write
294 cbuildbot_launch.SetBuildrootState('happy-branch', self.root)
295 results = cbuildbot_launch.GetBuildrootState(self.root)
296 self.assertEqual(results, (1, 'happy-branch'))
297
298 def testSetBuildrootState(self):
299 """Test SetBuildrootState."""
300 # Write out a state file.
301 osutils.SafeMakedirs(self.root)
302 cbuildbot_launch.SetBuildrootState('happy-branch', self.root)
303 self.assertEqual(osutils.ReadFile(self.state), '1 happy-branch')
304
305 # Change to a future version.
306 self.PatchObject(cbuildbot_launch, 'BUILDROOT_BUILDROOT_LAYOUT', 22)
307 cbuildbot_launch.SetBuildrootState('happy-branch', self.root)
308 self.assertEqual(osutils.ReadFile(self.state), '22 happy-branch')