2 # Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Test the archive_lib module."""
8 from __future__ import print_function
14 sys.path.insert(0, os.path.abspath('%s/../..' % os.path.dirname(__file__)))
15 from chromite.cbuildbot import archive_lib
16 from chromite.cbuildbot import cbuildbot_config
17 from chromite.cbuildbot import cbuildbot_run
18 from chromite.lib import cros_test_lib
19 from chromite.lib import parallel_unittest
23 DEFAULT_ARCHIVE_PREFIX = 'bogus_bucket/TheArchiveBase'
24 DEFAULT_ARCHIVE_BASE = 'gs://%s' % DEFAULT_ARCHIVE_PREFIX
25 DEFAULT_BUILDROOT = '/tmp/foo/bar/buildroot'
26 DEFAULT_BUILDNUMBER = 12345
27 DEFAULT_BRANCH = 'TheBranch'
28 DEFAULT_CHROME_BRANCH = 'TheChromeBranch'
29 DEFAULT_VERSION_STRING = 'TheVersionString'
30 DEFAULT_BOARD = 'TheBoard'
31 DEFAULT_BOT_NAME = 'TheCoolBot'
33 # Access to protected member.
34 # pylint: disable=W0212
36 DEFAULT_OPTIONS = cros_test_lib.EasyAttr(
37 archive_base=DEFAULT_ARCHIVE_BASE,
38 buildroot=DEFAULT_BUILDROOT,
39 buildnumber=DEFAULT_BUILDNUMBER,
41 branch=DEFAULT_BRANCH,
45 DEFAULT_CONFIG = cbuildbot_config._config(
46 name=DEFAULT_BOT_NAME,
48 boards=[DEFAULT_BOARD],
49 child_configs=[cbuildbot_config._config(name='foo'),
50 cbuildbot_config._config(name='bar'),
55 def _ExtendDefaultOptions(**kwargs):
56 """Extend DEFAULT_OPTIONS with keys/values in kwargs."""
57 options_kwargs = DEFAULT_OPTIONS.copy()
58 options_kwargs.update(kwargs)
59 return cros_test_lib.EasyAttr(**options_kwargs)
62 def _ExtendDefaultConfig(**kwargs):
63 """Extend DEFAULT_CONFIG with keys/values in kwargs."""
64 config_kwargs = DEFAULT_CONFIG.copy()
65 config_kwargs.update(kwargs)
66 return cbuildbot_config._config(**config_kwargs)
69 def _NewBuilderRun(options=None, config=None):
70 """Create a BuilderRun objection from options and config values.
73 options: Specify options or default to DEFAULT_OPTIONS.
74 config: Specify build config or default to DEFAULT_CONFIG.
79 manager = parallel_unittest.FakeMultiprocessManager()
80 options = options or DEFAULT_OPTIONS
81 config = config or DEFAULT_CONFIG
82 return cbuildbot_run.BuilderRun(options, config, manager)
85 class GetBaseUploadURITest(cros_test_lib.TestCase):
86 """Test the GetBaseUploadURI function."""
88 ARCHIVE_BASE = '/tmp/the/archive/base'
89 BOT_ID = 'TheNewBotId'
92 self.cfg = DEFAULT_CONFIG
94 def _GetBaseUploadURI(self, *args, **kwargs):
95 """Test GetBaseUploadURI with archive_base and no bot_id."""
96 return archive_lib.GetBaseUploadURI(self.cfg, *args, **kwargs)
98 def testArchiveBaseRemoteTrybotFalse(self):
99 expected_result = '%s/%s' % (self.ARCHIVE_BASE, DEFAULT_BOT_NAME)
100 result = self._GetBaseUploadURI(archive_base=self.ARCHIVE_BASE,
102 self.assertEqual(expected_result, result)
104 def testArchiveBaseRemoteTrybotTrue(self):
105 expected_result = '%s/trybot-%s' % (self.ARCHIVE_BASE, DEFAULT_BOT_NAME)
106 result = self._GetBaseUploadURI(archive_base=self.ARCHIVE_BASE,
108 self.assertEqual(expected_result, result)
110 def testArchiveBaseBotIdRemoteTrybotFalse(self):
111 expected_result = '%s/%s' % (self.ARCHIVE_BASE, self.BOT_ID)
112 result = self._GetBaseUploadURI(archive_base=self.ARCHIVE_BASE,
113 bot_id=self.BOT_ID, remote_trybot=False)
114 self.assertEqual(expected_result, result)
116 def testArchiveBaseBotIdRemoteTrybotTrue(self):
117 expected_result = '%s/%s' % (self.ARCHIVE_BASE, self.BOT_ID)
118 result = self._GetBaseUploadURI(archive_base=self.ARCHIVE_BASE,
119 bot_id=self.BOT_ID, remote_trybot=True)
120 self.assertEqual(expected_result, result)
122 def testRemoteTrybotTrue(self):
123 """Test GetBaseUploadURI with no archive base but remote_trybot is True."""
124 expected_result = ('%s/trybot-%s' %
125 (archive_lib.constants.DEFAULT_ARCHIVE_BUCKET,
127 result = self._GetBaseUploadURI(remote_trybot=True)
128 self.assertEqual(expected_result, result)
130 def testBotIdRemoteTrybotTrue(self):
131 expected_result = ('%s/%s' %
132 (archive_lib.constants.DEFAULT_ARCHIVE_BUCKET,
134 result = self._GetBaseUploadURI(bot_id=self.BOT_ID, remote_trybot=True)
135 self.assertEqual(expected_result, result)
137 def testDefaultGSPathRemoteTrybotFalse(self):
138 """Test GetBaseUploadURI with default gs_path value in config."""
139 self.cfg = _ExtendDefaultConfig(gs_path=cbuildbot_config.GS_PATH_DEFAULT)
141 # Test without bot_id.
142 expected_result = ('%s/%s' %
143 (archive_lib.constants.DEFAULT_ARCHIVE_BUCKET,
145 result = self._GetBaseUploadURI(remote_trybot=False)
146 self.assertEqual(expected_result, result)
149 expected_result = ('%s/%s' %
150 (archive_lib.constants.DEFAULT_ARCHIVE_BUCKET,
152 result = self._GetBaseUploadURI(bot_id=self.BOT_ID, remote_trybot=False)
153 self.assertEqual(expected_result, result)
155 def testOverrideGSPath(self):
156 """Test GetBaseUploadURI with default gs_path value in config."""
157 self.cfg = _ExtendDefaultConfig(gs_path='gs://funkytown/foo/bar')
159 # Test without bot_id.
160 expected_result = self.cfg.gs_path
161 result = self._GetBaseUploadURI(remote_trybot=False)
162 self.assertEqual(expected_result, result)
165 expected_result = self.cfg.gs_path
166 result = self._GetBaseUploadURI(bot_id=self.BOT_ID, remote_trybot=False)
167 self.assertEqual(expected_result, result)
170 class ArchiveTest(cros_test_lib.TestCase):
171 """Test the Archive class."""
172 _VERSION = '6543.2.1'
174 def _GetAttributeValue(self, attr, options=None, config=None):
175 with mock.patch.object(cbuildbot_run._BuilderRunBase, 'GetVersion') as m:
176 m.return_value = self._VERSION
178 run = _NewBuilderRun(options, config)
179 return getattr(run.GetArchive(), attr)
181 def testVersion(self):
182 value = self._GetAttributeValue('version')
183 self.assertEqual(self._VERSION, value)
185 def testVersionNotReady(self):
186 run = _NewBuilderRun()
187 self.assertRaises(AttributeError, getattr, run, 'version')
189 def testArchivePathTrybot(self):
190 options = _ExtendDefaultOptions(buildbot=False)
191 value = self._GetAttributeValue('archive_path', options=options)
192 expected_value = ('%s/%s/%s/%s' %
194 archive_lib.Archive._TRYBOT_ARCHIVE,
197 self.assertEqual(expected_value, value)
199 def testArchivePathBuildbot(self):
200 value = self._GetAttributeValue('archive_path')
201 expected_value = ('%s/%s/%s/%s' %
203 archive_lib.Archive._BUILDBOT_ARCHIVE,
206 self.assertEqual(expected_value, value)
208 def testUploadUri(self):
209 value = self._GetAttributeValue('upload_url')
210 expected_value = '%s/%s/%s' % (DEFAULT_ARCHIVE_BASE,
213 self.assertEqual(expected_value, value)
215 def testDownloadURLBuildbot(self):
216 value = self._GetAttributeValue('download_url')
217 expected_value = ('%s%s/%s/%s' %
218 (archive_lib.gs.PRIVATE_BASE_HTTPS_URL,
219 DEFAULT_ARCHIVE_PREFIX,
222 self.assertEqual(expected_value, value)
227 if __name__ == '__main__':
228 cros_test_lib.main(level=logging.DEBUG)