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 | bfb3353 | 2015-07-07 16:56:43 -0700 | [diff] [blame] | 9 | import mock |
| 10 | import os |
| 11 | |
Aviv Keshet | b7519e1 | 2016-10-04 00:50:00 -0700 | [diff] [blame] | 12 | from chromite.lib import constants |
Gaurav Shah | 298aa37 | 2014-01-31 09:27:24 -0800 | [diff] [blame] | 13 | from chromite.lib import cros_test_lib |
Don Garrett | bfb3353 | 2015-07-07 16:56:43 -0700 | [diff] [blame] | 14 | from chromite.lib import git |
Gaurav Shah | 298aa37 | 2014-01-31 09:27:24 -0800 | [diff] [blame] | 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 | |
Gaurav Shah | 298aa37 | 2014-01-31 09:27:24 -0800 | [diff] [blame] | 21 | class 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 Garrett | 597ddff | 2017-02-17 18:29:37 -0800 | [diff] [blame^] | 29 | (options, _) = cbuildbot.ParseCommandLine(parser, argv) |
Gaurav Shah | 298aa37 | 2014-01-31 09:27:24 -0800 | [diff] [blame] | 30 | options.buildbot = False |
Gaurav Shah | 298aa37 | 2014-01-31 09:27:24 -0800 | [diff] [blame] | 31 | |
| 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 Shah | 298aa37 | 2014-01-31 09:27:24 -0800 | [diff] [blame] | 46 | 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 Garrett | bfb3353 | 2015-07-07 16:56:43 -0700 | [diff] [blame] | 60 | |
| 61 | |
| 62 | class 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) |