blob: fe59862ef79ba384063d3d4bef63a4f4128f34a5 [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
10
11from chromite.cbuildbot import repository
Don Garrettc4114cc2016-11-01 20:04:06 -070012from chromite.lib import cros_build_lib_unittest
Don Garrett86881cb2017-02-15 15:41:55 -080013from chromite.lib import osutils
14from chromite.scripts import bootstrap
15
16# pylint
17EXPECTED_MANIFEST_URL = 'https://chrome-internal-review.googlesource.com/chromeos/manifest-internal' # pylint: disable=line-too-long
Don Garrettc4114cc2016-11-01 20:04:06 -070018
19
20class BootstrapTest(cros_build_lib_unittest.RunCommandTestCase):
21 """Tests for bootstrap script."""
22
Don Garrett86881cb2017-02-15 15:41:55 -080023 def testPreParseArguments(self):
Don Garrettc4114cc2016-11-01 20:04:06 -070024 """Test that we can correctly extract branch values from cbuildbot args."""
25 cases = (
Don Garrett86881cb2017-02-15 15:41:55 -080026 (['--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 Garrettc4114cc2016-11-01 20:04:06 -070032 )
33
Don Garrett86881cb2017-02-15 15:41:55 -080034 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 Garrettc4114cc2016-11-01 20:04:06 -070065
66 def testRunCbuildbot(self):
Don Garrett86881cb2017-02-15 15:41:55 -080067 """Ensure we invoke cbuildbot correctly."""
68 bootstrap.RunCbuildbot('/buildroot', ['foo', 'bar', 'arg'])
Don Garrettc4114cc2016-11-01 20:04:06 -070069 self.assertCommandContains(
Don Garrett86881cb2017-02-15 15:41:55 -080070 ['/buildroot/chromite/bin/cbuildbot', 'foo', 'bar', 'arg'])
Don Garrettc4114cc2016-11-01 20:04:06 -070071
Don Garrett86881cb2017-02-15 15:41:55 -080072 def testMainMin(self):
73 """Test a minimal set of command line options."""
74 mock_checkout = self.PatchObject(bootstrap, 'InitialCheckout',
75 autospec=True)
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 Garrett86881cb2017-02-15 15:41:55 -080079 # Ensure we checkout, as expected.
80 self.assertEqual(mock_checkout.mock_calls,
81 [mock.call(None, '/buildroot', None)])
Don Garrettc4114cc2016-11-01 20:04:06 -070082
Don Garrett86881cb2017-02-15 15:41:55 -080083 # 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 Garrettc4114cc2016-11-01 20:04:06 -070088
Don Garrett86881cb2017-02-15 15:41:55 -080089 def testMainMax(self):
90 """Test a maximal set of command line options."""
91 mock_checkout = self.PatchObject(bootstrap, 'InitialCheckout',
92 autospec=True)
Don Garrettc4114cc2016-11-01 20:04:06 -070093
Don Garrett86881cb2017-02-15 15:41:55 -080094 bootstrap.main(['--buildroot', '/buildroot', '--branch', 'branch',
95 '--git-cache-dir', '/git-cache', 'foo'])
Don Garrettc4114cc2016-11-01 20:04:06 -070096
Don Garrett86881cb2017-02-15 15:41:55 -080097 # 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)