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 | |
| 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') | |
| 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 upload_to_google_storage.upload_to_google_storage( | |
|
M-A Ruel
2013/03/09 12:41:13
Check the return value of the function at all the
Ryan Tseng
2013/03/11 17:35:14
Done.
| |
| 44 filenames, self.base_url, self.gsutil, True, False, 1, False) | |
| 45 self.assertEquals( | |
| 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.assertEquals( | |
| 54 open(output_filename, 'rb').read(), | |
| 55 '7871c8e24da15bad8b0be2c36edc9dc77e37727f') | |
| 56 os.remove(output_filename) | |
| 57 | |
| 58 def test_upload_single_file_remote_exists(self): | |
| 59 filenames = [self.lorem_ipsum] | |
| 60 output_filename = '%s.sha1' % self.lorem_ipsum | |
| 61 etag_string = 'ETag: 634d7c1ed3545383837428f031840a1e' | |
| 62 if os.path.exists(output_filename): | |
| 63 os.remove(output_filename) | |
| 64 self.gsutil.add_expected(0, '', '') | |
| 65 self.gsutil.add_expected(0, etag_string, '') | |
| 66 upload_to_google_storage.upload_to_google_storage( | |
| 67 filenames, self.base_url, self.gsutil, False, False, 1, False) | |
| 68 self.assertEquals( | |
| 69 self.gsutil.history, | |
| 70 [('check_call', | |
| 71 ('ls', '%s/%s' % (self.base_url, self.lorem_ipsum_sha1))), | |
| 72 ('check_call', | |
| 73 ('ls', '-L', '%s/%s' % (self.base_url, self.lorem_ipsum_sha1)))]) | |
| 74 self.assertTrue(os.path.exists(output_filename)) | |
| 75 self.assertEquals( | |
| 76 open(output_filename, 'rb').read(), | |
| 77 '7871c8e24da15bad8b0be2c36edc9dc77e37727f') | |
| 78 os.remove(output_filename) | |
| 79 | |
| 80 def test_upload_worker_errors(self): | |
| 81 work_queue = Queue.Queue() | |
| 82 work_queue.put((self.lorem_ipsum, self.lorem_ipsum_sha1)) | |
| 83 work_queue.put((None, None)) | |
| 84 self.gsutil.add_expected(1, '', '') # For the first ls call. | |
| 85 self.gsutil.add_expected(20, '', 'Expected error message') | |
| 86 # pylint: disable=W0212 | |
| 87 upload_to_google_storage._upload_worker( | |
| 88 0, | |
| 89 work_queue, | |
| 90 self.base_url, | |
| 91 self.gsutil, | |
| 92 threading.Lock(), | |
| 93 False, | |
| 94 False, | |
| 95 self.stdout_queue, | |
| 96 self.ret_codes) | |
| 97 expected_ret_codes = [ | |
| 98 (20, | |
| 99 'Encountered error on uploading %s to %s/%s\nExpected error message' % | |
| 100 (self.lorem_ipsum, self.base_url, self.lorem_ipsum_sha1))] | |
| 101 self.assertEquals(list(self.ret_codes.queue), expected_ret_codes) | |
| 102 | |
| 103 | |
| 104 def test_skip_hashing(self): | |
| 105 filenames = [self.lorem_ipsum] | |
| 106 output_filename = '%s.sha1' % self.lorem_ipsum | |
| 107 fake_hash = '6871c8e24da15bad8b0be2c36edc9dc77e37727f' | |
| 108 with open(output_filename, 'wb') as f: | |
| 109 f.write(fake_hash) # Fake hash. | |
| 110 upload_to_google_storage.upload_to_google_storage( | |
| 111 filenames, self.base_url, self.gsutil, False, False, 1, True) | |
| 112 self.assertEquals( | |
| 113 self.gsutil.history, | |
| 114 [('check_call', | |
| 115 ('ls', '%s/%s' % (self.base_url, fake_hash))), | |
| 116 ('check_call', | |
| 117 ('ls', '-L', '%s/%s' % (self.base_url, fake_hash))), | |
| 118 ('check_call', | |
| 119 ('cp', '-q', filenames[0], '%s/%s' % (self.base_url, fake_hash)))]) | |
| 120 self.assertEquals( | |
| 121 open(output_filename, 'rb').read(), fake_hash) | |
| 122 os.remove(output_filename) | |
| 123 | |
| 124 def test_get_targets_no_args(self): | |
| 125 try: | |
| 126 upload_to_google_storage.get_targets([], self.parser, False) | |
| 127 except SystemExit, e: | |
| 128 self.assertEquals(type(e), type(SystemExit())) | |
| 129 self.assertEquals(e.code, 2) | |
| 130 except Exception, e: | |
| 131 self.fail('unexpected exception: %s' % e) | |
| 132 else: | |
| 133 self.fail('SystemExit exception expected') | |
| 134 | |
| 135 def test_get_targets_passthrough(self): | |
| 136 result = upload_to_google_storage.get_targets( | |
| 137 ['a', 'b', 'c', 'd', 'e'], | |
| 138 self.parser, | |
| 139 False) | |
| 140 self.assertEquals(result, ['a', 'b', 'c', 'd', 'e']) | |
| 141 | |
| 142 def test_get_targets_multiple_stdin(self): | |
| 143 inputs = ['a', 'b', 'c', 'd', 'e'] | |
| 144 sys.stdin = StringIO.StringIO(os.linesep.join(inputs)) | |
| 145 result = upload_to_google_storage.get_targets( | |
| 146 ['-'], | |
| 147 self.parser, | |
| 148 False) | |
| 149 self.assertEquals(result, inputs) | |
| 150 | |
| 151 def test_get_targets_multiple_stdin_null(self): | |
| 152 inputs = ['a', 'b', 'c', 'd', 'e'] | |
| 153 sys.stdin = StringIO.StringIO('\0'.join(inputs)) | |
| 154 result = upload_to_google_storage.get_targets( | |
| 155 ['-'], | |
| 156 self.parser, | |
| 157 True) | |
| 158 self.assertEquals(result, inputs) | |
| 159 | |
| 160 if __name__ == '__main__': | |
| 161 unittest.main() | |
| OLD | NEW |