blob: e09afec9332af5395564245e05cfb40a07342d8a [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
David James8c846492011-01-25 17:07:29 -080011import os
David James8fa34ea2011-04-15 13:00:20 -070012import multiprocessing
David James8c846492011-01-25 17:07:29 -080013import tempfile
Chris Sosa471532a2011-02-01 15:10:06 -080014
Mike Frysinger6db648e2018-07-24 19:57:58 -040015import mock
16
David Jamesc5cbd472012-06-19 16:25:45 -070017from chromite.scripts import upload_prebuilts as prebuilt
Brian Harringc92788f2012-09-21 18:07:15 -070018from chromite.lib import cros_test_lib
David James2c7ccb42012-11-04 15:34:28 -080019from chromite.lib import gs
David James615e5b52011-06-03 11:10:15 -070020from chromite.lib import binpkg
Zdenek Behanc0e18762012-09-22 04:06:17 +020021from chromite.lib import osutils
Mike Frysinger36870f92015-04-12 02:59:55 -040022from chromite.lib import parallel_unittest
Prathmesh Prabhu421eef22014-10-16 17:13:19 -070023from chromite.lib import portage_util
David James8c846492011-01-25 17:07:29 -080024
Mike Frysinger68182472014-11-05 22:38:39 -050025
Mike Frysinger27e21b72018-07-12 14:20:21 -040026# pylint: disable=protected-access
27
28
David James615e5b52011-06-03 11:10:15 -070029PUBLIC_PACKAGES = [{'CPV': 'gtk+/public1', 'SHA1': '1', 'MTIME': '1'},
David James8c846492011-01-25 17:07:29 -080030 {'CPV': 'gtk+/public2', 'SHA1': '2',
David James615e5b52011-06-03 11:10:15 -070031 'PATH': 'gtk+/foo.tgz', 'MTIME': '2'}]
32PRIVATE_PACKAGES = [{'CPV': 'private', 'SHA1': '3', 'MTIME': '3'}]
David James8c846492011-01-25 17:07:29 -080033
34
35def SimplePackageIndex(header=True, packages=True):
Chris Sosa58669192011-06-30 12:45:03 -070036 pkgindex = binpkg.PackageIndex()
37 if header:
David James2c7ccb42012-11-04 15:34:28 -080038 pkgindex.header['URI'] = 'gs://example'
Chris Sosa58669192011-06-30 12:45:03 -070039 if packages:
40 pkgindex.packages = copy.deepcopy(PUBLIC_PACKAGES + PRIVATE_PACKAGES)
41 return pkgindex
David James8c846492011-01-25 17:07:29 -080042
43
Brian Harringc92788f2012-09-21 18:07:15 -070044class TestUpdateFile(cros_test_lib.TempDirTestCase):
Mike Frysingerc6824f62014-02-03 11:09:44 -050045 """Tests for the UpdateLocalFile function."""
David James8c846492011-01-25 17:07:29 -080046
47 def setUp(self):
Mike Frysingerd6e2df02014-11-26 02:55:04 -050048 self.contents_str = [
49 '# comment that should be skipped',
50 'PKGDIR="/var/lib/portage/pkgs"',
51 'PORTAGE_BINHOST="http://no.thanks.com"',
52 'portage portage-20100310.tar.bz2',
53 'COMPILE_FLAGS="some_value=some_other"',
54 ]
Zdenek Behanc0e18762012-09-22 04:06:17 +020055 self.version_file = os.path.join(self.tempdir, 'version')
56 osutils.WriteFile(self.version_file, '\n'.join(self.contents_str))
David James8c846492011-01-25 17:07:29 -080057
David James8c846492011-01-25 17:07:29 -080058 def _read_version_file(self, version_file=None):
59 """Read the contents of self.version_file and return as a list."""
60 if not version_file:
61 version_file = self.version_file
62
63 version_fh = open(version_file)
64 try:
65 return [line.strip() for line in version_fh.readlines()]
66 finally:
67 version_fh.close()
68
69 def _verify_key_pair(self, key, val):
70 file_contents = self._read_version_file()
71 # ensure key for verify is wrapped on quotes
72 if '"' not in val:
73 val = '"%s"' % val
74 for entry in file_contents:
75 if '=' not in entry:
76 continue
77 file_key, file_val = entry.split('=')
78 if file_key == key:
79 if val == file_val:
80 break
81 else:
82 self.fail('Could not find "%s=%s" in version file' % (key, val))
83
84 def testAddVariableThatDoesNotExist(self):
85 """Add in a new variable that was no present in the file."""
86 key = 'PORTAGE_BINHOST'
87 value = '1234567'
88 prebuilt.UpdateLocalFile(self.version_file, value)
Mike Frysinger383367e2014-09-16 15:06:17 -040089 print(self.version_file)
Chris Sosa58669192011-06-30 12:45:03 -070090 self._read_version_file()
David James8c846492011-01-25 17:07:29 -080091 self._verify_key_pair(key, value)
Mike Frysinger383367e2014-09-16 15:06:17 -040092 print(self.version_file)
David James8c846492011-01-25 17:07:29 -080093
94 def testUpdateVariable(self):
95 """Test updating a variable that already exists."""
96 key, val = self.contents_str[2].split('=')
97 new_val = 'test_update'
98 self._verify_key_pair(key, val)
99 prebuilt.UpdateLocalFile(self.version_file, new_val)
100 self._verify_key_pair(key, new_val)
101
102 def testUpdateNonExistentFile(self):
103 key = 'PORTAGE_BINHOST'
104 value = '1234567'
105 non_existent_file = tempfile.mktemp()
106 try:
107 prebuilt.UpdateLocalFile(non_existent_file, value)
108 file_contents = self._read_version_file(non_existent_file)
Chris Sosa739c8752012-05-16 19:30:35 -0700109 self.assertEqual(file_contents, ['%s="%s"' % (key, value)])
David James8c846492011-01-25 17:07:29 -0800110 finally:
111 if os.path.exists(non_existent_file):
112 os.remove(non_existent_file)
113
114
Mike Frysinger68182472014-11-05 22:38:39 -0500115class TestPrebuilt(cros_test_lib.MockTestCase):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500116 """Tests for Prebuilt logic."""
David James8c846492011-01-25 17:07:29 -0800117
Bertrand SIMONNET811bcde2014-11-20 15:21:25 -0800118 def setUp(self):
119 self._base_local_path = '/b/cbuild/build/chroot/build/x86-dogfood/'
120 self._gs_bucket_path = 'gs://chromeos-prebuilt/host/version'
121 self._local_path = os.path.join(self._base_local_path, 'public1.tbz2')
122
David James8c846492011-01-25 17:07:29 -0800123 def testGenerateUploadDict(self):
Mike Frysinger68182472014-11-05 22:38:39 -0500124 self.PatchObject(prebuilt.os.path, 'exists', return_true=True)
Mike Frysingerd6e2df02014-11-26 02:55:04 -0500125 pkgs = [{'CPV': 'public1'}]
Bertrand SIMONNET811bcde2014-11-20 15:21:25 -0800126 result = prebuilt.GenerateUploadDict(self._base_local_path,
127 self._gs_bucket_path, pkgs)
128 expected = {self._local_path: self._gs_bucket_path + '/public1.tbz2', }
129 self.assertEqual(result, expected)
130
131 def testGenerateUploadDictWithDebug(self):
132 self.PatchObject(prebuilt.os.path, 'exists', return_true=True)
133 pkgs = [{'CPV': 'public1', 'DEBUG_SYMBOLS': 'yes'}]
134 result = prebuilt.GenerateUploadDict(self._base_local_path,
135 self._gs_bucket_path, pkgs)
136 expected = {self._local_path: self._gs_bucket_path + '/public1.tbz2',
137 self._local_path.replace('.tbz2', '.debug.tbz2'):
138 self._gs_bucket_path + '/public1.debug.tbz2'}
David James8c846492011-01-25 17:07:29 -0800139 self.assertEqual(result, expected)
140
David James8c846492011-01-25 17:07:29 -0800141 def testDeterminePrebuiltConfHost(self):
142 """Test that the host prebuilt path comes back properly."""
143 expected_path = os.path.join(prebuilt._PREBUILT_MAKE_CONF['amd64'])
144 self.assertEqual(prebuilt.DeterminePrebuiltConfFile('fake_path', 'amd64'),
145 expected_path)
146
David James8c846492011-01-25 17:07:29 -0800147
David James2c7ccb42012-11-04 15:34:28 -0800148class TestPkgIndex(cros_test_lib.TestCase):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500149 """Helper for tests that update the Packages index file."""
David James2c7ccb42012-11-04 15:34:28 -0800150
151 def setUp(self):
152 self.db = {}
153 self.pkgindex = SimplePackageIndex()
154 self.empty = SimplePackageIndex(packages=False)
155
156 def assertURIs(self, uris):
157 """Verify that the duplicate DB has the specified URLs."""
158 expected = [v.uri for _, v in sorted(self.db.items())]
159 self.assertEqual(expected, uris)
160
161
162class TestPackagesFileFiltering(TestPkgIndex):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500163 """Tests for Packages filtering behavior."""
David James8c846492011-01-25 17:07:29 -0800164
165 def testFilterPkgIndex(self):
David James2c7ccb42012-11-04 15:34:28 -0800166 """Test filtering out of private packages."""
167 self.pkgindex.RemoveFilteredPackages(lambda pkg: pkg in PRIVATE_PACKAGES)
168 self.assertEqual(self.pkgindex.packages, PUBLIC_PACKAGES)
169 self.assertEqual(self.pkgindex.modified, True)
David James8c846492011-01-25 17:07:29 -0800170
171
David James2c7ccb42012-11-04 15:34:28 -0800172class TestPopulateDuplicateDB(TestPkgIndex):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500173 """Tests for the _PopulateDuplicateDB function."""
David James8c846492011-01-25 17:07:29 -0800174
175 def testEmptyIndex(self):
David James2c7ccb42012-11-04 15:34:28 -0800176 """Test population of the duplicate DB with an empty index."""
177 self.empty._PopulateDuplicateDB(self.db, 0)
178 self.assertEqual(self.db, {})
David James8c846492011-01-25 17:07:29 -0800179
180 def testNormalIndex(self):
David James2c7ccb42012-11-04 15:34:28 -0800181 """Test population of the duplicate DB with a full index."""
182 self.pkgindex._PopulateDuplicateDB(self.db, 0)
183 self.assertURIs(['gs://example/gtk+/public1.tbz2',
184 'gs://example/gtk+/foo.tgz',
185 'gs://example/private.tbz2'])
David James8c846492011-01-25 17:07:29 -0800186
187 def testMissingSHA1(self):
David James2c7ccb42012-11-04 15:34:28 -0800188 """Test population of the duplicate DB with a missing SHA1."""
189 del self.pkgindex.packages[0]['SHA1']
190 self.pkgindex._PopulateDuplicateDB(self.db, 0)
191 self.assertURIs(['gs://example/gtk+/foo.tgz',
192 'gs://example/private.tbz2'])
David James8c846492011-01-25 17:07:29 -0800193
194 def testFailedPopulate(self):
David James2c7ccb42012-11-04 15:34:28 -0800195 """Test failure conditions for the populate method."""
196 headerless = SimplePackageIndex(header=False)
197 self.assertRaises(KeyError, headerless._PopulateDuplicateDB, self.db, 0)
198 del self.pkgindex.packages[0]['CPV']
199 self.assertRaises(KeyError, self.pkgindex._PopulateDuplicateDB, self.db, 0)
David James8c846492011-01-25 17:07:29 -0800200
201
Mike Frysinger68182472014-11-05 22:38:39 -0500202class TestResolveDuplicateUploads(cros_test_lib.MockTestCase, TestPkgIndex):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500203 """Tests for the ResolveDuplicateUploads function."""
David James8c846492011-01-25 17:07:29 -0800204
David James615e5b52011-06-03 11:10:15 -0700205 def setUp(self):
Mike Frysinger68182472014-11-05 22:38:39 -0500206 self.PatchObject(binpkg.time, 'time', return_value=binpkg.TWO_WEEKS)
David James2c7ccb42012-11-04 15:34:28 -0800207 self.db = {}
208 self.dup = SimplePackageIndex()
209 self.expected_pkgindex = SimplePackageIndex()
210
211 def assertNoDuplicates(self, candidates):
212 """Verify no duplicates are found with the specified candidates."""
213 uploads = self.pkgindex.ResolveDuplicateUploads(candidates)
214 self.assertEqual(uploads, self.pkgindex.packages)
215 self.assertEqual(len(self.pkgindex.packages),
216 len(self.expected_pkgindex.packages))
217 for pkg1, pkg2 in zip(self.pkgindex.packages,
218 self.expected_pkgindex.packages):
219 self.assertNotEqual(pkg1['MTIME'], pkg2['MTIME'])
220 del pkg1['MTIME']
221 del pkg2['MTIME']
222 self.assertEqual(self.pkgindex.modified, False)
223 self.assertEqual(self.pkgindex.packages, self.expected_pkgindex.packages)
224
225 def assertAllDuplicates(self, candidates):
226 """Verify every package is a duplicate in the specified list."""
227 for pkg in self.expected_pkgindex.packages:
228 pkg.setdefault('PATH', pkg['CPV'] + '.tbz2')
229 self.pkgindex.ResolveDuplicateUploads(candidates)
230 self.assertEqual(self.pkgindex.packages, self.expected_pkgindex.packages)
David James615e5b52011-06-03 11:10:15 -0700231
David James8c846492011-01-25 17:07:29 -0800232 def testEmptyList(self):
David James2c7ccb42012-11-04 15:34:28 -0800233 """If no candidates are supplied, no duplicates should be found."""
234 self.assertNoDuplicates([])
David James8c846492011-01-25 17:07:29 -0800235
236 def testEmptyIndex(self):
David James2c7ccb42012-11-04 15:34:28 -0800237 """If no packages are supplied, no duplicates should be found."""
238 self.assertNoDuplicates([self.empty])
David James8c846492011-01-25 17:07:29 -0800239
David James2c7ccb42012-11-04 15:34:28 -0800240 def testDifferentURI(self):
241 """If the URI differs, no duplicates should be found."""
242 self.dup.header['URI'] = 'gs://example2'
243 self.assertNoDuplicates([self.dup])
244
245 def testUpdateModificationTime(self):
246 """When duplicates are found, we should use the latest mtime."""
247 for pkg in self.expected_pkgindex.packages:
248 pkg['MTIME'] = '10'
249 for pkg in self.dup.packages:
250 pkg['MTIME'] = '4'
251 self.assertAllDuplicates([self.expected_pkgindex, self.dup])
252
253 def testCanonicalUrl(self):
254 """If the URL is in a different format, we should still find duplicates."""
255 self.dup.header['URI'] = gs.PUBLIC_BASE_HTTPS_URL + 'example'
256 self.assertAllDuplicates([self.dup])
David James8c846492011-01-25 17:07:29 -0800257
258 def testMissingSHA1(self):
David James2c7ccb42012-11-04 15:34:28 -0800259 """We should not find duplicates if there is no SHA1."""
260 del self.pkgindex.packages[0]['SHA1']
261 del self.expected_pkgindex.packages[0]['SHA1']
262 for pkg in self.expected_pkgindex.packages[1:]:
David James8c846492011-01-25 17:07:29 -0800263 pkg.setdefault('PATH', pkg['CPV'] + '.tbz2')
David James2c7ccb42012-11-04 15:34:28 -0800264 self.pkgindex.ResolveDuplicateUploads([self.dup])
265 self.assertNotEqual(self.pkgindex.packages[0]['MTIME'],
266 self.expected_pkgindex.packages[0]['MTIME'])
267 del self.pkgindex.packages[0]['MTIME']
268 del self.expected_pkgindex.packages[0]['MTIME']
269 self.assertEqual(self.pkgindex.packages, self.expected_pkgindex.packages)
David James8c846492011-01-25 17:07:29 -0800270
Bertrand SIMONNET811bcde2014-11-20 15:21:25 -0800271 def testSymbolsAvailable(self):
272 """If symbols are available remotely, re-use them and set DEBUG_SYMBOLS."""
273 self.dup.packages[0]['DEBUG_SYMBOLS'] = 'yes'
274
275 uploads = self.pkgindex.ResolveDuplicateUploads([self.dup])
276 self.assertEqual(uploads, [])
277 self.assertEqual(self.pkgindex.packages[0].get('DEBUG_SYMBOLS'), 'yes')
278
279 def testSymbolsAvailableLocallyOnly(self):
280 """If the symbols are only available locally, reupload them."""
281 self.pkgindex.packages[0]['DEBUG_SYMBOLS'] = 'yes'
282
283 uploads = self.pkgindex.ResolveDuplicateUploads([self.dup])
284 self.assertEqual(uploads, [self.pkgindex.packages[0]])
285
David James8c846492011-01-25 17:07:29 -0800286
Mike Frysinger68182472014-11-05 22:38:39 -0500287class TestWritePackageIndex(cros_test_lib.MockTestCase, TestPkgIndex):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500288 """Tests for the WriteToNamedTemporaryFile function."""
David James8c846492011-01-25 17:07:29 -0800289
290 def testSimple(self):
David James2c7ccb42012-11-04 15:34:28 -0800291 """Test simple call of WriteToNamedTemporaryFile()"""
Mike Frysinger68182472014-11-05 22:38:39 -0500292 self.PatchObject(self.pkgindex, 'Write')
David James2c7ccb42012-11-04 15:34:28 -0800293 f = self.pkgindex.WriteToNamedTemporaryFile()
Mike Frysinger652057c2019-12-09 23:59:07 -0500294 self.assertEqual(f.read(), '')
David James8c846492011-01-25 17:07:29 -0800295
296
Mike Frysinger36870f92015-04-12 02:59:55 -0400297class TestUploadPrebuilt(cros_test_lib.MockTempDirTestCase):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500298 """Tests for the _UploadPrebuilt function."""
David James05bcb2b2011-02-09 09:25:47 -0800299
300 def setUp(self):
301 class MockTemporaryFile(object):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500302 """Mock out the temporary file logic."""
David James05bcb2b2011-02-09 09:25:47 -0800303 def __init__(self, name):
304 self.name = name
David James05bcb2b2011-02-09 09:25:47 -0800305 self.pkgindex = SimplePackageIndex()
Mike Frysinger36870f92015-04-12 02:59:55 -0400306 self.PatchObject(binpkg, 'GrabLocalPackageIndex',
307 return_value=self.pkgindex)
308 self.PatchObject(self.pkgindex, 'ResolveDuplicateUploads',
309 return_value=PRIVATE_PACKAGES)
310 self.PatchObject(self.pkgindex, 'WriteToNamedTemporaryFile',
311 return_value=MockTemporaryFile('fake'))
312 self.remote_up_mock = self.PatchObject(prebuilt, 'RemoteUpload')
313 self.gs_up_mock = self.PatchObject(prebuilt, '_GsUpload')
David James05bcb2b2011-02-09 09:25:47 -0800314
David James05bcb2b2011-02-09 09:25:47 -0800315 def testSuccessfulGsUpload(self):
Alex Deymo541ea6f2014-12-23 01:18:14 -0800316 uploads = {
317 os.path.join(self.tempdir, 'private.tbz2'): 'gs://foo/private.tbz2'}
Alex Deymo541ea6f2014-12-23 01:18:14 -0800318 packages = list(PRIVATE_PACKAGES)
319 packages.append({'CPV': 'dev-only-extras'})
Mike Frysinger36870f92015-04-12 02:59:55 -0400320 osutils.Touch(os.path.join(self.tempdir, 'dev-only-extras.tbz2'))
321 self.PatchObject(prebuilt, 'GenerateUploadDict',
322 return_value=uploads)
David James05bcb2b2011-02-09 09:25:47 -0800323 uploads = uploads.copy()
324 uploads['fake'] = 'gs://foo/suffix/Packages'
David Jamesfd0b0852011-02-23 11:15:36 -0800325 acl = 'public-read'
David Jamesc0f158a2011-02-22 16:07:29 -0800326 uri = self.pkgindex.header['URI']
David James8ece7ee2011-06-29 16:02:30 -0700327 uploader = prebuilt.PrebuiltUploader('gs://foo', acl, uri, [], '/', [],
Mike Frysinger8092a632014-05-24 13:25:46 -0400328 False, 'foo', False, 'x86-foo', [], '')
Alex Deymo541ea6f2014-12-23 01:18:14 -0800329 uploader._UploadPrebuilt(self.tempdir, 'suffix')
Mike Frysinger36870f92015-04-12 02:59:55 -0400330 self.remote_up_mock.assert_called_once_with(mock.ANY, acl, uploads)
331 self.assertTrue(self.gs_up_mock.called)
David James05bcb2b2011-02-09 09:25:47 -0800332
David James05bcb2b2011-02-09 09:25:47 -0800333
Mike Frysinger36870f92015-04-12 02:59:55 -0400334class TestSyncPrebuilts(cros_test_lib.MockTestCase):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500335 """Tests for the SyncHostPrebuilts function."""
David James05bcb2b2011-02-09 09:25:47 -0800336
337 def setUp(self):
Mike Frysinger36870f92015-04-12 02:59:55 -0400338 self.rev_mock = self.PatchObject(prebuilt, 'RevGitFile', return_value=None)
339 self.update_binhost_mock = self.PatchObject(
340 prebuilt, 'UpdateBinhostConfFile', return_value=None)
David James05bcb2b2011-02-09 09:25:47 -0800341 self.build_path = '/trunk'
342 self.upload_location = 'gs://upload/'
343 self.version = '1'
344 self.binhost = 'http://prebuilt/'
345 self.key = 'PORTAGE_BINHOST'
Mike Frysinger36870f92015-04-12 02:59:55 -0400346 self.upload_mock = self.PatchObject(prebuilt.PrebuiltUploader,
347 '_UploadPrebuilt', return_value=True)
David James05bcb2b2011-02-09 09:25:47 -0800348
David James05bcb2b2011-02-09 09:25:47 -0800349 def testSyncHostPrebuilts(self):
David Jamese2488642011-11-14 16:15:20 -0800350 board = 'x86-foo'
David James4058b0d2011-12-08 21:24:50 -0800351 target = prebuilt.BuildTarget(board, 'aura')
352 slave_targets = [prebuilt.BuildTarget('x86-bar', 'aura')]
David James05bcb2b2011-02-09 09:25:47 -0800353 package_path = os.path.join(self.build_path,
354 prebuilt._HOST_PACKAGES_PATH)
Mike Frysingerd6e2df02014-11-26 02:55:04 -0500355 url_suffix = prebuilt._REL_HOST_PATH % {
356 'version': self.version,
357 'host_arch': prebuilt._HOST_ARCH,
358 'target': target,
359 }
David James8fa34ea2011-04-15 13:00:20 -0700360 packages_url_suffix = '%s/packages' % url_suffix.rstrip('/')
David Jamesf0e6fd72011-04-15 15:58:07 -0700361 url_value = '%s/%s/' % (self.binhost.rstrip('/'),
362 packages_url_suffix.rstrip('/'))
David Jamese2488642011-11-14 16:15:20 -0800363 urls = [url_value.replace('foo', 'bar'), url_value]
David James20b2b6f2011-11-18 15:11:58 -0800364 binhost = ' '.join(urls)
David James615e5b52011-06-03 11:10:15 -0700365 uploader = prebuilt.PrebuiltUploader(
366 self.upload_location, 'public-read', self.binhost, [],
Mike Frysinger8092a632014-05-24 13:25:46 -0400367 self.build_path, [], False, 'foo', False, target, slave_targets,
368 self.version)
369 uploader.SyncHostPrebuilts(self.key, True, True)
Mike Frysinger36870f92015-04-12 02:59:55 -0400370 self.upload_mock.assert_called_once_with(package_path, packages_url_suffix)
371 self.rev_mock.assert_called_once_with(
372 mock.ANY, {self.key: binhost}, dryrun=False)
373 self.update_binhost_mock.assert_called_once_with(
374 mock.ANY, self.key, binhost)
David James05bcb2b2011-02-09 09:25:47 -0800375
376 def testSyncBoardPrebuilts(self):
David Jamese2488642011-11-14 16:15:20 -0800377 board = 'x86-foo'
David James4058b0d2011-12-08 21:24:50 -0800378 target = prebuilt.BuildTarget(board, 'aura')
379 slave_targets = [prebuilt.BuildTarget('x86-bar', 'aura')]
Mike Frysinger8092a632014-05-24 13:25:46 -0400380 board_path = os.path.join(
381 self.build_path, prebuilt._BOARD_PATH % {'board': board})
David James05bcb2b2011-02-09 09:25:47 -0800382 package_path = os.path.join(board_path, 'packages')
Mike Frysingerd6e2df02014-11-26 02:55:04 -0500383 url_suffix = prebuilt._REL_BOARD_PATH % {
384 'version': self.version,
385 'target': target,
386 }
David James8fa34ea2011-04-15 13:00:20 -0700387 packages_url_suffix = '%s/packages' % url_suffix.rstrip('/')
David Jamesf0e6fd72011-04-15 15:58:07 -0700388 url_value = '%s/%s/' % (self.binhost.rstrip('/'),
389 packages_url_suffix.rstrip('/'))
David Jamese2488642011-11-14 16:15:20 -0800390 bar_binhost = url_value.replace('foo', 'bar')
Mike Frysinger36870f92015-04-12 02:59:55 -0400391 determine_mock = self.PatchObject(prebuilt, 'DeterminePrebuiltConfFile',
392 side_effect=('bar', 'foo'))
393 self.PatchObject(prebuilt.PrebuiltUploader, '_UploadSdkTarball')
394 with parallel_unittest.ParallelMock():
395 multiprocessing.Process.exitcode = 0
396 uploader = prebuilt.PrebuiltUploader(
397 self.upload_location, 'public-read', self.binhost, [],
398 self.build_path, [], False, 'foo', False, target, slave_targets,
399 self.version)
Gilad Arnoldad333182015-05-27 15:50:41 -0700400 uploader.SyncBoardPrebuilts(self.key, True, True, True, None, None, None,
401 None, None)
Mike Frysinger36870f92015-04-12 02:59:55 -0400402 determine_mock.assert_has_calls([
403 mock.call(self.build_path, slave_targets[0]),
404 mock.call(self.build_path, target),
405 ])
406 self.upload_mock.assert_called_once_with(package_path, packages_url_suffix)
407 self.rev_mock.assert_has_calls([
408 mock.call('bar', {self.key: bar_binhost}, dryrun=False),
409 mock.call('foo', {self.key: url_value}, dryrun=False),
410 ])
411 self.update_binhost_mock.assert_has_calls([
412 mock.call(mock.ANY, self.key, bar_binhost),
413 mock.call(mock.ANY, self.key, url_value),
414 ])
David James05bcb2b2011-02-09 09:25:47 -0800415
416
Mike Frysinger36870f92015-04-12 02:59:55 -0400417class TestMain(cros_test_lib.MockTestCase):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500418 """Tests for the main() function."""
David Jamesc0f158a2011-02-22 16:07:29 -0800419
420 def testMain(self):
421 """Test that the main function works."""
Mike Frysinger36870f92015-04-12 02:59:55 -0400422 options = mock.MagicMock()
David Jamesc0f158a2011-02-22 16:07:29 -0800423 old_binhost = 'http://prebuilt/1'
424 options.previous_binhost_url = [old_binhost]
David Jamese2488642011-11-14 16:15:20 -0800425 options.board = 'x86-foo'
David James4058b0d2011-12-08 21:24:50 -0800426 options.profile = None
Chris Sosa6a5dceb2012-05-14 13:48:56 -0700427 target = prebuilt.BuildTarget(options.board, options.profile)
David Jamesc0f158a2011-02-22 16:07:29 -0800428 options.build_path = '/trunk'
Mike Frysinger86509232014-05-24 13:18:37 -0400429 options.dryrun = False
David Jamesfd0b0852011-02-23 11:15:36 -0800430 options.private = True
David James615e5b52011-06-03 11:10:15 -0700431 options.packages = []
David Jamesc0f158a2011-02-22 16:07:29 -0800432 options.sync_host = True
433 options.git_sync = True
David James8fa34ea2011-04-15 13:00:20 -0700434 options.upload_board_tarball = True
Zdenek Behan62a57792012-08-31 15:09:08 +0200435 options.prepackaged_tarball = None
Gilad Arnold2b79e2d2015-06-02 11:26:07 -0700436 options.toolchains_overlay_tarballs = []
437 options.toolchains_overlay_upload_path = ''
Mike Frysinger9e979b92012-11-29 02:55:09 -0500438 options.toolchain_tarballs = []
439 options.toolchain_upload_path = ''
David Jamesc0f158a2011-02-22 16:07:29 -0800440 options.upload = 'gs://upload/'
David Jamesfd0b0852011-02-23 11:15:36 -0800441 options.binhost_base_url = options.upload
David Jamesc0f158a2011-02-22 16:07:29 -0800442 options.prepend_version = True
David James8ece7ee2011-06-29 16:02:30 -0700443 options.set_version = None
444 options.skip_upload = False
David Jamesc0f158a2011-02-22 16:07:29 -0800445 options.filters = True
446 options.key = 'PORTAGE_BINHOST'
David Jamesb26b9312014-12-15 11:26:46 -0800447 options.binhost_conf_dir = None
David Jamesc0f158a2011-02-22 16:07:29 -0800448 options.sync_binhost_conf = True
David James4058b0d2011-12-08 21:24:50 -0800449 options.slave_targets = [prebuilt.BuildTarget('x86-bar', 'aura')]
Mike Frysinger36870f92015-04-12 02:59:55 -0400450 self.PatchObject(prebuilt, 'ParseOptions',
451 return_value=tuple([options, target]))
452 self.PatchObject(binpkg, 'GrabRemotePackageIndex', return_value=True)
453 init_mock = self.PatchObject(prebuilt.PrebuiltUploader, '__init__',
454 return_value=None)
455 expected_gs_acl_path = os.path.join('/fake_path',
Prathmesh Prabhu5f14da02014-10-17 15:13:56 -0700456 prebuilt._GOOGLESTORAGE_GSUTIL_FILE)
Mike Frysinger36870f92015-04-12 02:59:55 -0400457 self.PatchObject(portage_util, 'FindOverlayFile',
458 return_value=expected_gs_acl_path)
459 host_mock = self.PatchObject(
460 prebuilt.PrebuiltUploader, 'SyncHostPrebuilts', return_value=None)
461 board_mock = self.PatchObject(
462 prebuilt.PrebuiltUploader, 'SyncBoardPrebuilts', return_value=None)
463
464 prebuilt.main([])
465
466 init_mock.assert_called_once_with(options.upload, expected_gs_acl_path,
467 options.upload, mock.ANY,
468 options.build_path, options.packages,
469 False, None, False,
470 target, options.slave_targets,
471 mock.ANY)
472 board_mock.assert_called_once_with(
Mike Frysinger8092a632014-05-24 13:25:46 -0400473 options.key, options.git_sync,
Gilad Arnoldad333182015-05-27 15:50:41 -0700474 options.sync_binhost_conf, options.upload_board_tarball, None,
475 [], '', [], '')
Mike Frysinger36870f92015-04-12 02:59:55 -0400476 host_mock.assert_called_once_with(
477 options.key, options.git_sync, options.sync_binhost_conf)
David Jamesc0f158a2011-02-22 16:07:29 -0800478
Mike Frysinger9e979b92012-11-29 02:55:09 -0500479
Mike Frysinger68182472014-11-05 22:38:39 -0500480class TestSdk(cros_test_lib.MockTestCase):
Mike Frysinger9e979b92012-11-29 02:55:09 -0500481 """Test logic related to uploading SDK binaries"""
482
483 def setUp(self):
Mike Frysinger68182472014-11-05 22:38:39 -0500484 self.PatchObject(prebuilt, '_GsUpload',
485 side_effect=Exception('should not get called'))
486 self.PatchObject(prebuilt, 'UpdateBinhostConfFile',
487 side_effect=Exception('should not get called'))
488 self.upload_mock = self.PatchObject(prebuilt.PrebuiltUploader, '_Upload')
Mike Frysinger9e979b92012-11-29 02:55:09 -0500489
490 self.acl = 'magic-acl'
491
492 # All these args pretty much get ignored. Whee.
493 self.uploader = prebuilt.PrebuiltUploader(
494 'gs://foo', self.acl, 'prebuilt', [], '/', [],
Mike Frysinger8092a632014-05-24 13:25:46 -0400495 False, 'foo', False, 'x86-foo', [], 'chroot-1234')
Mike Frysinger9e979b92012-11-29 02:55:09 -0500496
Gilad Arnold2b79e2d2015-06-02 11:26:07 -0700497 def testSdkUpload(self, to_tarballs=(), to_upload_path=None,
Gilad Arnoldad333182015-05-27 15:50:41 -0700498 tc_tarballs=(), tc_upload_path=None):
Mike Frysinger9e979b92012-11-29 02:55:09 -0500499 """Make sure we can upload just an SDK tarball"""
500 tar = 'sdk.tar.xz'
501 ver = '1234'
502 vtar = 'cros-sdk-%s.tar.xz' % ver
503
Mike Frysinger68182472014-11-05 22:38:39 -0500504 calls = [
505 mock.call('%s.Manifest' % tar,
506 'gs://chromiumos-sdk/%s.Manifest' % vtar),
507 mock.call(tar, 'gs://chromiumos-sdk/%s' % vtar),
508 ]
Gilad Arnold2b79e2d2015-06-02 11:26:07 -0700509 for to in to_tarballs:
510 to = to.split(':')
Gilad Arnoldad333182015-05-27 15:50:41 -0700511 calls.append(mock.call(
Gilad Arnold2b79e2d2015-06-02 11:26:07 -0700512 to[1],
513 ('gs://chromiumos-sdk/' + to_upload_path) % {'toolchains': to[0]}))
Mike Frysinger68182472014-11-05 22:38:39 -0500514 for tc in tc_tarballs:
515 tc = tc.split(':')
516 calls.append(mock.call(
517 tc[1], ('gs://chromiumos-sdk/' + tc_upload_path) % {'target': tc[0]}))
518 calls.append(mock.call(
519 mock.ANY, 'gs://chromiumos-sdk/cros-sdk-latest.conf'))
Mike Frysinger9e979b92012-11-29 02:55:09 -0500520
Mike Frysinger8092a632014-05-24 13:25:46 -0400521 self.uploader._UploadSdkTarball('amd64-host', '',
Gilad Arnold2b79e2d2015-06-02 11:26:07 -0700522 tar, to_tarballs, to_upload_path,
Gilad Arnoldad333182015-05-27 15:50:41 -0700523 tc_tarballs, tc_upload_path)
Mike Frysinger68182472014-11-05 22:38:39 -0500524 self.upload_mock.assert_has_calls(calls)
Mike Frysinger9e979b92012-11-29 02:55:09 -0500525
Gilad Arnoldad333182015-05-27 15:50:41 -0700526 def testBoardOverlayTarballUpload(self):
527 """Make sure processing of board-specific overlay tarballs works."""
Gilad Arnold2b79e2d2015-06-02 11:26:07 -0700528 to_tarballs = (
529 ('i686-pc-linux-gnu:'
530 '/some/path/built-sdk-overlay-toolchains-i686-pc-linux-gnu.tar.xz'),
531 ('armv7a-cros-linux-gnueabi-arm-none-eabi:'
532 '/some/path/built-sdk-overlay-toolchains-armv7a-cros-linux-gnueabi-'
533 'arm-none-eabi'),
Gilad Arnoldad333182015-05-27 15:50:41 -0700534 )
Gilad Arnold2b79e2d2015-06-02 11:26:07 -0700535 to_upload_path = (
536 '1994/04/cros-sdk-overlay-toolchains-%(toolchains)s-1994.04.02.tar.xz')
537 self.testSdkUpload(to_tarballs=to_tarballs, to_upload_path=to_upload_path)
Gilad Arnoldad333182015-05-27 15:50:41 -0700538
539 def testToolchainTarballUpload(self):
540 """Make sure processing of toolchain tarballs works."""
Mike Frysinger9e979b92012-11-29 02:55:09 -0500541 tc_tarballs = (
542 'i686:/some/i686.tar.xz',
543 'arm-none:/some/arm.tar.xz',
544 )
545 tc_upload_path = '1994/04/%(target)s-1994.04.02.tar.xz'
Gilad Arnoldad333182015-05-27 15:50:41 -0700546 self.testSdkUpload(tc_tarballs=tc_tarballs, tc_upload_path=tc_upload_path)