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 |
| 10 | |
| 11 | from chromite.cbuildbot import repository |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 12 | from chromite.lib import cros_build_lib_unittest |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 13 | from chromite.lib import osutils |
| 14 | from chromite.scripts import bootstrap |
| 15 | |
| 16 | # pylint |
| 17 | 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] | 18 | |
| 19 | |
| 20 | class BootstrapTest(cros_build_lib_unittest.RunCommandTestCase): |
| 21 | """Tests for bootstrap script.""" |
| 22 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 23 | def testPreParseArguments(self): |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 24 | """Test that we can correctly extract branch values from cbuildbot args.""" |
| 25 | cases = ( |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 26 | (['--buildroot', '/build'], |
| 27 | None, '/build', None), |
| 28 | (['--branch', 'branch', '-r', '/build'], |
| 29 | 'branch', '/build', None), |
| 30 | (['-r', '/build', '-b', 'branch', 'config'], |
| 31 | 'branch', '/build', None), |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 32 | ) |
| 33 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 34 | for args, expected_branch, expected_root, expected_git_cache in cases: |
| 35 | result = bootstrap.PreParseArguments(args) |
| 36 | self.assertEqual(result.branch, expected_branch) |
| 37 | self.assertEqual(result.buildroot, expected_root) |
| 38 | self.assertEqual(result.git_cache_dir, expected_git_cache) |
| 39 | |
| 40 | def testInitialCheckoutMin(self): |
| 41 | """Test InitialCheckout with minimum settings.""" |
| 42 | mock_repo = self.PatchObject(repository, 'RepoRepository', autospec=True) |
| 43 | self.PatchObject(osutils, 'SafeMakedirs', autospec=True) |
| 44 | |
| 45 | bootstrap.InitialCheckout(None, '/buildroot', None) |
| 46 | |
| 47 | self.assertEqual(mock_repo.mock_calls, [ |
| 48 | mock.call(EXPECTED_MANIFEST_URL, '/buildroot', |
| 49 | branch=None, git_cache_dir=None), |
| 50 | mock.call().Sync() |
| 51 | ]) |
| 52 | |
| 53 | def testInitialCheckoutMax(self): |
| 54 | """Test InitialCheckout with all settings.""" |
| 55 | mock_repo = self.PatchObject(repository, 'RepoRepository', autospec=True) |
| 56 | self.PatchObject(osutils, 'SafeMakedirs', autospec=True) |
| 57 | |
| 58 | bootstrap.InitialCheckout('release-R56-9000.B', '/buildroot', '/git-cache') |
| 59 | |
| 60 | self.assertEqual(mock_repo.mock_calls, [ |
| 61 | mock.call(EXPECTED_MANIFEST_URL, '/buildroot', |
| 62 | branch='release-R56-9000.B', git_cache_dir='/git-cache'), |
| 63 | mock.call().Sync() |
| 64 | ]) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 65 | |
| 66 | def testRunCbuildbot(self): |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 67 | """Ensure we invoke cbuildbot correctly.""" |
| 68 | bootstrap.RunCbuildbot('/buildroot', ['foo', 'bar', 'arg']) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 69 | self.assertCommandContains( |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 70 | ['/buildroot/chromite/bin/cbuildbot', 'foo', 'bar', 'arg']) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 71 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 72 | def testMainMin(self): |
| 73 | """Test a minimal set of command line options.""" |
| 74 | mock_checkout = self.PatchObject(bootstrap, 'InitialCheckout', |
| 75 | autospec=True) |
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 | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 79 | # Ensure we checkout, as expected. |
| 80 | self.assertEqual(mock_checkout.mock_calls, |
| 81 | [mock.call(None, '/buildroot', None)]) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 82 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 83 | # Ensure we invoke cbuildbot, as expected. |
| 84 | self.assertCommandCalled( |
| 85 | ['/buildroot/chromite/bin/cbuildbot', |
| 86 | '--buildroot', '/buildroot', 'foo'], |
| 87 | cwd='/buildroot', error_code_ok=True) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 88 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 89 | def testMainMax(self): |
| 90 | """Test a maximal set of command line options.""" |
| 91 | mock_checkout = self.PatchObject(bootstrap, 'InitialCheckout', |
| 92 | autospec=True) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 93 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 94 | bootstrap.main(['--buildroot', '/buildroot', '--branch', 'branch', |
| 95 | '--git-cache-dir', '/git-cache', 'foo']) |
Don Garrett | c4114cc | 2016-11-01 20:04:06 -0700 | [diff] [blame] | 96 | |
Don Garrett | 86881cb | 2017-02-15 15:41:55 -0800 | [diff] [blame] | 97 | # Ensure we checkout, as expected. |
| 98 | self.assertEqual(mock_checkout.mock_calls, |
| 99 | [mock.call('branch', '/buildroot', '/git-cache')]) |
| 100 | |
| 101 | # Ensure we invoke cbuildbot, as expected. |
| 102 | self.assertCommandCalled( |
| 103 | ['/buildroot/chromite/bin/cbuildbot', |
| 104 | '--buildroot', '/buildroot', '--branch', 'branch', |
| 105 | '--git-cache-dir', '/git-cache', 'foo'], |
| 106 | cwd='/buildroot', error_code_ok=True) |