blob: b7d82635ec2f05974b9f1936dfef2c8647dea391 [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):
85 self.originalSudo = cros_build_lib.STRICT_SUDO
86 self.originalUmask = os.umask(0) # Have to set it to read it, make it bogus
87
88 def teardown(self):
89 cros_build_lib.STRICT_SUDO = self.originalSudo
90 os.umask(self.originalUmask)
91
92 @unittest.skip("Global side effects break other tests. Run serially?")
93 def testConfigureGlobalEnvironment(self):
94 cros_build_lib.STRICT_SUDO = False
95
96 cbuildbot_launch.ConfigureGlobalEnvironment()
97
98 self.assertTrue(cros_build_lib.STRICT_SUDO)
99 self.assertEqual(os.umask(0), 0o22)
100
101
Don Garrett597ddff2017-02-17 18:29:37 -0800102class RunTests(cros_build_lib_unittest.RunCommandTestCase):
Don Garrett0c54ed72017-03-03 11:18:57 -0800103 """Tests for cbuildbot_launch script."""
Don Garrett597ddff2017-02-17 18:29:37 -0800104
105 ARGS_BASE = ['--buildroot', '/buildroot']
106 ARGS_GIT_CACHE = ['--git-cache-dir', '/git-cache']
107 ARGS_CONFIG = ['config']
108 CMD = ['/buildroot/chromite/bin/cbuildbot']
109
110 def verifyRunCbuildbot(self, args, expected_cmd, version):
Don Garrett86881cb2017-02-15 15:41:55 -0800111 """Ensure we invoke cbuildbot correctly."""
Don Garrett0c54ed72017-03-03 11:18:57 -0800112 options = cbuildbot_launch.PreParseArguments(args)
Don Garrett597ddff2017-02-17 18:29:37 -0800113
114 self.PatchObject(
115 cros_build_lib, 'GetTargetChromiteApiVersion', autospec=True,
116 return_value=version)
117
Don Garrett0c54ed72017-03-03 11:18:57 -0800118 cbuildbot_launch.RunCbuildbot(options)
Don Garrett597ddff2017-02-17 18:29:37 -0800119
120 self.assertCommandCalled(
121 expected_cmd, cwd=options.buildroot, error_code_ok=True)
122
123 def testRunCbuildbotSimple(self):
124 """Ensure we invoke cbuildbot correctly."""
125 self.verifyRunCbuildbot(
126 self.ARGS_BASE + self.ARGS_CONFIG,
127 self.CMD + self.ARGS_CONFIG + self.ARGS_BASE,
128 (0, 4))
129
130 def testRunCbuildbotNotFiltered(self):
131 """Ensure we invoke cbuildbot correctly."""
132 self.verifyRunCbuildbot(
133 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
134 self.CMD + self.ARGS_CONFIG + self.ARGS_BASE + self.ARGS_GIT_CACHE,
135 (0, 4))
136
137 def testRunCbuildbotFiltered(self):
138 """Ensure we invoke cbuildbot correctly."""
139 self.verifyRunCbuildbot(
140 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
141 self.CMD + self.ARGS_CONFIG + self.ARGS_BASE,
142 (0, 2))
Don Garrettc4114cc2016-11-01 20:04:06 -0700143
Don Garrett86881cb2017-02-15 15:41:55 -0800144 def testMainMin(self):
145 """Test a minimal set of command line options."""
Don Garrett597ddff2017-02-17 18:29:37 -0800146 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
147 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
148 autospec=True, return_value=(0, 4))
Don Garrett0c54ed72017-03-03 11:18:57 -0800149 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildroot',
150 autospec=True)
151 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800152 autospec=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800153
Don Garrett0c54ed72017-03-03 11:18:57 -0800154 cbuildbot_launch.main(['--buildroot', '/buildroot', 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700155
Don Garrett7ade05a2017-02-17 13:31:47 -0800156 # Ensure we clean, as expected.
157 self.assertEqual(mock_clean.mock_calls,
158 [mock.call(None, '/buildroot')])
159
Don Garrett86881cb2017-02-15 15:41:55 -0800160 # Ensure we checkout, as expected.
161 self.assertEqual(mock_checkout.mock_calls,
162 [mock.call(None, '/buildroot', None)])
Don Garrettc4114cc2016-11-01 20:04:06 -0700163
Don Garrett86881cb2017-02-15 15:41:55 -0800164 # Ensure we invoke cbuildbot, as expected.
165 self.assertCommandCalled(
166 ['/buildroot/chromite/bin/cbuildbot',
Don Garrett597ddff2017-02-17 18:29:37 -0800167 'config', '--buildroot', '/buildroot'],
Don Garrett86881cb2017-02-15 15:41:55 -0800168 cwd='/buildroot', error_code_ok=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700169
Don Garrett86881cb2017-02-15 15:41:55 -0800170 def testMainMax(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800171 """Test a larger set of command line options."""
172 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
173 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
174 autospec=True, return_value=(0, 4))
Don Garrett0c54ed72017-03-03 11:18:57 -0800175 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildroot',
176 autospec=True)
177 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800178 autospec=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700179
Don Garrett0c54ed72017-03-03 11:18:57 -0800180 cbuildbot_launch.main(['--buildroot', '/buildroot',
181 '--git-cache-dir', '/git-cache',
182 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700183
Don Garrett7ade05a2017-02-17 13:31:47 -0800184 # Ensure we clean, as expected.
185 self.assertEqual(mock_clean.mock_calls,
Don Garrett597ddff2017-02-17 18:29:37 -0800186 [mock.call(None, '/buildroot')])
Don Garrett7ade05a2017-02-17 13:31:47 -0800187
Don Garrett86881cb2017-02-15 15:41:55 -0800188 # Ensure we checkout, as expected.
189 self.assertEqual(mock_checkout.mock_calls,
Don Garrett597ddff2017-02-17 18:29:37 -0800190 [mock.call(None, '/buildroot', '/git-cache')])
Don Garrett86881cb2017-02-15 15:41:55 -0800191
192 # Ensure we invoke cbuildbot, as expected.
193 self.assertCommandCalled(
194 ['/buildroot/chromite/bin/cbuildbot',
Don Garrett597ddff2017-02-17 18:29:37 -0800195 'config',
196 '--buildroot', '/buildroot',
197 '--git-cache-dir', '/git-cache'],
Don Garrett86881cb2017-02-15 15:41:55 -0800198 cwd='/buildroot', error_code_ok=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800199
200
201class CleanBuildrootTest(cros_test_lib.TempDirTestCase):
202 """Tests for CleanBuildroot method."""
203
204 def setUp(self):
205 """Create standard buildroot contents for cleanup."""
Don Garrett0c54ed72017-03-03 11:18:57 -0800206 self.state = os.path.join(self.tempdir, '.cbuildbot_launch_state')
Don Garrett7ade05a2017-02-17 13:31:47 -0800207 self.repo = os.path.join(self.tempdir, '.repo/repo')
208 self.chroot = os.path.join(self.tempdir, 'chroot/chroot')
209 self.general = os.path.join(self.tempdir, 'general/general')
Don Garrett39963602017-02-27 14:41:58 -0800210 # TODO: Add .cache, and distfiles.
Don Garrett7ade05a2017-02-17 13:31:47 -0800211
212 def populateBuildroot(self, state=None):
213 """Create standard buildroot contents for cleanup."""
214 if state:
215 osutils.WriteFile(self.state, state)
216
217 # Create files.
218 for f in (self.repo, self.chroot, self.general):
219 osutils.Touch(os.path.join(self.tempdir, f), makedirs=True)
220
221 def testBuildrootEmpty(self):
222 """Test CleanBuildroot with no history."""
Don Garrett0c54ed72017-03-03 11:18:57 -0800223 cbuildbot_launch.CleanBuildroot(None, self.tempdir)
Don Garrett7ade05a2017-02-17 13:31:47 -0800224
225 self.assertEqual(osutils.ReadFile(self.state), 'TOT')
226
227 def testBuildrootNoState(self):
228 """Test CleanBuildroot with no state information."""
229 self.populateBuildroot()
230
Don Garrett0c54ed72017-03-03 11:18:57 -0800231 cbuildbot_launch.CleanBuildroot(None, self.tempdir)
Don Garrett7ade05a2017-02-17 13:31:47 -0800232
233 self.assertEqual(osutils.ReadFile(self.state), 'TOT')
234 self.assertExists(self.repo)
235 self.assertNotExists(self.chroot)
236 self.assertExists(self.general)
237
238 def testBuildrootBranchChange(self):
239 """Test CleanBuildroot with a change in branches."""
240 self.populateBuildroot('branchA')
241
Don Garrett0c54ed72017-03-03 11:18:57 -0800242 cbuildbot_launch.CleanBuildroot('branchB', self.tempdir)
Don Garrett7ade05a2017-02-17 13:31:47 -0800243
244 self.assertEqual(osutils.ReadFile(self.state), 'branchB')
245 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."""
251 self.populateBuildroot('branchA')
252
Don Garrett0c54ed72017-03-03 11:18:57 -0800253 cbuildbot_launch.CleanBuildroot('branchA', self.tempdir)
Don Garrett7ade05a2017-02-17 13:31:47 -0800254
255 self.assertEqual(osutils.ReadFile(self.state), 'branchA')
256 self.assertExists(self.repo)
257 self.assertExists(self.chroot)
258 self.assertExists(self.general)