Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2012 The Chromium 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 """Unit tests for upload_to_google_storage.py.""" | |
| 7 | |
| 8 import os | |
| 9 import sys | |
| 10 import unittest | |
| 11 import threading | |
| 12 import StringIO | |
| 13 import Queue | |
| 14 import optparse | |
| 15 | |
| 16 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| 17 | |
| 18 import upload_to_google_storage | |
| 19 from download_from_google_storage_unittests import GsutilMock | |
| 20 | |
| 21 # ../third_party/gsutil/gsutil | |
| 22 GSUTIL_DEFAULT_PATH = os.path.join( | |
| 23 os.path.dirname(os.path.dirname(os.path.abspath(__file__))), | |
| 24 'third_party', 'gsutil', 'gsutil') | |
| 25 | |
|
M-A Ruel
2013/03/11 22:48:58
2 lines
Ryan Tseng
2013/03/11 23:14:20
Done.
| |
| 26 class UploadTests(unittest.TestCase): | |
| 27 def setUp(self): | |
| 28 self.gsutil = GsutilMock(GSUTIL_DEFAULT_PATH) | |
| 29 self.base_path = os.path.join( | |
| 30 os.path.dirname(os.path.abspath(__file__)), 'gstools') | |
|
M-A Ruel
2013/03/11 22:48:58
Use a global variable, otherwise this call could b
Ryan Tseng
2013/03/11 23:14:20
Done.
| |
| 31 self.base_url = 'gs://sometesturl' | |
| 32 self.parser = optparse.OptionParser() | |
| 33 self.ret_codes = Queue.Queue() | |
| 34 self.stdout_queue = Queue.Queue() | |
| 35 self.lorem_ipsum = os.path.join(self.base_path, 'lorem_ipsum.txt') | |
| 36 self.lorem_ipsum_sha1 = '7871c8e24da15bad8b0be2c36edc9dc77e37727f' | |
| 37 | |
| 38 def test_upload_single_file(self): | |
| 39 filenames = [self.lorem_ipsum] | |
| 40 output_filename = '%s.sha1' % self.lorem_ipsum | |
| 41 if os.path.exists(output_filename): | |
| 42 os.remove(output_filename) | |
| 43 code = upload_to_google_storage.upload_to_google_storage( | |
| 44 filenames, self.base_url, self.gsutil, True, False, 1, False) | |
| 45 self.assertEqual( | |
| 46 self.gsutil.history, | |
| 47 [('check_call', | |
| 48 ('ls', '%s/%s' % (self.base_url, self.lorem_ipsum_sha1))), | |
| 49 ('check_call', | |
| 50 ('cp', '-q', filenames[0], '%s/%s' % (self.base_url, | |
| 51 self.lorem_ipsum_sha1)))]) | |
| 52 self.assertTrue(os.path.exists(output_filename)) | |
| 53 self.assertEqual( | |
| 54 open(output_filename, 'rb').read(), | |
| 55 '7871c8e24da15bad8b0be2c36edc9dc77e37727f') | |
| 56 os.remove(output_filename) | |
| 57 self.assertEqual(code, 0) | |
| 58 | |
| 59 def test_upload_single_file_remote_exists(self): | |
| 60 filenames = [self.lorem_ipsum] | |
| 61 output_filename = '%s.sha1' % self.lorem_ipsum | |
| 62 etag_string = 'ETag: 634d7c1ed3545383837428f031840a1e' | |
| 63 if os.path.exists(output_filename): | |
|
M-A Ruel
2013/03/11 22:48:58
This is not necessary.
Ryan Tseng
2013/03/11 23:14:20
Removed.
| |
| 64 os.remove(output_filename) | |
| 65 self.gsutil.add_expected(0, '', '') | |
| 66 self.gsutil.add_expected(0, etag_string, '') | |
| 67 code = upload_to_google_storage.upload_to_google_storage( | |
| 68 filenames, self.base_url, self.gsutil, False, False, 1, False) | |
| 69 self.assertEqual( | |
| 70 self.gsutil.history, | |
| 71 [('check_call', | |
| 72 ('ls', '%s/%s' % (self.base_url, self.lorem_ipsum_sha1))), | |
| 73 ('check_call', | |
| 74 ('ls', '-L', '%s/%s' % (self.base_url, self.lorem_ipsum_sha1)))]) | |
| 75 self.assertTrue(os.path.exists(output_filename)) | |
| 76 self.assertEqual( | |
| 77 open(output_filename, 'rb').read(), | |
| 78 '7871c8e24da15bad8b0be2c36edc9dc77e37727f') | |
| 79 os.remove(output_filename) | |
| 80 self.assertEqual(code, 0) | |
| 81 | |
| 82 def test_upload_worker_errors(self): | |
| 83 work_queue = Queue.Queue() | |
| 84 work_queue.put((self.lorem_ipsum, self.lorem_ipsum_sha1)) | |
| 85 work_queue.put((None, None)) | |
| 86 self.gsutil.add_expected(1, '', '') # For the first ls call. | |
| 87 self.gsutil.add_expected(20, '', 'Expected error message') | |
| 88 # pylint: disable=W0212 | |
| 89 upload_to_google_storage._upload_worker( | |
| 90 0, | |
| 91 work_queue, | |
| 92 self.base_url, | |
| 93 self.gsutil, | |
| 94 threading.Lock(), | |
| 95 False, | |
| 96 False, | |
| 97 self.stdout_queue, | |
| 98 self.ret_codes) | |
| 99 expected_ret_codes = [ | |
| 100 (20, | |
| 101 'Encountered error on uploading %s to %s/%s\nExpected error message' % | |
| 102 (self.lorem_ipsum, self.base_url, self.lorem_ipsum_sha1))] | |
| 103 self.assertEqual(list(self.ret_codes.queue), expected_ret_codes) | |
| 104 | |
| 105 | |
|
M-A Ruel
2013/03/11 22:48:58
one line
Ryan Tseng
2013/03/11 23:14:20
Done.
| |
| 106 def test_skip_hashing(self): | |
| 107 filenames = [self.lorem_ipsum] | |
| 108 output_filename = '%s.sha1' % self.lorem_ipsum | |
| 109 fake_hash = '6871c8e24da15bad8b0be2c36edc9dc77e37727f' | |
| 110 with open(output_filename, 'wb') as f: | |
| 111 f.write(fake_hash) # Fake hash. | |
| 112 code = upload_to_google_storage.upload_to_google_storage( | |
| 113 filenames, self.base_url, self.gsutil, False, False, 1, True) | |
| 114 self.assertEqual( | |
| 115 self.gsutil.history, | |
| 116 [('check_call', | |
| 117 ('ls', '%s/%s' % (self.base_url, fake_hash))), | |
| 118 ('check_call', | |
| 119 ('ls', '-L', '%s/%s' % (self.base_url, fake_hash))), | |
| 120 ('check_call', | |
| 121 ('cp', '-q', filenames[0], '%s/%s' % (self.base_url, fake_hash)))]) | |
| 122 self.assertEqual( | |
| 123 open(output_filename, 'rb').read(), fake_hash) | |
| 124 os.remove(output_filename) | |
| 125 self.assertEqual(code, 0) | |
| 126 | |
| 127 def test_get_targets_no_args(self): | |
| 128 try: | |
| 129 upload_to_google_storage.get_targets([], self.parser, False) | |
| 130 self.fail() | |
| 131 except SystemExit, e: | |
| 132 self.assertEqual(e.code, 2) | |
| 133 | |
| 134 def test_get_targets_passthrough(self): | |
| 135 result = upload_to_google_storage.get_targets( | |
| 136 ['a', 'b', 'c', 'd', 'e'], | |
| 137 self.parser, | |
| 138 False) | |
| 139 self.assertEqual(result, ['a', 'b', 'c', 'd', 'e']) | |
| 140 | |
| 141 def test_get_targets_multiple_stdin(self): | |
| 142 inputs = ['a', 'b', 'c', 'd', 'e'] | |
| 143 sys.stdin = StringIO.StringIO(os.linesep.join(inputs)) | |
| 144 result = upload_to_google_storage.get_targets( | |
| 145 ['-'], | |
| 146 self.parser, | |
| 147 False) | |
| 148 self.assertEqual(result, inputs) | |
| 149 | |
| 150 def test_get_targets_multiple_stdin_null(self): | |
| 151 inputs = ['a', 'b', 'c', 'd', 'e'] | |
| 152 sys.stdin = StringIO.StringIO('\0'.join(inputs)) | |
|
M-A Ruel
2013/03/11 22:48:58
Note that this could affect the following test cas
Ryan Tseng
2013/03/11 23:14:20
Added sys.stdin = sys.__stdin__ in cleanUp().
| |
| 153 result = upload_to_google_storage.get_targets( | |
| 154 ['-'], | |
| 155 self.parser, | |
| 156 True) | |
| 157 self.assertEqual(result, inputs) | |
| 158 | |
|
M-A Ruel
2013/03/11 22:48:58
2 lines. When I say it in one file, I won't necess
Ryan Tseng
2013/03/11 23:14:20
Done.
| |
| 159 if __name__ == '__main__': | |
| 160 unittest.main() | |
| OLD | NEW |