blob: d5650b161660e784c43c5c6623f572018d2bf1a0 [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
David James4058b0d2011-12-08 21:24:50 -080025# pylint: disable=E1120,W0212,R0904
David James615e5b52011-06-03 11:10:15 -070026PUBLIC_PACKAGES = [{'CPV': 'gtk+/public1', 'SHA1': '1', 'MTIME': '1'},
David James8c846492011-01-25 17:07:29 -080027 {'CPV': 'gtk+/public2', 'SHA1': '2',
David James615e5b52011-06-03 11:10:15 -070028 'PATH': 'gtk+/foo.tgz', 'MTIME': '2'}]
29PRIVATE_PACKAGES = [{'CPV': 'private', 'SHA1': '3', 'MTIME': '3'}]
David James8c846492011-01-25 17:07:29 -080030
31
32def SimplePackageIndex(header=True, packages=True):
Chris Sosa58669192011-06-30 12:45:03 -070033 pkgindex = binpkg.PackageIndex()
34 if header:
David James2c7ccb42012-11-04 15:34:28 -080035 pkgindex.header['URI'] = 'gs://example'
Chris Sosa58669192011-06-30 12:45:03 -070036 if packages:
37 pkgindex.packages = copy.deepcopy(PUBLIC_PACKAGES + PRIVATE_PACKAGES)
38 return pkgindex
David James8c846492011-01-25 17:07:29 -080039
40
Brian Harringc92788f2012-09-21 18:07:15 -070041class TestUpdateFile(cros_test_lib.TempDirTestCase):
Mike Frysingerc6824f62014-02-03 11:09:44 -050042 """Tests for the UpdateLocalFile function."""
David James8c846492011-01-25 17:07:29 -080043
44 def setUp(self):
Mike Frysingerd6e2df02014-11-26 02:55:04 -050045 self.contents_str = [
46 '# comment that should be skipped',
47 'PKGDIR="/var/lib/portage/pkgs"',
48 'PORTAGE_BINHOST="http://no.thanks.com"',
49 'portage portage-20100310.tar.bz2',
50 'COMPILE_FLAGS="some_value=some_other"',
51 ]
Zdenek Behanc0e18762012-09-22 04:06:17 +020052 self.version_file = os.path.join(self.tempdir, 'version')
53 osutils.WriteFile(self.version_file, '\n'.join(self.contents_str))
David James8c846492011-01-25 17:07:29 -080054
David James8c846492011-01-25 17:07:29 -080055 def _read_version_file(self, version_file=None):
56 """Read the contents of self.version_file and return as a list."""
57 if not version_file:
58 version_file = self.version_file
59
60 version_fh = open(version_file)
61 try:
62 return [line.strip() for line in version_fh.readlines()]
63 finally:
64 version_fh.close()
65
66 def _verify_key_pair(self, key, val):
67 file_contents = self._read_version_file()
68 # ensure key for verify is wrapped on quotes
69 if '"' not in val:
70 val = '"%s"' % val
71 for entry in file_contents:
72 if '=' not in entry:
73 continue
74 file_key, file_val = entry.split('=')
75 if file_key == key:
76 if val == file_val:
77 break
78 else:
79 self.fail('Could not find "%s=%s" in version file' % (key, val))
80
81 def testAddVariableThatDoesNotExist(self):
82 """Add in a new variable that was no present in the file."""
83 key = 'PORTAGE_BINHOST'
84 value = '1234567'
85 prebuilt.UpdateLocalFile(self.version_file, value)
Mike Frysinger383367e2014-09-16 15:06:17 -040086 print(self.version_file)
Chris Sosa58669192011-06-30 12:45:03 -070087 self._read_version_file()
David James8c846492011-01-25 17:07:29 -080088 self._verify_key_pair(key, value)
Mike Frysinger383367e2014-09-16 15:06:17 -040089 print(self.version_file)
David James8c846492011-01-25 17:07:29 -080090
91 def testUpdateVariable(self):
92 """Test updating a variable that already exists."""
93 key, val = self.contents_str[2].split('=')
94 new_val = 'test_update'
95 self._verify_key_pair(key, val)
96 prebuilt.UpdateLocalFile(self.version_file, new_val)
97 self._verify_key_pair(key, new_val)
98
99 def testUpdateNonExistentFile(self):
100 key = 'PORTAGE_BINHOST'
101 value = '1234567'
102 non_existent_file = tempfile.mktemp()
103 try:
104 prebuilt.UpdateLocalFile(non_existent_file, value)
105 file_contents = self._read_version_file(non_existent_file)
Chris Sosa739c8752012-05-16 19:30:35 -0700106 self.assertEqual(file_contents, ['%s="%s"' % (key, value)])
David James8c846492011-01-25 17:07:29 -0800107 finally:
108 if os.path.exists(non_existent_file):
109 os.remove(non_existent_file)
110
111
Mike Frysinger68182472014-11-05 22:38:39 -0500112class TestPrebuilt(cros_test_lib.MockTestCase):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500113 """Tests for Prebuilt logic."""
David James8c846492011-01-25 17:07:29 -0800114
Bertrand SIMONNET811bcde2014-11-20 15:21:25 -0800115 def setUp(self):
116 self._base_local_path = '/b/cbuild/build/chroot/build/x86-dogfood/'
117 self._gs_bucket_path = 'gs://chromeos-prebuilt/host/version'
118 self._local_path = os.path.join(self._base_local_path, 'public1.tbz2')
119
David James8c846492011-01-25 17:07:29 -0800120 def testGenerateUploadDict(self):
Mike Frysinger68182472014-11-05 22:38:39 -0500121 self.PatchObject(prebuilt.os.path, 'exists', return_true=True)
Mike Frysingerd6e2df02014-11-26 02:55:04 -0500122 pkgs = [{'CPV': 'public1'}]
Bertrand SIMONNET811bcde2014-11-20 15:21:25 -0800123 result = prebuilt.GenerateUploadDict(self._base_local_path,
124 self._gs_bucket_path, pkgs)
125 expected = {self._local_path: self._gs_bucket_path + '/public1.tbz2', }
126 self.assertEqual(result, expected)
127
128 def testGenerateUploadDictWithDebug(self):
129 self.PatchObject(prebuilt.os.path, 'exists', return_true=True)
130 pkgs = [{'CPV': 'public1', 'DEBUG_SYMBOLS': 'yes'}]
131 result = prebuilt.GenerateUploadDict(self._base_local_path,
132 self._gs_bucket_path, pkgs)
133 expected = {self._local_path: self._gs_bucket_path + '/public1.tbz2',
134 self._local_path.replace('.tbz2', '.debug.tbz2'):
135 self._gs_bucket_path + '/public1.debug.tbz2'}
David James8c846492011-01-25 17:07:29 -0800136 self.assertEqual(result, expected)
137
David James8c846492011-01-25 17:07:29 -0800138 def testDeterminePrebuiltConfHost(self):
139 """Test that the host prebuilt path comes back properly."""
140 expected_path = os.path.join(prebuilt._PREBUILT_MAKE_CONF['amd64'])
141 self.assertEqual(prebuilt.DeterminePrebuiltConfFile('fake_path', 'amd64'),
142 expected_path)
143
David James8c846492011-01-25 17:07:29 -0800144
David James2c7ccb42012-11-04 15:34:28 -0800145class TestPkgIndex(cros_test_lib.TestCase):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500146 """Helper for tests that update the Packages index file."""
David James2c7ccb42012-11-04 15:34:28 -0800147
148 def setUp(self):
149 self.db = {}
150 self.pkgindex = SimplePackageIndex()
151 self.empty = SimplePackageIndex(packages=False)
152
153 def assertURIs(self, uris):
154 """Verify that the duplicate DB has the specified URLs."""
155 expected = [v.uri for _, v in sorted(self.db.items())]
156 self.assertEqual(expected, uris)
157
158
159class TestPackagesFileFiltering(TestPkgIndex):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500160 """Tests for Packages filtering behavior."""
David James8c846492011-01-25 17:07:29 -0800161
162 def testFilterPkgIndex(self):
David James2c7ccb42012-11-04 15:34:28 -0800163 """Test filtering out of private packages."""
164 self.pkgindex.RemoveFilteredPackages(lambda pkg: pkg in PRIVATE_PACKAGES)
165 self.assertEqual(self.pkgindex.packages, PUBLIC_PACKAGES)
166 self.assertEqual(self.pkgindex.modified, True)
David James8c846492011-01-25 17:07:29 -0800167
168
David James2c7ccb42012-11-04 15:34:28 -0800169class TestPopulateDuplicateDB(TestPkgIndex):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500170 """Tests for the _PopulateDuplicateDB function."""
David James8c846492011-01-25 17:07:29 -0800171
172 def testEmptyIndex(self):
David James2c7ccb42012-11-04 15:34:28 -0800173 """Test population of the duplicate DB with an empty index."""
174 self.empty._PopulateDuplicateDB(self.db, 0)
175 self.assertEqual(self.db, {})
David James8c846492011-01-25 17:07:29 -0800176
177 def testNormalIndex(self):
David James2c7ccb42012-11-04 15:34:28 -0800178 """Test population of the duplicate DB with a full index."""
179 self.pkgindex._PopulateDuplicateDB(self.db, 0)
180 self.assertURIs(['gs://example/gtk+/public1.tbz2',
181 'gs://example/gtk+/foo.tgz',
182 'gs://example/private.tbz2'])
David James8c846492011-01-25 17:07:29 -0800183
184 def testMissingSHA1(self):
David James2c7ccb42012-11-04 15:34:28 -0800185 """Test population of the duplicate DB with a missing SHA1."""
186 del self.pkgindex.packages[0]['SHA1']
187 self.pkgindex._PopulateDuplicateDB(self.db, 0)
188 self.assertURIs(['gs://example/gtk+/foo.tgz',
189 'gs://example/private.tbz2'])
David James8c846492011-01-25 17:07:29 -0800190
191 def testFailedPopulate(self):
David James2c7ccb42012-11-04 15:34:28 -0800192 """Test failure conditions for the populate method."""
193 headerless = SimplePackageIndex(header=False)
194 self.assertRaises(KeyError, headerless._PopulateDuplicateDB, self.db, 0)
195 del self.pkgindex.packages[0]['CPV']
196 self.assertRaises(KeyError, self.pkgindex._PopulateDuplicateDB, self.db, 0)
David James8c846492011-01-25 17:07:29 -0800197
198
Mike Frysinger68182472014-11-05 22:38:39 -0500199class TestResolveDuplicateUploads(cros_test_lib.MockTestCase, TestPkgIndex):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500200 """Tests for the ResolveDuplicateUploads function."""
David James8c846492011-01-25 17:07:29 -0800201
David James615e5b52011-06-03 11:10:15 -0700202 def setUp(self):
Mike Frysinger68182472014-11-05 22:38:39 -0500203 self.PatchObject(binpkg.time, 'time', return_value=binpkg.TWO_WEEKS)
David James2c7ccb42012-11-04 15:34:28 -0800204 self.db = {}
205 self.dup = SimplePackageIndex()
206 self.expected_pkgindex = SimplePackageIndex()
207
208 def assertNoDuplicates(self, candidates):
209 """Verify no duplicates are found with the specified candidates."""
210 uploads = self.pkgindex.ResolveDuplicateUploads(candidates)
211 self.assertEqual(uploads, self.pkgindex.packages)
212 self.assertEqual(len(self.pkgindex.packages),
213 len(self.expected_pkgindex.packages))
214 for pkg1, pkg2 in zip(self.pkgindex.packages,
215 self.expected_pkgindex.packages):
216 self.assertNotEqual(pkg1['MTIME'], pkg2['MTIME'])
217 del pkg1['MTIME']
218 del pkg2['MTIME']
219 self.assertEqual(self.pkgindex.modified, False)
220 self.assertEqual(self.pkgindex.packages, self.expected_pkgindex.packages)
221
222 def assertAllDuplicates(self, candidates):
223 """Verify every package is a duplicate in the specified list."""
224 for pkg in self.expected_pkgindex.packages:
225 pkg.setdefault('PATH', pkg['CPV'] + '.tbz2')
226 self.pkgindex.ResolveDuplicateUploads(candidates)
227 self.assertEqual(self.pkgindex.packages, self.expected_pkgindex.packages)
David James615e5b52011-06-03 11:10:15 -0700228
David James8c846492011-01-25 17:07:29 -0800229 def testEmptyList(self):
David James2c7ccb42012-11-04 15:34:28 -0800230 """If no candidates are supplied, no duplicates should be found."""
231 self.assertNoDuplicates([])
David James8c846492011-01-25 17:07:29 -0800232
233 def testEmptyIndex(self):
David James2c7ccb42012-11-04 15:34:28 -0800234 """If no packages are supplied, no duplicates should be found."""
235 self.assertNoDuplicates([self.empty])
David James8c846492011-01-25 17:07:29 -0800236
David James2c7ccb42012-11-04 15:34:28 -0800237 def testDifferentURI(self):
238 """If the URI differs, no duplicates should be found."""
239 self.dup.header['URI'] = 'gs://example2'
240 self.assertNoDuplicates([self.dup])
241
242 def testUpdateModificationTime(self):
243 """When duplicates are found, we should use the latest mtime."""
244 for pkg in self.expected_pkgindex.packages:
245 pkg['MTIME'] = '10'
246 for pkg in self.dup.packages:
247 pkg['MTIME'] = '4'
248 self.assertAllDuplicates([self.expected_pkgindex, self.dup])
249
250 def testCanonicalUrl(self):
251 """If the URL is in a different format, we should still find duplicates."""
252 self.dup.header['URI'] = gs.PUBLIC_BASE_HTTPS_URL + 'example'
253 self.assertAllDuplicates([self.dup])
David James8c846492011-01-25 17:07:29 -0800254
255 def testMissingSHA1(self):
David James2c7ccb42012-11-04 15:34:28 -0800256 """We should not find duplicates if there is no SHA1."""
257 del self.pkgindex.packages[0]['SHA1']
258 del self.expected_pkgindex.packages[0]['SHA1']
259 for pkg in self.expected_pkgindex.packages[1:]:
David James8c846492011-01-25 17:07:29 -0800260 pkg.setdefault('PATH', pkg['CPV'] + '.tbz2')
David James2c7ccb42012-11-04 15:34:28 -0800261 self.pkgindex.ResolveDuplicateUploads([self.dup])
262 self.assertNotEqual(self.pkgindex.packages[0]['MTIME'],
263 self.expected_pkgindex.packages[0]['MTIME'])
264 del self.pkgindex.packages[0]['MTIME']
265 del self.expected_pkgindex.packages[0]['MTIME']
266 self.assertEqual(self.pkgindex.packages, self.expected_pkgindex.packages)
David James8c846492011-01-25 17:07:29 -0800267
Bertrand SIMONNET811bcde2014-11-20 15:21:25 -0800268 def testSymbolsAvailable(self):
269 """If symbols are available remotely, re-use them and set DEBUG_SYMBOLS."""
270 self.dup.packages[0]['DEBUG_SYMBOLS'] = 'yes'
271
272 uploads = self.pkgindex.ResolveDuplicateUploads([self.dup])
273 self.assertEqual(uploads, [])
274 self.assertEqual(self.pkgindex.packages[0].get('DEBUG_SYMBOLS'), 'yes')
275
276 def testSymbolsAvailableLocallyOnly(self):
277 """If the symbols are only available locally, reupload them."""
278 self.pkgindex.packages[0]['DEBUG_SYMBOLS'] = 'yes'
279
280 uploads = self.pkgindex.ResolveDuplicateUploads([self.dup])
281 self.assertEqual(uploads, [self.pkgindex.packages[0]])
282
David James8c846492011-01-25 17:07:29 -0800283
Mike Frysinger68182472014-11-05 22:38:39 -0500284class TestWritePackageIndex(cros_test_lib.MockTestCase, TestPkgIndex):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500285 """Tests for the WriteToNamedTemporaryFile function."""
David James8c846492011-01-25 17:07:29 -0800286
287 def testSimple(self):
David James2c7ccb42012-11-04 15:34:28 -0800288 """Test simple call of WriteToNamedTemporaryFile()"""
Mike Frysinger68182472014-11-05 22:38:39 -0500289 self.PatchObject(self.pkgindex, 'Write')
David James2c7ccb42012-11-04 15:34:28 -0800290 f = self.pkgindex.WriteToNamedTemporaryFile()
David James8c846492011-01-25 17:07:29 -0800291 self.assertEqual(f.read(), '')
292
293
Mike Frysinger36870f92015-04-12 02:59:55 -0400294class TestUploadPrebuilt(cros_test_lib.MockTempDirTestCase):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500295 """Tests for the _UploadPrebuilt function."""
David James05bcb2b2011-02-09 09:25:47 -0800296
297 def setUp(self):
298 class MockTemporaryFile(object):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500299 """Mock out the temporary file logic."""
David James05bcb2b2011-02-09 09:25:47 -0800300 def __init__(self, name):
301 self.name = name
David James05bcb2b2011-02-09 09:25:47 -0800302 self.pkgindex = SimplePackageIndex()
Mike Frysinger36870f92015-04-12 02:59:55 -0400303 self.PatchObject(binpkg, 'GrabLocalPackageIndex',
304 return_value=self.pkgindex)
305 self.PatchObject(self.pkgindex, 'ResolveDuplicateUploads',
306 return_value=PRIVATE_PACKAGES)
307 self.PatchObject(self.pkgindex, 'WriteToNamedTemporaryFile',
308 return_value=MockTemporaryFile('fake'))
309 self.remote_up_mock = self.PatchObject(prebuilt, 'RemoteUpload')
310 self.gs_up_mock = self.PatchObject(prebuilt, '_GsUpload')
David James05bcb2b2011-02-09 09:25:47 -0800311
David James05bcb2b2011-02-09 09:25:47 -0800312 def testSuccessfulGsUpload(self):
Alex Deymo541ea6f2014-12-23 01:18:14 -0800313 uploads = {
314 os.path.join(self.tempdir, 'private.tbz2'): 'gs://foo/private.tbz2'}
Alex Deymo541ea6f2014-12-23 01:18:14 -0800315 packages = list(PRIVATE_PACKAGES)
316 packages.append({'CPV': 'dev-only-extras'})
Mike Frysinger36870f92015-04-12 02:59:55 -0400317 osutils.Touch(os.path.join(self.tempdir, 'dev-only-extras.tbz2'))
318 self.PatchObject(prebuilt, 'GenerateUploadDict',
319 return_value=uploads)
David James05bcb2b2011-02-09 09:25:47 -0800320 uploads = uploads.copy()
321 uploads['fake'] = 'gs://foo/suffix/Packages'
David Jamesfd0b0852011-02-23 11:15:36 -0800322 acl = 'public-read'
David Jamesc0f158a2011-02-22 16:07:29 -0800323 uri = self.pkgindex.header['URI']
David James8ece7ee2011-06-29 16:02:30 -0700324 uploader = prebuilt.PrebuiltUploader('gs://foo', acl, uri, [], '/', [],
Mike Frysinger8092a632014-05-24 13:25:46 -0400325 False, 'foo', False, 'x86-foo', [], '')
Alex Deymo541ea6f2014-12-23 01:18:14 -0800326 uploader._UploadPrebuilt(self.tempdir, 'suffix')
Mike Frysinger36870f92015-04-12 02:59:55 -0400327 self.remote_up_mock.assert_called_once_with(mock.ANY, acl, uploads)
328 self.assertTrue(self.gs_up_mock.called)
David James05bcb2b2011-02-09 09:25:47 -0800329
David James05bcb2b2011-02-09 09:25:47 -0800330
Mike Frysinger36870f92015-04-12 02:59:55 -0400331class TestSyncPrebuilts(cros_test_lib.MockTestCase):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500332 """Tests for the SyncHostPrebuilts function."""
David James05bcb2b2011-02-09 09:25:47 -0800333
334 def setUp(self):
Mike Frysinger36870f92015-04-12 02:59:55 -0400335 self.rev_mock = self.PatchObject(prebuilt, 'RevGitFile', return_value=None)
336 self.update_binhost_mock = self.PatchObject(
337 prebuilt, 'UpdateBinhostConfFile', return_value=None)
David James05bcb2b2011-02-09 09:25:47 -0800338 self.build_path = '/trunk'
339 self.upload_location = 'gs://upload/'
340 self.version = '1'
341 self.binhost = 'http://prebuilt/'
342 self.key = 'PORTAGE_BINHOST'
Mike Frysinger36870f92015-04-12 02:59:55 -0400343 self.upload_mock = self.PatchObject(prebuilt.PrebuiltUploader,
344 '_UploadPrebuilt', return_value=True)
David James05bcb2b2011-02-09 09:25:47 -0800345
David James05bcb2b2011-02-09 09:25:47 -0800346 def testSyncHostPrebuilts(self):
David Jamese2488642011-11-14 16:15:20 -0800347 board = 'x86-foo'
David James4058b0d2011-12-08 21:24:50 -0800348 target = prebuilt.BuildTarget(board, 'aura')
349 slave_targets = [prebuilt.BuildTarget('x86-bar', 'aura')]
David James05bcb2b2011-02-09 09:25:47 -0800350 package_path = os.path.join(self.build_path,
351 prebuilt._HOST_PACKAGES_PATH)
Mike Frysingerd6e2df02014-11-26 02:55:04 -0500352 url_suffix = prebuilt._REL_HOST_PATH % {
353 'version': self.version,
354 'host_arch': prebuilt._HOST_ARCH,
355 'target': target,
356 }
David James8fa34ea2011-04-15 13:00:20 -0700357 packages_url_suffix = '%s/packages' % url_suffix.rstrip('/')
David Jamesf0e6fd72011-04-15 15:58:07 -0700358 url_value = '%s/%s/' % (self.binhost.rstrip('/'),
359 packages_url_suffix.rstrip('/'))
David Jamese2488642011-11-14 16:15:20 -0800360 urls = [url_value.replace('foo', 'bar'), url_value]
David James20b2b6f2011-11-18 15:11:58 -0800361 binhost = ' '.join(urls)
David James615e5b52011-06-03 11:10:15 -0700362 uploader = prebuilt.PrebuiltUploader(
363 self.upload_location, 'public-read', self.binhost, [],
Mike Frysinger8092a632014-05-24 13:25:46 -0400364 self.build_path, [], False, 'foo', False, target, slave_targets,
365 self.version)
366 uploader.SyncHostPrebuilts(self.key, True, True)
Mike Frysinger36870f92015-04-12 02:59:55 -0400367 self.upload_mock.assert_called_once_with(package_path, packages_url_suffix)
368 self.rev_mock.assert_called_once_with(
369 mock.ANY, {self.key: binhost}, dryrun=False)
370 self.update_binhost_mock.assert_called_once_with(
371 mock.ANY, self.key, binhost)
David James05bcb2b2011-02-09 09:25:47 -0800372
373 def testSyncBoardPrebuilts(self):
David Jamese2488642011-11-14 16:15:20 -0800374 board = 'x86-foo'
David James4058b0d2011-12-08 21:24:50 -0800375 target = prebuilt.BuildTarget(board, 'aura')
376 slave_targets = [prebuilt.BuildTarget('x86-bar', 'aura')]
Mike Frysinger8092a632014-05-24 13:25:46 -0400377 board_path = os.path.join(
378 self.build_path, prebuilt._BOARD_PATH % {'board': board})
David James05bcb2b2011-02-09 09:25:47 -0800379 package_path = os.path.join(board_path, 'packages')
Mike Frysingerd6e2df02014-11-26 02:55:04 -0500380 url_suffix = prebuilt._REL_BOARD_PATH % {
381 'version': self.version,
382 'target': target,
383 }
David James8fa34ea2011-04-15 13:00:20 -0700384 packages_url_suffix = '%s/packages' % url_suffix.rstrip('/')
David Jamesf0e6fd72011-04-15 15:58:07 -0700385 url_value = '%s/%s/' % (self.binhost.rstrip('/'),
386 packages_url_suffix.rstrip('/'))
David Jamese2488642011-11-14 16:15:20 -0800387 bar_binhost = url_value.replace('foo', 'bar')
Mike Frysinger36870f92015-04-12 02:59:55 -0400388 determine_mock = self.PatchObject(prebuilt, 'DeterminePrebuiltConfFile',
389 side_effect=('bar', 'foo'))
390 self.PatchObject(prebuilt.PrebuiltUploader, '_UploadSdkTarball')
391 with parallel_unittest.ParallelMock():
392 multiprocessing.Process.exitcode = 0
393 uploader = prebuilt.PrebuiltUploader(
394 self.upload_location, 'public-read', self.binhost, [],
395 self.build_path, [], False, 'foo', False, target, slave_targets,
396 self.version)
Gilad Arnoldad333182015-05-27 15:50:41 -0700397 uploader.SyncBoardPrebuilts(self.key, True, True, True, None, None, None,
398 None, None)
Mike Frysinger36870f92015-04-12 02:59:55 -0400399 determine_mock.assert_has_calls([
400 mock.call(self.build_path, slave_targets[0]),
401 mock.call(self.build_path, target),
402 ])
403 self.upload_mock.assert_called_once_with(package_path, packages_url_suffix)
404 self.rev_mock.assert_has_calls([
405 mock.call('bar', {self.key: bar_binhost}, dryrun=False),
406 mock.call('foo', {self.key: url_value}, dryrun=False),
407 ])
408 self.update_binhost_mock.assert_has_calls([
409 mock.call(mock.ANY, self.key, bar_binhost),
410 mock.call(mock.ANY, self.key, url_value),
411 ])
David James05bcb2b2011-02-09 09:25:47 -0800412
413
Mike Frysinger36870f92015-04-12 02:59:55 -0400414class TestMain(cros_test_lib.MockTestCase):
Mike Frysingerc6824f62014-02-03 11:09:44 -0500415 """Tests for the main() function."""
David Jamesc0f158a2011-02-22 16:07:29 -0800416
417 def testMain(self):
418 """Test that the main function works."""
Mike Frysinger36870f92015-04-12 02:59:55 -0400419 options = mock.MagicMock()
David Jamesc0f158a2011-02-22 16:07:29 -0800420 old_binhost = 'http://prebuilt/1'
421 options.previous_binhost_url = [old_binhost]
David Jamese2488642011-11-14 16:15:20 -0800422 options.board = 'x86-foo'
David James4058b0d2011-12-08 21:24:50 -0800423 options.profile = None
Chris Sosa6a5dceb2012-05-14 13:48:56 -0700424 target = prebuilt.BuildTarget(options.board, options.profile)
David Jamesc0f158a2011-02-22 16:07:29 -0800425 options.build_path = '/trunk'
Mike Frysinger86509232014-05-24 13:18:37 -0400426 options.dryrun = False
David Jamesfd0b0852011-02-23 11:15:36 -0800427 options.private = True
David James615e5b52011-06-03 11:10:15 -0700428 options.packages = []
David Jamesc0f158a2011-02-22 16:07:29 -0800429 options.sync_host = True
430 options.git_sync = True
David James8fa34ea2011-04-15 13:00:20 -0700431 options.upload_board_tarball = True
Zdenek Behan62a57792012-08-31 15:09:08 +0200432 options.prepackaged_tarball = None
Gilad Arnold2b79e2d2015-06-02 11:26:07 -0700433 options.toolchains_overlay_tarballs = []
434 options.toolchains_overlay_upload_path = ''
Mike Frysinger9e979b92012-11-29 02:55:09 -0500435 options.toolchain_tarballs = []
436 options.toolchain_upload_path = ''
David Jamesc0f158a2011-02-22 16:07:29 -0800437 options.upload = 'gs://upload/'
David Jamesfd0b0852011-02-23 11:15:36 -0800438 options.binhost_base_url = options.upload
David Jamesc0f158a2011-02-22 16:07:29 -0800439 options.prepend_version = True
David James8ece7ee2011-06-29 16:02:30 -0700440 options.set_version = None
441 options.skip_upload = False
David Jamesc0f158a2011-02-22 16:07:29 -0800442 options.filters = True
443 options.key = 'PORTAGE_BINHOST'
David Jamesb26b9312014-12-15 11:26:46 -0800444 options.binhost_conf_dir = None
David Jamesc0f158a2011-02-22 16:07:29 -0800445 options.sync_binhost_conf = True
David James4058b0d2011-12-08 21:24:50 -0800446 options.slave_targets = [prebuilt.BuildTarget('x86-bar', 'aura')]
Mike Frysinger36870f92015-04-12 02:59:55 -0400447 self.PatchObject(prebuilt, 'ParseOptions',
448 return_value=tuple([options, target]))
449 self.PatchObject(binpkg, 'GrabRemotePackageIndex', return_value=True)
450 init_mock = self.PatchObject(prebuilt.PrebuiltUploader, '__init__',
451 return_value=None)
452 expected_gs_acl_path = os.path.join('/fake_path',
Prathmesh Prabhu5f14da02014-10-17 15:13:56 -0700453 prebuilt._GOOGLESTORAGE_GSUTIL_FILE)
Mike Frysinger36870f92015-04-12 02:59:55 -0400454 self.PatchObject(portage_util, 'FindOverlayFile',
455 return_value=expected_gs_acl_path)
456 host_mock = self.PatchObject(
457 prebuilt.PrebuiltUploader, 'SyncHostPrebuilts', return_value=None)
458 board_mock = self.PatchObject(
459 prebuilt.PrebuiltUploader, 'SyncBoardPrebuilts', return_value=None)
460
461 prebuilt.main([])
462
463 init_mock.assert_called_once_with(options.upload, expected_gs_acl_path,
464 options.upload, mock.ANY,
465 options.build_path, options.packages,
466 False, None, False,
467 target, options.slave_targets,
468 mock.ANY)
469 board_mock.assert_called_once_with(
Mike Frysinger8092a632014-05-24 13:25:46 -0400470 options.key, options.git_sync,
Gilad Arnoldad333182015-05-27 15:50:41 -0700471 options.sync_binhost_conf, options.upload_board_tarball, None,
472 [], '', [], '')
Mike Frysinger36870f92015-04-12 02:59:55 -0400473 host_mock.assert_called_once_with(
474 options.key, options.git_sync, options.sync_binhost_conf)
David Jamesc0f158a2011-02-22 16:07:29 -0800475
Mike Frysinger9e979b92012-11-29 02:55:09 -0500476
Mike Frysinger68182472014-11-05 22:38:39 -0500477class TestSdk(cros_test_lib.MockTestCase):
Mike Frysinger9e979b92012-11-29 02:55:09 -0500478 """Test logic related to uploading SDK binaries"""
479
480 def setUp(self):
Mike Frysinger68182472014-11-05 22:38:39 -0500481 self.PatchObject(prebuilt, '_GsUpload',
482 side_effect=Exception('should not get called'))
483 self.PatchObject(prebuilt, 'UpdateBinhostConfFile',
484 side_effect=Exception('should not get called'))
485 self.upload_mock = self.PatchObject(prebuilt.PrebuiltUploader, '_Upload')
Mike Frysinger9e979b92012-11-29 02:55:09 -0500486
487 self.acl = 'magic-acl'
488
489 # All these args pretty much get ignored. Whee.
490 self.uploader = prebuilt.PrebuiltUploader(
491 'gs://foo', self.acl, 'prebuilt', [], '/', [],
Mike Frysinger8092a632014-05-24 13:25:46 -0400492 False, 'foo', False, 'x86-foo', [], 'chroot-1234')
Mike Frysinger9e979b92012-11-29 02:55:09 -0500493
Gilad Arnold2b79e2d2015-06-02 11:26:07 -0700494 def testSdkUpload(self, to_tarballs=(), to_upload_path=None,
Gilad Arnoldad333182015-05-27 15:50:41 -0700495 tc_tarballs=(), tc_upload_path=None):
Mike Frysinger9e979b92012-11-29 02:55:09 -0500496 """Make sure we can upload just an SDK tarball"""
497 tar = 'sdk.tar.xz'
498 ver = '1234'
499 vtar = 'cros-sdk-%s.tar.xz' % ver
500
Mike Frysinger68182472014-11-05 22:38:39 -0500501 calls = [
502 mock.call('%s.Manifest' % tar,
503 'gs://chromiumos-sdk/%s.Manifest' % vtar),
504 mock.call(tar, 'gs://chromiumos-sdk/%s' % vtar),
505 ]
Gilad Arnold2b79e2d2015-06-02 11:26:07 -0700506 for to in to_tarballs:
507 to = to.split(':')
Gilad Arnoldad333182015-05-27 15:50:41 -0700508 calls.append(mock.call(
Gilad Arnold2b79e2d2015-06-02 11:26:07 -0700509 to[1],
510 ('gs://chromiumos-sdk/' + to_upload_path) % {'toolchains': to[0]}))
Mike Frysinger68182472014-11-05 22:38:39 -0500511 for tc in tc_tarballs:
512 tc = tc.split(':')
513 calls.append(mock.call(
514 tc[1], ('gs://chromiumos-sdk/' + tc_upload_path) % {'target': tc[0]}))
515 calls.append(mock.call(
516 mock.ANY, 'gs://chromiumos-sdk/cros-sdk-latest.conf'))
Mike Frysinger9e979b92012-11-29 02:55:09 -0500517
Mike Frysinger8092a632014-05-24 13:25:46 -0400518 self.uploader._UploadSdkTarball('amd64-host', '',
Gilad Arnold2b79e2d2015-06-02 11:26:07 -0700519 tar, to_tarballs, to_upload_path,
Gilad Arnoldad333182015-05-27 15:50:41 -0700520 tc_tarballs, tc_upload_path)
Mike Frysinger68182472014-11-05 22:38:39 -0500521 self.upload_mock.assert_has_calls(calls)
Mike Frysinger9e979b92012-11-29 02:55:09 -0500522
Gilad Arnoldad333182015-05-27 15:50:41 -0700523 def testBoardOverlayTarballUpload(self):
524 """Make sure processing of board-specific overlay tarballs works."""
Gilad Arnold2b79e2d2015-06-02 11:26:07 -0700525 to_tarballs = (
526 ('i686-pc-linux-gnu:'
527 '/some/path/built-sdk-overlay-toolchains-i686-pc-linux-gnu.tar.xz'),
528 ('armv7a-cros-linux-gnueabi-arm-none-eabi:'
529 '/some/path/built-sdk-overlay-toolchains-armv7a-cros-linux-gnueabi-'
530 'arm-none-eabi'),
Gilad Arnoldad333182015-05-27 15:50:41 -0700531 )
Gilad Arnold2b79e2d2015-06-02 11:26:07 -0700532 to_upload_path = (
533 '1994/04/cros-sdk-overlay-toolchains-%(toolchains)s-1994.04.02.tar.xz')
534 self.testSdkUpload(to_tarballs=to_tarballs, to_upload_path=to_upload_path)
Gilad Arnoldad333182015-05-27 15:50:41 -0700535
536 def testToolchainTarballUpload(self):
537 """Make sure processing of toolchain tarballs works."""
Mike Frysinger9e979b92012-11-29 02:55:09 -0500538 tc_tarballs = (
539 'i686:/some/i686.tar.xz',
540 'arm-none:/some/arm.tar.xz',
541 )
542 tc_upload_path = '1994/04/%(target)s-1994.04.02.tar.xz'
Gilad Arnoldad333182015-05-27 15:50:41 -0700543 self.testSdkUpload(tc_tarballs=tc_tarballs, tc_upload_path=tc_upload_path)