| Index: third_party/gsutil/gslib/test_wildcard_iterator.py
|
| diff --git a/third_party/gsutil/20110627/gslib/test_wildcard_iterator.py b/third_party/gsutil/gslib/test_wildcard_iterator.py
|
| similarity index 63%
|
| rename from third_party/gsutil/20110627/gslib/test_wildcard_iterator.py
|
| rename to third_party/gsutil/gslib/test_wildcard_iterator.py
|
| index 18cc618744e4a0faedbbd49e88d17da133fe38bc..af36952e1f79d38764802204cb0ac387a2ee60bf 100755
|
| --- a/third_party/gsutil/20110627/gslib/test_wildcard_iterator.py
|
| +++ b/third_party/gsutil/gslib/test_wildcard_iterator.py
|
| @@ -37,7 +37,9 @@ sys.path.insert(0, 'boto')
|
| from boto import InvalidUriError
|
| from gslib import test_util
|
| from gslib import wildcard_iterator
|
| -from wildcard_iterator import ResultType
|
| +from gslib.project_id import ProjectIdHandler
|
| +from tests.s3 import mock_storage_service
|
| +from wildcard_iterator import ContainsWildcard
|
|
|
|
|
| class CloudWildcardIteratorTests(unittest.TestCase):
|
| @@ -48,8 +50,10 @@ class CloudWildcardIteratorTests(unittest.TestCase):
|
|
|
| @classmethod
|
| def SetUpClass(cls):
|
| - """Creates 2 mock buckets, each containing 3 objects"""
|
| -
|
| + """Creates 2 mock buckets, each containing 4 objects, including 1 nested."""
|
| + cls.immed_child_obj_names = ['abcd', 'abdd', 'ade$']
|
| + cls.all_obj_names = ['abcd', 'abdd', 'ade$', 'nested1/nested2/xyz1',
|
| + 'nested1/nested2/xyz2']
|
| cls.base_uri_str = 'gs://gslib_test_%d' % int(time.time())
|
| cls.test_bucket0_uri, cls.test_bucket0_obj_uri_strs = (
|
| cls.__SetUpOneMockBucket(0)
|
| @@ -61,20 +65,18 @@ class CloudWildcardIteratorTests(unittest.TestCase):
|
|
|
| @classmethod
|
| def __SetUpOneMockBucket(cls, bucket_num):
|
| - """Creates a mock bucket containing 3 objects.
|
| -
|
| + """Creates a mock bucket containing 4 objects, including 1 nested.
|
| Args:
|
| - bucket_num: number for building bucket name.
|
| + bucket_num: Number for building bucket name.
|
|
|
| Returns:
|
| - tuple: (bucket name, set of object URI strings)
|
| + tuple: (bucket name, set of object URI strings).
|
| """
|
| -
|
| bucket_uri = test_util.test_storage_uri(
|
| '%s_%s' % (cls.base_uri_str, bucket_num))
|
| bucket_uri.create_bucket()
|
| obj_uri_strs = set()
|
| - for obj_name in ['abcd', 'abdd', 'ade$']:
|
| + for obj_name in cls.all_obj_names:
|
| obj_uri = test_util.test_storage_uri('%s%s' % (bucket_uri, obj_name))
|
| key = obj_uri.new_key()
|
| key.set_contents_from_string('')
|
| @@ -84,7 +86,6 @@ class CloudWildcardIteratorTests(unittest.TestCase):
|
| @classmethod
|
| def TearDownClass(cls):
|
| """Cleans up bucket and objects created by SetUpClass"""
|
| -
|
| if hasattr(cls, 'created_test_data'):
|
| for test_obj_uri_str in cls.test_bucket0_obj_uri_strs:
|
| test_util.test_storage_uri(test_obj_uri_str).delete_key()
|
| @@ -95,128 +96,156 @@ class CloudWildcardIteratorTests(unittest.TestCase):
|
|
|
| def TestNoOpObjectIterator(self):
|
| """Tests that bucket-only URI iterates just that one URI"""
|
| -
|
| - results = list(test_util.test_wildcard_iterator(self.test_bucket0_uri,
|
| - ResultType.URIS))
|
| + results = list(
|
| + test_util.test_wildcard_iterator(self.test_bucket0_uri).IterUris())
|
| self.assertEqual(1, len(results))
|
| self.assertEqual(str(self.test_bucket0_uri), str(results[0]))
|
|
|
| def TestMatchingAllObjects(self):
|
| """Tests matching all objects, based on wildcard"""
|
| -
|
| actual_obj_uri_strs = set(
|
| str(u) for u in test_util.test_wildcard_iterator(
|
| - self.test_bucket0_uri.clone_replace_name('*'), ResultType.URIS))
|
| + self.test_bucket0_uri.clone_replace_name('**')).IterUris())
|
| self.assertEqual(self.test_bucket0_obj_uri_strs, actual_obj_uri_strs)
|
|
|
| def TestMatchingObjectSubset(self):
|
| """Tests matching a subset of objects, based on wildcard"""
|
| -
|
| exp_obj_uri_strs = set(
|
| [str(self.test_bucket0_uri.clone_replace_name('abcd')),
|
| str(self.test_bucket0_uri.clone_replace_name('abdd'))])
|
| actual_obj_uri_strs = set(
|
| str(u) for u in test_util.test_wildcard_iterator(
|
| - self.test_bucket0_uri.clone_replace_name('ab??'), ResultType.URIS))
|
| + self.test_bucket0_uri.clone_replace_name('ab??')).IterUris())
|
| self.assertEqual(exp_obj_uri_strs, actual_obj_uri_strs)
|
|
|
| def TestMatchingNonWildcardedUri(self):
|
| """Tests matching a single named object"""
|
| -
|
| exp_obj_uri_strs = set([str(self.test_bucket0_uri.clone_replace_name('abcd')
|
| )])
|
| actual_obj_uri_strs = set(
|
| str(u) for u in test_util.test_wildcard_iterator(
|
| - self.test_bucket0_uri.clone_replace_name('abcd'), ResultType.URIS))
|
| + self.test_bucket0_uri.clone_replace_name('abcd')).IterUris())
|
| self.assertEqual(exp_obj_uri_strs, actual_obj_uri_strs)
|
|
|
| def TestWildcardedObjectUriWithVsWithoutPrefix(self):
|
| """Tests that wildcarding w/ and w/o server prefix get same result"""
|
| -
|
| + # (It's just more efficient to query w/o a prefix; wildcard
|
| + # iterator will filter the matches either way.)
|
| with_prefix_uri_strs = set(
|
| str(u) for u in test_util.test_wildcard_iterator(
|
| - self.test_bucket0_uri.clone_replace_name('abcd'), ResultType.URIS))
|
| + self.test_bucket0_uri.clone_replace_name('abcd')).IterUris())
|
| # By including a wildcard at the start of the string no prefix can be
|
| # used in server request.
|
| no_prefix_uri_strs = set(
|
| str(u) for u in test_util.test_wildcard_iterator(
|
| - self.test_bucket0_uri.clone_replace_name('?bcd'), ResultType.URIS))
|
| + self.test_bucket0_uri.clone_replace_name('?bcd')).IterUris())
|
| self.assertEqual(with_prefix_uri_strs, no_prefix_uri_strs)
|
|
|
| - def TestNoMatchingWildcardedObjectUri(self):
|
| - """Tests that we raise an exception for non-matching wildcarded URI"""
|
| + def TestWildcardedObjectUriNestedSubdirMatch(self):
|
| + """Tests wildcarding with a nested subdir"""
|
| + uri_strs = set()
|
| + prefixes = set()
|
| + for blr in test_util.test_wildcard_iterator(
|
| + self.test_bucket0_uri.clone_replace_name('*')):
|
| + if blr.HasPrefix():
|
| + prefixes.add(blr.GetPrefix().name)
|
| + else:
|
| + uri_strs.add(blr.GetUri().uri)
|
| + exp_obj_uri_strs = set(['%s_0/%s' % (self.base_uri_str, x)
|
| + for x in self.immed_child_obj_names])
|
| + self.assertEqual(exp_obj_uri_strs, uri_strs)
|
| + self.assertEqual(1, len(prefixes))
|
| + self.assertTrue('nested1/' in prefixes)
|
| +
|
| + def TestWildcardedObjectUriNestedSubSubdirMatch(self):
|
| + """Tests wildcarding with a nested sub-subdir"""
|
| + for final_char in ('', '/'):
|
| + uri_strs = set()
|
| + prefixes = set()
|
| + for blr in test_util.test_wildcard_iterator(
|
| + self.test_bucket0_uri.clone_replace_name('nested1/*%s' % final_char)):
|
| + if blr.HasPrefix():
|
| + prefixes.add(blr.GetPrefix().name)
|
| + else:
|
| + uri_strs.add(blr.GetUri().uri)
|
| + self.assertEqual(0, len(uri_strs))
|
| + self.assertEqual(1, len(prefixes))
|
| + self.assertTrue('nested1/nested2/' in prefixes)
|
|
|
| - try:
|
| - for unused_ in test_util.test_wildcard_iterator(
|
| - self.test_bucket0_uri.clone_replace_name('*x0'), ResultType.URIS):
|
| - self.fail('Expected WildcardException not raised.')
|
| - except wildcard_iterator.WildcardException, e:
|
| - # Expected behavior.
|
| - self.assertTrue(str(e).find('No matches') != -1)
|
| + def TestNoMatchingWildcardedObjectUri(self):
|
| + """Tests that get back an empty iterator for non-matching wildcarded URI"""
|
| + res = list(test_util.test_wildcard_iterator(
|
| + self.test_bucket0_uri.clone_replace_name('*x0')).IterUris())
|
| + self.assertEqual(0, len(res))
|
|
|
| def TestWildcardedInvalidObjectUri(self):
|
| """Tests that we raise an exception for wildcarded invalid URI"""
|
| -
|
| try:
|
| for unused_ in test_util.test_wildcard_iterator(
|
| - 'badscheme://asdf', ResultType.URIS):
|
| + 'badscheme://asdf').IterUris():
|
| self.assertFalse('Expected InvalidUriError not raised.')
|
| except InvalidUriError, e:
|
| # Expected behavior.
|
| self.assertTrue(e.message.find('Unrecognized scheme') != -1)
|
|
|
| - def TestWildcardedInvalidResultType(self):
|
| - """Tests that we raise an exception for wildcard with invalid ResultType"""
|
| -
|
| - try:
|
| - test_util.test_wildcard_iterator('gs://asdf/*', 'invalid')
|
| - self.fail('Expected WildcardException not raised.')
|
| - except wildcard_iterator.WildcardException, e:
|
| - # Expected behavior.
|
| - self.assertTrue(str(e).find('Invalid ResultType') != -1)
|
| -
|
| def TestSingleMatchWildcardedBucketUri(self):
|
| """Tests matching a single bucket based on a wildcarded bucket URI"""
|
| -
|
| exp_obj_uri_strs = set(['%s_1/' % self.base_uri_str])
|
| actual_obj_uri_strs = set(
|
| - str(u) for u in test_util.test_wildcard_iterator('%s*1' %
|
| - self.base_uri_str,
|
| - ResultType.URIS))
|
| + str(u) for u in test_util.test_wildcard_iterator(
|
| + '%s*1' % self.base_uri_str).IterUris())
|
| self.assertEqual(exp_obj_uri_strs, actual_obj_uri_strs)
|
|
|
| def TestMultiMatchWildcardedBucketUri(self):
|
| """Tests matching a multiple buckets based on a wildcarded bucket URI"""
|
| -
|
| exp_obj_uri_strs = set(['%s_%s/' %
|
| (self.base_uri_str, i) for i in range(2)])
|
| actual_obj_uri_strs = set(
|
| - str(u) for u in test_util.test_wildcard_iterator('%s*' %
|
| - self.base_uri_str,
|
| - ResultType.URIS))
|
| + str(u) for u in test_util.test_wildcard_iterator(
|
| + '%s*' % self.base_uri_str).IterUris())
|
| self.assertEqual(exp_obj_uri_strs, actual_obj_uri_strs)
|
|
|
| - def TestMultiLevelWildcardUri(self):
|
| + def TestWildcardBucketAndObjectUri(self):
|
| """Tests matching with both bucket and object wildcards"""
|
| + exp_obj_uri_strs = set([str(self.test_bucket0_uri.clone_replace_name(
|
| + 'abcd'))])
|
| + actual_obj_uri_strs = set(
|
| + str(u) for u in test_util.test_wildcard_iterator(
|
| + '%s_0*/abc*' % self.base_uri_str).IterUris())
|
| + self.assertEqual(exp_obj_uri_strs, actual_obj_uri_strs)
|
|
|
| - exp_obj_uri_strs = set([str(self.test_bucket0_uri.clone_replace_name('abcd'
|
| - ))])
|
| + def TestWildcardUpToFinalCharSubdirPlusObjectName(self):
|
| + """Tests wildcard subd*r/obj name"""
|
| + exp_obj_uri_strs = set([str(self.test_bucket0_uri.clone_replace_name(
|
| + 'nested1/nested2/xyz1'))])
|
| + x=list(test_util.test_wildcard_iterator(
|
| + '%s**' % self.test_bucket0_uri.uri).IterUris())
|
| actual_obj_uri_strs = set(
|
| - str(u) for u in test_util.test_wildcard_iterator('%s_0*/abc*' %
|
| - self.base_uri_str,
|
| - ResultType.URIS))
|
| + str(u) for u in test_util.test_wildcard_iterator(
|
| + '%snested1/nest*2/xyz1' % self.test_bucket0_uri.uri).IterUris())
|
| self.assertEqual(exp_obj_uri_strs, actual_obj_uri_strs)
|
|
|
| - def TestBucketOnlyWildcardWithResultTypeKeys(self):
|
| - """Tests that bucket-only wildcard with ResultType.KEYS raises exception"""
|
| + def TestPostRecursiveWildcard(self):
|
| + """Tests that wildcard containing ** followed by an additional wildcard works"""
|
| + exp_obj_uri_strs = set([str(self.test_bucket0_uri.clone_replace_name(
|
| + 'nested1/nested2/xyz2'))])
|
| + actual_obj_uri_strs = set(
|
| + str(u) for u in test_util.test_wildcard_iterator(
|
| + '%s**/*y*2' % self.test_bucket0_uri.uri).IterUris())
|
| + self.assertEqual(exp_obj_uri_strs, actual_obj_uri_strs)
|
|
|
| + def TestCallingGetKeyOnProviderOnlyWildcardIteration(self):
|
| + """Tests that attempting iterating provider-only wildcard raises"""
|
| try:
|
| - for unused_ in test_util.test_wildcard_iterator(
|
| - '%s*1' % self.base_uri_str, ResultType.KEYS):
|
| - self.fail('Expected WildcardException not raised.')
|
| - except wildcard_iterator.WildcardException, e:
|
| - # Expected behavior.
|
| - self.assertTrue(str(e).find('with ResultType.KEYS iteration') != -1)
|
| + from gslib.bucket_listing_ref import BucketListingRefException
|
| + for iter_result in wildcard_iterator.wildcard_iterator(
|
| + 'gs://', ProjectIdHandler(),
|
| + bucket_storage_uri_class=mock_storage_service.MockBucketStorageUri):
|
| + iter_result.GetKey()
|
| + self.fail('Expected BucketListingRefException not raised.')
|
| + except BucketListingRefException, e:
|
| + self.assertTrue(str(e).find(
|
| + 'Attempt to call GetKey() on Key-less BucketListingRef') != -1)
|
|
|
|
|
| class FileIteratorTests(unittest.TestCase):
|
| @@ -227,7 +256,8 @@ class FileIteratorTests(unittest.TestCase):
|
|
|
| @classmethod
|
| def SetUpClass(cls):
|
| - """Creates a test dir containing 3 files and one nested subdirectory + file.
|
| + """
|
| + Creates a test dir containing 3 files and one nested subdirectory + file.
|
| """
|
|
|
| # Create the test directories.
|
| @@ -260,47 +290,47 @@ class FileIteratorTests(unittest.TestCase):
|
| @classmethod
|
| def TearDownClass(cls):
|
| """Cleans up test dir and file created by SetUpClass"""
|
| -
|
| if hasattr(cls, 'test_dir'):
|
| shutil.rmtree(cls.test_dir)
|
|
|
| + def TestContainsWildcard(self):
|
| + """Tests ContainsWildcard call"""
|
| + self.assertTrue(ContainsWildcard('a*.txt'))
|
| + self.assertTrue(ContainsWildcard('a[0-9].txt'))
|
| + self.assertFalse(ContainsWildcard('0-9.txt'))
|
| + self.assertTrue(ContainsWildcard('?.txt'))
|
| +
|
| def TestNoOpDirectoryIterator(self):
|
| """Tests that directory-only URI iterates just that one URI"""
|
| -
|
| - results = list(test_util.test_wildcard_iterator('file:///tmp/',
|
| - ResultType.URIS))
|
| + results = list(test_util.test_wildcard_iterator('file:///tmp/').IterUris())
|
| self.assertEqual(1, len(results))
|
| self.assertEqual('file:///tmp/', str(results[0]))
|
|
|
| def TestMatchingAllFiles(self):
|
| """Tests matching all files, based on wildcard"""
|
| -
|
| uri = test_util.test_storage_uri('file://%s/*' % self.test_dir)
|
| actual_uri_strs = set(str(u) for u in
|
| - test_util.test_wildcard_iterator(uri, ResultType.KEYS)
|
| + test_util.test_wildcard_iterator(uri).IterUris()
|
| )
|
| self.assertEqual(self.immed_child_uri_strs, actual_uri_strs)
|
|
|
| def TestMatchingFileSubset(self):
|
| """Tests matching a subset of files, based on wildcard"""
|
| -
|
| exp_uri_strs = set(
|
| ['file://%s/abcd' % self.test_dir, 'file://%s/abdd' % self.test_dir]
|
| )
|
| uri = test_util.test_storage_uri('file://%s/ab??' % self.test_dir)
|
| actual_uri_strs = set(str(u) for u in
|
| - test_util.test_wildcard_iterator(uri, ResultType.KEYS)
|
| + test_util.test_wildcard_iterator(uri).IterUris()
|
| )
|
| self.assertEqual(exp_uri_strs, actual_uri_strs)
|
|
|
| def TestMatchingNonWildcardedUri(self):
|
| """Tests matching a single named file"""
|
| -
|
| exp_uri_strs = set(['file://%s/abcd' % self.test_dir])
|
| uri = test_util.test_storage_uri('file://%s/abcd' % self.test_dir)
|
| - actual_uri_strs = set(str(u) for u in
|
| - test_util.test_wildcard_iterator(uri, ResultType.KEYS)
|
| - )
|
| + actual_uri_strs = set(
|
| + str(u) for u in test_util.test_wildcard_iterator(uri).IterUris())
|
| self.assertEqual(exp_uri_strs, actual_uri_strs)
|
|
|
| def TestMatchingFilesIgnoringOtherRegexChars(self):
|
| @@ -308,62 +338,46 @@ class FileIteratorTests(unittest.TestCase):
|
|
|
| exp_uri_strs = set(['file://%s/ade$' % self.test_dir])
|
| uri = test_util.test_storage_uri('file://%s/ad*$' % self.test_dir)
|
| - actual_uri_strs = set(str(u) for u in
|
| - test_util.test_wildcard_iterator(uri, ResultType.KEYS)
|
| - )
|
| + actual_uri_strs = set(
|
| + str(u) for u in test_util.test_wildcard_iterator(uri).IterUris())
|
| self.assertEqual(exp_uri_strs, actual_uri_strs)
|
|
|
| def TestRecursiveDirectoryOnlyWildcarding(self):
|
| """Tests recusive expansion of directory-only '**' wildcard"""
|
| -
|
| uri = test_util.test_storage_uri('file://%s/**' % self.test_dir)
|
| - actual_uri_strs = set(str(u) for u in
|
| - test_util.test_wildcard_iterator(uri, ResultType.KEYS)
|
| - )
|
| + actual_uri_strs = set(
|
| + str(u) for u in test_util.test_wildcard_iterator(uri).IterUris())
|
| self.assertEqual(self.all_file_uri_strs, actual_uri_strs)
|
|
|
| def TestRecursiveDirectoryPlusFileWildcarding(self):
|
| """Tests recusive expansion of '**' directory plus '*' wildcard"""
|
| -
|
| uri = test_util.test_storage_uri('file://%s/**/*' % self.test_dir)
|
| - actual_uri_strs = set(str(u) for u in
|
| - test_util.test_wildcard_iterator(uri, ResultType.KEYS)
|
| - )
|
| + actual_uri_strs = set(
|
| + str(u) for u in test_util.test_wildcard_iterator(uri).IterUris())
|
| self.assertEqual(self.all_file_uri_strs, actual_uri_strs)
|
|
|
| def TestInvalidRecursiveDirectoryWildcard(self):
|
| """Tests that wildcard containing '***' raises exception"""
|
| -
|
| try:
|
| uri = test_util.test_storage_uri('file://%s/***/abcd' % self.test_dir)
|
| - for unused_ in test_util.test_wildcard_iterator(uri, ResultType.KEYS):
|
| + for unused_ in test_util.test_wildcard_iterator(uri).IterUris():
|
| self.fail('Expected WildcardException not raised.')
|
| except wildcard_iterator.WildcardException, e:
|
| # Expected behavior.
|
| self.assertTrue(str(e).find('more than 2 consecutive') != -1)
|
|
|
| def TestMissingDir(self):
|
| - """Tests that wildcard raises exception when directory doesn't exist"""
|
| -
|
| - try:
|
| - for unused_ in test_util.test_wildcard_iterator('file://no_such_dir/*',
|
| - ResultType.KEYS):
|
| - self.fail('Expected WildcardException not raised.')
|
| - except wildcard_iterator.WildcardException, e:
|
| - # Expected behavior.
|
| - self.assertTrue(str(e).find('No matches') != -1)
|
| + """Tests that wildcard gets empty iterator when directory doesn't exist"""
|
| + res = list(
|
| + test_util.test_wildcard_iterator('file://no_such_dir/*').IterUris())
|
| + self.assertEqual(0, len(res))
|
|
|
| def TestExistingDirNoFileMatch(self):
|
| - """Tests that wildcard raises exception when there's no match"""
|
| -
|
| - try:
|
| - uri = test_util.test_storage_uri(
|
| - 'file://%s/non_existent*' % self.test_dir)
|
| - for unused_ in test_util.test_wildcard_iterator(uri, ResultType.KEYS):
|
| - self.fail('Expected WildcardException not raised.')
|
| - except wildcard_iterator.WildcardException, e:
|
| - # Expected behavior.
|
| - self.assertTrue(str(e).find('No matches') != -1)
|
| + """Tests that wildcard returns empty iterator when there's no match"""
|
| + uri = test_util.test_storage_uri(
|
| + 'file://%s/non_existent*' % self.test_dir)
|
| + res = list(test_util.test_wildcard_iterator(uri).IterUris())
|
| + self.assertEqual(0, len(res))
|
|
|
|
|
| if __name__ == '__main__':
|
|
|