blob: 5423bb7d8d5a1dae5090dd490fa28e6d122fe057 [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(
107 expected_cmd, cwd=options.buildroot, error_code_ok=True)
108
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,
144 [mock.call(None, '/buildroot')])
145
Don Garrett86881cb2017-02-15 15:41:55 -0800146 # Ensure we checkout, as expected.
147 self.assertEqual(mock_checkout.mock_calls,
148 [mock.call(None, '/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 Garrett86881cb2017-02-15 15:41:55 -0800154 cwd='/buildroot', error_code_ok=True)
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',
167 '--git-cache-dir', '/git-cache',
168 '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 Garrett597ddff2017-02-17 18:29:37 -0800172 [mock.call(None, '/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 Garrett597ddff2017-02-17 18:29:37 -0800176 [mock.call(None, '/buildroot', '/git-cache')])
Don Garrett86881cb2017-02-15 15:41:55 -0800177
178 # Ensure we invoke cbuildbot, as expected.
179 self.assertCommandCalled(
180 ['/buildroot/chromite/bin/cbuildbot',
Don Garrett597ddff2017-02-17 18:29:37 -0800181 'config',
182 '--buildroot', '/buildroot',
183 '--git-cache-dir', '/git-cache'],
Don Garrett86881cb2017-02-15 15:41:55 -0800184 cwd='/buildroot', error_code_ok=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800185
186
Don Garrett60967922017-04-12 18:51:44 -0700187class CleanBuildrootTest(cros_test_lib.MockTempDirTestCase):
Don Garrett7ade05a2017-02-17 13:31:47 -0800188 """Tests for CleanBuildroot method."""
189
190 def setUp(self):
191 """Create standard buildroot contents for cleanup."""
Don Garrette17e1d92017-04-12 15:28:19 -0700192 self.root = os.path.join(self.tempdir, 'buildroot')
193 self.state = os.path.join(self.root, '.cbuildbot_launch_state')
194 self.repo = os.path.join(self.root, '.repo/repo')
195 self.chroot = os.path.join(self.root, 'chroot/chroot')
196 self.general = os.path.join(self.root, 'general/general')
Don Garrett39963602017-02-27 14:41:58 -0800197 # TODO: Add .cache, and distfiles.
Don Garrett7ade05a2017-02-17 13:31:47 -0800198
Don Garrett60967922017-04-12 18:51:44 -0700199 def populateBuildroot(self, state=None):
Don Garrett7ade05a2017-02-17 13:31:47 -0800200 """Create standard buildroot contents for cleanup."""
Don Garrett60967922017-04-12 18:51:44 -0700201 osutils.SafeMakedirs(self.root)
Don Garrette17e1d92017-04-12 15:28:19 -0700202
Don Garrett7ade05a2017-02-17 13:31:47 -0800203 if state:
204 osutils.WriteFile(self.state, state)
205
206 # Create files.
207 for f in (self.repo, self.chroot, self.general):
Don Garrette17e1d92017-04-12 15:28:19 -0700208 osutils.Touch(f, makedirs=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800209
Don Garrette17e1d92017-04-12 15:28:19 -0700210 def testNoBuildroot(self):
Don Garrett7ade05a2017-02-17 13:31:47 -0800211 """Test CleanBuildroot with no history."""
Don Garrette17e1d92017-04-12 15:28:19 -0700212 cbuildbot_launch.CleanBuildroot(None, self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800213
Don Garrett60967922017-04-12 18:51:44 -0700214 self.assertEqual(osutils.ReadFile(self.state), '1 default')
Don Garrett7ade05a2017-02-17 13:31:47 -0800215
216 def testBuildrootNoState(self):
217 """Test CleanBuildroot with no state information."""
218 self.populateBuildroot()
219
Don Garrette17e1d92017-04-12 15:28:19 -0700220 cbuildbot_launch.CleanBuildroot(None, self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800221
Don Garrett60967922017-04-12 18:51:44 -0700222 self.assertEqual(osutils.ReadFile(self.state), '1 default')
223 self.assertNotExists(self.repo)
Don Garrett7ade05a2017-02-17 13:31:47 -0800224 self.assertNotExists(self.chroot)
Don Garrett60967922017-04-12 18:51:44 -0700225 self.assertNotExists(self.general)
226
227 def testBuildrootFormatMismatch(self):
228 """Test CleanBuildroot with no state information."""
229 self.populateBuildroot('0 default')
230
231 cbuildbot_launch.CleanBuildroot(None, self.root)
232
233 self.assertEqual(osutils.ReadFile(self.state), '1 default')
234 self.assertNotExists(self.repo)
235 self.assertNotExists(self.chroot)
236 self.assertNotExists(self.general)
Don Garrett7ade05a2017-02-17 13:31:47 -0800237
238 def testBuildrootBranchChange(self):
239 """Test CleanBuildroot with a change in branches."""
Don Garrett60967922017-04-12 18:51:44 -0700240 self.populateBuildroot('1 branchA')
Don Garrett7ade05a2017-02-17 13:31:47 -0800241
Don Garrette17e1d92017-04-12 15:28:19 -0700242 cbuildbot_launch.CleanBuildroot('branchB', self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800243
Don Garrett60967922017-04-12 18:51:44 -0700244 self.assertEqual(osutils.ReadFile(self.state), '1 branchB')
Don Garrett7ade05a2017-02-17 13:31:47 -0800245 self.assertExists(self.repo)
246 self.assertNotExists(self.chroot)
247 self.assertExists(self.general)
248
249 def testBuildrootBranchMatch(self):
250 """Test CleanBuildroot with no change in branch."""
Don Garrett60967922017-04-12 18:51:44 -0700251 self.populateBuildroot('1 branchA')
Don Garrett7ade05a2017-02-17 13:31:47 -0800252
Don Garrette17e1d92017-04-12 15:28:19 -0700253 cbuildbot_launch.CleanBuildroot('branchA', self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800254
Don Garrett60967922017-04-12 18:51:44 -0700255 self.assertEqual(osutils.ReadFile(self.state), '1 branchA')
Don Garrett7ade05a2017-02-17 13:31:47 -0800256 self.assertExists(self.repo)
257 self.assertExists(self.chroot)
258 self.assertExists(self.general)
Don Garrette17e1d92017-04-12 15:28:19 -0700259
Don Garrett60967922017-04-12 18:51:44 -0700260 def testBuildrootBranchChangeToDefault(self):
261 """Test CleanBuildroot with a change in branches."""
262 self.populateBuildroot('1 branchA')
Don Garrette17e1d92017-04-12 15:28:19 -0700263
264 cbuildbot_launch.CleanBuildroot(None, self.root)
265
Don Garrett60967922017-04-12 18:51:44 -0700266 self.assertEqual(osutils.ReadFile(self.state), '1 default')
267 self.assertExists(self.repo)
Don Garrette17e1d92017-04-12 15:28:19 -0700268 self.assertNotExists(self.chroot)
Don Garrett60967922017-04-12 18:51:44 -0700269 self.assertExists(self.general)
270
271 def testBuildrootBranchMatchDefault(self):
272 """Test CleanBuildroot with no change in branch."""
273 self.populateBuildroot('1 default')
274
275 cbuildbot_launch.CleanBuildroot(None, self.root)
276
277 self.assertEqual(osutils.ReadFile(self.state), '1 default')
278 self.assertExists(self.repo)
279 self.assertExists(self.chroot)
280 self.assertExists(self.general)
281
282 def testGetBuildrootState(self):
283 """Test GetBuildrootState."""
284 # No root dir.
285 results = cbuildbot_launch.GetBuildrootState(self.root)
286 self.assertEqual(results, (0, ''))
287
288 # Empty root dir.
289 osutils.SafeMakedirs(self.root)
290 results = cbuildbot_launch.GetBuildrootState(self.root)
291 self.assertEqual(results, (0, ''))
292
293 # Empty Contents
294 osutils.WriteFile(self.state, '')
295 results = cbuildbot_launch.GetBuildrootState(self.root)
296 self.assertEqual(results, (0, ''))
297
298 # Old Format Contents
299 osutils.WriteFile(self.state, 'happy-branch')
300 results = cbuildbot_launch.GetBuildrootState(self.root)
301 self.assertEqual(results, (0, ''))
302
303 # Expected Contents
304 osutils.WriteFile(self.state, '1 happy-branch')
305 results = cbuildbot_launch.GetBuildrootState(self.root)
306 self.assertEqual(results, (1, 'happy-branch'))
307
308 # Future Contents
309 osutils.WriteFile(self.state, '22 master')
310 results = cbuildbot_launch.GetBuildrootState(self.root)
311 self.assertEqual(results, (22, 'master'))
312
313 # Read Write
314 cbuildbot_launch.SetBuildrootState('happy-branch', self.root)
315 results = cbuildbot_launch.GetBuildrootState(self.root)
316 self.assertEqual(results, (1, 'happy-branch'))
317
318 def testSetBuildrootState(self):
319 """Test SetBuildrootState."""
320 # Write out a state file.
321 osutils.SafeMakedirs(self.root)
322 cbuildbot_launch.SetBuildrootState('happy-branch', self.root)
323 self.assertEqual(osutils.ReadFile(self.state), '1 happy-branch')
324
325 # Change to a future version.
326 self.PatchObject(cbuildbot_launch, 'BUILDROOT_BUILDROOT_LAYOUT', 22)
327 cbuildbot_launch.SetBuildrootState('happy-branch', self.root)
328 self.assertEqual(osutils.ReadFile(self.state), '22 happy-branch')