OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 | 2 |
3 """Tests for gs_utils.py. | 3 """Tests for gs_utils.py. |
4 | 4 |
5 TODO(epoger): How should we exercise these self-tests? See http://skbug.com/2751 | 5 TODO(epoger): How should we exercise these self-tests? See http://skbug.com/2751 |
6 """ | 6 """ |
7 | 7 |
8 # System-level imports. | 8 # System-level imports. |
9 import os | 9 import os |
10 import posixpath | 10 import posixpath |
11 import random | 11 import random |
12 import shutil | 12 import shutil |
13 import sys | 13 import sys |
14 import tempfile | 14 import tempfile |
| 15 import time |
15 | 16 |
16 # Local imports. | 17 # Local imports. |
17 import gs_utils | 18 import gs_utils |
18 | 19 |
19 TEST_BUCKET = 'chromium-skia-testing' | 20 TEST_BUCKET = 'chromium-skia-testing' |
20 | 21 |
21 | 22 |
22 def _get_authenticated_gs_handle(): | 23 def _get_authenticated_gs_handle(): |
23 """Returns an instance of GSUtils using ~/.boto for authentication.""" | 24 """Returns an instance of GSUtils using ~/.boto for authentication.""" |
24 try: | 25 try: |
(...skipping 12 matching lines...) Expand all Loading... |
37 """Returns a unique directory name suitable for use in Google Storage.""" | 38 """Returns a unique directory name suitable for use in Google Storage.""" |
38 return 'gs_utils_manualtest/%d' % random.randint(0, sys.maxint) | 39 return 'gs_utils_manualtest/%d' % random.randint(0, sys.maxint) |
39 | 40 |
40 | 41 |
41 def _test_public_read(): | 42 def _test_public_read(): |
42 """Make sure we can read from public files without .boto file credentials.""" | 43 """Make sure we can read from public files without .boto file credentials.""" |
43 gs = gs_utils.GSUtils() | 44 gs = gs_utils.GSUtils() |
44 gs.list_bucket_contents(bucket=TEST_BUCKET, subdir=None) | 45 gs.list_bucket_contents(bucket=TEST_BUCKET, subdir=None) |
45 | 46 |
46 | 47 |
| 48 def _test_only_if_modified(): |
| 49 """Test only_if_modified param within upload_file().""" |
| 50 gs = _get_authenticated_gs_handle() |
| 51 filename = 'filename' |
| 52 remote_dir = _get_unique_posix_dir() |
| 53 dest_path = posixpath.join(remote_dir, filename) |
| 54 local_dir = tempfile.mkdtemp() |
| 55 try: |
| 56 # Create a file on local disk, and upload it for the first time. |
| 57 local_path = os.path.join(local_dir, filename) |
| 58 with open(local_path, 'w') as f: |
| 59 f.write('original contents') |
| 60 gs.upload_file(source_path=local_path, dest_bucket=TEST_BUCKET, |
| 61 dest_path=dest_path, only_if_modified=True) |
| 62 try: |
| 63 # Re-upload the same file one second later, with only_if_modified=False; |
| 64 # the timestamp should change. |
| 65 old_timestamp = gs.get_last_modified_time( |
| 66 bucket=TEST_BUCKET, path=dest_path) |
| 67 time.sleep(2) |
| 68 gs.upload_file(source_path=local_path, dest_bucket=TEST_BUCKET, |
| 69 dest_path=dest_path, only_if_modified=False) |
| 70 new_timestamp = gs.get_last_modified_time( |
| 71 bucket=TEST_BUCKET, path=dest_path) |
| 72 assert old_timestamp != new_timestamp, '%s != %s' % ( |
| 73 old_timestamp, new_timestamp) |
| 74 |
| 75 # Re-upload the same file one second later, with only_if_modified=True; |
| 76 # the timestamp should NOT change. |
| 77 old_timestamp = new_timestamp |
| 78 time.sleep(2) |
| 79 gs.upload_file(source_path=local_path, dest_bucket=TEST_BUCKET, |
| 80 dest_path=dest_path, only_if_modified=True) |
| 81 new_timestamp = gs.get_last_modified_time( |
| 82 bucket=TEST_BUCKET, path=dest_path) |
| 83 assert old_timestamp == new_timestamp, '%s == %s' % ( |
| 84 old_timestamp, new_timestamp) |
| 85 |
| 86 # MODIFY and re-upload the file one second later, with |
| 87 # only_if_modified=True; the timestamp SHOULD change. |
| 88 old_timestamp = new_timestamp |
| 89 with open(local_path, 'w') as f: |
| 90 f.write('modified contents') |
| 91 time.sleep(2) |
| 92 gs.upload_file(source_path=local_path, dest_bucket=TEST_BUCKET, |
| 93 dest_path=dest_path, only_if_modified=True) |
| 94 new_timestamp = gs.get_last_modified_time( |
| 95 bucket=TEST_BUCKET, path=dest_path) |
| 96 assert old_timestamp != new_timestamp, '%s != %s' % ( |
| 97 old_timestamp, new_timestamp) |
| 98 finally: |
| 99 # Clean up the remote_dir. |
| 100 gs.delete_file(bucket=TEST_BUCKET, path=dest_path) |
| 101 finally: |
| 102 # Clean up the local dir. |
| 103 shutil.rmtree(local_dir) |
| 104 |
47 def _test_authenticated_round_trip(): | 105 def _test_authenticated_round_trip(): |
48 gs = _get_authenticated_gs_handle() | 106 gs = _get_authenticated_gs_handle() |
49 remote_dir = _get_unique_posix_dir() | 107 remote_dir = _get_unique_posix_dir() |
50 subdir = 'subdir' | 108 subdir = 'subdir' |
51 filenames_to_upload = ['file1', 'file2'] | 109 filenames_to_upload = ['file1', 'file2'] |
52 | 110 |
53 # Upload test files to Google Storage, checking that their fine-grained | 111 # Upload test files to Google Storage, checking that their fine-grained |
54 # ACLs were set correctly. | 112 # ACLs were set correctly. |
55 id_type = gs.IdType.GROUP_BY_DOMAIN | 113 id_type = gs.IdType.GROUP_BY_DOMAIN |
56 id_value = 'chromium.org' | 114 id_value = 'chromium.org' |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 assert file_contents == 'contents of %s\n' % filename, ( | 257 assert file_contents == 'contents of %s\n' % filename, ( |
200 '%s == "contents of %s\n"' % (file_contents, filename)) | 258 '%s == "contents of %s\n"' % (file_contents, filename)) |
201 finally: | 259 finally: |
202 shutil.rmtree(local_dest_dir) | 260 shutil.rmtree(local_dest_dir) |
203 for filename in filenames: | 261 for filename in filenames: |
204 gs.delete_file(bucket=TEST_BUCKET, | 262 gs.delete_file(bucket=TEST_BUCKET, |
205 path=posixpath.join(remote_dir, subdir, filename)) | 263 path=posixpath.join(remote_dir, subdir, filename)) |
206 | 264 |
207 | 265 |
208 if __name__ == '__main__': | 266 if __name__ == '__main__': |
| 267 _test_only_if_modified() |
209 _test_public_read() | 268 _test_public_read() |
210 _test_authenticated_round_trip() | 269 _test_authenticated_round_trip() |
211 _test_dir_upload_and_download() | 270 _test_dir_upload_and_download() |
212 # TODO(epoger): Add _test_unauthenticated_access() to make sure we raise | 271 # TODO(epoger): Add _test_unauthenticated_access() to make sure we raise |
213 # an exception when we try to access without needed credentials. | 272 # an exception when we try to access without needed credentials. |
OLD | NEW |