blob: 58302f9da95780fc543f0f9880f0f818889f0b8e [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 Garrettb85658c2015-06-30 19:07:22 -07009import mock
10import os
11
12from chromite.cbuildbot import config_lib
Don Garrett88b8d782014-05-13 17:30:55 -070013from chromite.cbuildbot import constants
Gaurav Shah298aa372014-01-31 09:27:24 -080014from chromite.lib import cros_test_lib
15from chromite.scripts import cbuildbot
16
17
Don Garrettb85658c2015-06-30 19:07:22 -070018# pylint: disable=protected-access
19
20
21class SiteConfigTests(cros_test_lib.MockTestCase):
22 """Test cbuildbot._SetupSiteConfig."""
23 def setUp(self):
24 self.options = mock.Mock()
25 self.options.config_repo = None
26
27 self.expected_result = mock.Mock()
28
29 self.exists_mock = self.PatchObject(os.path, 'exists')
30 self.load_mock = self.PatchObject(config_lib, 'LoadConfigFromFile',
31 return_value=self.expected_result)
32
33 def testDefaultChromeOsBehavior(self):
34 # Setup Fakes and Mocks.
35 self.exists_mock.return_value = False
36
37 # Run Tests
38 result = cbuildbot._SetupSiteConfig(self.options)
39
40 # Evaluate Results
41 self.assertIs(result, self.expected_result)
42 self.load_mock.assert_called_once_with(constants.CHROMEOS_CONFIG_FILE)
43
44 def testDefaultSiteBehavior(self):
45 # Setup Fakes and Mocks.
46 self.exists_mock.return_value = True
47
48 # Run Tests
49 result = cbuildbot._SetupSiteConfig(self.options)
50
51 # Evaluate Results
52 self.assertIs(result, self.expected_result)
53 self.load_mock.assert_called_once_with(constants.SITE_CONFIG_FILE)
54
55 # TODO(dgarrett): Test a specified site URL, when it's implemented.
56
57
Gaurav Shah298aa372014-01-31 09:27:24 -080058class IsDistributedBuilderTest(cros_test_lib.TestCase):
59 """Test for cbuildbot._IsDistributedBuilder."""
60
61 # pylint: disable=W0212
62 def testIsDistributedBuilder(self):
63 """Tests for _IsDistributedBuilder() under various configurations."""
64 parser = cbuildbot._CreateParser()
65 argv = ['x86-generic-paladin']
Don Garrett0a873e02015-06-30 17:55:10 -070066 (options, _) = cbuildbot._ParseCommandLine(parser, argv)
Gaurav Shah298aa372014-01-31 09:27:24 -080067 options.buildbot = False
68 options.pre_cq = False
69
70 build_config = dict(pre_cq=False,
71 manifest_version=False)
72 chrome_rev = None
73
74 def _TestConfig(expected):
75 self.assertEquals(expected,
76 cbuildbot._IsDistributedBuilder(
77 options=options,
78 chrome_rev=chrome_rev,
79 build_config=build_config))
80
81 # Default options.
82 _TestConfig(False)
83
84 # In Pre-CQ mode, we run as as a distributed builder.
85 options.pre_cq = True
86 _TestConfig(True)
87
88 options.pre_cq = False
89 build_config['pre_cq'] = True
90 _TestConfig(True)
91
92 build_config['pre_cq'] = False
93 build_config['manifest_version'] = True
94 # Not running in buildbot mode even though manifest_version=True.
95 _TestConfig(False)
96 options.buildbot = True
97 _TestConfig(True)
98
99 for chrome_rev in (constants.CHROME_REV_TOT,
100 constants.CHROME_REV_LOCAL,
101 constants.CHROME_REV_SPEC):
102 _TestConfig(False)