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 input_filename = '%s/%s' % (self.base_url, sha1_hash) |
| 273 output_filename = os.path.join(tmp_dir, 'lorem_ipsum.txt') |
| 274 q.put(('7871c8e24da15bad8b0be2c36edc9dc77e37727f', output_filename)) |
| 275 q.put((None, None)) |
| 276 def _write_bad_file(): |
| 277 with open(output_filename, 'w') as f: |
| 278 f.write('foobar') |
| 279 self.gsutil.add_expected(0, '', '') |
| 280 self.gsutil.add_expected(0, '', '', _write_bad_file) |
| 281 download_from_google_storage._downloader_worker_thread( |
| 282 1, q, True, self.base_url, self.gsutil, out_q, ret_codes, True) |
| 283 self.assertTrue(q.empty()) |
| 284 msg = ('1> ERROR remote sha1 (%s) does not match expected sha1 (%s).' % |
| 285 ('8843d7f92416211de9ebb963ff4ce28125932878', sha1_hash)) |
| 286 self.assertEquals(out_q.get(), '1> Downloading %s...' % output_filename) |
| 287 self.assertEquals(out_q.get(), msg) |
| 288 self.assertEquals(ret_codes.get(), (20, msg)) |
| 289 self.assertTrue(out_q.empty()) |
| 290 self.assertTrue(ret_codes.empty()) |
| 291 |
| 292 |
260 def test_download_directory_no_recursive_non_force(self): | 293 def test_download_directory_no_recursive_non_force(self): |
261 sha1_hash = '7871c8e24da15bad8b0be2c36edc9dc77e37727f' | 294 sha1_hash = '7871c8e24da15bad8b0be2c36edc9dc77e37727f' |
262 input_filename = '%s/%s' % (self.base_url, sha1_hash) | 295 input_filename = '%s/%s' % (self.base_url, sha1_hash) |
263 output_filename = os.path.join(self.base_path, 'uploaded_lorem_ipsum.txt') | 296 output_filename = os.path.join(self.base_path, 'uploaded_lorem_ipsum.txt') |
264 code = download_from_google_storage.download_from_google_storage( | 297 code = download_from_google_storage.download_from_google_storage( |
265 input_filename=self.base_path, | 298 input_filename=self.base_path, |
266 base_url=self.base_url, | 299 base_url=self.base_url, |
267 gsutil=self.gsutil, | 300 gsutil=self.gsutil, |
268 num_threads=1, | 301 num_threads=1, |
269 directory=True, | 302 directory=True, |
(...skipping 13 matching lines...) Expand all Loading... |
283 expected_calls.append( | 316 expected_calls.append( |
284 ('check_call', | 317 ('check_call', |
285 ('stat', | 318 ('stat', |
286 'gs://sometesturl/7871c8e24da15bad8b0be2c36edc9dc77e37727f'))) | 319 'gs://sometesturl/7871c8e24da15bad8b0be2c36edc9dc77e37727f'))) |
287 self.assertEqual(self.gsutil.history, expected_calls) | 320 self.assertEqual(self.gsutil.history, expected_calls) |
288 self.assertEqual(code, 0) | 321 self.assertEqual(code, 0) |
289 | 322 |
290 | 323 |
291 if __name__ == '__main__': | 324 if __name__ == '__main__': |
292 unittest.main() | 325 unittest.main() |
OLD | NEW |