Skip to content

Commit

Permalink
Use randomized bucket name for non-existent S3 bucket name in integ t…
Browse files Browse the repository at this point in the history
…ests. (aws#9053)
  • Loading branch information
jonathan343 authored Nov 4, 2024
1 parent 980ea87 commit b54b44d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
4 changes: 2 additions & 2 deletions awscli/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def random_chars(num_chars):
return binascii.hexlify(os.urandom(int(num_chars / 2))).decode('ascii')


def random_bucket_name(prefix='awscli-s3integ-', num_random=15):
def random_bucket_name(prefix='awscli-s3integ', num_random=15):
"""Generate a random S3 bucket name.
:param prefix: A prefix to use in the bucket name. Useful
Expand All @@ -240,7 +240,7 @@ def random_bucket_name(prefix='awscli-s3integ-', num_random=15):
:returns: The name of a randomly generated bucket name as a string.
"""
return prefix + random_chars(num_random)
return f"{prefix}-{random_chars(num_random)}-{int(time.time())}"


class BaseCLIDriverTest(unittest.TestCase):
Expand Down
20 changes: 11 additions & 9 deletions tests/integration/customizations/s3/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
from tests.integration.customizations.s3 import BaseS3IntegrationTest


_NON_EXISTENT_BUCKET = random_bucket_name()


@pytest.fixture
def symlink_files(files):
nested_dir = os.path.join(files.rootdir, 'realfiles')
Expand Down Expand Up @@ -300,7 +303,7 @@ def test_mv_with_large_file(self, files, s3_utils, shared_bucket):

def test_mv_to_nonexistent_bucket(self, files):
full_path = files.create_file('foo.txt', 'this is foo.txt')
p = aws('s3 mv %s s3://bad-noexist-13143242/foo.txt' % (full_path,))
p = aws(f's3 mv {full_path} s3://{_NON_EXISTENT_BUCKET}/foo.txt')
assert p.rc == 1

def test_cant_move_file_onto_itself_small_file(self, s3_utils,
Expand Down Expand Up @@ -493,7 +496,7 @@ def test_cleans_up_aborted_uploads(self, files, s3_utils, shared_bucket):

def test_cp_to_nonexistent_bucket(self, files):
foo_txt = files.create_file('foo.txt', 'this is foo.txt')
p = aws('s3 cp %s s3://noexist-bucket-foo-bar123/foo.txt' % (foo_txt,))
p = aws(f's3 cp {foo_txt} s3://{_NON_EXISTENT_BUCKET}/foo.txt')
assert p.rc == 1

def test_cp_empty_file(self, files, s3_utils, shared_bucket):
Expand All @@ -504,7 +507,7 @@ def test_cp_empty_file(self, files, s3_utils, shared_bucket):
assert s3_utils.key_exists(shared_bucket, 'foo.txt')

def test_download_non_existent_key(self):
p = aws('s3 cp s3://jasoidfjasdjfasdofijasdf/foo.txt foo.txt')
p = aws(f's3 cp s3://{_NON_EXISTENT_BUCKET}/foo.txt foo.txt')
assert p.rc == 1
expected_err_msg = (
'An error occurred (404) when calling the '
Expand Down Expand Up @@ -754,7 +757,7 @@ def test_sync_to_nonexistent_bucket(self, files):
files.create_file('bar.txt', 'bar contents')

# Sync the directory and the bucket.
p = aws('s3 sync %s s3://noexist-bkt-nme-1412' % (files.rootdir,))
p = aws(f's3 sync {files.rootdir} s3://{_NON_EXISTENT_BUCKET}')
assert p.rc == 1

def test_sync_with_empty_files(self, files, s3_utils, shared_bucket):
Expand Down Expand Up @@ -1136,7 +1139,7 @@ def test_ls_bucket_with_s3_prefix(self):
self.assert_no_errors(p)

def test_ls_non_existent_bucket(self):
p = aws('s3 ls s3://foobara99842u4wbts829381')
p = aws(f's3 ls s3://{_NON_EXISTENT_BUCKET}')
assert p.rc == 254
error_msg = (
'An error occurred (NoSuchBucket) when calling the '
Expand Down Expand Up @@ -1279,7 +1282,7 @@ def test_error_output(self, files):
foo_txt = files.create_file('foo.txt', 'foo contents')

# Copy file into bucket.
p = aws('s3 cp %s s3://non-existant-bucket/' % foo_txt)
p = aws(f's3 cp {foo_txt} s3://{_NON_EXISTENT_BUCKET}/')
# Check that there were errors and that the error was print to stderr.
assert p.rc == 1
assert 'upload failed' in p.stderr
Expand All @@ -1288,7 +1291,7 @@ def test_error_ouput_quiet(self, files):
foo_txt = files.create_file('foo.txt', 'foo contents')

# Copy file into bucket.
p = aws('s3 cp %s s3://non-existant-bucket/ --quiet' % foo_txt)
p = aws(f's3 cp {foo_txt} s3://{_NON_EXISTENT_BUCKET}/ --quiet')
# Check that there were errors and that the error was not
# print to stderr.
assert p.rc == 1
Expand All @@ -1298,8 +1301,7 @@ def test_error_output_only_show_errors(self, files):
foo_txt = files.create_file('foo.txt', 'foo contents')

# Copy file into bucket.
p = aws('s3 cp %s s3://non-existant-bucket/ --only-show-errors'
% foo_txt)
p = aws(f's3 cp {foo_txt} s3://{_NON_EXISTENT_BUCKET}/ --only-show-errors')
# Check that there were errors and that the error was print to stderr.
assert p.rc == 1
assert 'upload failed' in p.stderr
Expand Down

0 comments on commit b54b44d

Please sign in to comment.