From c7816632b9b92021268cb4c4020fbfd26b45e62d Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Fri, 10 Jun 2011 10:15:47 -0700 Subject: [PATCH] Refactor to share code, hide counter implementation. --- test_s3.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/test_s3.py b/test_s3.py index 27df846..8fe95ac 100644 --- a/test_s3.py +++ b/test_s3.py @@ -153,6 +153,21 @@ def teardown(): bucket_counter = itertools.count(1) +def get_new_bucket_name(): + """ + Get a bucket name that probably does not exist. + + We make every attempt to use a unique random prefix, so if a + bucket by this name happens to exist, it's ok if tests give + false negatives. + """ + name = '{prefix}{num}'.format( + prefix=prefix, + num=next(bucket_counter), + ) + return name + + def get_new_bucket(connection=None): """ Get a bucket that exists and is empty. @@ -162,10 +177,7 @@ def get_new_bucket(connection=None): """ if connection is None: connection = s3.main - name = '{prefix}{num}'.format( - prefix=prefix, - num=next(bucket_counter), - ) + name = get_new_bucket_name() # the only way for this to fail with a pre-existing bucket is if # someone raced us between setup nuke_prefixed_buckets and here; # ignore that as astronomically unlikely @@ -833,10 +845,7 @@ def test_bucket_create_naming_good_contains_hyphen(): check_good_bucket_name('aaa-111') def test_bucket_delete_notexist(): - name = '{prefix}{num}'.format( - prefix=prefix, - num=next(bucket_counter), - ) + name = get_new_bucket_name() try: bucket = s3.main.delete_bucket(name) raise RuntimeError("S3 implementation allowed us to delete a bucket that shouldn't exist") @@ -844,10 +853,7 @@ def test_bucket_delete_notexist(): pass def test_object_write_to_nonexist_bucket(): - name = '{prefix}{num}'.format( - prefix=prefix, - num=next(bucket_counter), - ) + name = get_new_bucket_name() bucket = s3.main.get_bucket(name, validate=False) key = bucket.new_key('foo123bar') try: