Chromium Code Reviews| 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 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 32 into gs://%s? | 32 into gs://%s? |
| 33 """ % TEST_BUCKET | 33 """ % TEST_BUCKET |
| 34 raise | 34 raise |
| 35 | 35 |
| 36 | 36 |
| 37 def _get_unique_posix_dir(): | 37 def _get_unique_posix_dir(): |
| 38 """Returns a unique directory name suitable for use in Google Storage.""" | 38 """Returns a unique directory name suitable for use in Google Storage.""" |
| 39 return 'gs_utils_manualtest/%d' % random.randint(0, sys.maxint) | 39 return 'gs_utils_manualtest/%d' % random.randint(0, sys.maxint) |
| 40 | 40 |
| 41 | 41 |
| 42 def _test_static_methods(): | |
| 43 """Test all static methods.""" | |
| 44 gs = gs_utils.GSUtils | |
| 45 | |
| 46 # (input url, output bucket, output path) for each test case | |
| 47 testcases = [ | |
| 48 (None, None, None), | |
| 49 (5, None, None), | |
| 50 ('', None, None), | |
| 51 ('/one/two', None, None), | |
| 52 ('http://one/two', None, None), | |
| 53 ('gs:', None, None), | |
| 54 ('gs://', None, None), | |
| 55 ('gs:///', None, None), | |
| 56 ('gs://???', None, None), | |
| 57 ('gs:///bucket', None, None), | |
| 58 ('gs://bucket', 'bucket', ''), | |
| 59 ('gs://bucket/', 'bucket', ''), | |
| 60 ('gs://bucket/path1', 'bucket', 'path1'), | |
|
rmistry
2014/07/29 20:36:20
Could you also please add gs://bucket/path1/ and m
epoger
2014/07/30 01:10:49
Done.
| |
| 61 ('gs://bucket/path1/path2', 'bucket', 'path1/path2'), | |
|
rmistry
2014/07/29 20:36:20
Optional Nit: Instead of aligning some of these le
epoger
2014/07/30 01:10:49
Yeah, that makes sense, and the style guide agrees
| |
| 62 ] | |
| 63 for (url, bucket, path) in testcases: | |
| 64 is_legal_url = (bucket != None) | |
| 65 assert gs.is_gs_url(url) == is_legal_url, 'gs.is_gs_url("%s") == %s' % ( | |
| 66 url, is_legal_url) | |
| 67 if is_legal_url: | |
| 68 assert gs.split_gs_url(url) == (bucket, path), ( | |
| 69 'gs.split_gs_url("%s") == ("%s", "%s")' % (url, bucket, path)) | |
| 70 | |
| 71 | |
| 42 def _test_public_read(): | 72 def _test_public_read(): |
| 43 """Make sure we can read from public files without .boto file credentials.""" | 73 """Make sure we can read from public files without .boto file credentials.""" |
| 44 gs = gs_utils.GSUtils() | 74 gs = gs_utils.GSUtils() |
| 45 gs.list_bucket_contents(bucket=TEST_BUCKET, subdir=None) | 75 gs.list_bucket_contents(bucket=TEST_BUCKET, subdir=None) |
| 46 | 76 |
| 47 | 77 |
| 48 def _test_upload_if_one_file(): | 78 def _test_upload_if_one_file(): |
| 49 """Test upload_if param within upload_file().""" | 79 """Test upload_if param within upload_file().""" |
| 50 gs = _get_authenticated_gs_handle() | 80 gs = _get_authenticated_gs_handle() |
| 51 filename = 'filename' | 81 filename = 'filename' |
| (...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 348 assert file_contents == 'contents of %s\n' % filename, ( | 378 assert file_contents == 'contents of %s\n' % filename, ( |
| 349 '%s == "contents of %s\n"' % (file_contents, filename)) | 379 '%s == "contents of %s\n"' % (file_contents, filename)) |
| 350 finally: | 380 finally: |
| 351 shutil.rmtree(local_dest_dir) | 381 shutil.rmtree(local_dest_dir) |
| 352 for filename in filenames: | 382 for filename in filenames: |
| 353 gs.delete_file(bucket=TEST_BUCKET, | 383 gs.delete_file(bucket=TEST_BUCKET, |
| 354 path=posixpath.join(remote_dir, subdir, filename)) | 384 path=posixpath.join(remote_dir, subdir, filename)) |
| 355 | 385 |
| 356 | 386 |
| 357 if __name__ == '__main__': | 387 if __name__ == '__main__': |
| 388 _test_static_methods() | |
| 358 _test_upload_if_multiple_files() | 389 _test_upload_if_multiple_files() |
| 359 _test_upload_if_one_file() | 390 _test_upload_if_one_file() |
| 360 _test_public_read() | 391 _test_public_read() |
| 361 _test_authenticated_round_trip() | 392 _test_authenticated_round_trip() |
| 362 _test_dir_upload_and_download() | 393 _test_dir_upload_and_download() |
| 363 # TODO(epoger): Add _test_unauthenticated_access() to make sure we raise | 394 # TODO(epoger): Add _test_unauthenticated_access() to make sure we raise |
| 364 # an exception when we try to access without needed credentials. | 395 # an exception when we try to access without needed credentials. |
| OLD | NEW |