Skip to content

Commit

Permalink
Techdebt: Replace sure with regular asserts in CF (#6481)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblommers authored Jul 4, 2023
1 parent 4011a68 commit d98d8cf
Show file tree
Hide file tree
Showing 8 changed files with 1,021 additions and 1,138 deletions.
52 changes: 26 additions & 26 deletions tests/test_cloudformation/test_cloudformation_custom_resources.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import boto3
import json
import requests
import sure # noqa # pylint: disable=unused-import
import time
import pytest

Expand Down Expand Up @@ -69,9 +68,9 @@ def test_create_custom_lambda_resource():

# Verify the correct Output was returned
outputs = get_outputs(cf, stack_name)
outputs.should.have.length_of(1)
outputs[0].should.have.key("OutputKey").equals("infokey")
outputs[0].should.have.key("OutputValue").equals("special value")
assert len(outputs) == 1
assert outputs[0]["OutputKey"] == "infokey"
assert outputs[0]["OutputValue"] == "special value"


@mock_cloudformation
Expand Down Expand Up @@ -104,25 +103,25 @@ def test_create_custom_lambda_resource__verify_cfnresponse_failed():
execution_failed, logs = wait_for_log_msg(
expected_msg="failed executing http.request", log_group=log_group_name
)
execution_failed.should.equal(True)
assert execution_failed is True

printed_events = [
line for line in logs if line.startswith("{'RequestType': 'Create'")
]
printed_events.should.have.length_of(1)
assert len(printed_events) == 1
original_event = json.loads(printed_events[0].replace("'", '"'))
original_event.should.have.key("RequestType").equals("Create")
original_event.should.have.key("ServiceToken") # Should equal Lambda ARN
original_event.should.have.key("ResponseURL")
original_event.should.have.key("StackId")
original_event.should.have.key("RequestId") # type UUID
original_event.should.have.key("LogicalResourceId").equals("CustomInfo")
original_event.should.have.key("ResourceType").equals("Custom::Info")
original_event.should.have.key("ResourceProperties")
original_event["ResourceProperties"].should.have.key(
"ServiceToken"
assert original_event["RequestType"] == "Create"
assert "ServiceToken" in original_event # Should equal Lambda ARN
assert "ResponseURL" in original_event
assert "StackId" in original_event
assert "RequestId" in original_event # type UUID
assert original_event["LogicalResourceId"] == "CustomInfo"
assert original_event["ResourceType"] == "Custom::Info"
assert "ResourceProperties" in original_event
assert (
"ServiceToken" in original_event["ResourceProperties"]
) # Should equal Lambda ARN
original_event["ResourceProperties"].should.have.key("MyProperty").equals("stuff")
assert original_event["ResourceProperties"]["MyProperty"] == "stuff"


@mock_cloudformation
Expand Down Expand Up @@ -155,8 +154,8 @@ def test_create_custom_lambda_resource__verify_manual_request():
)
stack_id = stack["StackId"]
stack = cf.describe_stacks(StackName=stack_id)["Stacks"][0]
stack["Outputs"].should.equal([])
stack["StackStatus"].should.equal("CREATE_IN_PROGRESS")
assert stack["Outputs"] == []
assert stack["StackStatus"] == "CREATE_IN_PROGRESS"

callback_url = f"http://cloudformation.{region_name}.amazonaws.com/cloudformation_{region_name}/cfnresponse?stack={stack_id}"
data = {
Expand All @@ -168,10 +167,10 @@ def test_create_custom_lambda_resource__verify_manual_request():
requests.post(callback_url, json=data)

stack = cf.describe_stacks(StackName=stack_id)["Stacks"][0]
stack["StackStatus"].should.equal("CREATE_COMPLETE")
stack["Outputs"].should.equal(
[{"OutputKey": "infokey", "OutputValue": "resultfromthirdpartysystem"}]
)
assert stack["StackStatus"] == "CREATE_COMPLETE"
assert stack["Outputs"] == [
{"OutputKey": "infokey", "OutputValue": "resultfromthirdpartysystem"}
]


@mock_cloudformation
Expand All @@ -186,9 +185,10 @@ def test_create_custom_lambda_resource__unknown_arn():
Capabilities=["CAPABILITY_IAM"],
)
err = exc.value.response["Error"]
err["Code"].should.equal("ValidationError")
err["Message"].should.equal(
"Template error: instance of Fn::GetAtt references undefined resource InfoFunction"
assert err["Code"] == "ValidationError"
assert (
err["Message"]
== "Template error: instance of Fn::GetAtt references undefined resource InfoFunction"
)


Expand Down
61 changes: 32 additions & 29 deletions tests/test_cloudformation/test_cloudformation_multi_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ def _verify_stack_instance(self, accnt, region=None):
StackInstanceAccount=accnt,
StackInstanceRegion=region or "us-east-1",
)["StackInstance"]
resp.should.have.key("Account").equals(accnt)
assert resp["Account"] == accnt

def _verify_queues(self, accnt, expected, region=None):
list(sqs_backends[accnt][region or "us-east-1"].queues.keys()).should.equal(
expected
assert (
list(sqs_backends[accnt][region or "us-east-1"].queues.keys()) == expected
)


Expand All @@ -122,9 +122,10 @@ def test_create_instances__specifying_accounts(self):
StackSetName=self.name, Accounts=["888781156701"], Regions=["us-east-1"]
)
err = exc.value.response["Error"]
err["Code"].should.equal("ValidationError")
err["Message"].should.equal(
"StackSets with SERVICE_MANAGED permission model can only have OrganizationalUnit as target"
assert err["Code"] == "ValidationError"
assert (
err["Message"]
== "StackSets with SERVICE_MANAGED permission model can only have OrganizationalUnit as target"
)

def test_create_instances__specifying_only_accounts_in_deployment_targets(self):
Expand All @@ -137,8 +138,8 @@ def test_create_instances__specifying_only_accounts_in_deployment_targets(self):
Regions=["us-east-1"],
)
err = exc.value.response["Error"]
err["Code"].should.equal("ValidationError")
err["Message"].should.equal("OrganizationalUnitIds are required")
assert err["Code"] == "ValidationError"
assert err["Message"] == "OrganizationalUnitIds are required"

def test_create_instances___with_invalid_ou(self):
with pytest.raises(ClientError) as exc:
Expand All @@ -148,9 +149,10 @@ def test_create_instances___with_invalid_ou(self):
Regions=["us-east-1"],
)
err = exc.value.response["Error"]
err["Code"].should.equal("ValidationError")
err["Message"].should.equal(
"1 validation error detected: Value '[unknown]' at 'deploymentTargets.organizationalUnitIds' failed to satisfy constraint: Member must satisfy constraint: [Member must have length less than or equal to 68, Member must have length greater than or equal to 6, Member must satisfy regular expression pattern: ^(ou-[a-z0-9]{4,32}-[a-z0-9]{8,32}|r-[a-z0-9]{4,32})$]"
assert err["Code"] == "ValidationError"
assert (
err["Message"]
== "1 validation error detected: Value '[unknown]' at 'deploymentTargets.organizationalUnitIds' failed to satisfy constraint: Member must satisfy constraint: [Member must have length less than or equal to 68, Member must have length greater than or equal to 6, Member must satisfy regular expression pattern: ^(ou-[a-z0-9]{4,32}-[a-z0-9]{8,32}|r-[a-z0-9]{4,32})$]"
)

def test_create_instances__single_ou(self):
Expand Down Expand Up @@ -210,12 +212,12 @@ def test_create_instances__specifying_accounts(self):
self._verify_stack_instance(self.acct01, region="us-east-2")

# acct01 has a Stack
cf_backends[self.acct01]["us-east-2"].stacks.should.have.length_of(1)
assert len(cf_backends[self.acct01]["us-east-2"].stacks) == 1
# Other acounts do not
cf_backends[self.acct02]["us-east-2"].stacks.should.have.length_of(0)
cf_backends[self.acct21]["us-east-2"].stacks.should.have.length_of(0)
cf_backends[self.acct22]["us-east-2"].stacks.should.have.length_of(0)
cf_backends[DEFAULT_ACCOUNT_ID]["us-east-2"].stacks.should.have.length_of(0)
assert len(cf_backends[self.acct02]["us-east-2"].stacks) == 0
assert len(cf_backends[self.acct21]["us-east-2"].stacks) == 0
assert len(cf_backends[self.acct22]["us-east-2"].stacks) == 0
assert len(cf_backends[DEFAULT_ACCOUNT_ID]["us-east-2"].stacks) == 0

# acct01 has a queue
self._verify_queues(self.acct01, ["testqueue"], region="us-east-2")
Expand All @@ -233,12 +235,12 @@ def test_create_instances__multiple_accounts(self):
)

