| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 # pylint: disable=W0212 | 5 # pylint: disable=W0212 |
| 6 | 6 |
| 7 """Unit tests for download_from_google_storage.py.""" | 7 """Unit tests for download_from_google_storage.py.""" |
| 8 | 8 |
| 9 import optparse | 9 import optparse |
| 10 import os | 10 import os |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 | 29 |
| 30 class GsutilMock(object): | 30 class GsutilMock(object): |
| 31 def __init__(self, path, boto_path, timeout=None): | 31 def __init__(self, path, boto_path, timeout=None): |
| 32 self.path = path | 32 self.path = path |
| 33 self.timeout = timeout | 33 self.timeout = timeout |
| 34 self.boto_path = boto_path | 34 self.boto_path = boto_path |
| 35 self.expected = [] | 35 self.expected = [] |
| 36 self.history = [] | 36 self.history = [] |
| 37 self.lock = threading.Lock() | 37 self.lock = threading.Lock() |
| 38 | 38 |
| 39 def add_expected(self, return_code, out, err): | 39 def add_expected(self, return_code, out, err, fn=None): |
| 40 self.expected.append((return_code, out, err)) | 40 self.expected.append((return_code, out, err, fn)) |
| 41 | 41 |
| 42 def append_history(self, method, args): | 42 def append_history(self, method, args): |
| 43 self.history.append((method, args)) | 43 self.history.append((method, args)) |
| 44 | 44 |
| 45 def call(self, *args): | 45 def call(self, *args): |
| 46 with self.lock: | 46 with self.lock: |
| 47 self.append_history('call', args) | 47 self.append_history('call', args) |
| 48 if self.expected: | 48 if self.expected: |
| 49 return self.expected.pop(0)[0] | 49 code, _out, _err, fn = self.expected.pop(0) |
| 50 if fn: |
| 51 fn() |
| 52 return code |
| 50 else: | 53 else: |
| 51 return 0 | 54 return 0 |
| 52 | 55 |
| 53 def check_call(self, *args): | 56 def check_call(self, *args): |
| 54 with self.lock: | 57 with self.lock: |
| 55 self.append_history('check_call', args) | 58 self.append_history('check_call', args) |
| 56 if self.expected: | 59 if self.expected: |
| 57 return self.expected.pop(0) | 60 code, out, err, fn = self.expected.pop(0) |
| 61 if fn: |
| 62 fn() |
| 63 return code, out, err |
| 58 else: | 64 else: |
| 59 return (0, '', '') | 65 return (0, '', '') |
| 60 | 66 |
| 61 | 67 |
| 62 class GstoolsUnitTests(unittest.TestCase): | 68 class GstoolsUnitTests(unittest.TestCase): |
| 63 def setUp(self): | 69 def setUp(self): |
| 64 self.temp_dir = tempfile.mkdtemp(prefix='gstools_test') | 70 self.temp_dir = tempfile.mkdtemp(prefix='gstools_test') |
| 65 self.base_path = os.path.join(self.temp_dir, 'test_files') | 71 self.base_path = os.path.join(self.temp_dir, 'test_files') |
| 66 shutil.copytree(os.path.join(TEST_DIR, 'gstools'), self.base_path) | 72 shutil.copytree(os.path.join(TEST_DIR, 'gstools'), self.base_path) |
| 67 | 73 |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 ('cp', input_filename, output_filename)) | 256 ('cp', input_filename, output_filename)) |
| 251 ] | 257 ] |
| 252 if sys.platform != 'win32': | 258 if sys.platform != 'win32': |
| 253 expected_calls.append( | 259 expected_calls.append( |
| 254 ('check_call', | 260 ('check_call', |
| 255 ('stat', | 261 ('stat', |
| 256 'gs://sometesturl/7871c8e24da15bad8b0be2c36edc9dc77e37727f'))) | 262 'gs://sometesturl/7871c8e24da15bad8b0be2c36edc9dc77e37727f'))) |
| 257 self.assertEqual(self.gsutil.history, expected_calls) | 263 self.assertEqual(self.gsutil.history, expected_calls) |
| 258 self.assertEqual(code, 101) | 264 self.assertEqual(code, 101) |
| 259 | 265 |
| 266 def test_corrupt_download(self): |
| 267 q = Queue.Queue() |
| 268 out_q = Queue.Queue() |
| 269 ret_codes = Queue.Queue() |
| 270 tmp_dir = tempfile.mkdtemp() |
| 271 sha1_hash = '7871c8e24da15bad8b0be2c36edc9dc77e37727f' |
| 272 output_filename = os.path.join(tmp_dir, 'lorem_ipsum.txt') |
| 273 q.put(('7871c8e24da15bad8b0be2c36edc9dc77e37727f', output_filename)) |
| 274 q.put((None, None)) |
| 275 def _write_bad_file(): |
| 276 with open(output_filename, 'w') as f: |
| 277 f.write('foobar') |
| 278 self.gsutil.add_expected(0, '', '') |
| 279 self.gsutil.add_expected(0, '', '', _write_bad_file) |
| 280 download_from_google_storage._downloader_worker_thread( |
| 281 1, q, True, self.base_url, self.gsutil, out_q, ret_codes, True) |
| 282 self.assertTrue(q.empty()) |
| 283 msg = ('1> ERROR remote sha1 (%s) does not match expected sha1 (%s).' % |
| 284 ('8843d7f92416211de9ebb963ff4ce28125932878', sha1_hash)) |
| 285 self.assertEquals(out_q.get(), '1> Downloading %s...' % output_filename) |
| 286 self.assertEquals(out_q.get(), msg) |
| 287 self.assertEquals(ret_codes.get(), (20, msg)) |
| 288 self.assertTrue(out_q.empty()) |
| 289 self.assertTrue(ret_codes.empty()) |
| 290 |
| 291 |
| 260 def test_download_directory_no_recursive_non_force(self): | 292 def test_download_directory_no_recursive_non_force(self): |
| 261 sha1_hash = '7871c8e24da15bad8b0be2c36edc9dc77e37727f' | 293 sha1_hash = '7871c8e24da15bad8b0be2c36edc9dc77e37727f' |
| 262 input_filename = '%s/%s' % (self.base_url, sha1_hash) | 294 input_filename = '%s/%s' % (self.base_url, sha1_hash) |
| 263 output_filename = os.path.join(self.base_path, 'uploaded_lorem_ipsum.txt') | 295 output_filename = os.path.join(self.base_path, 'uploaded_lorem_ipsum.txt') |
| 264 code = download_from_google_storage.download_from_google_storage( | 296 code = download_from_google_storage.download_from_google_storage( |
| 265 input_filename=self.base_path, | 297 input_filename=self.base_path, |
| 266 base_url=self.base_url, | 298 base_url=self.base_url, |
| 267 gsutil=self.gsutil, | 299 gsutil=self.gsutil, |
| 268 num_threads=1, | 300 num_threads=1, |
| 269 directory=True, | 301 directory=True, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 283 expected_calls.append( | 315 expected_calls.append( |
| 284 ('check_call', | 316 ('check_call', |
| 285 ('stat', | 317 ('stat', |
| 286 'gs://sometesturl/7871c8e24da15bad8b0be2c36edc9dc77e37727f'))) | 318 'gs://sometesturl/7871c8e24da15bad8b0be2c36edc9dc77e37727f'))) |
| 287 self.assertEqual(self.gsutil.history, expected_calls) | 319 self.assertEqual(self.gsutil.history, expected_calls) |
| 288 self.assertEqual(code, 0) | 320 self.assertEqual(code, 0) |
| 289 | 321 |
| 290 | 322 |
| 291 if __name__ == '__main__': | 323 if __name__ == '__main__': |
| 292 unittest.main() | 324 unittest.main() |
| OLD | NEW |