blob: 38319eb8499498f051d1d615d9c2f03db0976e8a [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
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 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 Garrett597ddff2017-02-17 18:29:37 -080080
81class RunTests(cros_build_lib_unittest.RunCommandTestCase):
Don Garrett0c54ed72017-03-03 11:18:57 -080082 """Tests for cbuildbot_launch script."""
Don Garrett597ddff2017-02-17 18:29:37 -080083
84 ARGS_BASE = ['--buildroot', '/buildroot']
85 ARGS_GIT_CACHE = ['--git-cache-dir', '/git-cache']
86 ARGS_CONFIG = ['config']
87 CMD = ['/buildroot/chromite/bin/cbuildbot']
88
89 def verifyRunCbuildbot(self, args, expected_cmd, version):
Don Garrett86881cb2017-02-15 15:41:55 -080090 """Ensure we invoke cbuildbot correctly."""
Don Garrett0c54ed72017-03-03 11:18:57 -080091 options = cbuildbot_launch.PreParseArguments(args)
Don Garrett597ddff2017-02-17 18:29:37 -080092
93 self.PatchObject(
94 cros_build_lib, 'GetTargetChromiteApiVersion', autospec=True,
95 return_value=version)
96
Don Garrett0c54ed72017-03-03 11:18:57 -080097 cbuildbot_launch.RunCbuildbot(options)
Don Garrett597ddff2017-02-17 18:29:37 -080098
99 self.assertCommandCalled(
100 expected_cmd, cwd=options.buildroot, error_code_ok=True)
101
102 def testRunCbuildbotSimple(self):
103 """Ensure we invoke cbuildbot correctly."""
104 self.verifyRunCbuildbot(
105 self.ARGS_BASE + self.ARGS_CONFIG,
106 self.CMD + self.ARGS_CONFIG + self.ARGS_BASE,
107 (0, 4))
108
109 def testRunCbuildbotNotFiltered(self):
110 """Ensure we invoke cbuildbot correctly."""
111 self.verifyRunCbuildbot(
112 self.ARGS_BASE + self.ARGS_CONFIG + self.ARGS_GIT_CACHE,
113 self.CMD + self.ARGS_CONFIG + self.ARGS_BASE + self.ARGS_GIT_CACHE,
114 (0, 4))
115
116 def testRunCbuildbotFiltered(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,
121 (0, 2))
Don Garrettc4114cc2016-11-01 20:04:06 -0700122
Don Garrett86881cb2017-02-15 15:41:55 -0800123 def testMainMin(self):
124 """Test a minimal set of command line options."""
Don Garrett597ddff2017-02-17 18:29:37 -0800125 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
126 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
127 autospec=True, return_value=(0, 4))
Don Garrett0c54ed72017-03-03 11:18:57 -0800128 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildroot',
129 autospec=True)
130 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800131 autospec=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800132
Don Garrett0c54ed72017-03-03 11:18:57 -0800133 cbuildbot_launch.main(['--buildroot', '/buildroot', 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700134
Don Garrett7ade05a2017-02-17 13:31:47 -0800135 # Ensure we clean, as expected.
136 self.assertEqual(mock_clean.mock_calls,
137 [mock.call(None, '/buildroot')])
138
Don Garrett86881cb2017-02-15 15:41:55 -0800139 # Ensure we checkout, as expected.
140 self.assertEqual(mock_checkout.mock_calls,
141 [mock.call(None, '/buildroot', None)])
Don Garrettc4114cc2016-11-01 20:04:06 -0700142
Don Garrett86881cb2017-02-15 15:41:55 -0800143 # Ensure we invoke cbuildbot, as expected.
144 self.assertCommandCalled(
145 ['/buildroot/chromite/bin/cbuildbot',
Don Garrett597ddff2017-02-17 18:29:37 -0800146 'config', '--buildroot', '/buildroot'],
Don Garrett86881cb2017-02-15 15:41:55 -0800147 cwd='/buildroot', error_code_ok=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700148
Don Garrett86881cb2017-02-15 15:41:55 -0800149 def testMainMax(self):
Don Garrett597ddff2017-02-17 18:29:37 -0800150 """Test a larger set of command line options."""
151 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
152 self.PatchObject(cros_build_lib, 'GetTargetChromiteApiVersion',
153 autospec=True, return_value=(0, 4))
Don Garrett0c54ed72017-03-03 11:18:57 -0800154 mock_clean = self.PatchObject(cbuildbot_launch, 'CleanBuildroot',
155 autospec=True)
156 mock_checkout = self.PatchObject(cbuildbot_launch, 'InitialCheckout',
Don Garrett86881cb2017-02-15 15:41:55 -0800157 autospec=True)
Don Garrettc4114cc2016-11-01 20:04:06 -0700158
Don Garrett0c54ed72017-03-03 11:18:57 -0800159 cbuildbot_launch.main(['--buildroot', '/buildroot',
160 '--git-cache-dir', '/git-cache',
161 'config'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700162
Don Garrett7ade05a2017-02-17 13:31:47 -0800163 # Ensure we clean, as expected.
164 self.assertEqual(mock_clean.mock_calls,
Don Garrett597ddff2017-02-17 18:29:37 -0800165 [mock.call(None, '/buildroot')])
Don Garrett7ade05a2017-02-17 13:31:47 -0800166
Don Garrett86881cb2017-02-15 15:41:55 -0800167 # Ensure we checkout, as expected.
168 self.assertEqual(mock_checkout.mock_calls,
Don Garrett597ddff2017-02-17 18:29:37 -0800169 [mock.call(None, '/buildroot', '/git-cache')])
Don Garrett86881cb2017-02-15 15:41:55 -0800170
171 # Ensure we invoke cbuildbot, as expected.
172 self.assertCommandCalled(
173 ['/buildroot/chromite/bin/cbuildbot',
Don Garrett597ddff2017-02-17 18:29:37 -0800174 'config',
175 '--buildroot', '/buildroot',
176 '--git-cache-dir', '/git-cache'],
Don Garrett86881cb2017-02-15 15:41:55 -0800177 cwd='/buildroot', error_code_ok=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800178
179
180class CleanBuildrootTest(cros_test_lib.TempDirTestCase):
181 """Tests for CleanBuildroot method."""
182
183 def setUp(self):
184 """Create standard buildroot contents for cleanup."""
Don Garrett0c54ed72017-03-03 11:18:57 -0800185 self.state = os.path.join(self.tempdir, '.cbuildbot_launch_state')
Don Garrett7ade05a2017-02-17 13:31:47 -0800186 self.repo = os.path.join(self.tempdir, '.repo/repo')
187 self.chroot = os.path.join(self.tempdir, 'chroot/chroot')
188 self.general = os.path.join(self.tempdir, 'general/general')
Don Garrett39963602017-02-27 14:41:58 -0800189 # TODO: Add .cache, and distfiles.
Don Garrett7ade05a2017-02-17 13:31:47 -0800190
191 def populateBuildroot(self, state=None):
192 """Create standard buildroot contents for cleanup."""
193 if state:
194 osutils.WriteFile(self.state, state)
195
196 # Create files.
197 for f in (self.repo, self.chroot, self.general):
198 osutils.Touch(os.path.join(self.tempdir, f), makedirs=True)
199
200 def testBuildrootEmpty(self):
201 """Test CleanBuildroot with no history."""
Don Garrett0c54ed72017-03-03 11:18:57 -0800202 cbuildbot_launch.CleanBuildroot(None, self.tempdir)
Don Garrett7ade05a2017-02-17 13:31:47 -0800203
204 self.assertEqual(osutils.ReadFile(self.state), 'TOT')
205
206 def testBuildrootNoState(self):
207 """Test CleanBuildroot with no state information."""
208 self.populateBuildroot()
209
Don Garrett0c54ed72017-03-03 11:18:57 -0800210 cbuildbot_launch.CleanBuildroot(None, self.tempdir)
Don Garrett7ade05a2017-02-17 13:31:47 -0800211
212 self.assertEqual(osutils.ReadFile(self.state), 'TOT')
213 self.assertExists(self.repo)
214 self.assertNotExists(self.chroot)
215 self.assertExists(self.general)
216
217 def testBuildrootBranchChange(self):
218 """Test CleanBuildroot with a change in branches."""
219 self.populateBuildroot('branchA')
220
Don Garrett0c54ed72017-03-03 11:18:57 -0800221 cbuildbot_launch.CleanBuildroot('branchB', self.tempdir)
Don Garrett7ade05a2017-02-17 13:31:47 -0800222
223 self.assertEqual(osutils.ReadFile(self.state), 'branchB')
224 self.assertExists(self.repo)
225 self.assertNotExists(self.chroot)
226 self.assertExists(self.general)
227
228 def testBuildrootBranchMatch(self):
229 """Test CleanBuildroot with no change in branch."""
230 self.populateBuildroot('branchA')
231
Don Garrett0c54ed72017-03-03 11:18:57 -0800232 cbuildbot_launch.CleanBuildroot('branchA', self.tempdir)
Don Garrett7ade05a2017-02-17 13:31:47 -0800233
234 self.assertEqual(osutils.ReadFile(self.state), 'branchA')
235 self.assertExists(self.repo)
236 self.assertExists(self.chroot)
237 self.assertExists(self.general)