From 380db99a17a72735874f70e6796e2d78f9ad82f5 Mon Sep 17 00:00:00 2001 From: Steven Meyer <108885656+meyertst-aws@users.noreply.github.com> Date: Wed, 11 Dec 2024 11:39:10 -0500 Subject: [PATCH] Testing done --- .../s3-directory-buckets/requirements.txt | 3 +- .../s3_express_getting_started.py | 6 +- ...test_s3_express_getting_started_cleanup.py | 199 ++++++++++-------- ..._started_create_session_and_add_objects.py | 24 +-- ...ss_getting_started_create_vpc_and_users.py | 62 ++++-- ...getting_started_demonstrate_performance.py | 125 +++++------ ...tting_started_setup_clients_and_buckets.py | 68 ++++-- ...tarted_show_lexicographical_differences.py | 132 ++++++++++++ ..._started_show_lexigraphical_differences.py | 100 --------- 9 files changed, 407 insertions(+), 312 deletions(-) create mode 100644 python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_show_lexicographical_differences.py delete mode 100644 python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_show_lexigraphical_differences.py diff --git a/python/example_code/s3-directory-buckets/requirements.txt b/python/example_code/s3-directory-buckets/requirements.txt index 29d321d5e40..1320f639fd2 100644 --- a/python/example_code/s3-directory-buckets/requirements.txt +++ b/python/example_code/s3-directory-buckets/requirements.txt @@ -1,3 +1,4 @@ -boto3>=1.35.49 +boto3>=1.35.77 +botocore>=1.35.77 pytest>=7.2.1 requests>=2.28.2 diff --git a/python/example_code/s3-directory-buckets/s3_express_getting_started.py b/python/example_code/s3-directory-buckets/s3_express_getting_started.py index 9b11da2c86d..72ac5d9fc89 100644 --- a/python/example_code/s3-directory-buckets/s3_express_getting_started.py +++ b/python/example_code/s3-directory-buckets/s3_express_getting_started.py @@ -243,7 +243,7 @@ def create_session_and_add_objects(self) -> None: "Next, we'll copy the object into the Directory bucket using the regular client." ) print( - "This works fine, because Copy operations are not restricted for Directory buckets." + "This works fine, because copy operations are not restricted for Directory buckets." ) press_enter_to_continue() bucket_object = "basic-text-object" @@ -775,6 +775,8 @@ def tear_done_vpc(self) -> None: self.vpc_id, client_error.response["Error"]["Message"], ) + + # snippet-end:[python.example_code.s3.s3_express_basics] if __name__ == "__main__": @@ -811,5 +813,3 @@ def tear_done_vpc(self) -> None: logging.exception("Type error in demo!") if s3_express_scenario is not None: s3_express_scenario.cleanup() - - diff --git a/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_cleanup.py b/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_cleanup.py index d8eb96cb6c1..1418927cd2d 100644 --- a/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_cleanup.py +++ b/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_cleanup.py @@ -5,23 +5,122 @@ Tests for s3_express_getting_started.py. """ + import pytest -import boto3 from botocore import waiter -import os -import sys -script_dir = os.path.dirname(os.path.abspath(__file__)) -# Append directory for s3_express_getting_started.py -sys.path.append(os.path.join(script_dir, "..")) -import s3_express_getting_started +class MockManager: + def __init__(self, stub_runner, scenario_data): + self.scenario_data = scenario_data + self.my_uuid = "0000" + self.bucket_name_prefix = "amzn-s3-demo-bucket" + self.directory_bucket_name = ( + f"{self.bucket_name_prefix}-{self.my_uuid}--use1-az2--x-s3" + ) + self.regular_bucket_name = f"{self.bucket_name_prefix}-regular-{self.my_uuid}" + + self.object_name = "basic-text-object" + self.other_object = f"other/{self.object_name}" + self.alt_object = f"alt/{self.object_name}" + self.other_alt_object = f"other/alt/{self.object_name}" + + self.object_keys = [ + self.object_name, + self.other_object, + self.alt_object, + self.other_alt_object, + ] + + self.vpc_id = "XXXXXXXXXXXXXXXXXXXXX" + self.vpc_endpoint_id = f"vpce-{self.vpc_id}" + + self.stack_name = f"cfn-stack-s3-express-basics--{self.my_uuid}" + self.stack = scenario_data.cloud_formation_resource.Stack(self.stack_name) + self.stub_runner = stub_runner + + def setup_stubs( + self, + error, + stop_on, + ec2_stubber, + cloud_formation_stubber, + s3_stubber, + monkeypatch, + ): + with self.stub_runner(error, stop_on) as runner: + runner.add( + s3_stubber.stub_list_objects_v2, + self.directory_bucket_name, + self.object_keys, + ) + runner.add( + s3_stubber.stub_delete_objects, + self.directory_bucket_name, + self.object_keys, + ) + runner.add(s3_stubber.stub_delete_bucket, self.directory_bucket_name) + runner.add( + s3_stubber.stub_list_objects_v2, + self.regular_bucket_name, + self.object_keys, + ) + runner.add( + s3_stubber.stub_delete_objects, + self.regular_bucket_name, + self.object_keys, + ) + runner.add(s3_stubber.stub_delete_bucket, self.regular_bucket_name) + runner.add(cloud_formation_stubber.stub_delete_stack, self.stack_name) + runner.add(ec2_stubber.stub_delete_vpc_endpoints, [self.vpc_endpoint_id]) + runner.add(ec2_stubber.stub_delete_vpc, self.vpc_id) + + # Mock the waiters. + monkeypatch.setattr(waiter.Waiter, "wait", lambda arg, **kwargs: None) + + self.scenario_data.scenario.s3_express_client = self.scenario_data.s3_client + self.scenario_data.scenario.s3_regular_client = self.scenario_data.s3_client + self.scenario_data.scenario.directory_bucket_name = self.directory_bucket_name + # The cleanup function keeps executing after an exception is raised. + # This allows the app to clean up all possible resources. + # Because of this, exception testing fails because actions are called after the exception is raised. + # To avoid false negatives when testing exceptions, certain fields are set only when they should be tested. + + if stop_on is None or stop_on > 2: + self.scenario_data.scenario.regular_bucket_name = self.regular_bucket_name + + if stop_on is None or stop_on > 5: + self.scenario_data.scenario.stack = self.stack + + if stop_on is None or stop_on > 6: + self.scenario_data.scenario.vpc_endpoint_id = self.vpc_endpoint_id + + if stop_on is None or stop_on > 7: + self.scenario_data.scenario.vpc_id = self.vpc_id + + +@pytest.fixture +def mock_mgr(stub_runner, scenario_data): + return MockManager(stub_runner, scenario_data) @pytest.mark.integ +def test_scenario_cleanup(mock_mgr, capsys, monkeypatch): + mock_mgr.setup_stubs( + None, + None, + mock_mgr.scenario_data.ec2_stubber, + mock_mgr.scenario_data.cloud_formation_stubber, + mock_mgr.scenario_data.s3_stubber, + monkeypatch, + ) + + mock_mgr.scenario_data.scenario.cleanup() + + @pytest.mark.parametrize( - "error_code, stop_on_index", + "error, stop_on_index", [ ("TESTERROR-stub_list_objects_directory", 0), ("TESTERROR-stub_delete_objects_directory", 1), @@ -32,81 +131,17 @@ ("TESTERROR-stub_delete_stack", 6), ("TESTERROR-stub_delete_vpc_endpoints", 7), ("TESTERROR-stub_delete_vpc", 8), - (None, 8), ], ) -def test_s3_express_scenario_cleanup( - make_stubber, stub_runner, error_code, stop_on_index, monkeypatch -): - region = "us-east-1" - cloud_formation_resource = boto3.resource("cloudformation") - cloud_formation_stubber = make_stubber(cloud_formation_resource.meta.client) - - ec2_client = boto3.client("ec2", region_name=region) - ec2_stubber = make_stubber(ec2_client) - - iam_client = boto3.client("iam") - - s3_client = boto3.client("s3") - s3_stubber = make_stubber(s3_client) - - my_uuid = "0000" - - availability_zone_ids = ["use1-az2"] - - bucket_name_prefix = "amzn-s3-demo-bucket" - directory_bucket_name = ( - f"{bucket_name_prefix}-{my_uuid}--{availability_zone_ids[0]}--x-s3" - ) - regular_bucket_name = f"{bucket_name_prefix}-regular-{my_uuid}" - object_name = "basic-text-object" - other_object = f"other/{object_name}" - alt_object = f"alt/{object_name}" - other_alt_object = f"other/alt/{object_name}" - - object_keys = [object_name, other_object, alt_object, other_alt_object] - - my_uuid = "0000" - - stack_name = f"cfn-stack-s3-express-basics--{my_uuid}" - stack = cloud_formation_resource.Stack(stack_name) - vpc_id = "XXXXXXXXXXXXXXXXXXXXX" - vpc_endpoint_id = f"vpce-{vpc_id}" - - with stub_runner(error_code, stop_on_index) as runner: - runner.add(s3_stubber.stub_list_objects_v2, directory_bucket_name, object_keys) - runner.add(s3_stubber.stub_delete_objects, directory_bucket_name, object_keys) - runner.add(s3_stubber.stub_delete_bucket, directory_bucket_name) - runner.add(s3_stubber.stub_list_objects_v2, regular_bucket_name, object_keys) - runner.add(s3_stubber.stub_delete_objects, regular_bucket_name, object_keys) - runner.add(s3_stubber.stub_delete_bucket, regular_bucket_name) - runner.add(cloud_formation_stubber.stub_delete_stack, stack_name) - runner.add(ec2_stubber.stub_delete_vpc_endpoints, [vpc_endpoint_id]) - runner.add(ec2_stubber.stub_delete_vpc, vpc_id) - - scenario = s3_express_getting_started.S3ExpressScenario( - cloud_formation_resource, ec2_client, iam_client +@pytest.mark.integ +def test_scenario_cleanup_error(mock_mgr, caplog, error, stop_on_index, monkeypatch): + mock_mgr.setup_stubs( + error, + stop_on_index, + mock_mgr.scenario_data.ec2_stubber, + mock_mgr.scenario_data.cloud_formation_stubber, + mock_mgr.scenario_data.s3_stubber, + monkeypatch, ) - def mock_wait(self, **kwargs): - return - - # Mock the waiters. - monkeypatch.setattr(waiter.Waiter, "wait", mock_wait) - - scenario.s3_express_client = s3_client - scenario.s3_regular_client = s3_client - scenario.directory_bucket_name = directory_bucket_name - if stop_on_index > 2: - scenario.regular_bucket_name = regular_bucket_name - - if stop_on_index > 5: - scenario.stack = stack - - if stop_on_index > 6: - scenario.vpc_endpoint_id = vpc_endpoint_id - - if stop_on_index > 7: - scenario.vpc_id = vpc_id - - scenario.cleanup() + mock_mgr.scenario_data.scenario.cleanup() diff --git a/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_create_session_and_add_objects.py b/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_create_session_and_add_objects.py index de17158c11e..c5ace9c3c54 100644 --- a/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_create_session_and_add_objects.py +++ b/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_create_session_and_add_objects.py @@ -7,16 +7,8 @@ import pytest -import boto3 from botocore.exceptions import ClientError -import os -import sys -script_dir = os.path.dirname(os.path.abspath(__file__)) - -# Append directory for s3_express_getting_started.py -sys.path.append(os.path.join(script_dir, "..")) -import s3_express_getting_started class MockManager: def __init__(self, stub_runner, scenario_data, input_mocker): @@ -26,14 +18,11 @@ def __init__(self, stub_runner, scenario_data, input_mocker): self.availability_zone_ids = ["use1-az2"] self.bucket_name_prefix = "amzn-s3-demo-bucket" - self.directory_bucket_name = ( - f"{self.bucket_name_prefix}-{self.my_uuid}--{self.availability_zone_ids[0]}--x-s3" - ) + self.directory_bucket_name = f"{self.bucket_name_prefix}-{self.my_uuid}--{self.availability_zone_ids[0]}--x-s3" self.regular_bucket_name = f"{self.bucket_name_prefix}-regular-{self.my_uuid}" self.object_name = "basic-text-object" - answers = [] input_mocker.mock_answers(answers) self.stub_runner = stub_runner @@ -67,7 +56,7 @@ def mock_mgr(stub_runner, scenario_data, input_mocker): @pytest.mark.integ -def test_scenario_create_one_time_schedule(mock_mgr, capsys, monkeypatch): +def test_scenario_create_session_and_add_objects(mock_mgr, capsys, monkeypatch): mock_mgr.setup_stubs(None, None, mock_mgr.scenario_data.s3_stubber) mock_mgr.scenario_data.scenario.create_session_and_add_objects() @@ -79,12 +68,13 @@ def test_scenario_create_one_time_schedule(mock_mgr, capsys, monkeypatch): ("TESTERROR-stub_put_object", 0), ("TESTERROR-stub_create_session", 1), ("TESTERROR-stub_copy_object", 2), - ] + ], ) - @pytest.mark.integ -def test_scenario_create_one_time_schedule_error(mock_mgr, caplog, error, stop_on_index, monkeypatch): +def test_scenario_create_session_and_add_objects_error( + mock_mgr, caplog, error, stop_on_index, monkeypatch +): mock_mgr.setup_stubs(error, stop_on_index, mock_mgr.scenario_data.s3_stubber) - with pytest.raises(ClientError) as exc_info: + with pytest.raises(ClientError): mock_mgr.scenario_data.scenario.create_session_and_add_objects() diff --git a/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_create_vpc_and_users.py b/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_create_vpc_and_users.py index 29ffa4496cd..84c7103db10 100644 --- a/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_create_vpc_and_users.py +++ b/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_create_vpc_and_users.py @@ -32,7 +32,9 @@ def __init__(self, stub_runner, scenario_data, input_mocker): self.my_uuid = "0000" self.stack_name = f"cfn-stack-s3-express-basics--{self.my_uuid}" - self.template = s3_express_getting_started.S3ExpressScenario.get_template_as_string() + self.template = ( + s3_express_getting_started.S3ExpressScenario.get_template_as_string() + ) self.stack_id = f"arn:aws:cloudformation:{scenario_data.region}:000000000000:stack/{self.stack_name}" self.express_user_name = "s3-express-user" @@ -49,14 +51,22 @@ def __init__(self, stub_runner, scenario_data, input_mocker): input_mocker.mock_answers(answers) self.stub_runner = stub_runner - def setup_stubs(self, error, stop_on, ec2_stubber, cloud_formation_stubber, monkeypatch): + def setup_stubs( + self, error, stop_on, ec2_stubber, cloud_formation_stubber, monkeypatch + ): with self.stub_runner(error, stop_on) as runner: runner.add(ec2_stubber.stub_create_vpc, "10.0.0.0/16", self.vpc_id) runner.add( - ec2_stubber.stub_describe_route_tables, self.filters, self.vpc_id, self.route_table_id + ec2_stubber.stub_describe_route_tables, + self.filters, + self.vpc_id, + self.route_table_id, ) runner.add( - ec2_stubber.stub_create_vpc_endpoint, self.vpc_id, self.route_table_id, self.service_name + ec2_stubber.stub_create_vpc_endpoint, + self.vpc_id, + self.route_table_id, + self.service_name, ) runner.add( cloud_formation_stubber.stub_create_stack, @@ -75,7 +85,8 @@ def setup_stubs(self, error, stop_on, ec2_stubber, cloud_formation_stubber, monk monkeypatch.setattr(uuid, "uuid4", lambda: self.my_uuid) # Mock the waiters. - monkeypatch.setattr(waiter.Waiter, "wait", lambda self, **kwargs : None) + monkeypatch.setattr(waiter.Waiter, "wait", lambda arg, **kwargs: None) + @pytest.fixture def mock_mgr(stub_runner, scenario_data, input_mocker): @@ -83,9 +94,14 @@ def mock_mgr(stub_runner, scenario_data, input_mocker): @pytest.mark.integ -def test_scenario_create_one_time_schedule(mock_mgr, capsys, monkeypatch): - mock_mgr.setup_stubs(None, None, mock_mgr.scenario_data.ec2_stubber, - mock_mgr.scenario_data.cloud_formation_stubber, monkeypatch) +def test_scenario_create_vpc_and_users(mock_mgr, capsys, monkeypatch): + mock_mgr.setup_stubs( + None, + None, + mock_mgr.scenario_data.ec2_stubber, + mock_mgr.scenario_data.cloud_formation_stubber, + monkeypatch, + ) mock_mgr.scenario_data.scenario.create_vpc_and_users() @@ -93,18 +109,24 @@ def test_scenario_create_one_time_schedule(mock_mgr, capsys, monkeypatch): @pytest.mark.parametrize( "error, stop_on_index", [ - ("TESTERROR-stub_create_vpc", 0), - ("TESTERROR-stub_describe_route_tables", 1), - ("TESTERROR-stub_create_vpc_endpoint", 2), - ("TESTERROR-stub_create_stack", 3), - ("TESTERROR-stub_describe_stacks", 4), -] + ("TESTERROR-stub_create_vpc", 0), + ("TESTERROR-stub_describe_route_tables", 1), + ("TESTERROR-stub_create_vpc_endpoint", 2), + ("TESTERROR-stub_create_stack", 3), + ("TESTERROR-stub_describe_stacks", 4), + ], ) - @pytest.mark.integ -def test_scenario_create_one_time_schedule_error(mock_mgr, caplog, error, stop_on_index, monkeypatch): - mock_mgr.setup_stubs(error, stop_on_index, mock_mgr.scenario_data.ec2_stubber, - mock_mgr.scenario_data.cloud_formation_stubber, monkeypatch) - - with pytest.raises(ClientError) as exc_info: +def test_scenario_create_vpc_and_users_error( + mock_mgr, caplog, error, stop_on_index, monkeypatch +): + mock_mgr.setup_stubs( + error, + stop_on_index, + mock_mgr.scenario_data.ec2_stubber, + mock_mgr.scenario_data.cloud_formation_stubber, + monkeypatch, + ) + + with pytest.raises(ClientError): mock_mgr.scenario_data.scenario.create_vpc_and_users() diff --git a/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_demonstrate_performance.py b/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_demonstrate_performance.py index dc745f43b97..f6e0a8f2778 100644 --- a/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_demonstrate_performance.py +++ b/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_demonstrate_performance.py @@ -7,95 +7,84 @@ import pytest -import boto3 -from botocore.exceptions import ClientError -from botocore import waiter -import os -import sys -import uuid -script_dir = os.path.dirname(os.path.abspath(__file__)) +from botocore.exceptions import ClientError -# Append directory for s3_express_getting_started.py -sys.path.append(os.path.join(script_dir, "..")) -import s3_express_getting_started number_of_uploads = 10 -stop_on_index = [] -for i in range(len(stop_on_index), len(stop_on_index) + number_of_uploads): - stop_on_index.append((f"TESTERROR-stub_get_object_directory", i)) +class MockManager: + def __init__(self, stub_runner, scenario_data, input_mocker): + self.scenario_data = scenario_data + self.my_uuid = "0000" -for i in range(len(stop_on_index), len(stop_on_index) + number_of_uploads): - stop_on_index.append((f"TESTERROR-stub_get_object_regular", i)) + self.availability_zone_ids = ["use1-az2"] + self.bucket_name_prefix = "amzn-s3-demo-bucket" + self.directory_bucket_name = f"{self.bucket_name_prefix}-{self.my_uuid}--{self.availability_zone_ids[0]}--x-s3" + self.regular_bucket_name = f"{self.bucket_name_prefix}-regular-{self.my_uuid}" -@pytest.mark.integ -@pytest.mark.parametrize( - "error_code, stop_on_index", - stop_on_index, -) -def test_s3_express_scenario( - make_stubber, stub_runner, error_code, stop_on_index, monkeypatch -): - region = "us-east-1" - cloud_formation_resource = boto3.resource("cloudformation") - - ec2_client = boto3.client("ec2", region_name=region) + self.object_name = "basic-text-object" - iam_client = boto3.client("iam") + answers = [ + "y", + number_of_uploads, + ] + input_mocker.mock_answers(answers) + self.stub_runner = stub_runner - s3_client = boto3.client("s3") - s3_stubber = make_stubber(s3_client) + scenario_data.scenario.s3_express_client = scenario_data.s3_client + scenario_data.scenario.s3_regular_client = scenario_data.s3_client + scenario_data.scenario.regular_bucket_name = self.regular_bucket_name + scenario_data.scenario.directory_bucket_name = self.directory_bucket_name - my_uuid = "0000" + def setup_stubs(self, error, stop_on, s3_stubber): + with self.stub_runner(error, stop_on) as runner: + for _ in range(number_of_uploads): + runner.add( + s3_stubber.stub_get_object, + self.directory_bucket_name, + self.object_name, + ) - availability_zone_ids = ["use1-az2"] + for _ in range(number_of_uploads): + runner.add( + s3_stubber.stub_get_object, + self.regular_bucket_name, + self.object_name, + ) - bucket_name_prefix = "amzn-s3-demo-bucket" - directory_bucket_name = ( - f"{bucket_name_prefix}-{my_uuid}--{availability_zone_ids[0]}--x-s3" - ) - regular_bucket_name = f"{bucket_name_prefix}-regular-{my_uuid}" - object_name = "basic-text-object" +@pytest.fixture +def mock_mgr(stub_runner, scenario_data, input_mocker): + return MockManager(stub_runner, scenario_data, input_mocker) - inputs = [ - "y", - number_of_uploads, - ] - monkeypatch.setattr("builtins.input", lambda x: inputs.pop(0)) - s3_express_getting_started.use_press_enter_to_continue = False - - with stub_runner(error_code, stop_on_index) as runner: - for _ in range(number_of_uploads): - runner.add(s3_stubber.stub_get_object, directory_bucket_name, object_name) +@pytest.mark.integ +def test_scenario_demonstrate_performance(mock_mgr, capsys, monkeypatch): + mock_mgr.setup_stubs(None, None, mock_mgr.scenario_data.s3_stubber) - for _ in range(number_of_uploads): - runner.add(s3_stubber.stub_get_object, regular_bucket_name, object_name) + mock_mgr.scenario_data.scenario.demonstrate_performance(mock_mgr.object_name) - def mock_wait(self, **kwargs): - return - # Mock the waiters. - monkeypatch.setattr(waiter.Waiter, "wait", mock_wait) +parameter_values = [] +for i in range(len(parameter_values), len(parameter_values) + number_of_uploads): + parameter_values.append((f"TESTERROR-stub_get_object_directory", i)) - scenario = s3_express_getting_started.S3ExpressScenario( - cloud_formation_resource, ec2_client, iam_client - ) +for i in range(len(parameter_values), len(parameter_values) + number_of_uploads): + parameter_values.append((f"TESTERROR-stub_get_object_regular", i)) - scenario.s3_express_client = s3_client - scenario.s3_regular_client = s3_client - scenario.regular_bucket_name = regular_bucket_name - scenario.directory_bucket_name = directory_bucket_name - monkeypatch.setattr(uuid, "uuid4", lambda: my_uuid) +@pytest.mark.parametrize( + "error, stop_on_index", + parameter_values, +) +@pytest.mark.integ +def test_scenario_demonstrate_performance_error( + mock_mgr, caplog, error, stop_on_index, monkeypatch +): + mock_mgr.setup_stubs(error, stop_on_index, mock_mgr.scenario_data.s3_stubber) - if error_code is None: - scenario.demonstrate_performance(object_name) - else: - with pytest.raises(ClientError) as exc_info: - scenario.demonstrate_performance(object_name) - assert exc_info.value.response["Error"]["Code"] == error_code + with pytest.raises(ClientError): + mock_mgr.scenario_data.scenario.demonstrate_performance(mock_mgr.object_name) diff --git a/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_setup_clients_and_buckets.py b/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_setup_clients_and_buckets.py index a096c6479a2..286e17a1ffc 100644 --- a/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_setup_clients_and_buckets.py +++ b/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_setup_clients_and_buckets.py @@ -9,6 +9,7 @@ from botocore.exceptions import ClientError import uuid + class MockManager: def __init__(self, stub_runner, scenario_data, input_mocker): self.scenario_data = scenario_data @@ -18,28 +19,33 @@ def __init__(self, stub_runner, scenario_data, input_mocker): self.express_user_name = "s3-express-user" self.regular_user_name = "regular-user" - self.availability_zone_filter = [{"Name": "region-name", "Values": [self.scenario_data.region]}] + self.availability_zone_filter = [ + {"Name": "region-name", "Values": [self.scenario_data.region]} + ] self.availability_zone_ids = ["use1-az2"] self.bucket_name_prefix = "amzn-s3-demo-bucket" - self.directory_bucket_name = ( - f"{self.bucket_name_prefix}-{self.my_uuid}--{self.availability_zone_ids[0]}--x-s3" - ) + self.directory_bucket_name = f"{self.bucket_name_prefix}-{self.my_uuid}--{self.availability_zone_ids[0]}--x-s3" self.regular_bucket_name = f"{self.bucket_name_prefix}-regular-{self.my_uuid}" self.directory_bucket_configuration = { "Bucket": {"Type": "Directory", "DataRedundancy": "SingleAvailabilityZone"}, - "Location": {"Name": self.availability_zone_ids[0], "Type": "AvailabilityZone"}, + "Location": { + "Name": self.availability_zone_ids[0], + "Type": "AvailabilityZone", + }, } answers = [ - self.bucket_name_prefix, - "1", + self.bucket_name_prefix, + "1", ] input_mocker.mock_answers(answers) self.stub_runner = stub_runner - def setup_stubs(self, error, stop_on, ec2_stubber, iam_stubber, s3_stubber, monkeypatch): + def setup_stubs( + self, error, stop_on, ec2_stubber, iam_stubber, s3_stubber, monkeypatch + ): with self.stub_runner(error, stop_on) as runner: runner.add(iam_stubber.stub_create_access_key, self.regular_user_name) runner.add(iam_stubber.stub_create_access_key, self.express_user_name) @@ -60,22 +66,33 @@ def mock_create_s3_client(arg1): return self.scenario_data.s3_client monkeypatch.setattr( - self.scenario_data.scenario, "create_s3__client_with_access_key_credentials", mock_create_s3_client + self.scenario_data.scenario, + "create_s3__client_with_access_key_credentials", + mock_create_s3_client, ) monkeypatch.setattr(uuid, "uuid4", lambda: self.my_uuid) + @pytest.fixture def mock_mgr(stub_runner, scenario_data, input_mocker): return MockManager(stub_runner, scenario_data, input_mocker) @pytest.mark.integ -def test_scenario_create_one_time_schedule(mock_mgr, capsys, monkeypatch): - mock_mgr.setup_stubs(None, None, mock_mgr.scenario_data.ec2_stubber, - mock_mgr.scenario_data.iam_stubber, mock_mgr.scenario_data.s3_stubber, monkeypatch) +def test_scenario_setup_clients_and_buckets(mock_mgr, capsys, monkeypatch): + mock_mgr.setup_stubs( + None, + None, + mock_mgr.scenario_data.ec2_stubber, + mock_mgr.scenario_data.iam_stubber, + mock_mgr.scenario_data.s3_stubber, + monkeypatch, + ) - mock_mgr.scenario_data.scenario.setup_clients_and_buckets(mock_mgr.express_user_name, mock_mgr.regular_user_name) + mock_mgr.scenario_data.scenario.setup_clients_and_buckets( + mock_mgr.express_user_name, mock_mgr.regular_user_name + ) @pytest.mark.parametrize( @@ -86,13 +103,22 @@ def test_scenario_create_one_time_schedule(mock_mgr, capsys, monkeypatch): ("TESTERROR-stub_describe_availability_zones", 2), ("TESTERROR-stub_create_bucket_directory", 3), ("TESTERROR-stub_create_bucket_regular", 4), - ] + ], ) - @pytest.mark.integ -def test_scenario_create_one_time_schedule_error(mock_mgr, caplog, error, stop_on_index, monkeypatch): - mock_mgr.setup_stubs(error, stop_on_index, mock_mgr.scenario_data.ec2_stubber, - mock_mgr.scenario_data.iam_stubber, mock_mgr.scenario_data.s3_stubber, monkeypatch) - - with pytest.raises(ClientError) as exc_info: - mock_mgr.scenario_data.scenario.setup_clients_and_buckets(mock_mgr.express_user_name, mock_mgr.regular_user_name) +def test_scenario_setup_clients_and_buckets_error( + mock_mgr, caplog, error, stop_on_index, monkeypatch +): + mock_mgr.setup_stubs( + error, + stop_on_index, + mock_mgr.scenario_data.ec2_stubber, + mock_mgr.scenario_data.iam_stubber, + mock_mgr.scenario_data.s3_stubber, + monkeypatch, + ) + + with pytest.raises(ClientError): + mock_mgr.scenario_data.scenario.setup_clients_and_buckets( + mock_mgr.express_user_name, mock_mgr.regular_user_name + ) diff --git a/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_show_lexicographical_differences.py b/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_show_lexicographical_differences.py new file mode 100644 index 00000000000..ab1b5197648 --- /dev/null +++ b/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_show_lexicographical_differences.py @@ -0,0 +1,132 @@ +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +""" +Tests for s3_express_getting_started.py. +""" + +import pytest + +from botocore.exceptions import ClientError + + +class MockManager: + def __init__(self, stub_runner, scenario_data, input_mocker): + self.scenario_data = scenario_data + self.my_uuid = "0000" + + self.availability_zone_ids = ["use1-az2"] + + self.bucket_name_prefix = "amzn-s3-demo-bucket" + self.directory_bucket_name = f"{self.bucket_name_prefix}-{self.my_uuid}--{self.availability_zone_ids[0]}--x-s3" + self.regular_bucket_name = f"{self.bucket_name_prefix}-regular-{self.my_uuid}" + + self.object_name = "basic-text-object" + self.other_object = f"other/{self.object_name}" + self.alt_object = f"alt/{self.object_name}" + self.other_alt_object = f"other/alt/{self.object_name}" + + self.object_keys = [ + self.object_name, + self.other_object, + self.alt_object, + self.other_alt_object, + ] + + answers = [] + input_mocker.mock_answers(answers) + self.stub_runner = stub_runner + + scenario_data.scenario.s3_express_client = scenario_data.s3_client + scenario_data.scenario.s3_regular_client = scenario_data.s3_client + scenario_data.scenario.regular_bucket_name = self.regular_bucket_name + scenario_data.scenario.directory_bucket_name = self.directory_bucket_name + + def setup_stubs(self, error, stop_on, s3_stubber): + with self.stub_runner(error, stop_on) as runner: + runner.add( + s3_stubber.stub_put_object, + self.regular_bucket_name, + self.other_object, + "", + ) + runner.add( + s3_stubber.stub_put_object, + self.directory_bucket_name, + self.other_object, + "", + ) + runner.add( + s3_stubber.stub_put_object, + self.regular_bucket_name, + self.alt_object, + "", + ) + runner.add( + s3_stubber.stub_put_object, + self.directory_bucket_name, + self.alt_object, + "", + ) + runner.add( + s3_stubber.stub_put_object, + self.regular_bucket_name, + self.other_alt_object, + "", + ) + runner.add( + s3_stubber.stub_put_object, + self.directory_bucket_name, + self.other_alt_object, + "", + ) + + runner.add( + s3_stubber.stub_list_objects_v2, + self.directory_bucket_name, + self.object_keys, + ) + runner.add( + s3_stubber.stub_list_objects_v2, + self.regular_bucket_name, + self.object_keys, + ) + + +@pytest.fixture +def mock_mgr(stub_runner, scenario_data, input_mocker): + return MockManager(stub_runner, scenario_data, input_mocker) + + +@pytest.mark.integ +def test_scenario_show_lexicographical_differences(mock_mgr, capsys, monkeypatch): + mock_mgr.setup_stubs(None, None, mock_mgr.scenario_data.s3_stubber) + + mock_mgr.scenario_data.scenario.show_lexicographical_differences( + mock_mgr.object_name + ) + + +@pytest.mark.parametrize( + "error, stop_on_index", + [ + ("TESTERROR-stub_put_object_other_object", 0), + ("TESTERROR-stub_put_object_other_object", 1), + ("TESTERROR-stub_put_object_alt_object", 2), + ("TESTERROR-stub_put_object_alt_object", 3), + ("TESTERROR-stub_put_object_other_alt_object", 4), + ("TESTERROR-stub_put_object_other_alt_object", 5), + ("TESTERROR-stub_list_objects_directory", 6), + ("TESTERROR-stub_list_objects_regular", 7), + ], +) +@pytest.mark.integ +def test_scenario_show_lexicographical_differences_error( + mock_mgr, caplog, error, stop_on_index, monkeypatch +): + mock_mgr.setup_stubs(error, stop_on_index, mock_mgr.scenario_data.s3_stubber) + + with pytest.raises(ClientError): + mock_mgr.scenario_data.scenario.show_lexicographical_differences( + mock_mgr.object_name + ) diff --git a/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_show_lexigraphical_differences.py b/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_show_lexigraphical_differences.py deleted file mode 100644 index aef21c5c067..00000000000 --- a/python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_show_lexigraphical_differences.py +++ /dev/null @@ -1,100 +0,0 @@ -# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -# SPDX-License-Identifier: Apache-2.0 - -""" -Tests for s3_express_getting_started.py. -""" - -import pytest - -import boto3 -from botocore.exceptions import ClientError -import os -import sys - -script_dir = os.path.dirname(os.path.abspath(__file__)) - -# Append directory for s3_express_getting_started.py -sys.path.append(os.path.join(script_dir, "..")) -import s3_express_getting_started - - -@pytest.mark.integ -@pytest.mark.parametrize( - "error_code, stop_on_index", - [ - ("TESTERROR-stub_put_object_other_object", 0), - ("TESTERROR-stub_put_object_other_object", 1), - ("TESTERROR-stub_put_object_alt_object", 2), - ("TESTERROR-stub_put_object_alt_object", 3), - ("TESTERROR-stub_put_object_other_alt_object", 4), - ("TESTERROR-stub_put_object_other_alt_object", 5), - ("TESTERROR-stub_list_objects_directory", 6), - ("TESTERROR-stub_list_objects_regular", 7), - ], -) -def test_s3_express_scenario( - make_stubber, stub_runner, error_code, stop_on_index, monkeypatch -): - region = "us-east-1" - cloud_formation_resource = boto3.resource("cloudformation") - - ec2_client = boto3.client("ec2", region_name=region) - - iam_client = boto3.client("iam") - - s3_client = boto3.client("s3") - s3_stubber = make_stubber(s3_client) - - my_uuid = "0000" - - availability_zone_ids = ["use1-az2"] - - bucket_name_prefix = "amzn-s3-demo-bucket" - directory_bucket_name = ( - f"{bucket_name_prefix}-{my_uuid}--{availability_zone_ids[0]}--x-s3" - ) - regular_bucket_name = f"{bucket_name_prefix}-regular-{my_uuid}" - - object_name = "basic-text-object" - other_object = f"other/{object_name}" - alt_object = f"alt/{object_name}" - other_alt_object = f"other/alt/{object_name}" - - object_keys = [object_name, other_object, alt_object, other_alt_object] - - inputs = [] - - monkeypatch.setattr("builtins.input", lambda x: inputs.pop(0)) - - s3_express_getting_started.use_press_enter_to_continue = False - - with stub_runner(error_code, stop_on_index) as runner: - runner.add(s3_stubber.stub_put_object, regular_bucket_name, other_object, "") - runner.add(s3_stubber.stub_put_object, directory_bucket_name, other_object, "") - runner.add(s3_stubber.stub_put_object, regular_bucket_name, alt_object, "") - runner.add(s3_stubber.stub_put_object, directory_bucket_name, alt_object, "") - runner.add( - s3_stubber.stub_put_object, regular_bucket_name, other_alt_object, "" - ) - runner.add( - s3_stubber.stub_put_object, directory_bucket_name, other_alt_object, "" - ) - - runner.add(s3_stubber.stub_list_objects_v2, directory_bucket_name, object_keys) - runner.add(s3_stubber.stub_list_objects_v2, regular_bucket_name, object_keys) - - scenario = s3_express_getting_started.S3ExpressScenario( - cloud_formation_resource, ec2_client, iam_client - ) - scenario.s3_express_client = s3_client - scenario.s3_regular_client = s3_client - scenario.regular_bucket_name = regular_bucket_name - scenario.directory_bucket_name = directory_bucket_name - - if error_code is None: - scenario.show_lexigraphical_differences(object_name) - else: - with pytest.raises(ClientError) as exc_info: - scenario.show_lexigraphical_differences(object_name) - assert exc_info.value.response["Error"]["Code"] == error_code