|
5 | 5 | Tests for s3_express_getting_started.py. |
6 | 6 | """ |
7 | 7 |
|
| 8 | + |
8 | 9 | import pytest |
9 | 10 |
|
10 | | -import boto3 |
11 | 11 | from botocore import waiter |
12 | | -import os |
13 | | -import sys |
14 | 12 |
|
15 | | -script_dir = os.path.dirname(os.path.abspath(__file__)) |
16 | 13 |
|
17 | | -# Append directory for s3_express_getting_started.py |
18 | | -sys.path.append(os.path.join(script_dir, "..")) |
19 | | -import s3_express_getting_started |
| 14 | +class MockManager: |
| 15 | + def __init__(self, stub_runner, scenario_data): |
| 16 | + self.scenario_data = scenario_data |
| 17 | + self.my_uuid = "0000" |
| 18 | + self.bucket_name_prefix = "amzn-s3-demo-bucket" |
| 19 | + self.directory_bucket_name = ( |
| 20 | + f"{self.bucket_name_prefix}-{self.my_uuid}--use1-az2--x-s3" |
| 21 | + ) |
| 22 | + self.regular_bucket_name = f"{self.bucket_name_prefix}-regular-{self.my_uuid}" |
| 23 | + |
| 24 | + self.object_name = "basic-text-object" |
| 25 | + self.other_object = f"other/{self.object_name}" |
| 26 | + self.alt_object = f"alt/{self.object_name}" |
| 27 | + self.other_alt_object = f"other/alt/{self.object_name}" |
| 28 | + |
| 29 | + self.object_keys = [ |
| 30 | + self.object_name, |
| 31 | + self.other_object, |
| 32 | + self.alt_object, |
| 33 | + self.other_alt_object, |
| 34 | + ] |
| 35 | + |
| 36 | + self.vpc_id = "XXXXXXXXXXXXXXXXXXXXX" |
| 37 | + self.vpc_endpoint_id = f"vpce-{self.vpc_id}" |
| 38 | + |
| 39 | + self.stack_name = f"cfn-stack-s3-express-basics--{self.my_uuid}" |
| 40 | + self.stack = scenario_data.cloud_formation_resource.Stack(self.stack_name) |
| 41 | + self.stub_runner = stub_runner |
| 42 | + |
| 43 | + def setup_stubs( |
| 44 | + self, |
| 45 | + error, |
| 46 | + stop_on, |
| 47 | + ec2_stubber, |
| 48 | + cloud_formation_stubber, |
| 49 | + s3_stubber, |
| 50 | + monkeypatch, |
| 51 | + ): |
| 52 | + with self.stub_runner(error, stop_on) as runner: |
| 53 | + runner.add( |
| 54 | + s3_stubber.stub_list_objects_v2, |
| 55 | + self.directory_bucket_name, |
| 56 | + self.object_keys, |
| 57 | + ) |
| 58 | + runner.add( |
| 59 | + s3_stubber.stub_delete_objects, |
| 60 | + self.directory_bucket_name, |
| 61 | + self.object_keys, |
| 62 | + ) |
| 63 | + runner.add(s3_stubber.stub_delete_bucket, self.directory_bucket_name) |
| 64 | + runner.add( |
| 65 | + s3_stubber.stub_list_objects_v2, |
| 66 | + self.regular_bucket_name, |
| 67 | + self.object_keys, |
| 68 | + ) |
| 69 | + runner.add( |
| 70 | + s3_stubber.stub_delete_objects, |
| 71 | + self.regular_bucket_name, |
| 72 | + self.object_keys, |
| 73 | + ) |
| 74 | + runner.add(s3_stubber.stub_delete_bucket, self.regular_bucket_name) |
| 75 | + runner.add(cloud_formation_stubber.stub_delete_stack, self.stack_name) |
| 76 | + runner.add(ec2_stubber.stub_delete_vpc_endpoints, [self.vpc_endpoint_id]) |
| 77 | + runner.add(ec2_stubber.stub_delete_vpc, self.vpc_id) |
| 78 | + |
| 79 | + # Mock the waiters. |
| 80 | + monkeypatch.setattr(waiter.Waiter, "wait", lambda arg, **kwargs: None) |
| 81 | + |
| 82 | + self.scenario_data.scenario.s3_express_client = self.scenario_data.s3_client |
| 83 | + self.scenario_data.scenario.s3_regular_client = self.scenario_data.s3_client |
| 84 | + self.scenario_data.scenario.directory_bucket_name = self.directory_bucket_name |
| 85 | + # The cleanup function keeps executing after an exception is raised. |
| 86 | + # This allows the app to clean up all possible resources. |
| 87 | + # Because of this, exception testing fails because actions are called after the exception is raised. |
| 88 | + # To avoid false negatives when testing exceptions, certain fields are set only when they should be tested. |
| 89 | + |
| 90 | + if stop_on is None or stop_on > 2: |
| 91 | + self.scenario_data.scenario.regular_bucket_name = self.regular_bucket_name |
| 92 | + |
| 93 | + if stop_on is None or stop_on > 5: |
| 94 | + self.scenario_data.scenario.stack = self.stack |
| 95 | + |
| 96 | + if stop_on is None or stop_on > 6: |
| 97 | + self.scenario_data.scenario.vpc_endpoint_id = self.vpc_endpoint_id |
| 98 | + |
| 99 | + if stop_on is None or stop_on > 7: |
| 100 | + self.scenario_data.scenario.vpc_id = self.vpc_id |
| 101 | + |
| 102 | + |
| 103 | +@pytest.fixture |
| 104 | +def mock_mgr(stub_runner, scenario_data): |
| 105 | + return MockManager(stub_runner, scenario_data) |
20 | 106 |
|
21 | 107 |
|
22 | 108 | @pytest.mark.integ |
| 109 | +def test_scenario_cleanup(mock_mgr, capsys, monkeypatch): |
| 110 | + mock_mgr.setup_stubs( |
| 111 | + None, |
| 112 | + None, |
| 113 | + mock_mgr.scenario_data.ec2_stubber, |
| 114 | + mock_mgr.scenario_data.cloud_formation_stubber, |
| 115 | + mock_mgr.scenario_data.s3_stubber, |
| 116 | + monkeypatch, |
| 117 | + ) |
| 118 | + |
| 119 | + mock_mgr.scenario_data.scenario.cleanup() |
| 120 | + |
| 121 | + |
23 | 122 | @pytest.mark.parametrize( |
24 | | - "error_code, stop_on_index", |
| 123 | + "error, stop_on_index", |
25 | 124 | [ |
26 | 125 | ("TESTERROR-stub_list_objects_directory", 0), |
27 | 126 | ("TESTERROR-stub_delete_objects_directory", 1), |
|
32 | 131 | ("TESTERROR-stub_delete_stack", 6), |
33 | 132 | ("TESTERROR-stub_delete_vpc_endpoints", 7), |
34 | 133 | ("TESTERROR-stub_delete_vpc", 8), |
35 | | - (None, 8), |
36 | 134 | ], |
37 | 135 | ) |
38 | | -def test_s3_express_scenario_cleanup( |
39 | | - make_stubber, stub_runner, error_code, stop_on_index, monkeypatch |
40 | | -): |
41 | | - region = "us-east-1" |
42 | | - cloud_formation_resource = boto3.resource("cloudformation") |
43 | | - cloud_formation_stubber = make_stubber(cloud_formation_resource.meta.client) |
44 | | - |
45 | | - ec2_client = boto3.client("ec2", region_name=region) |
46 | | - ec2_stubber = make_stubber(ec2_client) |
47 | | - |
48 | | - iam_client = boto3.client("iam") |
49 | | - |
50 | | - s3_client = boto3.client("s3") |
51 | | - s3_stubber = make_stubber(s3_client) |
52 | | - |
53 | | - my_uuid = "0000" |
54 | | - |
55 | | - availability_zone_ids = ["use1-az2"] |
56 | | - |
57 | | - bucket_name_prefix = "amzn-s3-demo-bucket" |
58 | | - directory_bucket_name = ( |
59 | | - f"{bucket_name_prefix}-{my_uuid}--{availability_zone_ids[0]}--x-s3" |
60 | | - ) |
61 | | - regular_bucket_name = f"{bucket_name_prefix}-regular-{my_uuid}" |
62 | | - object_name = "basic-text-object" |
63 | | - other_object = f"other/{object_name}" |
64 | | - alt_object = f"alt/{object_name}" |
65 | | - other_alt_object = f"other/alt/{object_name}" |
66 | | - |
67 | | - object_keys = [object_name, other_object, alt_object, other_alt_object] |
68 | | - |
69 | | - my_uuid = "0000" |
70 | | - |
71 | | - stack_name = f"cfn-stack-s3-express-basics--{my_uuid}" |
72 | | - stack = cloud_formation_resource.Stack(stack_name) |
73 | | - vpc_id = "XXXXXXXXXXXXXXXXXXXXX" |
74 | | - vpc_endpoint_id = f"vpce-{vpc_id}" |
75 | | - |
76 | | - with stub_runner(error_code, stop_on_index) as runner: |
77 | | - runner.add(s3_stubber.stub_list_objects_v2, directory_bucket_name, object_keys) |
78 | | - runner.add(s3_stubber.stub_delete_objects, directory_bucket_name, object_keys) |
79 | | - runner.add(s3_stubber.stub_delete_bucket, directory_bucket_name) |
80 | | - runner.add(s3_stubber.stub_list_objects_v2, regular_bucket_name, object_keys) |
81 | | - runner.add(s3_stubber.stub_delete_objects, regular_bucket_name, object_keys) |
82 | | - runner.add(s3_stubber.stub_delete_bucket, regular_bucket_name) |
83 | | - runner.add(cloud_formation_stubber.stub_delete_stack, stack_name) |
84 | | - runner.add(ec2_stubber.stub_delete_vpc_endpoints, [vpc_endpoint_id]) |
85 | | - runner.add(ec2_stubber.stub_delete_vpc, vpc_id) |
86 | | - |
87 | | - scenario = s3_express_getting_started.S3ExpressScenario( |
88 | | - cloud_formation_resource, ec2_client, iam_client |
| 136 | +@pytest.mark.integ |
| 137 | +def test_scenario_cleanup_error(mock_mgr, caplog, error, stop_on_index, monkeypatch): |
| 138 | + mock_mgr.setup_stubs( |
| 139 | + error, |
| 140 | + stop_on_index, |
| 141 | + mock_mgr.scenario_data.ec2_stubber, |
| 142 | + mock_mgr.scenario_data.cloud_formation_stubber, |
| 143 | + mock_mgr.scenario_data.s3_stubber, |
| 144 | + monkeypatch, |
89 | 145 | ) |
90 | 146 |
|
91 | | - def mock_wait(self, **kwargs): |
92 | | - return |
93 | | - |
94 | | - # Mock the waiters. |
95 | | - monkeypatch.setattr(waiter.Waiter, "wait", mock_wait) |
96 | | - |
97 | | - scenario.s3_express_client = s3_client |
98 | | - scenario.s3_regular_client = s3_client |
99 | | - scenario.directory_bucket_name = directory_bucket_name |
100 | | - if stop_on_index > 2: |
101 | | - scenario.regular_bucket_name = regular_bucket_name |
102 | | - |
103 | | - if stop_on_index > 5: |
104 | | - scenario.stack = stack |
105 | | - |
106 | | - if stop_on_index > 6: |
107 | | - scenario.vpc_endpoint_id = vpc_endpoint_id |
108 | | - |
109 | | - if stop_on_index > 7: |
110 | | - scenario.vpc_id = vpc_id |
111 | | - |
112 | | - scenario.cleanup() |
| 147 | + mock_mgr.scenario_data.scenario.cleanup() |
0 commit comments