blob: 2e8c221ca3c2366856a8ee1bcf22b2e4a50f970f [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')
186
187 def populateBuildroot(self, state=None):
188 """Create standard buildroot contents for cleanup."""
189 if state:
190 osutils.WriteFile(self.state, state)
191
192 # Create files.
193 for f in (self.repo, self.chroot, self.general):
194 osutils.Touch(os.path.join(self.tempdir, f), makedirs=True)
195
196 def testBuildrootEmpty(self):
197 """Test CleanBuildroot with no history."""
198 bootstrap.CleanBuildroot(None, self.tempdir)
199
200 self.assertEqual(osutils.ReadFile(self.state), 'TOT')
201
202 def testBuildrootNoState(self):
203 """Test CleanBuildroot with no state information."""
204 self.populateBuildroot()
205
206 bootstrap.CleanBuildroot(None, self.tempdir)
207
208 self.assertEqual(osutils.ReadFile(self.state), 'TOT')
209 self.assertExists(self.repo)
210 self.assertNotExists(self.chroot)
211 self.assertExists(self.general)
212
213 def testBuildrootBranchChange(self):
214 """Test CleanBuildroot with a change in branches."""
215 self.populateBuildroot('branchA')
216
217 bootstrap.CleanBuildroot('branchB', self.tempdir)
218
219 self.assertEqual(osutils.ReadFile(self.state), 'branchB')
220 self.assertExists(self.repo)
221 self.assertNotExists(self.chroot)
222 self.assertExists(self.general)
223
224 def testBuildrootBranchMatch(self):
225 """Test CleanBuildroot with no change in branch."""
226 self.populateBuildroot('branchA')
227
228 bootstrap.CleanBuildroot('branchA', self.tempdir)
229
230 self.assertEqual(osutils.ReadFile(self.state), 'branchA')
231 self.assertExists(self.repo)
232 self.assertExists(self.chroot)
233 self.assertExists(self.general)