Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 1 | # 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 | |
| 7 | from __future__ import print_function |
| 8 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 9 | import mock |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame^] | 10 | import os |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 11 | |
| 12 | from chromite.cbuildbot import repository |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 13 | from chromite.lib import cros_build_lib_unittest |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame^] | 14 | from chromite.lib import cros_test_lib |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 15 | from chromite.lib import osutils |
| 16 | from chromite.scripts import bootstrap |
| 17 | |
| 18 | # pylint |
| 19 | EXPECTED_MANIFEST_URL = 'https://chrome-internal-review.googlesource.com/chromeos/manifest-internal' # pylint: disable=line-too-long |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 20 | |
| 21 | |
| 22 | class BootstrapTest(cros_build_lib_unittest.RunCommandTestCase): |
| 23 | """Tests for bootstrap script.""" |
| 24 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 25 | def testPreParseArguments(self): |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 26 | """Test that we can correctly extract branch values from cbuildbot args.""" |
| 27 | cases = ( |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame^] | 28 | (['--buildroot', '/build'], None, '/build', None), |
| 29 | (['--branch', 'branch', '-r', '/build'], 'branch', '/build', None), |
| 30 | (['-r', '/build', '-b', 'branch', 'config'], 'branch', '/build', None), |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 31 | ) |
| 32 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 33 | 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 Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 42 | |
| 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 Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 54 | |
| 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 Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 62 | |
| 63 | def testRunCbuildbot(self): |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 64 | """Ensure we invoke cbuildbot correctly.""" |
| 65 | bootstrap.RunCbuildbot('/buildroot', ['foo', 'bar', 'arg']) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 66 | self.assertCommandContains( |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 67 | ['/buildroot/chromite/bin/cbuildbot', 'foo', 'bar', 'arg']) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 68 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 69 | def testMainMin(self): |
| 70 | """Test a minimal set of command line options.""" |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame^] | 71 | mock_clean = self.PatchObject(bootstrap, 'CleanBuildroot', autospec=True) |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 72 | mock_checkout = self.PatchObject(bootstrap, 'InitialCheckout', |
| 73 | autospec=True) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame^] | 74 | self.PatchObject(osutils, 'SafeMakedirs', autospec=True) |
| 75 | |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 76 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 77 | bootstrap.main(['--buildroot', '/buildroot', 'foo']) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 78 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame^] | 79 | # Ensure we clean, as expected. |
| 80 | self.assertEqual(mock_clean.mock_calls, |
| 81 | [mock.call(None, '/buildroot')]) |
| 82 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 83 | # Ensure we checkout, as expected. |
| 84 | self.assertEqual(mock_checkout.mock_calls, |
| 85 | [mock.call(None, '/buildroot', None)]) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 86 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 87 | # 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 Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 92 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 93 | def testMainMax(self): |
| 94 | """Test a maximal set of command line options.""" |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame^] | 95 | mock_clean = self.PatchObject(bootstrap, 'CleanBuildroot', autospec=True) |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 96 | mock_checkout = self.PatchObject(bootstrap, 'InitialCheckout', |
| 97 | autospec=True) |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame^] | 98 | self.PatchObject(osutils, 'SafeMakedirs', autospec=True) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 99 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 100 | bootstrap.main(['--buildroot', '/buildroot', '--branch', 'branch', |
| 101 | '--git-cache-dir', '/git-cache', 'foo']) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 102 | |
Don Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame^] | 103 | # Ensure we clean, as expected. |
| 104 | self.assertEqual(mock_clean.mock_calls, |
| 105 | [mock.call('branch', '/buildroot')]) |
| 106 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 107 | # 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 Garrett | 7ade05a | 2017-02-17 13:31:47 -0800 | [diff] [blame^] | 117 | |
| 118 | |
| 119 | class 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) |