blob: c74708d5aead6bff6900f8abcf9c3dad7ab9a31b [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
17from chromite.scripts import bootstrap
18
19# pylint
20EXPECTED_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 Garrett597ddff2017-02-17 18:29:37 -080023class BootstrapTest(cros_test_lib.MockTestCase):
Don Garrettc4114cc2016-11-01 20:04:06 -070024 """Tests for bootstrap script."""
25
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
49 options = bootstrap.PreParseArguments(cmd_args)
50
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
59 bootstrap.InitialCheckout(None, '/buildroot', None)
60
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
71 bootstrap.InitialCheckout('release-R56-9000.B', '/buildroot', '/git-cache')
72
73 self.assertEqual(mock_repo.mock_calls, [
74 mock.call(EXPECTED_MANIFEST_URL, '/buildroot',
75 branch='release-R56-9000.B', git_cache_dir='/git-cache'),
76 mock.call().Sync()
77 ])
Don Garrettc4114cc2016-11-01 20:04:06 -070078
Don Garrett597ddff2017-02-17 18:29:37 -080079
80class RunTests(cros_build_lib_unittest.RunCommandTestCase):
81 """Tests for bootstrap script."""
82
83 ARGS_BASE = ['--buildroot', '/buildroot']
84 ARGS_GIT_CACHE = ['--git-cache-dir', '/git-cache']
85 ARGS_CONFIG = ['config']
86 CMD = ['/buildroot/chromite/bin/cbuildbot']
87
88 def verifyRunCbuildbot(self, args, expected_cmd, version):
Don Garrett86881cb2017-02-15 15:41:55 -080089 """Ensure we invoke cbuildbot correctly."""
Don Garrett597ddff2017-02-17 18:29:37 -080090 options = bootstrap.PreParseArguments(args)
91
92 self.PatchObject(
93 cros_build_lib, 'GetTargetChromiteApiVersion', autospec=True,
94 return_value=version)
95
96 bootstrap.RunCbuildbot(options)
97
98 self.assertCommandCalled(
99 expected_cmd, cwd=options.buildroot, error_code_ok=True)
100
101 def testRunCbuildbotSimple(self):
102 """Ensure we invoke cbuildbot correctly."""
103 self.verifyRunCbuildbot(
104 self.ARGS_BASE + self.ARGS_CONFIG,
105 self.CMD + self.ARGS_CONFIG + self.ARGS_BASE,
106 (0, 4))
107
108 def testRunCbuildbotNotFiltered(self):
109 """Ensure we invoke cbuildbot correctly."""
110 self.verifyRunCbuildbot(
111 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
112 self.CMD + self.ARGS_CONFIG + self.ARGS_BASE + self.ARGS_GIT_CACHE,
113 (0, 4))
114
115 def testRunCbuildbotFiltered(self):
116 """Ensure we invoke cbuildbot correctly."""
117 self.verifyRunCbuildbot(
118 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
119 self.CMD + self.ARGS_CONFIG + self.ARGS_BASE,
120 (0, 2))
Don Garrettc4114cc2016-11-01 20:04:06 -0700121
Don Garrett86881cb2017-02-15 15:41:55 -0800122 def testMainMin(self):
123 """Test a minimal set of command line options."""
Don Garrett597ddff2017-02-17 18:29:37 -0800124 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
125 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
126 autospec=True, return_value=(0, 4))
Don Garrett7ade05a2017-02-17 13:31:47 -0800127 mock_clean = self.PatchObject(bootstrap, 'CleanBuildroot', autospec=True)
Don Garrett86881cb2017-02-15 15:41:55 -0800128 mock_checkout = self.PatchObject(bootstrap, 'InitialCheckout',
129 autospec=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800130
Don Garrett597ddff2017-02-17 18:29:37 -0800131 bootstrap.main(['--buildroot', '/buildroot', 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700132
Don Garrett7ade05a2017-02-17 13:31:47 -0800133 # Ensure we clean, as expected.
134 self.assertEqual(mock_clean.mock_calls,
135 [mock.call(None, '/buildroot')])
136
Don Garrett86881cb2017-02-15 15:41:55 -0800137 # Ensure we checkout, as expected.
138 self.assertEqual(mock_checkout.mock_calls,
139 [mock.call(None, '/buildroot', None)])
Don Garrettc4114cc2016-11-01 20:04:06 -0700140
Don Garrett86881cb2017-02-15 15:41:55 -0800141 # Ensure we invoke cbuildbot, as expected.
142 self.assertCommandCalled(
143 ['/buildroot/chromite/bin/cbuildbot',
Don Garrett597ddff2017-02-17 18:29:37 -0800144 'config', '--buildroot', '/buildroot'],
Don Garrett86881cb2017-02-15 15:41:55 -0800145 cwd='/buildroot', error_code_ok=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700146
Don Garrett86881cb2017-02-15 15:41:55 -0800147 def testMainMax(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800148 """Test a larger set of command line options."""
149 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
150 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
151 autospec=True, return_value=(0, 4))
Don Garrett7ade05a2017-02-17 13:31:47 -0800152 mock_clean = self.PatchObject(bootstrap, 'CleanBuildroot', autospec=True)
Don Garrett86881cb2017-02-15 15:41:55 -0800153 mock_checkout = self.PatchObject(bootstrap, 'InitialCheckout',
154 autospec=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700155
Don Garrett597ddff2017-02-17 18:29:37 -0800156 bootstrap.main(['--buildroot', '/buildroot',
157 '--git-cache-dir', '/git-cache',
158 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700159
Don Garrett7ade05a2017-02-17 13:31:47 -0800160 # Ensure we clean, as expected.
161 self.assertEqual(mock_clean.mock_calls,
Don Garrett597ddff2017-02-17 18:29:37 -0800162 [mock.call(None, '/buildroot')])
Don Garrett7ade05a2017-02-17 13:31:47 -0800163
Don Garrett86881cb2017-02-15 15:41:55 -0800164 # Ensure we checkout, as expected.
165 self.assertEqual(mock_checkout.mock_calls,
Don Garrett597ddff2017-02-17 18:29:37 -0800166 [mock.call(None, '/buildroot', '/git-cache')])
Don Garrett86881cb2017-02-15 15:41:55 -0800167
168 # Ensure we invoke cbuildbot, as expected.
169 self.assertCommandCalled(
170 ['/buildroot/chromite/bin/cbuildbot',
Don Garrett597ddff2017-02-17 18:29:37 -0800171 'config',
172 '--buildroot', '/buildroot',
173 '--git-cache-dir', '/git-cache'],
Don Garrett86881cb2017-02-15 15:41:55 -0800174 cwd='/buildroot', error_code_ok=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800175
176
177class CleanBuildrootTest(cros_test_lib.TempDirTestCase):
178 """Tests for CleanBuildroot method."""
179
180 def setUp(self):
181 """Create standard buildroot contents for cleanup."""
182 self.state = os.path.join(self.tempdir, '.bootstrap_state')
183 self.repo = os.path.join(self.tempdir, '.repo/repo')
184 self.chroot = os.path.join(self.tempdir, 'chroot/chroot')
185 self.general = os.path.join(self.tempdir, 'general/general')
Don Garrett39963602017-02-27 14:41:58 -0800186 # TODO: Add .cache, and distfiles.
Don Garrett7ade05a2017-02-17 13:31:47 -0800187
188 def populateBuildroot(self, state=None):
189 """Create standard buildroot contents for cleanup."""
190 if state:
191 osutils.WriteFile(self.state, state)
192
193 # Create files.
194 for f in (self.repo, self.chroot, self.general):
195 osutils.Touch(os.path.join(self.tempdir, f), makedirs=True)
196
197 def testBuildrootEmpty(self):
198 """Test CleanBuildroot with no history."""
199 bootstrap.CleanBuildroot(None, self.tempdir)
200
201 self.assertEqual(osutils.ReadFile(self.state), 'TOT')
202
203 def testBuildrootNoState(self):
204 """Test CleanBuildroot with no state information."""
205 self.populateBuildroot()
206
207 bootstrap.CleanBuildroot(None, self.tempdir)
208
209 self.assertEqual(osutils.ReadFile(self.state), 'TOT')
210 self.assertExists(self.repo)
211 self.assertNotExists(self.chroot)
212 self.assertExists(self.general)
213
214 def testBuildrootBranchChange(self):
215 """Test CleanBuildroot with a change in branches."""
216 self.populateBuildroot('branchA')
217
218 bootstrap.CleanBuildroot('branchB', self.tempdir)
219
220 self.assertEqual(osutils.ReadFile(self.state), 'branchB')
221 self.assertExists(self.repo)
222 self.assertNotExists(self.chroot)
223 self.assertExists(self.general)
224
225 def testBuildrootBranchMatch(self):
226 """Test CleanBuildroot with no change in branch."""
227 self.populateBuildroot('branchA')
228
229 bootstrap.CleanBuildroot('branchA', self.tempdir)
230
231 self.assertEqual(osutils.ReadFile(self.state), 'branchA')
232 self.assertExists(self.repo)
233 self.assertExists(self.chroot)
234 self.assertExists(self.general)