blob: 6ed52c326bb829e966265317f07dca26d9655095 [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
David Jamesbb20ac82012-07-18 10:59:16 -07002# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
David James8c846492011-01-25 17:07:29 -08003# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
Mike Frysingerc6824f62014-02-03 11:09:44 -05006"""Unittests for upload_prebuilts.py."""
7
Mike Frysinger383367e2014-09-16 15:06:17 -04008from __future__ import print_function
9
David James8c846492011-01-25 17:07:29 -080010import copy
Mike Frysingerea838d12014-12-08 11:55:32 -050011import mock
David James8c846492011-01-25 17:07:29 -080012import os
David James8fa34ea2011-04-15 13:00:20 -070013import multiprocessing
David James8c846492011-01-25 17:07:29 -080014import tempfile
Chris Sosa471532a2011-02-01 15:10:06 -080015
David Jamesc5cbd472012-06-19 16:25:45 -070016from chromite.scripts import upload_prebuilts as prebuilt
Brian Harringc92788f2012-09-21 18:07:15 -070017from chromite.lib import cros_test_lib
David James2c7ccb42012-11-04 15:34:28 -080018from chromite.lib import gs
David James615e5b52011-06-03 11:10:15 -070019from chromite.lib import binpkg
Zdenek Behanc0e18762012-09-22 04:06:17 +020020from chromite.lib import osutils
Mike Frysinger36870f92015-04-12 02:59:55 -040021from chromite.lib import parallel_unittest
Prathmesh Prabhu421eef22014-10-16 17:13:19 -070022from chromite.lib import portage_util
David James8c846492011-01-25 17:07:29 -080023
Mike Frysinger68182472014-11-05 22:38:39 -050024
Mike Frysinger27e21b72018-07-12 14:20:21 -040025# pylint: disable=protected-access
26
27
David James615e5b52011-06-03 11:10:15 -070028PUBLIC_PACKAGES = [{'CPV': 'gtk+/public1', 'SHA1': '1', 'MTIME': '1'},
David James8c846492011-01-25 17:07:29 -080029 {'CPV': 'gtk+/public2', 'SHA1': '2',
David James615e5b52011-06-03 11:10:15 -070030 'PATH': 'gtk+/foo.tgz', 'MTIME': '2'}]
31PRIVATE_PACKAGES = [{'CPV': 'private', 'SHA1': '3', 'MTIME': '3'}]
David James8c846492011-01-25 17:07:29 -080032
33
34def SimplePackageIndex(header=True, packages=True):
Chris Sosa58669192011-06-30 12:45:03 -070035 pkgindex = binpkg.PackageIndex()
36 if header:
David James2c7ccb42012-11-04 15:34:28 -080037 pkgindex.header['URI'] = 'gs://example'
Chris Sosa58669192011-06-30 12:45:03 -070038 if packages:
39 pkgindex.packages = copy.deepcopy(PUBLIC_PACKAGES + PRIVATE_PACKAGES)
40 return pkgindex
David James8c846492011-01-25 17:07:29 -080041
42
Brian Harringc92788f2012-09-21 18:07:15 -070043class TestUpdateFile(cros_test_lib.TempDirTestCase):
Mike Frysingerc6824f62014-02-03 11:09:44 -050044 """Tests for the UpdateLocalFile function."""
David James8c846492011-01-25 17:07:29 -080045
46 def setUp(self):
Mike Frysingerd6e2df02014-11-26 02:55:04 -050047 self.contents_str = [
48 '# comment that should be skipped',
49 'PKGDIR="/var/lib/portage/pkgs"',
50 'PORTAGE_BINHOST="http://no.thanks.com"',
51 'portage portage-20100310.tar.bz2',
52 'COMPILE_FLAGS="some_value=some_other"',
53 ]
Zdenek Behanc0e18762012-09-22 04:06:17 +020054 self.version_file = os.path.join(self.tempdir, 'version')
55 osutils.WriteFile(self.version_file, '\n'.join(self.contents_str))
David James8c846492011-01-25 17:07:29 -080056
David James8c846492011-01-25 17:07:29 -080057 def _read_version_file(self, version_file=None):
58 """Read the contents of self.version_file and return as a list."""
59 if not version_file:
60 version_file = self.version_file
61
62 version_fh = open(version_file)
63 try:
64 return [line.strip() for line in version_fh.readlines()]
65 finally:
66 version_fh.close()
67
68 def _verify_key_pair(self, key, val):
69 file_contents = self._read_version_file()
70 # ensure key for verify is wrapped on quotes
71 if '"' not in val:
72 val = '"%s"' % val
73 for entry in file_contents:
74 if '=' not in entry:
75 continue
76 file_key, file_val = entry.split('=')
77 if file_key == key:
78 if val == file_val:
79 break
80 else:
81 self.fail('Could not find "%s=%s" in version file' % (key, val))
82
83 def testAddVariableThatDoesNotExist(self):
84 """Add in a new variable that was no present in the file."""
85 key = 'PORTAGE_BINHOST'
86 value = '1234567'
87 prebuilt.UpdateLocalFile(self.version_file, value)
Mike Frysinger383367e2014-09-16 15:06:17 -040088 print(self.version_file)
Chris Sosa58669192011-06-30 12:45:03 -070089 self._read_version_file()
David James8c846492011-01-25 17:07:29 -080090 self._verify_key_pair(key, value)
Mike Frysinger383367e2014-09-16 15:06:17 -040091 print(self.version_file)
David James8c846492011-01-25 17:07:29 -080092
93 def testUpdateVariable(self):
94 """Test updating a variable that already exists."""
95 key, val = self.contents_str[2].split('=')
96 new_val = 'test_update'
97 self._verify_key_pair(key, val)
98 prebuilt.UpdateLocalFile(self.version_file, new_val)
99 self._verify_key_pair(key, new_val)
100
101 def testUpdateNonExistentFile(self):
102 key = 'PORTAGE_BINHOST'
103 value = '1234567'
104 non_existent_file = tempfile.mktemp()
105 try:
106 prebuilt.UpdateLocalFile(non_existent_file, value)
107 file_contents = self._read_version_file(non_existent_file)
Chris Sosa739c8752012-05-16 19:30:35 -0700108 self.assertEqual(file_contents, ['%s="%s"' % (key, value)])
David James8c846492011-01-25 17:07:29 -0800109 finally:
110 if os.path.exists(non_existent_file):
111 os.remove(non_existent_file)
112
113
Mike Frysinger68182472014-11-05 22:38:39 -0500114class TestPrebuilt(cros_test_lib.MockTestCase):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500115 """Tests for Prebuilt logic."""
David James8c846492011-01-25 17:07:29 -0800116
Bertrand SIMONNET811bcde2014-11-20 15:21:25 -0800117 def setUp(self):
118 self._base_local_path = '/b/cbuild/build/chroot/build/x86-dogfood/'
119 self._gs_bucket_path = 'gs://chromeos-prebuilt/host/version'
120 self._local_path = os.path.join(self._base_local_path, 'public1.tbz2')
121
David James8c846492011-01-25 17:07:29 -0800122 def testGenerateUploadDict(self):
Mike Frysinger68182472014-11-05 22:38:39 -0500123 self.PatchObject(prebuilt.os.path, 'exists', return_true=True)
Mike Frysingerd6e2df02014-11-26 02:55:04 -0500124 pkgs = [{'CPV': 'public1'}]
Bertrand SIMONNET811bcde2014-11-20 15:21:25 -0800125 result = prebuilt.GenerateUploadDict(self._base_local_path,
126 self._gs_bucket_path, pkgs)
127 expected = {self._local_path: self._gs_bucket_path + '/public1.tbz2', }
128 self.assertEqual(result, expected)
129
130 def testGenerateUploadDictWithDebug(self):
131 self.PatchObject(prebuilt.os.path, 'exists', return_true=True)
132 pkgs = [{'CPV': 'public1', 'DEBUG_SYMBOLS': 'yes'}]
133 result = prebuilt.GenerateUploadDict(self._base_local_path,
134 self._gs_bucket_path, pkgs)
135 expected = {self._local_path: self._gs_bucket_path + '/public1.tbz2',
136 self._local_path.replace('.tbz2', '.debug.tbz2'):
137 self._gs_bucket_path + '/public1.debug.tbz2'}
David James8c846492011-01-25 17:07:29 -0800138 self.assertEqual(result, expected)
139
David James8c846492011-01-25 17:07:29 -0800140 def testDeterminePrebuiltConfHost(self):
141 """Test that the host prebuilt path comes back properly."""
142 expected_path = os.path.join(prebuilt._PREBUILT_MAKE_CONF['amd64'])
143 self.assertEqual(prebuilt.DeterminePrebuiltConfFile('fake_path', 'amd64'),
144 expected_path)
145
David James8c846492011-01-25 17:07:29 -0800146
David James2c7ccb42012-11-04 15:34:28 -0800147class TestPkgIndex(cros_test_lib.TestCase):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500148 """Helper for tests that update the Packages index file."""
David James2c7ccb42012-11-04 15:34:28 -0800149
150 def setUp(self):
151 self.db = {}
152 self.pkgindex = SimplePackageIndex()
153 self.empty = SimplePackageIndex(packages=False)
154
155 def assertURIs(self, uris):
156 """Verify that the duplicate DB has the specified URLs."""
157 expected = [v.uri for _, v in sorted(self.db.items())]
158 self.assertEqual(expected, uris)
159
160
161class TestPackagesFileFiltering(TestPkgIndex):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500162 """Tests for Packages filtering behavior."""
David James8c846492011-01-25 17:07:29 -0800163
164 def testFilterPkgIndex(self):
David James2c7ccb42012-11-04 15:34:28 -0800165 """Test filtering out of private packages."""
166 self.pkgindex.RemoveFilteredPackages(lambda pkg: pkg in PRIVATE_PACKAGES)
167 self.assertEqual(self.pkgindex.packages, PUBLIC_PACKAGES)
168 self.assertEqual(self.pkgindex.modified, True)
David James8c846492011-01-25 17:07:29 -0800169
170
David James2c7ccb42012-11-04 15:34:28 -0800171class TestPopulateDuplicateDB(TestPkgIndex):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500172 """Tests for the _PopulateDuplicateDB function."""
David James8c846492011-01-25 17:07:29 -0800173
174 def testEmptyIndex(self):
David James2c7ccb42012-11-04 15:34:28 -0800175 """Test population of the duplicate DB with an empty index."""
176 self.empty._PopulateDuplicateDB(self.db, 0)
177 self.assertEqual(self.db, {})
David James8c846492011-01-25 17:07:29 -0800178
179 def testNormalIndex(self):
David James2c7ccb42012-11-04 15:34:28 -0800180 """Test population of the duplicate DB with a full index."""
181 self.pkgindex._PopulateDuplicateDB(self.db, 0)
182 self.assertURIs(['gs://example/gtk+/public1.tbz2',
183 'gs://example/gtk+/foo.tgz',
184 'gs://example/private.tbz2'])
David James8c846492011-01-25 17:07:29 -0800185
186 def testMissingSHA1(self):
David James2c7ccb42012-11-04 15:34:28 -0800187 """Test population of the duplicate DB with a missing SHA1."""
188 del self.pkgindex.packages[0]['SHA1']
189 self.pkgindex._PopulateDuplicateDB(self.db, 0)
190 self.assertURIs(['gs://example/gtk+/foo.tgz',
191 'gs://example/private.tbz2'])
David James8c846492011-01-25 17:07:29 -0800192
193 def testFailedPopulate(self):
David James2c7ccb42012-11-04 15:34:28 -0800194 """Test failure conditions for the populate method."""
195 headerless = SimplePackageIndex(header=False)
196 self.assertRaises(KeyError, headerless._PopulateDuplicateDB, self.db, 0)
197 del self.pkgindex.packages[0]['CPV']
198 self.assertRaises(KeyError, self.pkgindex._PopulateDuplicateDB, self.db, 0)
David James8c846492011-01-25 17:07:29 -0800199
200
Mike Frysinger68182472014-11-05 22:38:39 -0500201class TestResolveDuplicateUploads(cros_test_lib.MockTestCase, TestPkgIndex):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500202 """Tests for the ResolveDuplicateUploads function."""
David James8c846492011-01-25 17:07:29 -0800203
David James615e5b52011-06-03 11:10:15 -0700204 def setUp(self):
Mike Frysinger68182472014-11-05 22:38:39 -0500205 self.PatchObject(binpkg.time, 'time', return_value=binpkg.TWO_WEEKS)
David James2c7ccb42012-11-04 15:34:28 -0800206 self.db = {}
207 self.dup = SimplePackageIndex()
208 self.expected_pkgindex = SimplePackageIndex()
209
210 def assertNoDuplicates(self, candidates):
211 """Verify no duplicates are found with the specified candidates."""
212 uploads = self.pkgindex.ResolveDuplicateUploads(candidates)
213 self.assertEqual(uploads, self.pkgindex.packages)
214 self.assertEqual(len(self.pkgindex.packages),
215 len(self.expected_pkgindex.packages))
216 for pkg1, pkg2 in zip(self.pkgindex.packages,
217 self.expected_pkgindex.packages):
218 self.assertNotEqual(pkg1['MTIME'], pkg2['MTIME'])
219 del pkg1['MTIME']
220 del pkg2['MTIME']
221 self.assertEqual(self.pkgindex.modified, False)
222 self.assertEqual(self.pkgindex.packages, self.expected_pkgindex.packages)
223
224 def assertAllDuplicates(self, candidates):
225 """Verify every package is a duplicate in the specified list."""
226 for pkg in self.expected_pkgindex.packages:
227 pkg.setdefault('PATH', pkg['CPV'] + '.tbz2')
228 self.pkgindex.ResolveDuplicateUploads(candidates)
229 self.assertEqual(self.pkgindex.packages, self.expected_pkgindex.packages)
David James615e5b52011-06-03 11:10:15 -0700230
David James8c846492011-01-25 17:07:29 -0800231 def testEmptyList(self):
David James2c7ccb42012-11-04 15:34:28 -0800232 """If no candidates are supplied, no duplicates should be found."""
233 self.assertNoDuplicates([])
David James8c846492011-01-25 17:07:29 -0800234
235 def testEmptyIndex(self):
David James2c7ccb42012-11-04 15:34:28 -0800236 """If no packages are supplied, no duplicates should be found."""
237 self.assertNoDuplicates([self.empty])
David James8c846492011-01-25 17:07:29 -0800238
David James2c7ccb42012-11-04 15:34:28 -0800239 def testDifferentURI(self):
240 """If the URI differs, no duplicates should be found."""
241 self.dup.header['URI'] = 'gs://example2'
242 self.assertNoDuplicates([self.dup])
243
244 def testUpdateModificationTime(self):
245 """When duplicates are found, we should use the latest mtime."""
246 for pkg in self.expected_pkgindex.packages:
247 pkg['MTIME'] = '10'
248 for pkg in self.dup.packages:
249 pkg['MTIME'] = '4'
250 self.assertAllDuplicates([self.expected_pkgindex, self.dup])
251
252 def testCanonicalUrl(self):
253 """If the URL is in a different format, we should still find duplicates."""
254 self.dup.header['URI'] = gs.PUBLIC_BASE_HTTPS_URL + 'example'
255 self.assertAllDuplicates([self.dup])
David James8c846492011-01-25 17:07:29 -0800256
257 def testMissingSHA1(self):
David James2c7ccb42012-11-04 15:34:28 -0800258 """We should not find duplicates if there is no SHA1."""
259 del self.pkgindex.packages[0]['SHA1']
260 del self.expected_pkgindex.packages[0]['SHA1']
261 for pkg in self.expected_pkgindex.packages[1:]:
David James8c846492011-01-25 17:07:29 -0800262 pkg.setdefault('PATH', pkg['CPV'] + '.tbz2')
David James2c7ccb42012-11-04 15:34:28 -0800263 self.pkgindex.ResolveDuplicateUploads([self.dup])
264 self.assertNotEqual(self.pkgindex.packages[0]['MTIME'],
265 self.expected_pkgindex.packages[0]['MTIME'])
266 del self.pkgindex.packages[0]['MTIME']
267 del self.expected_pkgindex.packages[0]['MTIME']
268 self.assertEqual(self.pkgindex.packages, self.expected_pkgindex.packages)
David James8c846492011-01-25 17:07:29 -0800269
Bertrand SIMONNET811bcde2014-11-20 15:21:25 -0800270 def testSymbolsAvailable(self):
271 """If symbols are available remotely, re-use them and set DEBUG_SYMBOLS."""
272 self.dup.packages[0]['DEBUG_SYMBOLS'] = 'yes'
273
274 uploads = self.pkgindex.ResolveDuplicateUploads([self.dup])
275 self.assertEqual(uploads, [])
276 self.assertEqual(self.pkgindex.packages[0].get('DEBUG_SYMBOLS'), 'yes')
277
278 def testSymbolsAvailableLocallyOnly(self):
279 """If the symbols are only available locally, reupload them."""
280 self.pkgindex.packages[0]['DEBUG_SYMBOLS'] = 'yes'
281
282 uploads = self.pkgindex.ResolveDuplicateUploads([self.dup])
283 self.assertEqual(uploads, [self.pkgindex.packages[0]])
284
David James8c846492011-01-25 17:07:29 -0800285
Mike Frysinger68182472014-11-05 22:38:39 -0500286class TestWritePackageIndex(cros_test_lib.MockTestCase, TestPkgIndex):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500287 """Tests for the WriteToNamedTemporaryFile function."""
David James8c846492011-01-25 17:07:29 -0800288
289 def testSimple(self):
David James2c7ccb42012-11-04 15:34:28 -0800290 """Test simple call of WriteToNamedTemporaryFile()"""
Mike Frysinger68182472014-11-05 22:38:39 -0500291 self.PatchObject(self.pkgindex, 'Write')
David James2c7ccb42012-11-04 15:34:28 -0800292 f = self.pkgindex.WriteToNamedTemporaryFile()
David James8c846492011-01-25 17:07:29 -0800293 self.assertEqual(f.read(), '')
294
295
Mike Frysinger36870f92015-04-12 02:59:55 -0400296class TestUploadPrebuilt(cros_test_lib.MockTempDirTestCase):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500297 """Tests for the _UploadPrebuilt function."""
David James05bcb2b2011-02-09 09:25:47 -0800298
299 def setUp(self):
300 class MockTemporaryFile(object):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500301 """Mock out the temporary file logic."""
David James05bcb2b2011-02-09 09:25:47 -0800302 def __init__(self, name):
303 self.name = name
David James05bcb2b2011-02-09 09:25:47 -0800304 self.pkgindex = SimplePackageIndex()
Mike Frysinger36870f92015-04-12 02:59:55 -0400305 self.PatchObject(binpkg, 'GrabLocalPackageIndex',
306 return_value=self.pkgindex)
307 self.PatchObject(self.pkgindex, 'ResolveDuplicateUploads',
308 return_value=PRIVATE_PACKAGES)
309 self.PatchObject(self.pkgindex, 'WriteToNamedTemporaryFile',
310 return_value=MockTemporaryFile('fake'))
311 self.remote_up_mock = self.PatchObject(prebuilt, 'RemoteUpload')
312 self.gs_up_mock = self.PatchObject(prebuilt, '_GsUpload')
David James05bcb2b2011-02-09 09:25:47 -0800313
David James05bcb2b2011-02-09 09:25:47 -0800314 def testSuccessfulGsUpload(self):
Alex Deymo541ea6f2014-12-23 01:18:14 -0800315 uploads = {
316 os.path.join(self.tempdir, 'private.tbz2'): 'gs://foo/private.tbz2'}
Alex Deymo541ea6f2014-12-23 01:18:14 -0800317 packages = list(PRIVATE_PACKAGES)
318 packages.append({'CPV': 'dev-only-extras'})
Mike Frysinger36870f92015-04-12 02:59:55 -0400319 osutils.Touch(os.path.join(self.tempdir, 'dev-only-extras.tbz2'))
320 self.PatchObject(prebuilt, 'GenerateUploadDict',
321 return_value=uploads)
David James05bcb2b2011-02-09 09:25:47 -0800322 uploads = uploads.copy()
323 uploads['fake'] = 'gs://foo/suffix/Packages'
David Jamesfd0b0852011-02-23 11:15:36 -0800324 acl = 'public-read'
David Jamesc0f158a2011-02-22 16:07:29 -0800325 uri = self.pkgindex.header['URI']
David James8ece7ee2011-06-29 16:02:30 -0700326 uploader = prebuilt.PrebuiltUploader('gs://foo', acl, uri, [], '/', [],
Mike Frysinger8092a632014-05-24 13:25:46 -0400327 False, 'foo', False, 'x86-foo', [], '')
Alex Deymo541ea6f2014-12-23 01:18:14 -0800328 uploader._UploadPrebuilt(self.tempdir, 'suffix')
Mike Frysinger36870f92015-04-12 02:59:55 -0400329 self.remote_up_mock.assert_called_once_with(mock.ANY, acl, uploads)
330 self.assertTrue(self.gs_up_mock.called)
David James05bcb2b2011-02-09 09:25:47 -0800331
David James05bcb2b2011-02-09 09:25:47 -0800332
Mike Frysinger36870f92015-04-12 02:59:55 -0400333class TestSyncPrebuilts(cros_test_lib.MockTestCase):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500334 """Tests for the SyncHostPrebuilts function."""
David James05bcb2b2011-02-09 09:25:47 -0800335
336 def setUp(self):
Mike Frysinger36870f92015-04-12 02:59:55 -0400337 self.rev_mock = self.PatchObject(prebuilt, 'RevGitFile', return_value=None)
338 self.update_binhost_mock = self.PatchObject(
339 prebuilt, 'UpdateBinhostConfFile', return_value=None)
David James05bcb2b2011-02-09 09:25:47 -0800340 self.build_path = '/trunk'
341 self.upload_location = 'gs://upload/'
342 self.version = '1'
343 self.binhost = 'http://prebuilt/'
344 self.key = 'PORTAGE_BINHOST'
Mike Frysinger36870f92015-04-12 02:59:55 -0400345 self.upload_mock = self.PatchObject(prebuilt.PrebuiltUploader,
346 '_UploadPrebuilt', return_value=True)
David James05bcb2b2011-02-09 09:25:47 -0800347
David James05bcb2b2011-02-09 09:25:47 -0800348 def testSyncHostPrebuilts(self):
David Jamese2488642011-11-14 16:15:20 -0800349 board = 'x86-foo'
David James4058b0d2011-12-08 21:24:50 -0800350 target = prebuilt.BuildTarget(board, 'aura')
351 slave_targets = [prebuilt.BuildTarget('x86-bar', 'aura')]
David James05bcb2b2011-02-09 09:25:47 -0800352 package_path = os.path.join(self.build_path,
353 prebuilt._HOST_PACKAGES_PATH)
Mike Frysingerd6e2df02014-11-26 02:55:04 -0500354 url_suffix = prebuilt._REL_HOST_PATH % {
355 'version': self.version,
356 'host_arch': prebuilt._HOST_ARCH,
357 'target': target,
358 }
David James8fa34ea2011-04-15 13:00:20 -0700359 packages_url_suffix = '%s/packages' % url_suffix.rstrip('/')
David Jamesf0e6fd72011-04-15 15:58:07 -0700360 url_value = '%s/%s/' % (self.binhost.rstrip('/'),
361 packages_url_suffix.rstrip('/'))
David Jamese2488642011-11-14 16:15:20 -0800362 urls = [url_value.replace('foo', 'bar'), url_value]
David James20b2b6f2011-11-18 15:11:58 -0800363 binhost = ' '.join(urls)
David James615e5b52011-06-03 11:10:15 -0700364 uploader = prebuilt.PrebuiltUploader(
365 self.upload_location, 'public-read', self.binhost, [],
Mike Frysinger8092a632014-05-24 13:25:46 -0400366 self.build_path, [], False, 'foo', False, target, slave_targets,
367 self.version)
368 uploader.SyncHostPrebuilts(self.key, True, True)
Mike Frysinger36870f92015-04-12 02:59:55 -0400369 self.upload_mock.assert_called_once_with(package_path, packages_url_suffix)
370 self.rev_mock.assert_called_once_with(
371 mock.ANY, {self.key: binhost}, dryrun=False)
372 self.update_binhost_mock.assert_called_once_with(
373 mock.ANY, self.key, binhost)
David James05bcb2b2011-02-09 09:25:47 -0800374
375 def testSyncBoardPrebuilts(self):
David Jamese2488642011-11-14 16:15:20 -0800376 board = 'x86-foo'
David James4058b0d2011-12-08 21:24:50 -0800377 target = prebuilt.BuildTarget(board, 'aura')
378 slave_targets = [prebuilt.BuildTarget('x86-bar', 'aura')]
Mike Frysinger8092a632014-05-24 13:25:46 -0400379 board_path = os.path.join(
380 self.build_path, prebuilt._BOARD_PATH % {'board': board})
David James05bcb2b2011-02-09 09:25:47 -0800381 package_path = os.path.join(board_path, 'packages')
Mike Frysingerd6e2df02014-11-26 02:55:04 -0500382 url_suffix = prebuilt._REL_BOARD_PATH % {
383 'version': self.version,
384 'target': target,
385 }
David James8fa34ea2011-04-15 13:00:20 -0700386 packages_url_suffix = '%s/packages' % url_suffix.rstrip('/')
David Jamesf0e6fd72011-04-15 15:58:07 -0700387 url_value = '%s/%s/' % (self.binhost.rstrip('/'),
388 packages_url_suffix.rstrip('/'))
David Jamese2488642011-11-14 16:15:20 -0800389 bar_binhost = url_value.replace('foo', 'bar')
Mike Frysinger36870f92015-04-12 02:59:55 -0400390 determine_mock = self.PatchObject(prebuilt, 'DeterminePrebuiltConfFile',
391 side_effect=('bar', 'foo'))
392 self.PatchObject(prebuilt.PrebuiltUploader, '_UploadSdkTarball')
393 with parallel_unittest.ParallelMock():
394 multiprocessing.Process.exitcode = 0
395 uploader = prebuilt.PrebuiltUploader(
396 self.upload_location, 'public-read', self.binhost, [],
397 self.build_path, [], False, 'foo', False, target, slave_targets,
398 self.version)
Gilad Arnoldad333182015-05-27 15:50:41 -0700399 uploader.SyncBoardPrebuilts(self.key, True, True, True, None, None, None,
400 None, None)
Mike Frysinger36870f92015-04-12 02:59:55 -0400401 determine_mock.assert_has_calls([
402 mock.call(self.build_path, slave_targets[0]),
403 mock.call(self.build_path, target),
404 ])
405 self.upload_mock.assert_called_once_with(package_path, packages_url_suffix)
406 self.rev_mock.assert_has_calls([
407 mock.call('bar', {self.key: bar_binhost}, dryrun=False),
408 mock.call('foo', {self.key: url_value}, dryrun=False),
409 ])
410 self.update_binhost_mock.assert_has_calls([
411 mock.call(mock.ANY, self.key, bar_binhost),
412 mock.call(mock.ANY, self.key, url_value),
413 ])
David James05bcb2b2011-02-09 09:25:47 -0800414
415
Mike Frysinger36870f92015-04-12 02:59:55 -0400416class TestMain(cros_test_lib.MockTestCase):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500417 """Tests for the main() function."""
David Jamesc0f158a2011-02-22 16:07:29 -0800418
419 def testMain(self):
420 """Test that the main function works."""
Mike Frysinger36870f92015-04-12 02:59:55 -0400421 options = mock.MagicMock()
David Jamesc0f158a2011-02-22 16:07:29 -0800422 old_binhost = 'http://prebuilt/1'
423 options.previous_binhost_url = [old_binhost]
David Jamese2488642011-11-14 16:15:20 -0800424 options.board = 'x86-foo'
David James4058b0d2011-12-08 21:24:50 -0800425 options.profile = None
Chris Sosa6a5dceb2012-05-14 13:48:56 -0700426 target = prebuilt.BuildTarget(options.board, options.profile)
David Jamesc0f158a2011-02-22 16:07:29 -0800427 options.build_path = '/trunk'
Mike Frysinger86509232014-05-24 13:18:37 -0400428 options.dryrun = False
David Jamesfd0b0852011-02-23 11:15:36 -0800429 options.private = True
David James615e5b52011-06-03 11:10:15 -0700430 options.packages = []
David Jamesc0f158a2011-02-22 16:07:29 -0800431 options.sync_host = True
432 options.git_sync = True
David James8fa34ea2011-04-15 13:00:20 -0700433 options.upload_board_tarball = True
Zdenek Behan62a57792012-08-31 15:09:08 +0200434 options.prepackaged_tarball = None
Gilad Arnold2b79e2d2015-06-02 11:26:07 -0700435 options.toolchains_overlay_tarballs = []
436 options.toolchains_overlay_upload_path = ''
Mike Frysinger9e979b92012-11-29 02:55:09 -0500437 options.toolchain_tarballs = []
438 options.toolchain_upload_path = ''
David Jamesc0f158a2011-02-22 16:07:29 -0800439 options.upload = 'gs://upload/'
David Jamesfd0b0852011-02-23 11:15:36 -0800440 options.binhost_base_url = options.upload
David Jamesc0f158a2011-02-22 16:07:29 -0800441 options.prepend_version = True
David James8ece7ee2011-06-29 16:02:30 -0700442 options.set_version = None
443 options.skip_upload = False
David Jamesc0f158a2011-02-22 16:07:29 -0800444 options.filters = True
445 options.key = 'PORTAGE_BINHOST'
David Jamesb26b9312014-12-15 11:26:46 -0800446 options.binhost_conf_dir = None
David Jamesc0f158a2011-02-22 16:07:29 -0800447 options.sync_binhost_conf = True
David James4058b0d2011-12-08 21:24:50 -0800448 options.slave_targets = [prebuilt.BuildTarget('x86-bar', 'aura')]
Mike Frysinger36870f92015-04-12 02:59:55 -0400449 self.PatchObject(prebuilt, 'ParseOptions',
450 return_value=tuple([options, target]))
451 self.PatchObject(binpkg, 'GrabRemotePackageIndex', return_value=True)
452 init_mock = self.PatchObject(prebuilt.PrebuiltUploader, '__init__',
453 return_value=None)
454 expected_gs_acl_path = os.path.join('/fake_path',
Prathmesh Prabhu5f14da02014-10-17 15:13:56 -0700455 prebuilt._GOOGLESTORAGE_GSUTIL_FILE)
Mike Frysinger36870f92015-04-12 02:59:55 -0400456 self.PatchObject(portage_util, 'FindOverlayFile',
457 return_value=expected_gs_acl_path)
458 host_mock = self.PatchObject(
459 prebuilt.PrebuiltUploader, 'SyncHostPrebuilts', return_value=None)
460 board_mock = self.PatchObject(
461 prebuilt.PrebuiltUploader, 'SyncBoardPrebuilts', return_value=None)
462
463 prebuilt.main([])
464
465 init_mock.assert_called_once_with(options.upload, expected_gs_acl_path,
466 options.upload, mock.ANY,
467 options.build_path, options.packages,
468 False, None, False,
469 target, options.slave_targets,
470 mock.ANY)
471 board_mock.assert_called_once_with(
Mike Frysinger8092a632014-05-24 13:25:46 -0400472 options.key, options.git_sync,
Gilad Arnoldad333182015-05-27 15:50:41 -0700473 options.sync_binhost_conf, options.upload_board_tarball, None,
474 [], '', [], '')
Mike Frysinger36870f92015-04-12 02:59:55 -0400475 host_mock.assert_called_once_with(
476 options.key, options.git_sync, options.sync_binhost_conf)
David Jamesc0f158a2011-02-22 16:07:29 -0800477
Mike Frysinger9e979b92012-11-29 02:55:09 -0500478
Mike Frysinger68182472014-11-05 22:38:39 -0500479class TestSdk(cros_test_lib.MockTestCase):
Mike Frysinger9e979b92012-11-29 02:55:09 -0500480 """Test logic related to uploading SDK binaries"""
481
482 def setUp(self):
Mike Frysinger68182472014-11-05 22:38:39 -0500483 self.PatchObject(prebuilt, '_GsUpload',
484 side_effect=Exception('should not get called'))
485 self.PatchObject(prebuilt, 'UpdateBinhostConfFile',
486 side_effect=Exception('should not get called'))
487 self.upload_mock = self.PatchObject(prebuilt.PrebuiltUploader, '_Upload')
Mike Frysinger9e979b92012-11-29 02:55:09 -0500488
489 self.acl = 'magic-acl'
490
491 # All these args pretty much get ignored. Whee.
492 self.uploader = prebuilt.PrebuiltUploader(
493 'gs://foo', self.acl, 'prebuilt', [], '/', [],
Mike Frysinger8092a632014-05-24 13:25:46 -0400494 False, 'foo', False, 'x86-foo', [], 'chroot-1234')
Mike Frysinger9e979b92012-11-29 02:55:09 -0500495
Gilad Arnold2b79e2d2015-06-02 11:26:07 -0700496 def testSdkUpload(self, to_tarballs=(), to_upload_path=None,
Gilad Arnoldad333182015-05-27 15:50:41 -0700497 tc_tarballs=(), tc_upload_path=None):
Mike Frysinger9e979b92012-11-29 02:55:09 -0500498 """Make sure we can upload just an SDK tarball"""
499 tar = 'sdk.tar.xz'
500 ver = '1234'
501 vtar = 'cros-sdk-%s.tar.xz' % ver
502
Mike Frysinger68182472014-11-05 22:38:39 -0500503 calls = [
504 mock.call('%s.Manifest' % tar,
505 'gs://chromiumos-sdk/%s.Manifest' % vtar),
506 mock.call(tar, 'gs://chromiumos-sdk/%s' % vtar),
507 ]
Gilad Arnold2b79e2d2015-06-02 11:26:07 -0700508 for to in to_tarballs:
509 to = to.split(':')
Gilad Arnoldad333182015-05-27 15:50:41 -0700510 calls.append(mock.call(
Gilad Arnold2b79e2d2015-06-02 11:26:07 -0700511 to[1],
512 ('gs://chromiumos-sdk/' + to_upload_path) % {'toolchains': to[0]}))
Mike Frysinger68182472014-11-05 22:38:39 -0500513 for tc in tc_tarballs:
514 tc = tc.split(':')
515 calls.append(mock.call(
516 tc[1], ('gs://chromiumos-sdk/' + tc_upload_path) % {'target': tc[0]}))
517 calls.append(mock.call(
518 mock.ANY, 'gs://chromiumos-sdk/cros-sdk-latest.conf'))
Mike Frysinger9e979b92012-11-29 02:55:09 -0500519
Mike Frysinger8092a632014-05-24 13:25:46 -0400520 self.uploader._UploadSdkTarball('amd64-host', '',
Gilad Arnold2b79e2d2015-06-02 11:26:07 -0700521 tar, to_tarballs, to_upload_path,
Gilad Arnoldad333182015-05-27 15:50:41 -0700522 tc_tarballs, tc_upload_path)
Mike Frysinger68182472014-11-05 22:38:39 -0500523 self.upload_mock.assert_has_calls(calls)
Mike Frysinger9e979b92012-11-29 02:55:09 -0500524
Gilad Arnoldad333182015-05-27 15:50:41 -0700525 def testBoardOverlayTarballUpload(self):
526 """Make sure processing of board-specific overlay tarballs works."""
Gilad Arnold2b79e2d2015-06-02 11:26:07 -0700527 to_tarballs = (
528 ('i686-pc-linux-gnu:'
529 '/some/path/built-sdk-overlay-toolchains-i686-pc-linux-gnu.tar.xz'),
530 ('armv7a-cros-linux-gnueabi-arm-none-eabi:'
531 '/some/path/built-sdk-overlay-toolchains-armv7a-cros-linux-gnueabi-'
532 'arm-none-eabi'),
Gilad Arnoldad333182015-05-27 15:50:41 -0700533 )
Gilad Arnold2b79e2d2015-06-02 11:26:07 -0700534 to_upload_path = (
535 '1994/04/cros-sdk-overlay-toolchains-%(toolchains)s-1994.04.02.tar.xz')
536 self.testSdkUpload(to_tarballs=to_tarballs, to_upload_path=to_upload_path)
Gilad Arnoldad333182015-05-27 15:50:41 -0700537
538 def testToolchainTarballUpload(self):
539 """Make sure processing of toolchain tarballs works."""
Mike Frysinger9e979b92012-11-29 02:55:09 -0500540 tc_tarballs = (
541 'i686:/some/i686.tar.xz',
542 'arm-none:/some/arm.tar.xz',
543 )
544 tc_upload_path = '1994/04/%(target)s-1994.04.02.tar.xz'
Gilad Arnoldad333182015-05-27 15:50:41 -0700545 self.testSdkUpload(tc_tarballs=tc_tarballs, tc_upload_path=tc_upload_path)