# acct01 and 02 have a Stack
cf_backends[self.acct01]["us-east-2"].stacks.should.have.length_of(1)
cf_backends[self.acct02]["us-east-2"].stacks.should.have.length_of(1)
assert len(cf_backends[self.acct01]["us-east-2"].stacks) == 1
assert len(cf_backends[self.acct02]["us-east-2"].stacks) == 1
# Other acounts do not
cf_backends[self.acct21]["us-east-2"].stacks.should.have.length_of(0)
cf_backends[self.acct22]["us-east-2"].stacks.should.have.length_of(0)
cf_backends[DEFAULT_ACCOUNT_ID]["us-east-2"].stacks.should.have.length_of(0)
assert len(cf_backends[self.acct21]["us-east-2"].stacks) == 0
assert len(cf_backends[self.acct22]["us-east-2"].stacks) == 0
assert len(cf_backends[DEFAULT_ACCOUNT_ID]["us-east-2"].stacks) == 0

# acct01 and 02 have the queue
self._verify_queues(self.acct01, ["testqueue"], region="us-east-2")
Expand All @@ -265,12 +267,12 @@ def test_delete_instances(self):
)

# Act02 still has it's stack
cf_backends[self.acct02]["us-east-2"].stacks.should.have.length_of(1)
assert len(cf_backends[self.acct02]["us-east-2"].stacks) == 1
# Other Stacks are removed
cf_backends[self.acct01]["us-east-2"].stacks.should.have.length_of(0)
cf_backends[self.acct21]["us-east-2"].stacks.should.have.length_of(0)
cf_backends[self.acct22]["us-east-2"].stacks.should.have.length_of(0)
cf_backends[DEFAULT_ACCOUNT_ID]["us-east-2"].stacks.should.have.length_of(0)
assert len(cf_backends[self.acct01]["us-east-2"].stacks) == 0
assert len(cf_backends[self.acct21]["us-east-2"].stacks) == 0
assert len(cf_backends[self.acct22]["us-east-2"].stacks) == 0
assert len(cf_backends[DEFAULT_ACCOUNT_ID]["us-east-2"].stacks) == 0

# Acct02 still has it's queue as well
self._verify_queues(self.acct02, ["testqueue"], region="us-east-2")
Expand All @@ -288,7 +290,8 @@ def test_create_instances__single_ou(self):
Regions=["us-east-1"],
)
err = exc.value.response["Error"]
err["Code"].should.equal("ValidationError")
err["Message"].should.equal(
"StackSets with SELF_MANAGED permission model can only have accounts as target"
assert err["Code"] == "ValidationError"
assert (
err["Message"]
== "StackSets with SELF_MANAGED permission model can only have accounts as target"
)
Loading

0 comments on commit d98d8cf

Please sign in to comment.