Skip to content

Commit f88758b

Browse files
committed
Testing done
1 parent 2fa12ad commit f88758b

9 files changed

+407
-312
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
boto3>=1.35.49
1+
boto3>=1.35.77
2+
botocore>=1.35.77
23
pytest>=7.2.1
34
requests>=2.28.2

python/example_code/s3-directory-buckets/s3_express_getting_started.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def create_session_and_add_objects(self) -> None:
243243
"Next, we'll copy the object into the Directory bucket using the regular client."
244244
)
245245
print(
246-
"This works fine, because Copy operations are not restricted for Directory buckets."
246+
"This works fine, because copy operations are not restricted for Directory buckets."
247247
)
248248
press_enter_to_continue()
249249
bucket_object = "basic-text-object"
@@ -775,6 +775,8 @@ def tear_done_vpc(self) -> None:
775775
self.vpc_id,
776776
client_error.response["Error"]["Message"],
777777
)
778+
779+
778780
# snippet-end:[python.example_code.s3.s3_express_basics]
779781

780782
if __name__ == "__main__":
@@ -811,5 +813,3 @@ def tear_done_vpc(self) -> None:
811813
logging.exception("Type error in demo!")
812814
if s3_express_scenario is not None:
813815
s3_express_scenario.cleanup()
814-
815-

python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_cleanup.py

Lines changed: 117 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,122 @@
55
Tests for s3_express_getting_started.py.
66
"""
77

8+
89
import pytest
910

10-
import boto3
1111
from botocore import waiter
12-
import os
13-
import sys
1412

15-
script_dir = os.path.dirname(os.path.abspath(__file__))
1613

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)
20106

21107

22108
@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+
23122
@pytest.mark.parametrize(
24-
"error_code, stop_on_index",
123+
"error, stop_on_index",
25124
[
26125
("TESTERROR-stub_list_objects_directory", 0),
27126
("TESTERROR-stub_delete_objects_directory", 1),
@@ -32,81 +131,17 @@
32131
("TESTERROR-stub_delete_stack", 6),
33132
("TESTERROR-stub_delete_vpc_endpoints", 7),
34133
("TESTERROR-stub_delete_vpc", 8),
35-
(None, 8),
36134
],
37135
)
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,
89145
)
90146

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()

python/example_code/s3-directory-buckets/tests/test_s3_express_getting_started_create_session_and_add_objects.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,8 @@
77

88
import pytest
99

10-
import boto3
1110
from botocore.exceptions import ClientError
12-
import os
13-
import sys
1411

15-
script_dir = os.path.dirname(os.path.abspath(__file__))
16-
17-
# Append directory for s3_express_getting_started.py
18-
sys.path.append(os.path.join(script_dir, ".."))
19-
import s3_express_getting_started
2012

2113
class MockManager:
2214
def __init__(self, stub_runner, scenario_data, input_mocker):
@@ -26,14 +18,11 @@ def __init__(self, stub_runner, scenario_data, input_mocker):
2618
self.availability_zone_ids = ["use1-az2"]
2719

2820
self.bucket_name_prefix = "amzn-s3-demo-bucket"
29-
self.directory_bucket_name = (
30-
f"{self.bucket_name_prefix}-{self.my_uuid}--{self.availability_zone_ids[0]}--x-s3"
31-
)
21+
self.directory_bucket_name = f"{self.bucket_name_prefix}-{self.my_uuid}--{self.availability_zone_ids[0]}--x-s3"
3222
self.regular_bucket_name = f"{self.bucket_name_prefix}-regular-{self.my_uuid}"
3323

3424
self.object_name = "basic-text-object"
3525

36-
3726
answers = []
3827
input_mocker.mock_answers(answers)
3928
self.stub_runner = stub_runner
@@ -67,7 +56,7 @@ def mock_mgr(stub_runner, scenario_data, input_mocker):
6756

6857

6958
@pytest.mark.integ
70-
def test_scenario_create_one_time_schedule(mock_mgr, capsys, monkeypatch):
59+
def test_scenario_create_session_and_add_objects(mock_mgr, capsys, monkeypatch):
7160
mock_mgr.setup_stubs(None, None, mock_mgr.scenario_data.s3_stubber)
7261

7362
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):
7968
("TESTERROR-stub_put_object", 0),
8069
("TESTERROR-stub_create_session", 1),
8170
("TESTERROR-stub_copy_object", 2),
82-
]
71+
],
8372
)
84-
8573
@pytest.mark.integ
86-
def test_scenario_create_one_time_schedule_error(mock_mgr, caplog, error, stop_on_index, monkeypatch):
74+
def test_scenario_create_session_and_add_objects_error(
75+
mock_mgr, caplog, error, stop_on_index, monkeypatch
76+
):
8777
mock_mgr.setup_stubs(error, stop_on_index, mock_mgr.scenario_data.s3_stubber)
8878

89-
with pytest.raises(ClientError) as exc_info:
79+
with pytest.raises(ClientError):
9080
mock_mgr.scenario_data.scenario.create_session_and_add_objects()

0 commit comments

Comments
 (0)