blob: 26af2ab1ae3082d24e5d2bb08d18f64a078aaa64 [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -04002# Copyright (c) 2013 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.
5
Mike Frysingereb753bf2013-11-22 16:05:35 -05006"""Unittests for upload_symbols.py"""
7
Mike Frysinger7f9be142014-01-15 02:16:42 -05008from __future__ import print_function
9
Mike Frysinger06da5202014-09-26 17:30:33 -050010import errno
Don Garretta28be6d2016-06-16 18:09:35 -070011import itertools
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -040012import os
Mike Frysinger0a2fd922014-09-12 20:23:42 -070013import signal
Mike Frysinger06da5202014-09-26 17:30:33 -050014import socket
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -040015import sys
Aviv Keshetd1f04632014-05-09 11:33:46 -070016import time
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -040017
Mike Frysinger6db648e2018-07-24 19:57:58 -040018import mock
Mike Frysinger3d465162019-08-28 00:40:23 -040019from six.moves import BaseHTTPServer
20from six.moves import socketserver
Mike Frysinger3dcacee2019-08-23 17:09:11 -040021from six.moves import urllib
Mike Frysinger6db648e2018-07-24 19:57:58 -040022
Mike Frysinger079863c2014-10-09 23:16:46 -040023# We specifically set up a local server to connect to, so make sure we
24# delete any proxy settings that might screw that up. We also need to
25# do it here because modules that are imported below will implicitly
26# initialize with this proxy setting rather than dynamically pull it
27# on the fly :(.
Mike Frysinger92bdef52019-08-21 21:05:13 -040028# pylint: disable=wrong-import-position
Mike Frysinger079863c2014-10-09 23:16:46 -040029os.environ.pop('http_proxy', None)
30
Aviv Keshetb7519e12016-10-04 00:50:00 -070031from chromite.lib import constants
Mike Frysingerbbd1f112016-09-08 18:25:11 -040032
33# The isolateserver includes a bunch of third_party python packages that clash
34# with chromite's bundled third_party python packages (like oauth2client).
35# Since upload_symbols is not imported in to other parts of chromite, and there
36# are no deps in third_party we care about, purge the chromite copy. This way
37# we can use isolateserver for deduping.
38# TODO: If we ever sort out third_party/ handling and make it per-script opt-in,
39# we can purge this logic.
40third_party = os.path.join(constants.CHROMITE_DIR, 'third_party')
41while True:
42 try:
43 sys.path.remove(third_party)
44 except ValueError:
45 break
Mike Frysingerbbd1f112016-09-08 18:25:11 -040046del third_party
47
Ralph Nathan446aee92015-03-23 14:44:56 -070048from chromite.lib import cros_logging as logging
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -040049from chromite.lib import cros_test_lib
50from chromite.lib import osutils
51from chromite.lib import parallel
Mike Frysinger0a2fd922014-09-12 20:23:42 -070052from chromite.lib import remote_access
Mike Frysinger5e30a4b2014-02-12 20:23:04 -050053from chromite.scripts import cros_generate_breakpad_symbols
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -040054from chromite.scripts import upload_symbols
55
Mike Frysinger0c0efa22014-02-09 23:32:23 -050056
Don Garretta28be6d2016-06-16 18:09:35 -070057class SymbolsTestBase(cros_test_lib.MockTempDirTestCase):
58 """Base class for most symbols tests."""
59
60 SLIM_CONTENT = """
61some junk
62"""
63
64 FAT_CONTENT = """
65STACK CFI 1234
66some junk
67STACK CFI 1234
68"""
69
70 def setUp(self):
71 # Make certain we don't use the network.
Mike Frysinger3dcacee2019-08-23 17:09:11 -040072 self.urlopen_mock = self.PatchObject(urllib.request, 'urlopen')
Mike Nichols90f7c152019-04-09 15:14:08 -060073 self.request_mock = self.PatchObject(upload_symbols, 'ExecRequest',
74 return_value={'uploadUrl':
75 'testurl',
76 'uploadKey':
77 'asdgasgas'})
Don Garretta28be6d2016-06-16 18:09:35 -070078
79 # Make 'uploads' go fast.
80 self.PatchObject(upload_symbols, 'SLEEP_DELAY', 0)
81 self.PatchObject(upload_symbols, 'INITIAL_RETRY_DELAY', 0)
82
83 # So our symbol file content doesn't have to be real.
84 self.PatchObject(cros_generate_breakpad_symbols, 'ReadSymsHeader',
85 return_value=cros_generate_breakpad_symbols.SymbolHeader(
86 os='os', cpu='cpu', id='id', name='name'))
87
88 self.working = os.path.join(self.tempdir, 'expand')
89 osutils.SafeMakedirs(self.working)
90
91 self.data = os.path.join(self.tempdir, 'data')
92 osutils.SafeMakedirs(self.data)
93
94 def createSymbolFile(self, filename, content=FAT_CONTENT, size=0,
95 status=None, dedupe=False):
96 fullname = os.path.join(self.data, filename)
97 osutils.SafeMakedirs(os.path.dirname(fullname))
98
99 # If a file size is given, force that to be the minimum file size. Create
100 # a sparse file so large files are practical.
101 with open(fullname, 'w+b') as f:
102 f.truncate(size)
103 f.seek(0)
Mike Frysingerac75f602019-11-14 23:18:51 -0500104 f.write(content.encode('utf-8'))
Don Garretta28be6d2016-06-16 18:09:35 -0700105
106 result = upload_symbols.SymbolFile(display_path=filename,
107 file_name=fullname)
108
109 if status:
110 result.status = status
111
112 if dedupe:
113 result.dedupe_item = upload_symbols.DedupeItem(result)
114 result.dedupe_push_state = 'push_state'
115
116 return result
117
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400118
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700119class SymbolServerRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
120 """HTTP handler for symbol POSTs"""
121
122 RESP_CODE = None
123 RESP_MSG = None
124
125 def do_POST(self):
126 """Handle a POST request"""
127 # Drain the data from the client. If we don't, we might write the response
128 # and close the socket before the client finishes, so they die with EPIPE.
129 clen = int(self.headers.get('Content-Length', '0'))
130 self.rfile.read(clen)
131
132 self.send_response(self.RESP_CODE, self.RESP_MSG)
133 self.end_headers()
134
Mike Frysingera4418152019-09-22 04:00:24 -0400135 # pylint: disable=arguments-differ
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700136 def log_message(self, *args, **kwargs):
137 """Stub the logger as it writes to stderr"""
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700138
139
Mike Frysinger3d465162019-08-28 00:40:23 -0400140class SymbolServer(socketserver.ThreadingTCPServer, BaseHTTPServer.HTTPServer):
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700141 """Simple HTTP server that forks each request"""
142
143
144class UploadSymbolsServerTest(cros_test_lib.MockTempDirTestCase):
145 """Tests for UploadSymbols() and a local HTTP server"""
146
147 SYM_CONTENTS = """MODULE Linux arm 123-456 blkid
148PUBLIC 1471 0 main"""
149
150 def SpawnServer(self, RequestHandler):
151 """Spawn a new http server"""
Mike Frysinger06da5202014-09-26 17:30:33 -0500152 while True:
153 try:
154 port = remote_access.GetUnusedPort()
155 address = ('', port)
156 self.httpd = SymbolServer(address, RequestHandler)
157 break
158 except socket.error as e:
159 if e.errno == errno.EADDRINUSE:
160 continue
161 raise
Don Garretta28be6d2016-06-16 18:09:35 -0700162 self.server_url = 'http://localhost:%i/post/path' % port
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700163 self.httpd_pid = os.fork()
164 if self.httpd_pid == 0:
165 self.httpd.serve_forever(poll_interval=0.1)
166 sys.exit(0)
Mike Frysingerac75f602019-11-14 23:18:51 -0500167 # The child runs the server, so close the socket in the parent.
168 self.httpd.server_close()
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700169
170 def setUp(self):
171 self.httpd_pid = None
172 self.httpd = None
Don Garretta28be6d2016-06-16 18:09:35 -0700173 self.server_url = None
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700174 self.sym_file = os.path.join(self.tempdir, 'test.sym')
175 osutils.WriteFile(self.sym_file, self.SYM_CONTENTS)
176
Don Garretta28be6d2016-06-16 18:09:35 -0700177 # Stop sleeps and retries for these tests.
178 self.PatchObject(upload_symbols, 'SLEEP_DELAY', 0)
179 self.PatchObject(upload_symbols, 'INITIAL_RETRY_DELAY', 0)
180 self.PatchObject(upload_symbols, 'MAX_RETRIES', 0)
181
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700182 def tearDown(self):
183 # Only kill the server if we forked one.
184 if self.httpd_pid:
185 os.kill(self.httpd_pid, signal.SIGUSR1)
186
187 def testSuccess(self):
188 """The server returns success for all uploads"""
189 class Handler(SymbolServerRequestHandler):
190 """Always return 200"""
191 RESP_CODE = 200
Mike Nichols90f7c152019-04-09 15:14:08 -0600192 self.PatchObject(upload_symbols, 'ExecRequest',
193 return_value={'uploadUrl': 'testurl',
194 'uploadKey': 'testSuccess'})
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700195 self.SpawnServer(Handler)
Don Garretta28be6d2016-06-16 18:09:35 -0700196 ret = upload_symbols.UploadSymbols(
197 sym_paths=[self.sym_file] * 10,
198 upload_url=self.server_url,
Mike Nichols90f7c152019-04-09 15:14:08 -0600199 api_key='testSuccess')
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700200 self.assertEqual(ret, 0)
201
202 def testError(self):
203 """The server returns errors for all uploads"""
204 class Handler(SymbolServerRequestHandler):
Mike Nichols90f7c152019-04-09 15:14:08 -0600205 """All connections error"""
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700206 RESP_CODE = 500
207 RESP_MSG = 'Internal Server Error'
208
209 self.SpawnServer(Handler)
Don Garretta28be6d2016-06-16 18:09:35 -0700210 ret = upload_symbols.UploadSymbols(
211 sym_paths=[self.sym_file] * 10,
212 upload_url=self.server_url,
Mike Nichols90f7c152019-04-09 15:14:08 -0600213 api_key='testkey')
Don Garretta28be6d2016-06-16 18:09:35 -0700214 self.assertEqual(ret, 10)
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700215
216 def testHungServer(self):
217 """The server chokes, but we recover"""
218 class Handler(SymbolServerRequestHandler):
219 """All connections choke forever"""
Mike Nichols137e82d2019-05-15 18:40:34 -0600220 self.PatchObject(upload_symbols, 'ExecRequest',
221 return_value={'pairs': []})
222
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700223 def do_POST(self):
224 while True:
225 time.sleep(1000)
226
227 self.SpawnServer(Handler)
228 with mock.patch.object(upload_symbols, 'GetUploadTimeout') as m:
Don Garretta28be6d2016-06-16 18:09:35 -0700229 m.return_value = 0.01
Mike Frysinger58312e92014-03-18 04:18:36 -0400230 ret = upload_symbols.UploadSymbols(
Don Garretta28be6d2016-06-16 18:09:35 -0700231 sym_paths=[self.sym_file] * 10,
232 upload_url=self.server_url,
Mike Nichols137e82d2019-05-15 18:40:34 -0600233 timeout=m.return_value,
Mike Nichols90f7c152019-04-09 15:14:08 -0600234 api_key='testkey')
Don Garretta28be6d2016-06-16 18:09:35 -0700235 self.assertEqual(ret, 10)
Aviv Keshetd1f04632014-05-09 11:33:46 -0700236
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400237
Don Garretta28be6d2016-06-16 18:09:35 -0700238class UploadSymbolsHelpersTest(cros_test_lib.TestCase):
239 """Test assorted helper functions and classes."""
Don Garretta28be6d2016-06-16 18:09:35 -0700240 def testIsTarball(self):
241 notTar = [
242 '/foo/bar/test.bin',
243 '/foo/bar/test.tar.bin',
244 '/foo/bar/test.faketar.gz',
245 '/foo/bar/test.nottgz',
Mike Frysinger0c0efa22014-02-09 23:32:23 -0500246 ]
Mike Frysinger0c0efa22014-02-09 23:32:23 -0500247
Don Garretta28be6d2016-06-16 18:09:35 -0700248 isTar = [
249 '/foo/bar/test.tar',
250 '/foo/bar/test.bin.tar',
251 '/foo/bar/test.bin.tar.bz2',
252 '/foo/bar/test.bin.tar.gz',
253 '/foo/bar/test.bin.tar.xz',
254 '/foo/bar/test.tbz2',
255 '/foo/bar/test.tbz',
256 '/foo/bar/test.tgz',
257 '/foo/bar/test.txz',
258 ]
Mike Frysinger0c0efa22014-02-09 23:32:23 -0500259
Don Garretta28be6d2016-06-16 18:09:35 -0700260 for p in notTar:
261 self.assertFalse(upload_symbols.IsTarball(p))
262
263 for p in isTar:
264 self.assertTrue(upload_symbols.IsTarball(p))
265
266 def testBatchGenerator(self):
267 result = upload_symbols.BatchGenerator([], 2)
268 self.assertEqual(list(result), [])
269
Mike Frysinger66848312019-07-03 02:12:39 -0400270 # BatchGenerator accepts iterators, so passing it here is safe.
271 # pylint: disable=range-builtin-not-iterating
Mike Frysinger79cca962019-06-13 15:26:53 -0400272 result = upload_symbols.BatchGenerator(range(6), 2)
Don Garretta28be6d2016-06-16 18:09:35 -0700273 self.assertEqual(list(result), [[0, 1], [2, 3], [4, 5]])
274
Mike Frysinger79cca962019-06-13 15:26:53 -0400275 result = upload_symbols.BatchGenerator(range(7), 2)
Don Garretta28be6d2016-06-16 18:09:35 -0700276 self.assertEqual(list(result), [[0, 1], [2, 3], [4, 5], [6]])
277
278 # Prove that we are streaming the results, not generating them all at once.
279 result = upload_symbols.BatchGenerator(itertools.repeat(0), 2)
Mike Frysinger67d90242019-07-03 19:03:58 -0400280 self.assertEqual(next(result), [0, 0])
Don Garretta28be6d2016-06-16 18:09:35 -0700281
282
283class FindSymbolFilesTest(SymbolsTestBase):
284 """Test FindSymbolFiles."""
285 def setUp(self):
286 self.symfile = self.createSymbolFile('root.sym').file_name
287 self.innerfile = self.createSymbolFile(
288 os.path.join('nested', 'inner.sym')).file_name
289
290 # CreateTarball is having issues outside the chroot from open file tests.
291 #
292 # self.tarball = os.path.join(self.tempdir, 'syms.tar.gz')
293 # cros_build_lib.CreateTarball(
294 # 'syms.tar.gz', self.tempdir, inputs=(self.data))
295
296 def testEmpty(self):
297 symbols = list(upload_symbols.FindSymbolFiles(
298 self.working, []))
299 self.assertEqual(symbols, [])
300
301 def testFile(self):
302 symbols = list(upload_symbols.FindSymbolFiles(
303 self.working, [self.symfile]))
304
305 self.assertEqual(len(symbols), 1)
306 sf = symbols[0]
307
308 self.assertEqual(sf.display_name, 'root.sym')
309 self.assertEqual(sf.display_path, self.symfile)
310 self.assertEqual(sf.file_name, self.symfile)
311 self.assertEqual(sf.status, upload_symbols.SymbolFile.INITIAL)
312 self.assertEqual(sf.FileSize(), len(self.FAT_CONTENT))
313
314 def testDir(self):
315 symbols = list(upload_symbols.FindSymbolFiles(
316 self.working, [self.data]))
317
318 self.assertEqual(len(symbols), 2)
319 root = symbols[0]
320 nested = symbols[1]
321
322 self.assertEqual(root.display_name, 'root.sym')
323 self.assertEqual(root.display_path, 'root.sym')
324 self.assertEqual(root.file_name, self.symfile)
325 self.assertEqual(root.status, upload_symbols.SymbolFile.INITIAL)
326 self.assertEqual(root.FileSize(), len(self.FAT_CONTENT))
327
328 self.assertEqual(nested.display_name, 'inner.sym')
329 self.assertEqual(nested.display_path, 'nested/inner.sym')
330 self.assertEqual(nested.file_name, self.innerfile)
331 self.assertEqual(nested.status, upload_symbols.SymbolFile.INITIAL)
332 self.assertEqual(nested.FileSize(), len(self.FAT_CONTENT))
333
334
335class AdjustSymbolFileSizeTest(SymbolsTestBase):
336 """Test AdjustSymbolFileSize."""
337 def setUp(self):
338 self.slim = self.createSymbolFile('slim.sym', self.SLIM_CONTENT)
339 self.fat = self.createSymbolFile('fat.sym', self.FAT_CONTENT)
340
341 self.warn_mock = self.PatchObject(logging, 'PrintBuildbotStepWarnings')
342
343 def _testNotStripped(self, symbol, size=None, content=None):
344 start_file = symbol.file_name
345 after = upload_symbols.AdjustSymbolFileSize(
346 symbol, self.working, size)
347 self.assertIs(after, symbol)
348 self.assertEqual(after.file_name, start_file)
349 if content is not None:
350 self.assertEqual(osutils.ReadFile(after.file_name), content)
351
352 def _testStripped(self, symbol, size=None, content=None):
353 after = upload_symbols.AdjustSymbolFileSize(
354 symbol, self.working, size)
355 self.assertIs(after, symbol)
356 self.assertTrue(after.file_name.startswith(self.working))
357 if content is not None:
358 self.assertEqual(osutils.ReadFile(after.file_name), content)
359
360 def testSmall(self):
361 """Ensure that files smaller than the limit are not modified."""
362 self._testNotStripped(self.slim, 1024, self.SLIM_CONTENT)
363 self._testNotStripped(self.fat, 1024, self.FAT_CONTENT)
364
365 def testLarge(self):
366 """Ensure that files larger than the limit are modified."""
367 self._testStripped(self.slim, 1, self.SLIM_CONTENT)
368 self._testStripped(self.fat, 1, self.SLIM_CONTENT)
369
370 def testMixed(self):
371 """Test mix of large and small."""
372 strip_size = len(self.SLIM_CONTENT) + 1
373
374 self._testNotStripped(self.slim, strip_size, self.SLIM_CONTENT)
375 self._testStripped(self.fat, strip_size, self.SLIM_CONTENT)
376
377 def testSizeWarnings(self):
378 large = self.createSymbolFile(
379 'large.sym', content=self.SLIM_CONTENT,
380 size=upload_symbols.CRASH_SERVER_FILE_LIMIT*2)
381
382 # Would like to Strip as part of this test, but that really copies all
383 # of the sparse file content, which is too expensive for a unittest.
384 self._testNotStripped(large, None, None)
385
386 self.assertEqual(self.warn_mock.call_count, 1)
387
388
Mike Nichols137e82d2019-05-15 18:40:34 -0600389class DeduplicateTest(SymbolsTestBase):
390 """Test server Deduplication."""
391 def setUp(self):
392 self.PatchObject(upload_symbols, 'ExecRequest',
393 return_value={'pairs': [
394 {'status': 'FOUND',
395 'symbolId':
396 {'debugFile': 'sym1_sym',
397 'debugId': 'BEAA9BE'}},
398 {'status': 'FOUND',
399 'symbolId':
400 {'debugFile': 'sym2_sym',
401 'debugId': 'B6B1A36'}},
402 {'status': 'MISSING',
403 'symbolId':
404 {'debugFile': 'sym3_sym',
405 'debugId': 'D4FC0FC'}}]})
406
407 def testFindDuplicates(self):
408 # The first two symbols will be duplicate, the third new.
409 sym1 = self.createSymbolFile('sym1.sym')
410 sym1.header = cros_generate_breakpad_symbols.SymbolHeader('cpu', 'BEAA9BE',
411 'sym1_sym', 'os')
412 sym2 = self.createSymbolFile('sym2.sym')
413 sym2.header = cros_generate_breakpad_symbols.SymbolHeader('cpu', 'B6B1A36',
414 'sym2_sym', 'os')
415 sym3 = self.createSymbolFile('sym3.sym')
416 sym3.header = cros_generate_breakpad_symbols.SymbolHeader('cpu', 'D4FC0FC',
417 'sym3_sym', 'os')
418
419 result = upload_symbols.FindDuplicates((sym1, sym2, sym3), 'fake_url',
420 api_key='testkey')
421 self.assertEqual(list(result), [sym1, sym2, sym3])
422
423 self.assertEqual(sym1.status, upload_symbols.SymbolFile.DUPLICATE)
424 self.assertEqual(sym2.status, upload_symbols.SymbolFile.DUPLICATE)
425 self.assertEqual(sym3.status, upload_symbols.SymbolFile.INITIAL)
426
427
Don Garrettdeb2e032016-07-06 16:44:14 -0700428class PerformSymbolFilesUploadTest(SymbolsTestBase):
Don Garretta28be6d2016-06-16 18:09:35 -0700429 """Test PerformSymbolFile, and it's helper methods."""
430 def setUp(self):
Don Garrettdeb2e032016-07-06 16:44:14 -0700431 self.sym_initial = self.createSymbolFile(
432 'initial.sym')
433 self.sym_error = self.createSymbolFile(
434 'error.sym', status=upload_symbols.SymbolFile.ERROR)
435 self.sym_duplicate = self.createSymbolFile(
436 'duplicate.sym', status=upload_symbols.SymbolFile.DUPLICATE)
437 self.sym_uploaded = self.createSymbolFile(
438 'uploaded.sym', status=upload_symbols.SymbolFile.UPLOADED)
Don Garretta28be6d2016-06-16 18:09:35 -0700439
440 def testGetUploadTimeout(self):
441 """Test GetUploadTimeout helper function."""
442 # Timeout for small file.
Don Garrettdeb2e032016-07-06 16:44:14 -0700443 self.assertEqual(upload_symbols.GetUploadTimeout(self.sym_initial),
Don Garretta28be6d2016-06-16 18:09:35 -0700444 upload_symbols.UPLOAD_MIN_TIMEOUT)
445
Ian Barkley-Yeung22ba8122020-02-05 15:39:02 -0800446 # Timeout for 512M file.
447 large = self.createSymbolFile('large.sym', size=(512 * 1024 * 1024))
448 self.assertEqual(upload_symbols.GetUploadTimeout(large), 15 * 60)
Don Garretta28be6d2016-06-16 18:09:35 -0700449
450 def testUploadSymbolFile(self):
Mike Nichols90f7c152019-04-09 15:14:08 -0600451 upload_symbols.UploadSymbolFile('fake_url', self.sym_initial,
452 api_key='testkey')
Don Garretta28be6d2016-06-16 18:09:35 -0700453 # TODO: Examine mock in more detail to make sure request is correct.
Mike Nichols137e82d2019-05-15 18:40:34 -0600454 self.assertEqual(self.request_mock.call_count, 3)
Don Garretta28be6d2016-06-16 18:09:35 -0700455
Don Garrettdeb2e032016-07-06 16:44:14 -0700456 def testPerformSymbolsFileUpload(self):
Don Garretta28be6d2016-06-16 18:09:35 -0700457 """We upload on first try."""
Don Garrettdeb2e032016-07-06 16:44:14 -0700458 symbols = [self.sym_initial]
Don Garretta28be6d2016-06-16 18:09:35 -0700459
Don Garrettdeb2e032016-07-06 16:44:14 -0700460 result = upload_symbols.PerformSymbolsFileUpload(
Mike Nichols90f7c152019-04-09 15:14:08 -0600461 symbols, 'fake_url', api_key='testkey')
Don Garrettdeb2e032016-07-06 16:44:14 -0700462
463 self.assertEqual(list(result), symbols)
464 self.assertEqual(self.sym_initial.status,
465 upload_symbols.SymbolFile.UPLOADED)
Mike Nichols137e82d2019-05-15 18:40:34 -0600466 self.assertEqual(self.request_mock.call_count, 3)
Don Garretta28be6d2016-06-16 18:09:35 -0700467
Don Garrettdeb2e032016-07-06 16:44:14 -0700468 def testPerformSymbolsFileUploadFailure(self):
Don Garretta28be6d2016-06-16 18:09:35 -0700469 """All network requests fail."""
Mike Frysinger3dcacee2019-08-23 17:09:11 -0400470 self.request_mock.side_effect = IOError('network failure')
Don Garrettdeb2e032016-07-06 16:44:14 -0700471 symbols = [self.sym_initial]
Don Garretta28be6d2016-06-16 18:09:35 -0700472
Don Garrettdeb2e032016-07-06 16:44:14 -0700473 result = upload_symbols.PerformSymbolsFileUpload(
Mike Nichols90f7c152019-04-09 15:14:08 -0600474 symbols, 'fake_url', api_key='testkey')
Don Garretta28be6d2016-06-16 18:09:35 -0700475
Don Garrettdeb2e032016-07-06 16:44:14 -0700476 self.assertEqual(list(result), symbols)
477 self.assertEqual(self.sym_initial.status, upload_symbols.SymbolFile.ERROR)
Mike Nichols90f7c152019-04-09 15:14:08 -0600478 self.assertEqual(self.request_mock.call_count, 7)
Don Garretta28be6d2016-06-16 18:09:35 -0700479
Don Garrettdeb2e032016-07-06 16:44:14 -0700480 def testPerformSymbolsFileUploadTransisentFailure(self):
Don Garretta28be6d2016-06-16 18:09:35 -0700481 """We fail once, then succeed."""
Mike Frysinger3dcacee2019-08-23 17:09:11 -0400482 self.urlopen_mock.side_effect = (IOError('network failure'), None)
Don Garrettdeb2e032016-07-06 16:44:14 -0700483 symbols = [self.sym_initial]
Don Garretta28be6d2016-06-16 18:09:35 -0700484
Don Garrettdeb2e032016-07-06 16:44:14 -0700485 result = upload_symbols.PerformSymbolsFileUpload(
Mike Nichols90f7c152019-04-09 15:14:08 -0600486 symbols, 'fake_url', api_key='testkey')
Don Garretta28be6d2016-06-16 18:09:35 -0700487
Don Garrettdeb2e032016-07-06 16:44:14 -0700488 self.assertEqual(list(result), symbols)
489 self.assertEqual(self.sym_initial.status,
490 upload_symbols.SymbolFile.UPLOADED)
Mike Nichols137e82d2019-05-15 18:40:34 -0600491 self.assertEqual(self.request_mock.call_count, 3)
Don Garrettdeb2e032016-07-06 16:44:14 -0700492
493 def testPerformSymbolsFileUploadMixed(self):
494 """Upload symbols in mixed starting states.
495
496 Demonstrate that INITIAL and ERROR are uploaded, but DUPLICATE/UPLOADED are
497 ignored.
498 """
499 symbols = [self.sym_initial, self.sym_error,
500 self.sym_duplicate, self.sym_uploaded]
501
502 result = upload_symbols.PerformSymbolsFileUpload(
Mike Nichols90f7c152019-04-09 15:14:08 -0600503 symbols, 'fake_url', api_key='testkey')
Don Garrettdeb2e032016-07-06 16:44:14 -0700504
505 #
506 self.assertEqual(list(result), symbols)
507 self.assertEqual(self.sym_initial.status,
508 upload_symbols.SymbolFile.UPLOADED)
509 self.assertEqual(self.sym_error.status,
510 upload_symbols.SymbolFile.UPLOADED)
511 self.assertEqual(self.sym_duplicate.status,
512 upload_symbols.SymbolFile.DUPLICATE)
513 self.assertEqual(self.sym_uploaded.status,
514 upload_symbols.SymbolFile.UPLOADED)
Mike Nichols137e82d2019-05-15 18:40:34 -0600515 self.assertEqual(self.request_mock.call_count, 6)
Don Garrettdeb2e032016-07-06 16:44:14 -0700516
517
518 def testPerformSymbolsFileUploadErrorOut(self):
519 """Demonstate we exit only after X errors."""
520
521 symbol_count = upload_symbols.MAX_TOTAL_ERRORS_FOR_RETRY + 10
522 symbols = []
523 fail_file = None
524
525 # potentially twice as many errors as we should attempt.
Mike Frysinger79cca962019-06-13 15:26:53 -0400526 for _ in range(symbol_count):
Don Garrettdeb2e032016-07-06 16:44:14 -0700527 # Each loop will get unique SymbolFile instances that use the same files.
528 fail = self.createSymbolFile('fail.sym')
529 fail_file = fail.file_name
530 symbols.append(self.createSymbolFile('pass.sym'))
531 symbols.append(fail)
532
533 # Mock out UploadSymbolFile and fail for fail.sym files.
Mike Nichols90f7c152019-04-09 15:14:08 -0600534 def failSome(_url, symbol, _api_key):
Don Garrettdeb2e032016-07-06 16:44:14 -0700535 if symbol.file_name == fail_file:
Mike Frysinger3dcacee2019-08-23 17:09:11 -0400536 raise IOError('network failure')
Don Garrettdeb2e032016-07-06 16:44:14 -0700537
Luigi Semenzato5104f3c2016-10-12 12:37:42 -0700538 upload_mock = self.PatchObject(upload_symbols, 'UploadSymbolFile',
539 side_effect=failSome)
540 upload_mock.__name__ = 'UploadSymbolFileMock2'
Don Garrettdeb2e032016-07-06 16:44:14 -0700541
542 result = upload_symbols.PerformSymbolsFileUpload(
Mike Nichols90f7c152019-04-09 15:14:08 -0600543 symbols, 'fake_url', api_key='testkey')
Don Garrettdeb2e032016-07-06 16:44:14 -0700544
545 self.assertEqual(list(result), symbols)
546
547 passed = sum(s.status == upload_symbols.SymbolFile.UPLOADED
548 for s in symbols)
549 failed = sum(s.status == upload_symbols.SymbolFile.ERROR
550 for s in symbols)
551 skipped = sum(s.status == upload_symbols.SymbolFile.INITIAL
552 for s in symbols)
553
554 # Shows we all pass.sym files worked until limit hit.
555 self.assertEqual(passed, upload_symbols.MAX_TOTAL_ERRORS_FOR_RETRY)
556
557 # Shows we all fail.sym files failed until limit hit.
558 self.assertEqual(failed, upload_symbols.MAX_TOTAL_ERRORS_FOR_RETRY)
559
560 # Shows both pass/fail were skipped after limit hit.
561 self.assertEqual(skipped, 10 * 2)
Don Garretta28be6d2016-06-16 18:09:35 -0700562
563
564class UploadSymbolsTest(SymbolsTestBase):
565 """Test UploadSymbols, along with most helper methods."""
566 def setUp(self):
567 # Results gathering.
568 self.failure_file = os.path.join(self.tempdir, 'failures.txt')
569
570 def testUploadSymbolsEmpty(self):
571 """Upload dir is empty."""
Mike Nichols137e82d2019-05-15 18:40:34 -0600572 result = upload_symbols.UploadSymbols([self.data], 'fake_url')
Don Garretta28be6d2016-06-16 18:09:35 -0700573
Mike Frysinger2d589a12019-08-25 14:15:12 -0400574 self.assertEqual(result, 0)
Don Garretta28be6d2016-06-16 18:09:35 -0700575 self.assertEqual(self.urlopen_mock.call_count, 0)
576
577 def testUploadSymbols(self):
578 """Upload a few files."""
579 self.createSymbolFile('slim.sym', self.SLIM_CONTENT)
580 self.createSymbolFile(os.path.join('nested', 'inner.sym'))
581 self.createSymbolFile('fat.sym', self.FAT_CONTENT)
582
583 result = upload_symbols.UploadSymbols(
Mike Nichols90f7c152019-04-09 15:14:08 -0600584 [self.data], 'fake_url',
585 failed_list=self.failure_file, strip_cfi=len(self.SLIM_CONTENT)+1,
586 api_key='testkey')
Don Garretta28be6d2016-06-16 18:09:35 -0700587
Mike Frysinger2d589a12019-08-25 14:15:12 -0400588 self.assertEqual(result, 0)
Mike Nichols137e82d2019-05-15 18:40:34 -0600589 self.assertEqual(self.request_mock.call_count, 10)
Mike Frysinger2d589a12019-08-25 14:15:12 -0400590 self.assertEqual(osutils.ReadFile(self.failure_file), '')
Don Garretta28be6d2016-06-16 18:09:35 -0700591
592 def testUploadSymbolsLimited(self):
593 """Upload a few files."""
594 self.createSymbolFile('slim.sym', self.SLIM_CONTENT)
595 self.createSymbolFile(os.path.join('nested', 'inner.sym'))
596 self.createSymbolFile('fat.sym', self.FAT_CONTENT)
597
598 result = upload_symbols.UploadSymbols(
Mike Nichols90f7c152019-04-09 15:14:08 -0600599 [self.data], 'fake_url', upload_limit=2,
600 api_key='testkey')
Don Garretta28be6d2016-06-16 18:09:35 -0700601
Mike Frysinger2d589a12019-08-25 14:15:12 -0400602 self.assertEqual(result, 0)
Mike Nichols137e82d2019-05-15 18:40:34 -0600603 self.assertEqual(self.request_mock.call_count, 7)
Mike Frysingerf2fa7d62017-12-14 18:33:59 -0500604 self.assertNotExists(self.failure_file)
Don Garretta28be6d2016-06-16 18:09:35 -0700605
606 def testUploadSymbolsFailures(self):
607 """Upload a few files."""
608 self.createSymbolFile('pass.sym')
609 fail = self.createSymbolFile('fail.sym')
610
Mike Nichols90f7c152019-04-09 15:14:08 -0600611 def failSome(_url, symbol, _api_key):
Don Garretta28be6d2016-06-16 18:09:35 -0700612 if symbol.file_name == fail.file_name:
Mike Frysinger3dcacee2019-08-23 17:09:11 -0400613 raise IOError('network failure')
Don Garretta28be6d2016-06-16 18:09:35 -0700614
615 # Mock out UploadSymbolFile so it's easy to see which file to fail for.
616 upload_mock = self.PatchObject(upload_symbols, 'UploadSymbolFile',
617 side_effect=failSome)
Luigi Semenzato5104f3c2016-10-12 12:37:42 -0700618 # Mock __name__ for logging.
619 upload_mock.__name__ = 'UploadSymbolFileMock'
Don Garretta28be6d2016-06-16 18:09:35 -0700620
621 result = upload_symbols.UploadSymbols(
Mike Nichols90f7c152019-04-09 15:14:08 -0600622 [self.data], 'fake_url',
623 failed_list=self.failure_file, api_key='testkey')
Don Garretta28be6d2016-06-16 18:09:35 -0700624
Mike Frysinger2d589a12019-08-25 14:15:12 -0400625 self.assertEqual(result, 1)
Don Garretta28be6d2016-06-16 18:09:35 -0700626 self.assertEqual(upload_mock.call_count, 8)
Mike Frysinger2d589a12019-08-25 14:15:12 -0400627 self.assertEqual(osutils.ReadFile(self.failure_file), 'fail.sym\n')
Don Garretta28be6d2016-06-16 18:09:35 -0700628
Don Garretta28be6d2016-06-16 18:09:35 -0700629# TODO: We removed --network integration tests.
Mike Frysinger0c0efa22014-02-09 23:32:23 -0500630
631
Mike Frysingerea838d12014-12-08 11:55:32 -0500632def main(_argv):
Mike Frysinger27e21b72018-07-12 14:20:21 -0400633 # pylint: disable=protected-access
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400634 # Set timeouts small so that if the unit test hangs, it won't hang for long.
635 parallel._BackgroundTask.STARTUP_TIMEOUT = 5
636 parallel._BackgroundTask.EXIT_TIMEOUT = 5
637
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400638 # Run the tests.
Mike Frysingerba167372015-01-21 10:37:03 -0500639 cros_test_lib.main(level='info', module=__name__)