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 | |
15 import tempfile | 14 import tempfile |
16 import threading | 15 import threading |
17 import unittest | 16 import unittest |
18 | 17 |
19 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | 18 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
20 | 19 |
21 import upload_to_google_storage | 20 import upload_to_google_storage |
22 from download_from_google_storage_unittests import GsutilMock | 21 from download_from_google_storage_unittests import GsutilMock |
23 from download_from_google_storage_unittests import ChangedWorkingDirectory | |
24 | 22 |
25 # ../third_party/gsutil/gsutil | 23 # ../third_party/gsutil/gsutil |
26 GSUTIL_DEFAULT_PATH = os.path.join( | 24 GSUTIL_DEFAULT_PATH = os.path.join( |
27 os.path.dirname(os.path.dirname(os.path.abspath(__file__))), | 25 os.path.dirname(os.path.dirname(os.path.abspath(__file__))), |
28 'third_party', 'gsutil', 'gsutil') | 26 'third_party', 'gsutil', 'gsutil') |
29 TEST_DIR = os.path.dirname(os.path.abspath(__file__)) | 27 TEST_DIR = os.path.dirname(os.path.abspath(__file__)) |
30 | 28 |
31 | 29 |
32 class UploadTests(unittest.TestCase): | 30 class UploadTests(unittest.TestCase): |
33 def setUp(self): | 31 def setUp(self): |
(...skipping 24 matching lines...) Expand all Loading... |
58 ('check_call', | 56 ('check_call', |
59 ('cp', '-z', 'txt', filenames[0], | 57 ('cp', '-z', 'txt', filenames[0], |
60 '%s/%s' % (self.base_url, self.lorem_ipsum_sha1)))]) | 58 '%s/%s' % (self.base_url, self.lorem_ipsum_sha1)))]) |
61 self.assertTrue(os.path.exists(output_filename)) | 59 self.assertTrue(os.path.exists(output_filename)) |
62 self.assertEqual( | 60 self.assertEqual( |
63 open(output_filename, 'rb').read(), | 61 open(output_filename, 'rb').read(), |
64 '7871c8e24da15bad8b0be2c36edc9dc77e37727f') | 62 '7871c8e24da15bad8b0be2c36edc9dc77e37727f') |
65 os.remove(output_filename) | 63 os.remove(output_filename) |
66 self.assertEqual(code, 0) | 64 self.assertEqual(code, 0) |
67 | 65 |
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 | |
92 def test_upload_single_file_remote_exists(self): | 66 def test_upload_single_file_remote_exists(self): |
93 filenames = [self.lorem_ipsum] | 67 filenames = [self.lorem_ipsum] |
94 output_filename = '%s.sha1' % self.lorem_ipsum | 68 output_filename = '%s.sha1' % self.lorem_ipsum |
95 etag_string = 'ETag: 634d7c1ed3545383837428f031840a1e' | 69 etag_string = 'ETag: 634d7c1ed3545383837428f031840a1e' |
96 self.gsutil.add_expected(0, '', '') | 70 self.gsutil.add_expected(0, '', '') |
97 self.gsutil.add_expected(0, etag_string, '') | 71 self.gsutil.add_expected(0, etag_string, '') |
98 code = upload_to_google_storage.upload_to_google_storage( | 72 code = upload_to_google_storage.upload_to_google_storage( |
99 filenames, self.base_url, self.gsutil, False, False, 1, False, None) | 73 filenames, self.base_url, self.gsutil, False, False, 1, False, None) |
100 self.assertEqual( | 74 self.assertEqual( |
101 self.gsutil.history, | 75 self.gsutil.history, |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 sys.stdin = StringIO.StringIO('\0'.join(inputs)) | 157 sys.stdin = StringIO.StringIO('\0'.join(inputs)) |
184 result = upload_to_google_storage.get_targets( | 158 result = upload_to_google_storage.get_targets( |
185 ['-'], | 159 ['-'], |
186 self.parser, | 160 self.parser, |
187 True) | 161 True) |
188 self.assertEqual(result, inputs) | 162 self.assertEqual(result, inputs) |
189 | 163 |
190 | 164 |
191 if __name__ == '__main__': | 165 if __name__ == '__main__': |
192 unittest.main() | 166 unittest.main() |
OLD | NEW |