blob: 7d94127a00019841f47b638dd0ddbb8cf113a892 [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 Garrettf15d65b2017-04-12 12:39:55 -070011import unittest
Don Garrett86881cb2017-02-15 15:41:55 -080012
13from chromite.cbuildbot import repository
Don Garrett597ddff2017-02-17 18:29:37 -080014from chromite.lib import cros_build_lib
Don Garrettc4114cc2016-11-01 20:04:06 -070015from chromite.lib import cros_build_lib_unittest
Don Garrett7ade05a2017-02-17 13:31:47 -080016from chromite.lib import cros_test_lib
Don Garrett86881cb2017-02-15 15:41:55 -080017from chromite.lib import osutils
Don Garrett0c54ed72017-03-03 11:18:57 -080018from chromite.scripts import cbuildbot_launch
Don Garrett86881cb2017-02-15 15:41:55 -080019
20# pylint
21EXPECTED_MANIFEST_URL = 'https://chrome-internal-review.googlesource.com/chromeos/manifest-internal' # pylint: disable=line-too-long
Don Garrettc4114cc2016-11-01 20:04:06 -070022
23
Don Garrett0c54ed72017-03-03 11:18:57 -080024class CbuildbotLaunchTest(cros_test_lib.MockTestCase):
25 """Tests for cbuildbot_launch script."""
Don Garrettc4114cc2016-11-01 20:04:06 -070026
Don Garrett86881cb2017-02-15 15:41:55 -080027 def testPreParseArguments(self):
Don Garrettc4114cc2016-11-01 20:04:06 -070028 """Test that we can correctly extract branch values from cbuildbot args."""
Don Garrett597ddff2017-02-17 18:29:37 -080029 CASES = (
30 (['--buildroot', '/buildroot', 'daisy-incremental'],
31 (None, '/buildroot', None)),
32
33 (['--buildbot', '--buildroot', '/buildroot',
34 '--git-cache-dir', '/git-cache',
35 '-b', 'release-R57-9202.B',
36 'daisy-incremental'],
37 ('release-R57-9202.B', '/buildroot', '/git-cache')),
38
39 (['--debug', '--buildbot', '--notests',
40 '--buildroot', '/buildroot',
41 '--git-cache-dir', '/git-cache',
42 '--branch', 'release-R57-9202.B',
43 'daisy-incremental'],
44 ('release-R57-9202.B', '/buildroot', '/git-cache')),
Don Garrettc4114cc2016-11-01 20:04:06 -070045 )
46
Don Garrett597ddff2017-02-17 18:29:37 -080047 for cmd_args, expected in CASES:
48 expected_branch, expected_buildroot, expected_cache_dir = expected
49
Don Garrett0c54ed72017-03-03 11:18:57 -080050 options = cbuildbot_launch.PreParseArguments(cmd_args)
Don Garrett597ddff2017-02-17 18:29:37 -080051
52 self.assertEqual(options.branch, expected_branch)
53 self.assertEqual(options.buildroot, expected_buildroot)
54 self.assertEqual(options.git_cache_dir, expected_cache_dir)
Don Garrett86881cb2017-02-15 15:41:55 -080055
56 def testInitialCheckoutMin(self):
57 """Test InitialCheckout with minimum settings."""
58 mock_repo = self.PatchObject(repository, 'RepoRepository', autospec=True)
Don Garrett86881cb2017-02-15 15:41:55 -080059
Don Garrett0c54ed72017-03-03 11:18:57 -080060 cbuildbot_launch.InitialCheckout(None, '/buildroot', None)
Don Garrett86881cb2017-02-15 15:41:55 -080061
62 self.assertEqual(mock_repo.mock_calls, [
63 mock.call(EXPECTED_MANIFEST_URL, '/buildroot',
64 branch=None, git_cache_dir=None),
65 mock.call().Sync()
66 ])
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'),
78 mock.call().Sync()
79 ])
Don Garrettc4114cc2016-11-01 20:04:06 -070080
Don Garrett597ddff2017-02-17 18:29:37 -080081
Don Garrettf15d65b2017-04-12 12:39:55 -070082class CbuildbotLaunchGlobalTest(cros_test_lib.TestCase):
83 """Validate our global setup function."""
84 def setUp(self):
Don Garrettf15d65b2017-04-12 12:39:55 -070085 self.originalUmask = os.umask(0) # Have to set it to read it, make it bogus
86
87 def teardown(self):
Don Garrettf15d65b2017-04-12 12:39:55 -070088 os.umask(self.originalUmask)
89
90 @unittest.skip("Global side effects break other tests. Run serially?")
91 def testConfigureGlobalEnvironment(self):
Don Garrettf15d65b2017-04-12 12:39:55 -070092 cbuildbot_launch.ConfigureGlobalEnvironment()
93
Don Garrettf15d65b2017-04-12 12:39:55 -070094 self.assertEqual(os.umask(0), 0o22)
95
96
Don Garrett597ddff2017-02-17 18:29:37 -080097class RunTests(cros_build_lib_unittest.RunCommandTestCase):
Don Garrett0c54ed72017-03-03 11:18:57 -080098 """Tests for cbuildbot_launch script."""
Don Garrett597ddff2017-02-17 18:29:37 -080099
100 ARGS_BASE = ['--buildroot', '/buildroot']
101 ARGS_GIT_CACHE = ['--git-cache-dir', '/git-cache']
102 ARGS_CONFIG = ['config']
103 CMD = ['/buildroot/chromite/bin/cbuildbot']
104
105 def verifyRunCbuildbot(self, args, expected_cmd, version):
Don Garrett86881cb2017-02-15 15:41:55 -0800106 """Ensure we invoke cbuildbot correctly."""
Don Garrett0c54ed72017-03-03 11:18:57 -0800107 options = cbuildbot_launch.PreParseArguments(args)
Don Garrett597ddff2017-02-17 18:29:37 -0800108
109 self.PatchObject(
110 cros_build_lib, 'GetTargetChromiteApiVersion', autospec=True,
111 return_value=version)
112
Don Garrett0c54ed72017-03-03 11:18:57 -0800113 cbuildbot_launch.RunCbuildbot(options)
Don Garrett597ddff2017-02-17 18:29:37 -0800114
115 self.assertCommandCalled(
116 expected_cmd, cwd=options.buildroot, error_code_ok=True)
117
118 def testRunCbuildbotSimple(self):
119 """Ensure we invoke cbuildbot correctly."""
120 self.verifyRunCbuildbot(
121 self.ARGS_BASE + self.ARGS_CONFIG,
122 self.CMD + self.ARGS_CONFIG + self.ARGS_BASE,
123 (0, 4))
124
125 def testRunCbuildbotNotFiltered(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 + self.ARGS_GIT_CACHE,
130 (0, 4))
131
132 def testRunCbuildbotFiltered(self):
133 """Ensure we invoke cbuildbot correctly."""
134 self.verifyRunCbuildbot(
135 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
136 self.CMD + self.ARGS_CONFIG + self.ARGS_BASE,
137 (0, 2))
Don Garrettc4114cc2016-11-01 20:04:06 -0700138
Don Garrett86881cb2017-02-15 15:41:55 -0800139 def testMainMin(self):
140 """Test a minimal set of command line options."""
Don Garrett597ddff2017-02-17 18:29:37 -0800141 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
142 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
143 autospec=True, return_value=(0, 4))
Don Garrett0c54ed72017-03-03 11:18:57 -0800144 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildroot',
145 autospec=True)
146 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800147 autospec=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800148
Don Garrett0c54ed72017-03-03 11:18:57 -0800149 cbuildbot_launch.main(['--buildroot', '/buildroot', 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700150
Don Garrett7ade05a2017-02-17 13:31:47 -0800151 # Ensure we clean, as expected.
152 self.assertEqual(mock_clean.mock_calls,
153 [mock.call(None, '/buildroot')])
154
Don Garrett86881cb2017-02-15 15:41:55 -0800155 # Ensure we checkout, as expected.
156 self.assertEqual(mock_checkout.mock_calls,
157 [mock.call(None, '/buildroot', None)])
Don Garrettc4114cc2016-11-01 20:04:06 -0700158
Don Garrett86881cb2017-02-15 15:41:55 -0800159 # Ensure we invoke cbuildbot, as expected.
160 self.assertCommandCalled(
161 ['/buildroot/chromite/bin/cbuildbot',
Don Garrett597ddff2017-02-17 18:29:37 -0800162 'config', '--buildroot', '/buildroot'],
Don Garrett86881cb2017-02-15 15:41:55 -0800163 cwd='/buildroot', error_code_ok=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700164
Don Garrett86881cb2017-02-15 15:41:55 -0800165 def testMainMax(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800166 """Test a larger set of command line options."""
167 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
168 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
169 autospec=True, return_value=(0, 4))
Don Garrett0c54ed72017-03-03 11:18:57 -0800170 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildroot',
171 autospec=True)
172 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800173 autospec=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700174
Don Garrett0c54ed72017-03-03 11:18:57 -0800175 cbuildbot_launch.main(['--buildroot', '/buildroot',
176 '--git-cache-dir', '/git-cache',
177 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700178
Don Garrett7ade05a2017-02-17 13:31:47 -0800179 # Ensure we clean, as expected.
180 self.assertEqual(mock_clean.mock_calls,
Don Garrett597ddff2017-02-17 18:29:37 -0800181 [mock.call(None, '/buildroot')])
Don Garrett7ade05a2017-02-17 13:31:47 -0800182
Don Garrett86881cb2017-02-15 15:41:55 -0800183 # Ensure we checkout, as expected.
184 self.assertEqual(mock_checkout.mock_calls,
Don Garrett597ddff2017-02-17 18:29:37 -0800185 [mock.call(None, '/buildroot', '/git-cache')])
Don Garrett86881cb2017-02-15 15:41:55 -0800186
187 # Ensure we invoke cbuildbot, as expected.
188 self.assertCommandCalled(
189 ['/buildroot/chromite/bin/cbuildbot',
Don Garrett597ddff2017-02-17 18:29:37 -0800190 'config',
191 '--buildroot', '/buildroot',
192 '--git-cache-dir', '/git-cache'],
Don Garrett86881cb2017-02-15 15:41:55 -0800193 cwd='/buildroot', error_code_ok=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800194
195
196class CleanBuildrootTest(cros_test_lib.TempDirTestCase):
197 """Tests for CleanBuildroot method."""
198
199 def setUp(self):
200 """Create standard buildroot contents for cleanup."""
Don Garrette17e1d92017-04-12 15:28:19 -0700201 self.root = os.path.join(self.tempdir, 'buildroot')
202 self.state = os.path.join(self.root, '.cbuildbot_launch_state')
203 self.repo = os.path.join(self.root, '.repo/repo')
204 self.chroot = os.path.join(self.root, 'chroot/chroot')
205 self.general = os.path.join(self.root, 'general/general')
Don Garrett39963602017-02-27 14:41:58 -0800206 # TODO: Add .cache, and distfiles.
Don Garrett7ade05a2017-02-17 13:31:47 -0800207
Don Garrette17e1d92017-04-12 15:28:19 -0700208 def populateBuildroot(self, state=None, group_readable=True):
Don Garrett7ade05a2017-02-17 13:31:47 -0800209 """Create standard buildroot contents for cleanup."""
Don Garrette17e1d92017-04-12 15:28:19 -0700210 buildroot_mode = 0o775 if group_readable else 0o700
211 osutils.SafeMakedirs(self.root, mode=buildroot_mode)
212
Don Garrett7ade05a2017-02-17 13:31:47 -0800213 if state:
214 osutils.WriteFile(self.state, state)
215
216 # Create files.
217 for f in (self.repo, self.chroot, self.general):
Don Garrette17e1d92017-04-12 15:28:19 -0700218 osutils.Touch(f, makedirs=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800219
Don Garrette17e1d92017-04-12 15:28:19 -0700220 def testNoBuildroot(self):
Don Garrett7ade05a2017-02-17 13:31:47 -0800221 """Test CleanBuildroot with no history."""
Don Garrette17e1d92017-04-12 15:28:19 -0700222 cbuildbot_launch.CleanBuildroot(None, self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800223
224 self.assertEqual(osutils.ReadFile(self.state), 'TOT')
225
226 def testBuildrootNoState(self):
227 """Test CleanBuildroot with no state information."""
228 self.populateBuildroot()
229
Don Garrette17e1d92017-04-12 15:28:19 -0700230 cbuildbot_launch.CleanBuildroot(None, self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800231
232 self.assertEqual(osutils.ReadFile(self.state), 'TOT')
233 self.assertExists(self.repo)
234 self.assertNotExists(self.chroot)
235 self.assertExists(self.general)
236
237 def testBuildrootBranchChange(self):
238 """Test CleanBuildroot with a change in branches."""
239 self.populateBuildroot('branchA')
240
Don Garrette17e1d92017-04-12 15:28:19 -0700241 cbuildbot_launch.CleanBuildroot('branchB', self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800242
243 self.assertEqual(osutils.ReadFile(self.state), 'branchB')
244 self.assertExists(self.repo)
245 self.assertNotExists(self.chroot)
246 self.assertExists(self.general)
247
248 def testBuildrootBranchMatch(self):
249 """Test CleanBuildroot with no change in branch."""
250 self.populateBuildroot('branchA')
251
Don Garrette17e1d92017-04-12 15:28:19 -0700252 cbuildbot_launch.CleanBuildroot('branchA', self.root)
Don Garrett7ade05a2017-02-17 13:31:47 -0800253
254 self.assertEqual(osutils.ReadFile(self.state), 'branchA')
255 self.assertExists(self.repo)
256 self.assertExists(self.chroot)
257 self.assertExists(self.general)
Don Garrette17e1d92017-04-12 15:28:19 -0700258
259
260 def testBuildrootNotGroupReadable(self):
261 """Test CleanBuildroot with no state information."""
262 self.populateBuildroot(group_readable=False)
263
264 cbuildbot_launch.CleanBuildroot(None, self.root)
265
266 self.assertEqual(osutils.ReadFile(self.state), 'TOT')
267 self.assertNotExists(self.repo)
268 self.assertNotExists(self.chroot)
269 self.assertNotExists(self.general)