blob: c9540c763e1f9325c343032a0801023bed75ba4c [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
Mike Frysinger2688ef62020-02-16 00:00:46 -050057assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
58
59
Don Garretta28be6d2016-06-16 18:09:35 -070060class SymbolsTestBase(cros_test_lib.MockTempDirTestCase):
61 """Base class for most symbols tests."""
62
63 SLIM_CONTENT = """
64some junk
65"""
66
67 FAT_CONTENT = """
68STACK CFI 1234
69some junk
70STACK CFI 1234
71"""
72
73 def setUp(self):
74 # Make certain we don't use the network.
Mike Frysinger3dcacee2019-08-23 17:09:11 -040075 self.urlopen_mock = self.PatchObject(urllib.request, 'urlopen')
Mike Nichols90f7c152019-04-09 15:14:08 -060076 self.request_mock = self.PatchObject(upload_symbols, 'ExecRequest',
77 return_value={'uploadUrl':
78 'testurl',
79 'uploadKey':
80 'asdgasgas'})
Don Garretta28be6d2016-06-16 18:09:35 -070081
82 # Make 'uploads' go fast.
83 self.PatchObject(upload_symbols, 'SLEEP_DELAY', 0)
84 self.PatchObject(upload_symbols, 'INITIAL_RETRY_DELAY', 0)
85
86 # So our symbol file content doesn't have to be real.
87 self.PatchObject(cros_generate_breakpad_symbols, 'ReadSymsHeader',
88 return_value=cros_generate_breakpad_symbols.SymbolHeader(
89 os='os', cpu='cpu', id='id', name='name'))
90
91 self.working = os.path.join(self.tempdir, 'expand')
92 osutils.SafeMakedirs(self.working)
93
94 self.data = os.path.join(self.tempdir, 'data')
95 osutils.SafeMakedirs(self.data)
96
97 def createSymbolFile(self, filename, content=FAT_CONTENT, size=0,
98 status=None, dedupe=False):
99 fullname = os.path.join(self.data, filename)
100 osutils.SafeMakedirs(os.path.dirname(fullname))
101
102 # If a file size is given, force that to be the minimum file size. Create
103 # a sparse file so large files are practical.
104 with open(fullname, 'w+b') as f:
105 f.truncate(size)
106 f.seek(0)
Mike Frysingerac75f602019-11-14 23:18:51 -0500107 f.write(content.encode('utf-8'))
Don Garretta28be6d2016-06-16 18:09:35 -0700108
109 result = upload_symbols.SymbolFile(display_path=filename,
110 file_name=fullname)
111
112 if status:
113 result.status = status
114
115 if dedupe:
116 result.dedupe_item = upload_symbols.DedupeItem(result)
117 result.dedupe_push_state = 'push_state'
118
119 return result
120
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400121
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700122class SymbolServerRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
123 """HTTP handler for symbol POSTs"""
124
125 RESP_CODE = None
126 RESP_MSG = None
127
128 def do_POST(self):
129 """Handle a POST request"""
130 # Drain the data from the client. If we don't, we might write the response
131 # and close the socket before the client finishes, so they die with EPIPE.
132 clen = int(self.headers.get('Content-Length', '0'))
133 self.rfile.read(clen)
134
135 self.send_response(self.RESP_CODE, self.RESP_MSG)
136 self.end_headers()
137
Mike Frysingera4418152019-09-22 04:00:24 -0400138 # pylint: disable=arguments-differ
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700139 def log_message(self, *args, **kwargs):
140 """Stub the logger as it writes to stderr"""
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700141
142
Mike Frysinger3d465162019-08-28 00:40:23 -0400143class SymbolServer(socketserver.ThreadingTCPServer, BaseHTTPServer.HTTPServer):
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700144 """Simple HTTP server that forks each request"""
145
146
Chris McDonald5330b7f2020-04-15 04:16:16 -0600147@cros_test_lib.pytestmark_leaks_process
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700148class UploadSymbolsServerTest(cros_test_lib.MockTempDirTestCase):
149 """Tests for UploadSymbols() and a local HTTP server"""
150
151 SYM_CONTENTS = """MODULE Linux arm 123-456 blkid
152PUBLIC 1471 0 main"""
153
154 def SpawnServer(self, RequestHandler):
155 """Spawn a new http server"""
Mike Frysinger06da5202014-09-26 17:30:33 -0500156 while True:
157 try:
158 port = remote_access.GetUnusedPort()
159 address = ('', port)
160 self.httpd = SymbolServer(address, RequestHandler)
161 break
162 except socket.error as e:
163 if e.errno == errno.EADDRINUSE:
164 continue
165 raise
Don Garretta28be6d2016-06-16 18:09:35 -0700166 self.server_url = 'http://localhost:%i/post/path' % port
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700167 self.httpd_pid = os.fork()
168 if self.httpd_pid == 0:
169 self.httpd.serve_forever(poll_interval=0.1)
170 sys.exit(0)
Mike Frysingerac75f602019-11-14 23:18:51 -0500171 # The child runs the server, so close the socket in the parent.
172 self.httpd.server_close()
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700173
174 def setUp(self):
175 self.httpd_pid = None
176 self.httpd = None
Don Garretta28be6d2016-06-16 18:09:35 -0700177 self.server_url = None
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700178 self.sym_file = os.path.join(self.tempdir, 'test.sym')
179 osutils.WriteFile(self.sym_file, self.SYM_CONTENTS)
180
Don Garretta28be6d2016-06-16 18:09:35 -0700181 # Stop sleeps and retries for these tests.
182 self.PatchObject(upload_symbols, 'SLEEP_DELAY', 0)
183 self.PatchObject(upload_symbols, 'INITIAL_RETRY_DELAY', 0)
184 self.PatchObject(upload_symbols, 'MAX_RETRIES', 0)
185
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700186 def tearDown(self):
187 # Only kill the server if we forked one.
188 if self.httpd_pid:
189 os.kill(self.httpd_pid, signal.SIGUSR1)
190
191 def testSuccess(self):
192 """The server returns success for all uploads"""
193 class Handler(SymbolServerRequestHandler):
194 """Always return 200"""
195 RESP_CODE = 200
Mike Nichols90f7c152019-04-09 15:14:08 -0600196 self.PatchObject(upload_symbols, 'ExecRequest',
197 return_value={'uploadUrl': 'testurl',
198 'uploadKey': 'testSuccess'})
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700199 self.SpawnServer(Handler)
Don Garretta28be6d2016-06-16 18:09:35 -0700200 ret = upload_symbols.UploadSymbols(
201 sym_paths=[self.sym_file] * 10,
202 upload_url=self.server_url,
Mike Nichols90f7c152019-04-09 15:14:08 -0600203 api_key='testSuccess')
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700204 self.assertEqual(ret, 0)
205
206 def testError(self):
207 """The server returns errors for all uploads"""
208 class Handler(SymbolServerRequestHandler):
Mike Nichols90f7c152019-04-09 15:14:08 -0600209 """All connections error"""
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700210 RESP_CODE = 500
211 RESP_MSG = 'Internal Server Error'
212
213 self.SpawnServer(Handler)
Don Garretta28be6d2016-06-16 18:09:35 -0700214 ret = upload_symbols.UploadSymbols(
215 sym_paths=[self.sym_file] * 10,
216 upload_url=self.server_url,
Mike Nichols90f7c152019-04-09 15:14:08 -0600217 api_key='testkey')
Don Garretta28be6d2016-06-16 18:09:35 -0700218 self.assertEqual(ret, 10)
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700219
220 def testHungServer(self):
221 """The server chokes, but we recover"""
222 class Handler(SymbolServerRequestHandler):
223 """All connections choke forever"""
Mike Nichols137e82d2019-05-15 18:40:34 -0600224 self.PatchObject(upload_symbols, 'ExecRequest',
225 return_value={'pairs': []})
226
Mike Frysinger0a2fd922014-09-12 20:23:42 -0700227 def do_POST(self):
228 while True:
229 time.sleep(1000)
230
231 self.SpawnServer(Handler)
232 with mock.patch.object(upload_symbols, 'GetUploadTimeout') as m:
Don Garretta28be6d2016-06-16 18:09:35 -0700233 m.return_value = 0.01
Mike Frysinger58312e92014-03-18 04:18:36 -0400234 ret = upload_symbols.UploadSymbols(
Don Garretta28be6d2016-06-16 18:09:35 -0700235 sym_paths=[self.sym_file] * 10,
236 upload_url=self.server_url,
Mike Nichols137e82d2019-05-15 18:40:34 -0600237 timeout=m.return_value,
Mike Nichols90f7c152019-04-09 15:14:08 -0600238 api_key='testkey')
Don Garretta28be6d2016-06-16 18:09:35 -0700239 self.assertEqual(ret, 10)
Aviv Keshetd1f04632014-05-09 11:33:46 -0700240
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400241
Don Garretta28be6d2016-06-16 18:09:35 -0700242class UploadSymbolsHelpersTest(cros_test_lib.TestCase):
243 """Test assorted helper functions and classes."""
Don Garretta28be6d2016-06-16 18:09:35 -0700244 def testIsTarball(self):
245 notTar = [
246 '/foo/bar/test.bin',
247 '/foo/bar/test.tar.bin',
248 '/foo/bar/test.faketar.gz',
249 '/foo/bar/test.nottgz',
Mike Frysinger0c0efa22014-02-09 23:32:23 -0500250 ]
Mike Frysinger0c0efa22014-02-09 23:32:23 -0500251
Don Garretta28be6d2016-06-16 18:09:35 -0700252 isTar = [
253 '/foo/bar/test.tar',
254 '/foo/bar/test.bin.tar',
255 '/foo/bar/test.bin.tar.bz2',
256 '/foo/bar/test.bin.tar.gz',
257 '/foo/bar/test.bin.tar.xz',
258 '/foo/bar/test.tbz2',
259 '/foo/bar/test.tbz',
260 '/foo/bar/test.tgz',
261 '/foo/bar/test.txz',
262 ]
Mike Frysinger0c0efa22014-02-09 23:32:23 -0500263
Don Garretta28be6d2016-06-16 18:09:35 -0700264 for p in notTar:
265 self.assertFalse(upload_symbols.IsTarball(p))
266
267 for p in isTar:
268 self.assertTrue(upload_symbols.IsTarball(p))
269
270 def testBatchGenerator(self):
271 result = upload_symbols.BatchGenerator([], 2)
272 self.assertEqual(list(result), [])
273
Mike Frysinger66848312019-07-03 02:12:39 -0400274 # BatchGenerator accepts iterators, so passing it here is safe.
275 # pylint: disable=range-builtin-not-iterating
Mike Frysinger79cca962019-06-13 15:26:53 -0400276 result = upload_symbols.BatchGenerator(range(6), 2)
Don Garretta28be6d2016-06-16 18:09:35 -0700277 self.assertEqual(list(result), [[0, 1], [2, 3], [4, 5]])
278
Mike Frysinger79cca962019-06-13 15:26:53 -0400279 result = upload_symbols.BatchGenerator(range(7), 2)
Don Garretta28be6d2016-06-16 18:09:35 -0700280 self.assertEqual(list(result), [[0, 1], [2, 3], [4, 5], [6]])
281
282 # Prove that we are streaming the results, not generating them all at once.
283 result = upload_symbols.BatchGenerator(itertools.repeat(0), 2)
Mike Frysinger67d90242019-07-03 19:03:58 -0400284 self.assertEqual(next(result), [0, 0])
Don Garretta28be6d2016-06-16 18:09:35 -0700285
286
287class FindSymbolFilesTest(SymbolsTestBase):
288 """Test FindSymbolFiles."""
289 def setUp(self):
290 self.symfile = self.createSymbolFile('root.sym').file_name
291 self.innerfile = self.createSymbolFile(
292 os.path.join('nested', 'inner.sym')).file_name
293
294 # CreateTarball is having issues outside the chroot from open file tests.
295 #
296 # self.tarball = os.path.join(self.tempdir, 'syms.tar.gz')
297 # cros_build_lib.CreateTarball(
298 # 'syms.tar.gz', self.tempdir, inputs=(self.data))
299
300 def testEmpty(self):
301 symbols = list(upload_symbols.FindSymbolFiles(
302 self.working, []))
303 self.assertEqual(symbols, [])
304
305 def testFile(self):
306 symbols = list(upload_symbols.FindSymbolFiles(
307 self.working, [self.symfile]))
308
309 self.assertEqual(len(symbols), 1)
310 sf = symbols[0]
311
312 self.assertEqual(sf.display_name, 'root.sym')
313 self.assertEqual(sf.display_path, self.symfile)
314 self.assertEqual(sf.file_name, self.symfile)
315 self.assertEqual(sf.status, upload_symbols.SymbolFile.INITIAL)
316 self.assertEqual(sf.FileSize(), len(self.FAT_CONTENT))
317
318 def testDir(self):
319 symbols = list(upload_symbols.FindSymbolFiles(
320 self.working, [self.data]))
321
322 self.assertEqual(len(symbols), 2)
323 root = symbols[0]
324 nested = symbols[1]
325
326 self.assertEqual(root.display_name, 'root.sym')
327 self.assertEqual(root.display_path, 'root.sym')
328 self.assertEqual(root.file_name, self.symfile)
329 self.assertEqual(root.status, upload_symbols.SymbolFile.INITIAL)
330 self.assertEqual(root.FileSize(), len(self.FAT_CONTENT))
331
332 self.assertEqual(nested.display_name, 'inner.sym')
333 self.assertEqual(nested.display_path, 'nested/inner.sym')
334 self.assertEqual(nested.file_name, self.innerfile)
335 self.assertEqual(nested.status, upload_symbols.SymbolFile.INITIAL)
336 self.assertEqual(nested.FileSize(), len(self.FAT_CONTENT))
337
338
339class AdjustSymbolFileSizeTest(SymbolsTestBase):
340 """Test AdjustSymbolFileSize."""
341 def setUp(self):
342 self.slim = self.createSymbolFile('slim.sym', self.SLIM_CONTENT)
343 self.fat = self.createSymbolFile('fat.sym', self.FAT_CONTENT)
344
345 self.warn_mock = self.PatchObject(logging, 'PrintBuildbotStepWarnings')
346
347 def _testNotStripped(self, symbol, size=None, content=None):
348 start_file = symbol.file_name
349 after = upload_symbols.AdjustSymbolFileSize(
350 symbol, self.working, size)
351 self.assertIs(after, symbol)
352 self.assertEqual(after.file_name, start_file)
353 if content is not None:
354 self.assertEqual(osutils.ReadFile(after.file_name), content)
355
356 def _testStripped(self, symbol, size=None, content=None):
357 after = upload_symbols.AdjustSymbolFileSize(
358 symbol, self.working, size)
359 self.assertIs(after, symbol)
360 self.assertTrue(after.file_name.startswith(self.working))
361 if content is not None:
362 self.assertEqual(osutils.ReadFile(after.file_name), content)
363
364 def testSmall(self):
365 """Ensure that files smaller than the limit are not modified."""
366 self._testNotStripped(self.slim, 1024, self.SLIM_CONTENT)
367 self._testNotStripped(self.fat, 1024, self.FAT_CONTENT)
368
369 def testLarge(self):
370 """Ensure that files larger than the limit are modified."""
371 self._testStripped(self.slim, 1, self.SLIM_CONTENT)
372 self._testStripped(self.fat, 1, self.SLIM_CONTENT)
373
374 def testMixed(self):
375 """Test mix of large and small."""
376 strip_size = len(self.SLIM_CONTENT) + 1
377
378 self._testNotStripped(self.slim, strip_size, self.SLIM_CONTENT)
379 self._testStripped(self.fat, strip_size, self.SLIM_CONTENT)
380
381 def testSizeWarnings(self):
382 large = self.createSymbolFile(
383 'large.sym', content=self.SLIM_CONTENT,
384 size=upload_symbols.CRASH_SERVER_FILE_LIMIT*2)
385
386 # Would like to Strip as part of this test, but that really copies all
387 # of the sparse file content, which is too expensive for a unittest.
388 self._testNotStripped(large, None, None)
389
390 self.assertEqual(self.warn_mock.call_count, 1)
391
392
Mike Nichols137e82d2019-05-15 18:40:34 -0600393class DeduplicateTest(SymbolsTestBase):
394 """Test server Deduplication."""
395 def setUp(self):
396 self.PatchObject(upload_symbols, 'ExecRequest',
397 return_value={'pairs': [
398 {'status': 'FOUND',
399 'symbolId':
400 {'debugFile': 'sym1_sym',
401 'debugId': 'BEAA9BE'}},
402 {'status': 'FOUND',
403 'symbolId':
404 {'debugFile': 'sym2_sym',
405 'debugId': 'B6B1A36'}},
406 {'status': 'MISSING',
407 'symbolId':
408 {'debugFile': 'sym3_sym',
409 'debugId': 'D4FC0FC'}}]})
410
411 def testFindDuplicates(self):
412 # The first two symbols will be duplicate, the third new.
413 sym1 = self.createSymbolFile('sym1.sym')
414 sym1.header = cros_generate_breakpad_symbols.SymbolHeader('cpu', 'BEAA9BE',
415 'sym1_sym', 'os')
416 sym2 = self.createSymbolFile('sym2.sym')
417 sym2.header = cros_generate_breakpad_symbols.SymbolHeader('cpu', 'B6B1A36',
418 'sym2_sym', 'os')
419 sym3 = self.createSymbolFile('sym3.sym')
420 sym3.header = cros_generate_breakpad_symbols.SymbolHeader('cpu', 'D4FC0FC',
421 'sym3_sym', 'os')
422
423 result = upload_symbols.FindDuplicates((sym1, sym2, sym3), 'fake_url',
424 api_key='testkey')
425 self.assertEqual(list(result), [sym1, sym2, sym3])
426
427 self.assertEqual(sym1.status, upload_symbols.SymbolFile.DUPLICATE)
428 self.assertEqual(sym2.status, upload_symbols.SymbolFile.DUPLICATE)
429 self.assertEqual(sym3.status, upload_symbols.SymbolFile.INITIAL)
430
431
Don Garrettdeb2e032016-07-06 16:44:14 -0700432class PerformSymbolFilesUploadTest(SymbolsTestBase):
Don Garretta28be6d2016-06-16 18:09:35 -0700433 """Test PerformSymbolFile, and it's helper methods."""
434 def setUp(self):
Don Garrettdeb2e032016-07-06 16:44:14 -0700435 self.sym_initial = self.createSymbolFile(
436 'initial.sym')
437 self.sym_error = self.createSymbolFile(
438 'error.sym', status=upload_symbols.SymbolFile.ERROR)
439 self.sym_duplicate = self.createSymbolFile(
440 'duplicate.sym', status=upload_symbols.SymbolFile.DUPLICATE)
441 self.sym_uploaded = self.createSymbolFile(
442 'uploaded.sym', status=upload_symbols.SymbolFile.UPLOADED)
Don Garretta28be6d2016-06-16 18:09:35 -0700443
444 def testGetUploadTimeout(self):
445 """Test GetUploadTimeout helper function."""
446 # Timeout for small file.
Don Garrettdeb2e032016-07-06 16:44:14 -0700447 self.assertEqual(upload_symbols.GetUploadTimeout(self.sym_initial),
Don Garretta28be6d2016-06-16 18:09:35 -0700448 upload_symbols.UPLOAD_MIN_TIMEOUT)
449
Ian Barkley-Yeung22ba8122020-02-05 15:39:02 -0800450 # Timeout for 512M file.
451 large = self.createSymbolFile('large.sym', size=(512 * 1024 * 1024))
452 self.assertEqual(upload_symbols.GetUploadTimeout(large), 15 * 60)
Don Garretta28be6d2016-06-16 18:09:35 -0700453
454 def testUploadSymbolFile(self):
Mike Nichols90f7c152019-04-09 15:14:08 -0600455 upload_symbols.UploadSymbolFile('fake_url', self.sym_initial,
456 api_key='testkey')
Don Garretta28be6d2016-06-16 18:09:35 -0700457 # TODO: Examine mock in more detail to make sure request is correct.
Mike Nichols137e82d2019-05-15 18:40:34 -0600458 self.assertEqual(self.request_mock.call_count, 3)
Don Garretta28be6d2016-06-16 18:09:35 -0700459
Don Garrettdeb2e032016-07-06 16:44:14 -0700460 def testPerformSymbolsFileUpload(self):
Don Garretta28be6d2016-06-16 18:09:35 -0700461 """We upload on first try."""
Don Garrettdeb2e032016-07-06 16:44:14 -0700462 symbols = [self.sym_initial]
Don Garretta28be6d2016-06-16 18:09:35 -0700463
Don Garrettdeb2e032016-07-06 16:44:14 -0700464 result = upload_symbols.PerformSymbolsFileUpload(
Mike Nichols90f7c152019-04-09 15:14:08 -0600465 symbols, 'fake_url', api_key='testkey')
Don Garrettdeb2e032016-07-06 16:44:14 -0700466
467 self.assertEqual(list(result), symbols)
468 self.assertEqual(self.sym_initial.status,
469 upload_symbols.SymbolFile.UPLOADED)
Mike Nichols137e82d2019-05-15 18:40:34 -0600470 self.assertEqual(self.request_mock.call_count, 3)
Don Garretta28be6d2016-06-16 18:09:35 -0700471
Don Garrettdeb2e032016-07-06 16:44:14 -0700472 def testPerformSymbolsFileUploadFailure(self):
Don Garretta28be6d2016-06-16 18:09:35 -0700473 """All network requests fail."""
Mike Frysinger3dcacee2019-08-23 17:09:11 -0400474 self.request_mock.side_effect = IOError('network failure')
Don Garrettdeb2e032016-07-06 16:44:14 -0700475 symbols = [self.sym_initial]
Don Garretta28be6d2016-06-16 18:09:35 -0700476
Don Garrettdeb2e032016-07-06 16:44:14 -0700477 result = upload_symbols.PerformSymbolsFileUpload(
Mike Nichols90f7c152019-04-09 15:14:08 -0600478 symbols, 'fake_url', api_key='testkey')
Don Garretta28be6d2016-06-16 18:09:35 -0700479
Don Garrettdeb2e032016-07-06 16:44:14 -0700480 self.assertEqual(list(result), symbols)
481 self.assertEqual(self.sym_initial.status, upload_symbols.SymbolFile.ERROR)
Mike Nichols90f7c152019-04-09 15:14:08 -0600482 self.assertEqual(self.request_mock.call_count, 7)
Don Garretta28be6d2016-06-16 18:09:35 -0700483
Don Garrettdeb2e032016-07-06 16:44:14 -0700484 def testPerformSymbolsFileUploadTransisentFailure(self):
Don Garretta28be6d2016-06-16 18:09:35 -0700485 """We fail once, then succeed."""
Mike Frysinger3dcacee2019-08-23 17:09:11 -0400486 self.urlopen_mock.side_effect = (IOError('network failure'), None)
Don Garrettdeb2e032016-07-06 16:44:14 -0700487 symbols = [self.sym_initial]
Don Garretta28be6d2016-06-16 18:09:35 -0700488
Don Garrettdeb2e032016-07-06 16:44:14 -0700489 result = upload_symbols.PerformSymbolsFileUpload(
Mike Nichols90f7c152019-04-09 15:14:08 -0600490 symbols, 'fake_url', api_key='testkey')
Don Garretta28be6d2016-06-16 18:09:35 -0700491
Don Garrettdeb2e032016-07-06 16:44:14 -0700492 self.assertEqual(list(result), symbols)
493 self.assertEqual(self.sym_initial.status,
494 upload_symbols.SymbolFile.UPLOADED)
Mike Nichols137e82d2019-05-15 18:40:34 -0600495 self.assertEqual(self.request_mock.call_count, 3)
Don Garrettdeb2e032016-07-06 16:44:14 -0700496
497 def testPerformSymbolsFileUploadMixed(self):
498 """Upload symbols in mixed starting states.
499
500 Demonstrate that INITIAL and ERROR are uploaded, but DUPLICATE/UPLOADED are
501 ignored.
502 """
503 symbols = [self.sym_initial, self.sym_error,
504 self.sym_duplicate, self.sym_uploaded]
505
506 result = upload_symbols.PerformSymbolsFileUpload(
Mike Nichols90f7c152019-04-09 15:14:08 -0600507 symbols, 'fake_url', api_key='testkey')
Don Garrettdeb2e032016-07-06 16:44:14 -0700508
509 #
510 self.assertEqual(list(result), symbols)
511 self.assertEqual(self.sym_initial.status,
512 upload_symbols.SymbolFile.UPLOADED)
513 self.assertEqual(self.sym_error.status,
514 upload_symbols.SymbolFile.UPLOADED)
515 self.assertEqual(self.sym_duplicate.status,
516 upload_symbols.SymbolFile.DUPLICATE)
517 self.assertEqual(self.sym_uploaded.status,
518 upload_symbols.SymbolFile.UPLOADED)
Mike Nichols137e82d2019-05-15 18:40:34 -0600519 self.assertEqual(self.request_mock.call_count, 6)
Don Garrettdeb2e032016-07-06 16:44:14 -0700520
521
522 def testPerformSymbolsFileUploadErrorOut(self):
523 """Demonstate we exit only after X errors."""
524
525 symbol_count = upload_symbols.MAX_TOTAL_ERRORS_FOR_RETRY + 10
526 symbols = []
527 fail_file = None
528
529 # potentially twice as many errors as we should attempt.
Mike Frysinger79cca962019-06-13 15:26:53 -0400530 for _ in range(symbol_count):
Don Garrettdeb2e032016-07-06 16:44:14 -0700531 # Each loop will get unique SymbolFile instances that use the same files.
532 fail = self.createSymbolFile('fail.sym')
533 fail_file = fail.file_name
534 symbols.append(self.createSymbolFile('pass.sym'))
535 symbols.append(fail)
536
537 # Mock out UploadSymbolFile and fail for fail.sym files.
Mike Nichols90f7c152019-04-09 15:14:08 -0600538 def failSome(_url, symbol, _api_key):
Don Garrettdeb2e032016-07-06 16:44:14 -0700539 if symbol.file_name == fail_file:
Mike Frysinger3dcacee2019-08-23 17:09:11 -0400540 raise IOError('network failure')
Don Garrettdeb2e032016-07-06 16:44:14 -0700541
Luigi Semenzato5104f3c2016-10-12 12:37:42 -0700542 upload_mock = self.PatchObject(upload_symbols, 'UploadSymbolFile',
543 side_effect=failSome)
544 upload_mock.__name__ = 'UploadSymbolFileMock2'
Don Garrettdeb2e032016-07-06 16:44:14 -0700545
546 result = upload_symbols.PerformSymbolsFileUpload(
Mike Nichols90f7c152019-04-09 15:14:08 -0600547 symbols, 'fake_url', api_key='testkey')
Don Garrettdeb2e032016-07-06 16:44:14 -0700548
549 self.assertEqual(list(result), symbols)
550
551 passed = sum(s.status == upload_symbols.SymbolFile.UPLOADED
552 for s in symbols)
553 failed = sum(s.status == upload_symbols.SymbolFile.ERROR
554 for s in symbols)
555 skipped = sum(s.status == upload_symbols.SymbolFile.INITIAL
556 for s in symbols)
557
558 # Shows we all pass.sym files worked until limit hit.
559 self.assertEqual(passed, upload_symbols.MAX_TOTAL_ERRORS_FOR_RETRY)
560
561 # Shows we all fail.sym files failed until limit hit.
562 self.assertEqual(failed, upload_symbols.MAX_TOTAL_ERRORS_FOR_RETRY)
563
564 # Shows both pass/fail were skipped after limit hit.
565 self.assertEqual(skipped, 10 * 2)
Don Garretta28be6d2016-06-16 18:09:35 -0700566
567
Chris McDonald5330b7f2020-04-15 04:16:16 -0600568@cros_test_lib.pytestmark_leaks_process
Don Garretta28be6d2016-06-16 18:09:35 -0700569class UploadSymbolsTest(SymbolsTestBase):
570 """Test UploadSymbols, along with most helper methods."""
571 def setUp(self):
572 # Results gathering.
573 self.failure_file = os.path.join(self.tempdir, 'failures.txt')
574
575 def testUploadSymbolsEmpty(self):
576 """Upload dir is empty."""
Mike Nichols137e82d2019-05-15 18:40:34 -0600577 result = upload_symbols.UploadSymbols([self.data], 'fake_url')
Don Garretta28be6d2016-06-16 18:09:35 -0700578
Mike Frysinger2d589a12019-08-25 14:15:12 -0400579 self.assertEqual(result, 0)
Don Garretta28be6d2016-06-16 18:09:35 -0700580 self.assertEqual(self.urlopen_mock.call_count, 0)
581
582 def testUploadSymbols(self):
583 """Upload a few files."""
584 self.createSymbolFile('slim.sym', self.SLIM_CONTENT)
585 self.createSymbolFile(os.path.join('nested', 'inner.sym'))
586 self.createSymbolFile('fat.sym', self.FAT_CONTENT)
587
588 result = upload_symbols.UploadSymbols(
Mike Nichols90f7c152019-04-09 15:14:08 -0600589 [self.data], 'fake_url',
590 failed_list=self.failure_file, strip_cfi=len(self.SLIM_CONTENT)+1,
591 api_key='testkey')
Don Garretta28be6d2016-06-16 18:09:35 -0700592
Mike Frysinger2d589a12019-08-25 14:15:12 -0400593 self.assertEqual(result, 0)
Mike Nichols137e82d2019-05-15 18:40:34 -0600594 self.assertEqual(self.request_mock.call_count, 10)
Mike Frysinger2d589a12019-08-25 14:15:12 -0400595 self.assertEqual(osutils.ReadFile(self.failure_file), '')
Don Garretta28be6d2016-06-16 18:09:35 -0700596
597 def testUploadSymbolsLimited(self):
598 """Upload a few files."""
599 self.createSymbolFile('slim.sym', self.SLIM_CONTENT)
600 self.createSymbolFile(os.path.join('nested', 'inner.sym'))
601 self.createSymbolFile('fat.sym', self.FAT_CONTENT)
602
603 result = upload_symbols.UploadSymbols(
Mike Nichols90f7c152019-04-09 15:14:08 -0600604 [self.data], 'fake_url', upload_limit=2,
605 api_key='testkey')
Don Garretta28be6d2016-06-16 18:09:35 -0700606
Mike Frysinger2d589a12019-08-25 14:15:12 -0400607 self.assertEqual(result, 0)
Mike Nichols137e82d2019-05-15 18:40:34 -0600608 self.assertEqual(self.request_mock.call_count, 7)
Mike Frysingerf2fa7d62017-12-14 18:33:59 -0500609 self.assertNotExists(self.failure_file)
Don Garretta28be6d2016-06-16 18:09:35 -0700610
611 def testUploadSymbolsFailures(self):
612 """Upload a few files."""
613 self.createSymbolFile('pass.sym')
614 fail = self.createSymbolFile('fail.sym')
615
Mike Nichols90f7c152019-04-09 15:14:08 -0600616 def failSome(_url, symbol, _api_key):
Don Garretta28be6d2016-06-16 18:09:35 -0700617 if symbol.file_name == fail.file_name:
Mike Frysinger3dcacee2019-08-23 17:09:11 -0400618 raise IOError('network failure')
Don Garretta28be6d2016-06-16 18:09:35 -0700619
620 # Mock out UploadSymbolFile so it's easy to see which file to fail for.
621 upload_mock = self.PatchObject(upload_symbols, 'UploadSymbolFile',
622 side_effect=failSome)
Luigi Semenzato5104f3c2016-10-12 12:37:42 -0700623 # Mock __name__ for logging.
624 upload_mock.__name__ = 'UploadSymbolFileMock'
Don Garretta28be6d2016-06-16 18:09:35 -0700625
626 result = upload_symbols.UploadSymbols(
Mike Nichols90f7c152019-04-09 15:14:08 -0600627 [self.data], 'fake_url',
628 failed_list=self.failure_file, api_key='testkey')
Don Garretta28be6d2016-06-16 18:09:35 -0700629
Mike Frysinger2d589a12019-08-25 14:15:12 -0400630 self.assertEqual(result, 1)
Don Garretta28be6d2016-06-16 18:09:35 -0700631 self.assertEqual(upload_mock.call_count, 8)
Mike Frysinger2d589a12019-08-25 14:15:12 -0400632 self.assertEqual(osutils.ReadFile(self.failure_file), 'fail.sym\n')
Don Garretta28be6d2016-06-16 18:09:35 -0700633
Don Garretta28be6d2016-06-16 18:09:35 -0700634# TODO: We removed --network integration tests.
Mike Frysinger0c0efa22014-02-09 23:32:23 -0500635
636
Mike Frysingerea838d12014-12-08 11:55:32 -0500637def main(_argv):
Mike Frysinger27e21b72018-07-12 14:20:21 -0400638 # pylint: disable=protected-access
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400639 # Set timeouts small so that if the unit test hangs, it won't hang for long.
640 parallel._BackgroundTask.STARTUP_TIMEOUT = 5
641 parallel._BackgroundTask.EXIT_TIMEOUT = 5
642
Mike Frysingerd5fcb3a2013-05-30 21:10:50 -0400643 # Run the tests.
Mike Frysingerba167372015-01-21 10:37:03 -0500644 cros_test_lib.main(level='info', module=__name__)