Skip to content

Commit df041e7

Browse files
authored
CFn test plan reorganization (#6973)
1 parent 827ff94 commit df041e7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+861
-696
lines changed

tests/integration/cloudformation/api/__init__.py

Whitespace-only changes.

tests/integration/cloudformation/test_cloudformation_changesets.py renamed to tests/integration/cloudformation/api/test_changesets.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_create_change_set_without_parameters(
1919
stack_name = f"stack-{short_uid()}"
2020
change_set_name = f"change-set-{short_uid()}"
2121

22-
template_path = os.path.join(os.path.dirname(__file__), "../templates/sns_topic_simple.yaml")
22+
template_path = os.path.join(os.path.dirname(__file__), "../../templates/sns_topic_simple.yaml")
2323
response = cfn_client.create_change_set(
2424
StackName=stack_name,
2525
ChangeSetName=change_set_name,
@@ -80,7 +80,7 @@ def test_create_change_set_update_without_parameters(
8080
change_set_name = f"change-set-{short_uid()}"
8181
change_set_name2 = f"change-set-{short_uid()}"
8282

83-
template_path = os.path.join(os.path.dirname(__file__), "../templates/sns_topic_simple.yaml")
83+
template_path = os.path.join(os.path.dirname(__file__), "../../templates/sns_topic_simple.yaml")
8484

8585
response = cfn_client.create_change_set(
8686
StackName=stack_name,
@@ -147,7 +147,7 @@ def test_create_change_set_create_existing(
147147
stack_name = f"stack-{short_uid()}"
148148
change_set_name = f"change-set-{short_uid()}"
149149

150-
template_path = os.path.join(os.path.dirname(__file__), "../templates/sns_topic_simple.yaml")
150+
template_path = os.path.join(os.path.dirname(__file__), "../../templates/sns_topic_simple.yaml")
151151
response = cfn_client.create_change_set(
152152
StackName=stack_name,
153153
ChangeSetName=change_set_name,
@@ -179,7 +179,7 @@ def test_create_change_set_create_existing(
179179
def test_create_change_set_update_nonexisting(cfn_client):
180180
stack_name = f"stack-{short_uid()}"
181181
change_set_name = f"change-set-{short_uid()}"
182-
template_path = os.path.join(os.path.dirname(__file__), "../templates/sns_topic_simple.yaml")
182+
template_path = os.path.join(os.path.dirname(__file__), "../../templates/sns_topic_simple.yaml")
183183

184184
with pytest.raises(Exception) as ex:
185185
response = cfn_client.create_change_set(
@@ -206,7 +206,7 @@ def test_create_change_set_import(cfn_client):
206206
def test_create_change_set_invalid_params(cfn_client):
207207
stack_name = f"stack-{short_uid()}"
208208
change_set_name = f"change-set-{short_uid()}"
209-
template_path = os.path.join(os.path.dirname(__file__), "../templates/sns_topic_simple.yaml")
209+
template_path = os.path.join(os.path.dirname(__file__), "../../templates/sns_topic_simple.yaml")
210210
with pytest.raises(ClientError) as ex:
211211
cfn_client.create_change_set(
212212
StackName=stack_name,
@@ -221,7 +221,7 @@ def test_create_change_set_invalid_params(cfn_client):
221221
def test_create_change_set_missing_stackname(cfn_client):
222222
"""in this case boto doesn't even let us send the request"""
223223
change_set_name = f"change-set-{short_uid()}"
224-
template_path = os.path.join(os.path.dirname(__file__), "../templates/sns_topic_simple.yaml")
224+
template_path = os.path.join(os.path.dirname(__file__), "../../templates/sns_topic_simple.yaml")
225225
with pytest.raises(Exception):
226226
cfn_client.create_change_set(
227227
StackName="",
@@ -251,7 +251,7 @@ def test_create_change_set_with_ssm_parameter(
251251

252252
ssm_client.put_parameter(Name=parameter_name, Value=parameter_value, Type="String")
253253
template_path = os.path.join(
254-
os.path.dirname(__file__), "../templates/dynamicparameter_ssm_string.yaml"
254+
os.path.dirname(__file__), "../../templates/dynamicparameter_ssm_string.yaml"
255255
)
256256
template_rendered = render_template(
257257
load_template_raw(template_path), parameter_name=parameter_name
@@ -333,7 +333,7 @@ def test_execute_change_set(
333333

334334
stack_name = f"stack-{short_uid()}"
335335
change_set_name = f"change-set-{short_uid()}"
336-
template_path = os.path.join(os.path.dirname(__file__), "../templates/sns_topic_simple.yaml")
336+
template_path = os.path.join(os.path.dirname(__file__), "../../templates/sns_topic_simple.yaml")
337337
template_body = load_template_raw(template_path)
338338

339339
response = cfn_client.create_change_set(
@@ -403,7 +403,7 @@ def test_delete_change_set_exception(cfn_client, snapshot):
403403
def test_create_and_then_remove_non_supported_resource_change_set(deploy_cfn_template):
404404
# first deploy cfn with a CodeArtifact resource that is not actually supported
405405
template_path = os.path.join(
406-
os.path.dirname(__file__), "../templates/code_artifact_template.yaml"
406+
os.path.dirname(__file__), "../../templates/code_artifact_template.yaml"
407407
)
408408
template_body = load_template_raw(template_path)
409409
stack = deploy_cfn_template(
@@ -413,7 +413,7 @@ def test_create_and_then_remove_non_supported_resource_change_set(deploy_cfn_tem
413413

414414
# removal of CodeArtifact should not throw exception
415415
template_path = os.path.join(
416-
os.path.dirname(__file__), "../templates/code_artifact_remove_template.yaml"
416+
os.path.dirname(__file__), "../../templates/code_artifact_remove_template.yaml"
417417
)
418418
template_body = load_template_raw(template_path)
419419
deploy_cfn_template(
@@ -427,7 +427,9 @@ def test_create_and_then_remove_non_supported_resource_change_set(deploy_cfn_tem
427427
def test_create_and_then_remove_supported_resource_change_set(deploy_cfn_template, s3_client):
428428
first_bucket_name = f"test-bucket-1-{short_uid()}"
429429
second_bucket_name = f"test-bucket-2-{short_uid()}"
430-
template_path = os.path.join(os.path.dirname(__file__), "../templates/for_removal_setup.yaml")
430+
template_path = os.path.join(
431+
os.path.dirname(__file__), "../../templates/for_removal_setup.yaml"
432+
)
431433
template_body = load_template_raw(template_path)
432434

433435
stack = deploy_cfn_template(
@@ -443,7 +445,9 @@ def test_create_and_then_remove_supported_resource_change_set(deploy_cfn_templat
443445
assert first_bucket_name in bucket_names
444446
assert second_bucket_name in bucket_names
445447

446-
template_path = os.path.join(os.path.dirname(__file__), "../templates/for_removal_remove.yaml")
448+
template_path = os.path.join(
449+
os.path.dirname(__file__), "../../templates/for_removal_remove.yaml"
450+
)
447451
template_body = load_template_raw(template_path)
448452
deploy_cfn_template(
449453
is_update=True,
@@ -479,7 +483,7 @@ def test_empty_changeset(cfn_client, snapshot, cleanups):
479483
change_set_name_nochange = f"change-set-nochange-{short_uid()}"
480484
cleanups.append(lambda: cfn_client.delete_stack(StackName=stack_name))
481485

482-
template_path = os.path.join(os.path.dirname(__file__), "../templates/cdkmetadata.yaml")
486+
template_path = os.path.join(os.path.dirname(__file__), "../../templates/cdkmetadata.yaml")
483487
template = load_template_file(template_path)
484488

485489
# 1. create change set and execute
@@ -554,7 +558,7 @@ def test_deleted_changeset(cfn_client, snapshot, cleanups):
554558

555559
snapshot.add_transformer(snapshot.transform.regex(stack_name, "<stack-name>"))
556560

557-
template_path = os.path.join(os.path.dirname(__file__), "../templates/cdkmetadata.yaml")
561+
template_path = os.path.join(os.path.dirname(__file__), "../../templates/cdkmetadata.yaml")
558562
template = load_template_file(template_path)
559563

560564
# 1. create change set

tests/integration/cloudformation/test_cloudformation_changesets.snapshot.json renamed to tests/integration/cloudformation/api/test_changesets.snapshot.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"tests/integration/cloudformation/test_cloudformation_changesets.py::test_create_change_set_update_without_parameters": {
2+
"tests/integration/cloudformation/api/test_changesets.py::test_create_change_set_update_without_parameters": {
33
"recorded-date": "31-05-2022, 09:32:02",
44
"recorded-content": {
55
"create_change_set": {
@@ -73,7 +73,7 @@
7373
}
7474
}
7575
},
76-
"tests/integration/cloudformation/test_cloudformation_changesets.py::test_empty_changeset": {
76+
"tests/integration/cloudformation/api/test_changesets.py::test_empty_changeset": {
7777
"recorded-date": "10-08-2022, 10:52:55",
7878
"recorded-content": {
7979
"first_changeset": {
@@ -151,7 +151,7 @@
151151
"error_execute_failed": "An error occurred (InvalidChangeSetStatus) when calling the ExecuteChangeSet operation: ChangeSet [arn:aws:cloudformation:<region>:111111111111:changeSet/<resource:3>] cannot be executed in its current status of [FAILED]"
152152
}
153153
},
154-
"tests/integration/cloudformation/test_cloudformation_changesets.py::test_deleted_changeset": {
154+
"tests/integration/cloudformation/api/test_changesets.py::test_deleted_changeset": {
155155
"recorded-date": "11-08-2022, 11:11:47",
156156
"recorded-content": {
157157
"create": {
@@ -165,13 +165,13 @@
165165
"postdelete_changeset_notfound": "An error occurred (ChangeSetNotFound) when calling the DescribeChangeSet operation: ChangeSet [arn:aws:cloudformation:<region>:111111111111:changeSet/<resource:1>] does not exist"
166166
}
167167
},
168-
"tests/integration/cloudformation/test_cloudformation_changesets.py::test_describe_change_set_nonexisting": {
168+
"tests/integration/cloudformation/api/test_changesets.py::test_describe_change_set_nonexisting": {
169169
"recorded-date": "11-08-2022, 13:22:01",
170170
"recorded-content": {
171171
"exception": "An error occurred (ValidationError) when calling the DescribeChangeSet operation: Stack [somestack] does not exist"
172172
}
173173
},
174-
"tests/integration/cloudformation/test_cloudformation_changesets.py::test_delete_change_set_exception": {
174+
"tests/integration/cloudformation/api/test_changesets.py::test_delete_change_set_exception": {
175175
"recorded-date": "11-08-2022, 14:07:38",
176176
"recorded-content": {
177177
"e1": "<ExceptionInfo ClientError('An error occurred (ValidationError) when calling the DeleteChangeSet operation: Stack [nostack] does not exist') tblen=3>",
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import os
2+
3+
from localstack.utils.files import load_file
4+
from localstack.utils.strings import short_uid
5+
from localstack.utils.sync import retry
6+
7+
8+
def test_nested_stack(s3_client, cfn_client, deploy_cfn_template, s3_create_bucket):
9+
# upload template to S3
10+
artifacts_bucket = f"cf-artifacts-{short_uid()}"
11+
artifacts_path = "stack.yaml"
12+
s3_create_bucket(Bucket=artifacts_bucket, ACL="public-read")
13+
s3_client.put_object(
14+
Bucket=artifacts_bucket,
15+
Key=artifacts_path,
16+
Body=load_file(os.path.join(os.path.dirname(__file__), "../../templates/template5.yaml")),
17+
)
18+
19+
# deploy template
20+
param_value = short_uid()
21+
stack_bucket_name = f"test-{param_value}" # this is the bucket name generated by template5
22+
23+
deploy_cfn_template(
24+
template=load_file(
25+
os.path.join(os.path.dirname(__file__), "../../templates/template6.yaml")
26+
)
27+
% (artifacts_bucket, artifacts_path),
28+
parameters={"GlobalParam": param_value},
29+
)
30+
31+
# assert that nested resources have been created
32+
def assert_bucket_exists():
33+
response = s3_client.head_bucket(Bucket=stack_bucket_name)
34+
assert 200 == response["ResponseMetadata"]["HTTPStatusCode"]
35+
36+
retry(assert_bucket_exists)
37+
38+
39+
def test_nested_stack_output_refs(cfn_client, deploy_cfn_template, s3_client, s3_create_bucket):
40+
"""test output handling of nested stacks incl. referencing the nested output in the parent stack"""
41+
bucket_name = s3_create_bucket()
42+
nested_bucket_name = f"test-bucket-nested-{short_uid()}"
43+
key = f"test-key-{short_uid()}"
44+
s3_client.upload_file(
45+
os.path.join(
46+
os.path.dirname(__file__), "../../templates/nested-stack-output-refs.nested.yaml"
47+
),
48+
Bucket=bucket_name,
49+
Key=key,
50+
)
51+
result = deploy_cfn_template(
52+
template_path=os.path.join(
53+
os.path.dirname(__file__), "../../templates/nested-stack-output-refs.yaml"
54+
),
55+
template_mapping={
56+
"s3_bucket_url": f"/{bucket_name}/{key}",
57+
"nested_bucket_name": nested_bucket_name,
58+
},
59+
)
60+
61+
nested_stack_id = result.outputs["CustomNestedStackId"]
62+
nested_stack_details = cfn_client.describe_stacks(StackName=nested_stack_id)
63+
nested_stack_outputs = nested_stack_details["Stacks"][0]["Outputs"]
64+
assert "InnerCustomOutput" not in result.outputs
65+
assert (
66+
nested_bucket_name
67+
== [
68+
o["OutputValue"] for o in nested_stack_outputs if o["OutputKey"] == "InnerCustomOutput"
69+
][0]
70+
)
71+
assert f"{nested_bucket_name}-suffix" == result.outputs["CustomOutput"]

0 commit comments

Comments
 (0)