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 | 5 |
6 """Unit tests for upload_to_google_storage.py.""" | 6 """Unit tests for upload_to_google_storage.py.""" |
7 | 7 |
8 import optparse | 8 import optparse |
9 import os | 9 import os |
10 import Queue | 10 import Queue |
11 import shutil | 11 import shutil |
12 import StringIO | 12 import StringIO |
13 import sys | 13 import sys |
| 14 import tarfile |
14 import tempfile | 15 import tempfile |
15 import threading | 16 import threading |
16 import unittest | 17 import unittest |
17 | 18 |
18 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | 19 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
19 | 20 |
20 import upload_to_google_storage | 21 import upload_to_google_storage |
21 from download_from_google_storage_unittests import GsutilMock | 22 from download_from_google_storage_unittests import GsutilMock |
| 23 from download_from_google_storage_unittests import ChangedWorkingDirectory |
22 | 24 |
23 # ../third_party/gsutil/gsutil | 25 # ../third_party/gsutil/gsutil |
24 GSUTIL_DEFAULT_PATH = os.path.join( | 26 GSUTIL_DEFAULT_PATH = os.path.join( |
25 os.path.dirname(os.path.dirname(os.path.abspath(__file__))), | 27 os.path.dirname(os.path.dirname(os.path.abspath(__file__))), |
26 'third_party', 'gsutil', 'gsutil') | 28 'third_party', 'gsutil', 'gsutil') |
27 TEST_DIR = os.path.dirname(os.path.abspath(__file__)) | 29 TEST_DIR = os.path.dirname(os.path.abspath(__file__)) |
28 | 30 |
29 | 31 |
30 class UploadTests(unittest.TestCase): | 32 class UploadTests(unittest.TestCase): |
31 def setUp(self): | 33 def setUp(self): |
(...skipping 24 matching lines...) Expand all Loading... |
56 ('check_call', | 58 ('check_call', |
57 ('cp', '-z', 'txt', filenames[0], | 59 ('cp', '-z', 'txt', filenames[0], |
58 '%s/%s' % (self.base_url, self.lorem_ipsum_sha1)))]) | 60 '%s/%s' % (self.base_url, self.lorem_ipsum_sha1)))]) |
59 self.assertTrue(os.path.exists(output_filename)) | 61 self.assertTrue(os.path.exists(output_filename)) |
60 self.assertEqual( | 62 self.assertEqual( |
61 open(output_filename, 'rb').read(), | 63 open(output_filename, 'rb').read(), |
62 '7871c8e24da15bad8b0be2c36edc9dc77e37727f') | 64 '7871c8e24da15bad8b0be2c36edc9dc77e37727f') |
63 os.remove(output_filename) | 65 os.remove(output_filename) |
64 self.assertEqual(code, 0) | 66 self.assertEqual(code, 0) |
65 | 67 |
| 68 def test_create_archive(self): |
| 69 work_dir = os.path.join(self.base_path, 'download_test_data') |
| 70 with ChangedWorkingDirectory(work_dir): |
| 71 dirname = 'subfolder' |
| 72 dirs = [dirname] |
| 73 tar_gz_file = '%s.tar.gz' % dirname |
| 74 self.assertTrue(upload_to_google_storage.validate_archive_dirs(dirs)) |
| 75 upload_to_google_storage.create_archives(dirs) |
| 76 self.assertTrue(os.path.exists(tar_gz_file)) |
| 77 with tarfile.open(tar_gz_file, 'r:gz') as tar: |
| 78 content = map(lambda x: x.name, tar.getmembers()) |
| 79 self.assertTrue(dirname in content) |
| 80 self.assertTrue(os.path.join(dirname, 'subfolder_text.txt') in content) |
| 81 self.assertTrue( |
| 82 os.path.join(dirname, 'subfolder_text.txt.sha1') in content) |
| 83 |
| 84 def test_validate_archive_dirs_fails(self): |
| 85 work_dir = os.path.join(self.base_path, 'download_test_data') |
| 86 with ChangedWorkingDirectory(work_dir): |
| 87 symlink = 'link' |
| 88 os.symlink(os.path.join(self.base_path, 'subfolder'), symlink) |
| 89 self.assertFalse(upload_to_google_storage.validate_archive_dirs([symlink])) |
| 90 self.assertFalse(upload_to_google_storage.validate_archive_dirs(['foobar'])) |
| 91 |
66 def test_upload_single_file_remote_exists(self): | 92 def test_upload_single_file_remote_exists(self): |
67 filenames = [self.lorem_ipsum] | 93 filenames = [self.lorem_ipsum] |
68 output_filename = '%s.sha1' % self.lorem_ipsum | 94 output_filename = '%s.sha1' % self.lorem_ipsum |
69 etag_string = 'ETag: 634d7c1ed3545383837428f031840a1e' | 95 etag_string = 'ETag: 634d7c1ed3545383837428f031840a1e' |
70 self.gsutil.add_expected(0, '', '') | 96 self.gsutil.add_expected(0, '', '') |
71 self.gsutil.add_expected(0, etag_string, '') | 97 self.gsutil.add_expected(0, etag_string, '') |
72 code = upload_to_google_storage.upload_to_google_storage( | 98 code = upload_to_google_storage.upload_to_google_storage( |
73 filenames, self.base_url, self.gsutil, False, False, 1, False, None) | 99 filenames, self.base_url, self.gsutil, False, False, 1, False, None) |
74 self.assertEqual( | 100 self.assertEqual( |
75 self.gsutil.history, | 101 self.gsutil.history, |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 sys.stdin = StringIO.StringIO('\0'.join(inputs)) | 183 sys.stdin = StringIO.StringIO('\0'.join(inputs)) |
158 result = upload_to_google_storage.get_targets( | 184 result = upload_to_google_storage.get_targets( |
159 ['-'], | 185 ['-'], |
160 self.parser, | 186 self.parser, |
161 True) | 187 True) |
162 self.assertEqual(result, inputs) | 188 self.assertEqual(result, inputs) |
163 | 189 |
164 | 190 |
165 if __name__ == '__main__': | 191 if __name__ == '__main__': |
166 unittest.main() | 192 unittest.main() |
OLD | NEW |