Gaurav Shah | 298aa37 | 2014-01-31 09:27:24 -0800 | [diff] [blame] | 1 | # 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 Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 7 | from __future__ import print_function |
| 8 | |
Don Garrett | b85658c | 2015-06-30 19:07:22 -0700 | [diff] [blame^] | 9 | import mock |
| 10 | import os |
| 11 | |
| 12 | from chromite.cbuildbot import config_lib |
Don Garrett | 88b8d78 | 2014-05-13 17:30:55 -0700 | [diff] [blame] | 13 | from chromite.cbuildbot import constants |
Gaurav Shah | 298aa37 | 2014-01-31 09:27:24 -0800 | [diff] [blame] | 14 | from chromite.lib import cros_test_lib |
| 15 | from chromite.scripts import cbuildbot |
| 16 | |
| 17 | |
Don Garrett | b85658c | 2015-06-30 19:07:22 -0700 | [diff] [blame^] | 18 | # pylint: disable=protected-access |
| 19 | |
| 20 | |
| 21 | class 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 Shah | 298aa37 | 2014-01-31 09:27:24 -0800 | [diff] [blame] | 58 | class 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 Garrett | 0a873e0 | 2015-06-30 17:55:10 -0700 | [diff] [blame] | 66 | (options, _) = cbuildbot._ParseCommandLine(parser, argv) |
Gaurav Shah | 298aa37 | 2014-01-31 09:27:24 -0800 | [diff] [blame] | 67 | 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) |