blob: cb631b929ca0e94b03fc1a06ff25975664d7759f [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 Garrettc4114cc2016-11-01 20:04:06 -070013from chromite.lib import cros_build_lib_unittest
Don Garrett7ade05a2017-02-17 13:31:47 -080014from chromite.lib import cros_test_lib
Don Garrett86881cb2017-02-15 15:41:55 -080015from chromite.lib import osutils
16from chromite.scripts import bootstrap
17
18# pylint
19EXPECTED_MANIFEST_URL = 'https://chrome-internal-review.googlesource.com/chromeos/manifest-internal' # pylint: disable=line-too-long
Don Garrettc4114cc2016-11-01 20:04:06 -070020
21
22class BootstrapTest(cros_build_lib_unittest.RunCommandTestCase):
23 """Tests for bootstrap script."""
24
Don Garrett86881cb2017-02-15 15:41:55 -080025 def testPreParseArguments(self):
Don Garrettc4114cc2016-11-01 20:04:06 -070026 """Test that we can correctly extract branch values from cbuildbot args."""
27 cases = (
Don Garrett7ade05a2017-02-17 13:31:47 -080028 (['--buildroot', '/build'], None, '/build', None),
29 (['--branch', 'branch', '-r', '/build'], 'branch', '/build', None),
30 (['-r', '/build', '-b', 'branch', 'config'], 'branch', '/build', None),
Don Garrettc4114cc2016-11-01 20:04:06 -070031 )
32
Don Garrett86881cb2017-02-15 15:41:55 -080033 for args, expected_branch, expected_root, expected_git_cache in cases:
34 result = bootstrap.PreParseArguments(args)
35 self.assertEqual(result.branch, expected_branch)
36 self.assertEqual(result.buildroot, expected_root)
37 self.assertEqual(result.git_cache_dir, expected_git_cache)
38
39 def testInitialCheckoutMin(self):
40 """Test InitialCheckout with minimum settings."""
41 mock_repo = self.PatchObject(repository, 'RepoRepository', autospec=True)
Don Garrett86881cb2017-02-15 15:41:55 -080042
43 bootstrap.InitialCheckout(None, '/buildroot', None)
44
45 self.assertEqual(mock_repo.mock_calls, [
46 mock.call(EXPECTED_MANIFEST_URL, '/buildroot',
47 branch=None, git_cache_dir=None),
48 mock.call().Sync()
49 ])
50
51 def testInitialCheckoutMax(self):
52 """Test InitialCheckout with all settings."""
53 mock_repo = self.PatchObject(repository, 'RepoRepository', autospec=True)
Don Garrett86881cb2017-02-15 15:41:55 -080054
55 bootstrap.InitialCheckout('release-R56-9000.B', '/buildroot', '/git-cache')
56
57 self.assertEqual(mock_repo.mock_calls, [
58 mock.call(EXPECTED_MANIFEST_URL, '/buildroot',
59 branch='release-R56-9000.B', git_cache_dir='/git-cache'),
60 mock.call().Sync()
61 ])
Don Garrettc4114cc2016-11-01 20:04:06 -070062
63 def testRunCbuildbot(self):
Don Garrett86881cb2017-02-15 15:41:55 -080064 """Ensure we invoke cbuildbot correctly."""
65 bootstrap.RunCbuildbot('/buildroot', ['foo', 'bar', 'arg'])
Don Garrettc4114cc2016-11-01 20:04:06 -070066 self.assertCommandContains(
Don Garrett86881cb2017-02-15 15:41:55 -080067 ['/buildroot/chromite/bin/cbuildbot', 'foo', 'bar', 'arg'])
Don Garrettc4114cc2016-11-01 20:04:06 -070068
Don Garrett86881cb2017-02-15 15:41:55 -080069 def testMainMin(self):
70 """Test a minimal set of command line options."""
Don Garrett7ade05a2017-02-17 13:31:47 -080071 mock_clean = self.PatchObject(bootstrap, 'CleanBuildroot', autospec=True)
Don Garrett86881cb2017-02-15 15:41:55 -080072 mock_checkout = self.PatchObject(bootstrap, 'InitialCheckout',
73 autospec=True)
Don Garrett7ade05a2017-02-17 13:31:47 -080074 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
75
Don Garrettc4114cc2016-11-01 20:04:06 -070076
Don Garrett86881cb2017-02-15 15:41:55 -080077 bootstrap.main(['--buildroot', '/buildroot', 'foo'])
Don Garrettc4114cc2016-11-01 20:04:06 -070078
Don Garrett7ade05a2017-02-17 13:31:47 -080079 # Ensure we clean, as expected.
80 self.assertEqual(mock_clean.mock_calls,
81 [mock.call(None, '/buildroot')])
82
Don Garrett86881cb2017-02-15 15:41:55 -080083 # Ensure we checkout, as expected.
84 self.assertEqual(mock_checkout.mock_calls,
85 [mock.call(None, '/buildroot', None)])
Don Garrettc4114cc2016-11-01 20:04:06 -070086
Don Garrett86881cb2017-02-15 15:41:55 -080087 # Ensure we invoke cbuildbot, as expected.
88 self.assertCommandCalled(
89 ['/buildroot/chromite/bin/cbuildbot',
90 '--buildroot', '/buildroot', 'foo'],
91 cwd='/buildroot', error_code_ok=True)
Don Garrettc4114cc2016-11-01 20:04:06 -070092
Don Garrett86881cb2017-02-15 15:41:55 -080093 def testMainMax(self):
94 """Test a maximal set of command line options."""
Don Garrett7ade05a2017-02-17 13:31:47 -080095 mock_clean = self.PatchObject(bootstrap, 'CleanBuildroot', autospec=True)
Don Garrett86881cb2017-02-15 15:41:55 -080096 mock_checkout = self.PatchObject(bootstrap, 'InitialCheckout',
97 autospec=True)
Don Garrett7ade05a2017-02-17 13:31:47 -080098 self.PatchObject(osutils, 'SafeMakedirs', autospec=True)
Don Garrettc4114cc2016-11-01 20:04:06 -070099
Don Garrett86881cb2017-02-15 15:41:55 -0800100 bootstrap.main(['--buildroot', '/buildroot', '--branch', 'branch',
101 '--git-cache-dir', '/git-cache', 'foo'])
Don Garrettc4114cc2016-11-01 20:04:06 -0700102
Don Garrett7ade05a2017-02-17 13:31:47 -0800103 # Ensure we clean, as expected.
104 self.assertEqual(mock_clean.mock_calls,
105 [mock.call('branch', '/buildroot')])
106
Don Garrett86881cb2017-02-15 15:41:55 -0800107 # Ensure we checkout, as expected.
108 self.assertEqual(mock_checkout.mock_calls,
109 [mock.call('branch', '/buildroot', '/git-cache')])
110
111 # Ensure we invoke cbuildbot, as expected.
112 self.assertCommandCalled(
113 ['/buildroot/chromite/bin/cbuildbot',
114 '--buildroot', '/buildroot', '--branch', 'branch',
115 '--git-cache-dir', '/git-cache', 'foo'],
116 cwd='/buildroot', error_code_ok=True)
Don Garrett7ade05a2017-02-17 13:31:47 -0800117
118
119class CleanBuildrootTest(cros_test_lib.TempDirTestCase):
120 """Tests for CleanBuildroot method."""
121
122 def setUp(self):
123 """Create standard buildroot contents for cleanup."""
124 self.state = os.path.join(self.tempdir, '.bootstrap_state')
125 self.repo = os.path.join(self.tempdir, '.repo/repo')
126 self.chroot = os.path.join(self.tempdir, 'chroot/chroot')
127 self.general = os.path.join(self.tempdir, 'general/general')
128
129 def populateBuildroot(self, state=None):
130 """Create standard buildroot contents for cleanup."""
131 if state:
132 osutils.WriteFile(self.state, state)
133
134 # Create files.
135 for f in (self.repo, self.chroot, self.general):
136 osutils.Touch(os.path.join(self.tempdir, f), makedirs=True)
137
138 def testBuildrootEmpty(self):
139 """Test CleanBuildroot with no history."""
140 bootstrap.CleanBuildroot(None, self.tempdir)
141
142 self.assertEqual(osutils.ReadFile(self.state), 'TOT')
143
144 def testBuildrootNoState(self):
145 """Test CleanBuildroot with no state information."""
146 self.populateBuildroot()
147
148 bootstrap.CleanBuildroot(None, self.tempdir)
149
150 self.assertEqual(osutils.ReadFile(self.state), 'TOT')
151 self.assertExists(self.repo)
152 self.assertNotExists(self.chroot)
153 self.assertExists(self.general)
154
155 def testBuildrootBranchChange(self):
156 """Test CleanBuildroot with a change in branches."""
157 self.populateBuildroot('branchA')
158
159 bootstrap.CleanBuildroot('branchB', self.tempdir)
160
161 self.assertEqual(osutils.ReadFile(self.state), 'branchB')
162 self.assertExists(self.repo)
163 self.assertNotExists(self.chroot)
164 self.assertExists(self.general)
165
166 def testBuildrootBranchMatch(self):
167 """Test CleanBuildroot with no change in branch."""
168 self.populateBuildroot('branchA')
169
170 bootstrap.CleanBuildroot('branchA', self.tempdir)
171
172 self.assertEqual(osutils.ReadFile(self.state), 'branchA')
173 self.assertExists(self.repo)
174 self.assertExists(self.chroot)
175 self.assertExists(self.general)