blob: 6021de3390008e310576c6133fc87003cea2289a [file] [log] [blame]
Gaurav Shah298aa372014-01-31 09:27:24 -08001# Copyright (c) 2014 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 the cbuildbot program"""
6
Mike Frysinger383367e2014-09-16 15:06:17 -04007from __future__ import print_function
8
Don Garrettbfb33532015-07-07 16:56:43 -07009import mock
10import os
11
Aviv Keshetb7519e12016-10-04 00:50:00 -070012from chromite.lib import constants
Gaurav Shah298aa372014-01-31 09:27:24 -080013from chromite.lib import cros_test_lib
Don Garrettbfb33532015-07-07 16:56:43 -070014from chromite.lib import git
Gaurav Shah298aa372014-01-31 09:27:24 -080015from chromite.scripts import cbuildbot
16
17
Don Garrettb85658c2015-06-30 19:07:22 -070018# pylint: disable=protected-access
19
20
Gaurav Shah298aa372014-01-31 09:27:24 -080021class IsDistributedBuilderTest(cros_test_lib.TestCase):
22 """Test for cbuildbot._IsDistributedBuilder."""
23
24 # pylint: disable=W0212
25 def testIsDistributedBuilder(self):
26 """Tests for _IsDistributedBuilder() under various configurations."""
27 parser = cbuildbot._CreateParser()
28 argv = ['x86-generic-paladin']
Don Garrett597ddff2017-02-17 18:29:37 -080029 (options, _) = cbuildbot.ParseCommandLine(parser, argv)
Gaurav Shah298aa372014-01-31 09:27:24 -080030 options.buildbot = False
Gaurav Shah298aa372014-01-31 09:27:24 -080031
32 build_config = dict(pre_cq=False,
33 manifest_version=False)
34 chrome_rev = None
35
36 def _TestConfig(expected):
37 self.assertEquals(expected,
38 cbuildbot._IsDistributedBuilder(
39 options=options,
40 chrome_rev=chrome_rev,
41 build_config=build_config))
42
43 # Default options.
44 _TestConfig(False)
45
Gaurav Shah298aa372014-01-31 09:27:24 -080046 build_config['pre_cq'] = True
47 _TestConfig(True)
48
49 build_config['pre_cq'] = False
50 build_config['manifest_version'] = True
51 # Not running in buildbot mode even though manifest_version=True.
52 _TestConfig(False)
53 options.buildbot = True
54 _TestConfig(True)
55
56 for chrome_rev in (constants.CHROME_REV_TOT,
57 constants.CHROME_REV_LOCAL,
58 constants.CHROME_REV_SPEC):
59 _TestConfig(False)
Don Garrettbfb33532015-07-07 16:56:43 -070060
61
62class FetchInitialBootstrapConfigRepoTest(cros_test_lib.MockTempDirTestCase):
63 """Test for cbuildbot._FetchInitialBootstrapConfig."""
64
65
66 def setUp(self):
67 self.config_dir = os.path.join(self.tempdir, 'config')
68
69 self.PatchObject(constants, "SOURCE_ROOT", self.tempdir)
70 self.PatchObject(constants, "SITE_CONFIG_DIR", self.config_dir)
71 self.mockGit = self.PatchObject(git, "RunGit")
72
73 def testDoesClone(self):
74 # Test
75 cbuildbot._FetchInitialBootstrapConfigRepo('repo_url', None)
76 # Verify
77 self.mockGit.assert_called_once_with(
78 self.config_dir, ['clone', 'repo_url', self.config_dir])
79
80 def testDoesCloneBranch(self):
81 # Test
82 cbuildbot._FetchInitialBootstrapConfigRepo('repo_url', 'test_branch')
83 # Verify
84 self.assertEqual(
85 self.mockGit.mock_calls,
86 [mock.call(self.config_dir, ['clone', 'repo_url', self.config_dir]),
87 mock.call(self.config_dir, ['checkout', 'test_branch'])])
88
89 def testNoCloneForRepo(self):
90 # Setup
91 os.mkdir(os.path.join(self.tempdir, '.repo'))
92 # Test
93 cbuildbot._FetchInitialBootstrapConfigRepo('repo_url', None)
94 # Verify
95 self.assertEqual(self.mockGit.call_count, 0)
96
97 def testNoCloneIfExists(self):
98 # Setup
99 os.mkdir(os.path.join(self.tempdir, 'config'))
100 # Test
101 cbuildbot._FetchInitialBootstrapConfigRepo('repo_url', None)
102 # Verify
103 self.assertEqual(self.mockGit.call_count, 0)