Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # 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 | |
| 6 | import ctypes |
| 7 | import logging |
| 8 | import os |
| 9 | import sys |
| 10 | |
| 11 | sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), |
| 12 | '..', '..')) |
| 13 | from chromite.lib import cros_build_lib |
| 14 | from chromite.lib import cros_test_lib |
| 15 | from chromite.lib import osutils |
| 16 | from chromite.lib import parallel |
| 17 | from chromite.lib import parallel_unittest |
| 18 | from chromite.scripts import upload_symbols |
| 19 | |
| 20 | # TODO(build): Finish test wrapper (http://crosbug.com/37517). |
| 21 | # Until then, this has to be after the chromite imports. |
| 22 | import mock |
| 23 | |
| 24 | |
| 25 | class UploadSymbolsTest(cros_test_lib.MockTempDirTestCase): |
| 26 | |
| 27 | def setUp(self): |
| 28 | for d in ('foo', 'bar', 'some/dir/here'): |
| 29 | d = os.path.join(self.tempdir, d) |
| 30 | osutils.SafeMakedirs(d) |
| 31 | for f in ('ignored', 'real.sym', 'no.sym.here'): |
| 32 | f = os.path.join(d, f) |
| 33 | osutils.Touch(f) |
| 34 | |
| 35 | def _testUploadURL(self, official, expected_url): |
| 36 | """Helper for checking the url used""" |
| 37 | m = upload_symbols.UploadSymbol = mock.Mock(return_value=0) |
| 38 | with parallel_unittest.ParallelMock(): |
| 39 | ret = upload_symbols.UploadSymbols('', official=official, |
| 40 | breakpad_dir=self.tempdir, sleep=0) |
| 41 | self.assertEqual(ret, 0) |
| 42 | self.assertEqual(m.call_count, 3) |
| 43 | for call_args in m.call_args_list: |
| 44 | sym_file, url = call_args[0] |
| 45 | self.assertEqual(url, expected_url) |
| 46 | self.assertTrue(sym_file.endswith('.sym')) |
| 47 | |
| 48 | def testOfficialUploadURL(self): |
| 49 | """Verify we upload to the real crash server for official builds""" |
| 50 | self._testUploadURL(True, upload_symbols.OFFICIAL_UPLOAD_URL) |
| 51 | |
| 52 | def testUnofficialUploadURL(self): |
| 53 | """Verify we upload to the staging crash server for unofficial builds""" |
| 54 | self._testUploadURL(False, upload_symbols.STAGING_UPLOAD_URL) |
| 55 | |
| 56 | def testUploadSymbolFailureSimple(self): |
| 57 | """Verify that when UploadSymbol fails, the error count is passed up""" |
| 58 | def UploadSymbol(*_args, **kwargs): |
| 59 | kwargs['num_errors'].value = 4 |
| 60 | upload_symbols.UploadSymbol = mock.Mock(side_effect=UploadSymbol) |
| 61 | with parallel_unittest.ParallelMock(): |
| 62 | ret = upload_symbols.UploadSymbols('', breakpad_dir=self.tempdir, sleep=0) |
| 63 | self.assertEquals(ret, 4) |
| 64 | |
| 65 | def testUploadCount(self): |
| 66 | """Verify we can limit the number of uploaded symbols""" |
| 67 | m = upload_symbols.UploadSymbol = mock.Mock(return_value=0) |
| 68 | for c in xrange(3): |
| 69 | m.reset_mock() |
| 70 | with parallel_unittest.ParallelMock(): |
| 71 | ret = upload_symbols.UploadSymbols('', breakpad_dir=self.tempdir, |
| 72 | sleep=0, upload_count=c) |
| 73 | self.assertEquals(ret, 0) |
| 74 | self.assertEqual(m.call_count, c) |
| 75 | |
| 76 | |
| 77 | class UploadSymbolTest(cros_test_lib.MockTempDirTestCase): |
| 78 | |
| 79 | def setUp(self): |
| 80 | self.good_result = cros_build_lib.CommandResult(returncode=0) |
| 81 | self.bad_result = cros_build_lib.CommandResult(returncode=1) |
| 82 | self.excp_result = cros_build_lib.RunCommandError('failed', self.bad_result) |
| 83 | self.sym_file = os.path.join(self.tempdir, 'foo.sym') |
| 84 | self.url = 'http://eatit' |
| 85 | |
| 86 | def testUploadSymbolNormal(self): |
| 87 | """Verify we try to upload on a normal file""" |
| 88 | m = upload_symbols.SymUpload = mock.Mock(return_value=self.good_result) |
| 89 | osutils.Touch(self.sym_file) |
| 90 | ret = upload_symbols.UploadSymbol(self.sym_file, self.url) |
| 91 | self.assertEqual(ret, 0) |
| 92 | m.assert_called_with(self.sym_file, self.url) |
| 93 | self.assertEqual(m.call_count, 1) |
| 94 | |
| 95 | def testUploadSymbolErrorCountExceeded(self): |
| 96 | """Verify that when the error count gets too high, we stop uploading""" |
| 97 | errors = ctypes.c_int(10000) |
| 98 | # Pass in garbage values so that we crash if num_errors isn't handled. |
| 99 | ret = upload_symbols.UploadSymbol(None, None, sleep=None, num_errors=errors) |
| 100 | self.assertEqual(ret, 0) |
| 101 | |
| 102 | def testUploadRetryErrors(self): |
| 103 | """Verify that we retry errors (and eventually give up)""" |
| 104 | m = upload_symbols.SymUpload = mock.Mock(side_effect=self.excp_result) |
| 105 | errors = ctypes.c_int() |
| 106 | ret = upload_symbols.UploadSymbol('/dev/null', self.url, num_errors=errors) |
| 107 | self.assertEqual(ret, 1) |
| 108 | m.assert_called_with('/dev/null', self.url) |
| 109 | self.assertTrue(m.call_count >= upload_symbols.MAX_RETRIES) |
| 110 | |
| 111 | def testTruncateTooBigFiles(self): |
| 112 | """Verify we shrink big files""" |
| 113 | def SymUpload(sym_file, _url): |
| 114 | content = osutils.ReadFile(sym_file) |
| 115 | self.assertEqual(content, 'some junk\n') |
| 116 | return self.good_result |
| 117 | m = upload_symbols.SymUpload = mock.Mock(side_effect=SymUpload) |
| 118 | content = ( |
| 119 | 'STACK CFI 1234', |
| 120 | 'some junk', |
| 121 | 'STACK CFI 1234', |
| 122 | ) |
| 123 | osutils.WriteFile(self.sym_file, '\n'.join(content)) |
| 124 | ret = upload_symbols.UploadSymbol(self.sym_file, self.url, file_limit=1) |
| 125 | self.assertEqual(ret, 0) |
| 126 | self.assertNotEqual(m.call_args[0][1], self.sym_file) |
| 127 | self.assertEqual(m.call_count, 1) |
| 128 | |
| 129 | def testTruncateReallyLargeFiles(self): |
| 130 | """Verify we try to shrink really big files""" |
| 131 | m = upload_symbols.SymUpload = mock.Mock(return_value=self.good_result) |
| 132 | with open(self.sym_file, 'w+b') as f: |
| 133 | f.truncate(upload_symbols.CRASH_SERVER_FILE_LIMIT + 100) |
| 134 | f.seek(0) |
| 135 | f.write('STACK CFI 1234\n\n') |
| 136 | ret = upload_symbols.UploadSymbol(self.sym_file, self.url) |
| 137 | self.assertEqual(ret, 1) |
| 138 | self.assertNotEqual(m.call_args[0][1], self.sym_file) |
| 139 | self.assertEqual(m.call_count, 1) |
| 140 | |
| 141 | |
Mike Frysinger | d5fcb3a | 2013-05-30 21:10:50 -0400 | [diff] [blame] | 142 | if __name__ == '__main__': |
| 143 | # pylint: disable=W0212 |
| 144 | # Set timeouts small so that if the unit test hangs, it won't hang for long. |
| 145 | parallel._BackgroundTask.STARTUP_TIMEOUT = 5 |
| 146 | parallel._BackgroundTask.EXIT_TIMEOUT = 5 |
| 147 | |
| 148 | # We want to test retry behavior, so make sure we don't sleep. |
| 149 | upload_symbols.INITIAL_RETRY_DELAY = 0 |
| 150 | |
| 151 | # Run the tests. |
| 152 | cros_test_lib.main(level=logging.INFO) |