Skip to content

Instantly share code, notes, and snippets.

@thorin-schiffer
Created May 13, 2022 14:14
Show Gist options
  • Save thorin-schiffer/94c7ecb54d0bca5f2fe564c65ee40da6 to your computer and use it in GitHub Desktop.
Save thorin-schiffer/94c7ecb54d0bca5f2fe564c65ee40da6 to your computer and use it in GitHub Desktop.
Testing CDK config with pytest
def get_single_properties(element):
return list(element.values())[0]
def has_role_perm(role, perm):
value = get_single_properties(role)
for prop in value["Properties"]["ManagedPolicyArns"]:
if perm in str(prop):
return True
def get_lambda(name, template):
lambdas = template.find_resources("AWS::Lambda::Function")
for logical_id, l in lambdas.items():
if l["Properties"]["FunctionName"] == name:
return l
def has_resource(resources, resource_to_find):
for resource in resources:
if resource_to_find in resource["Fn::Join"][1]:
return True
return False
@fixture
def stack():
app = core.App()
stack = MainStack(app, "test", with_dependencies=False)
return assertions.Template.from_stack(stack)
def test_lambda(stack):
stack.has_resource_properties("AWS::Lambda::Function", {"FunctionName": "test"})
test_lambda = get_lambda("test", stack)["Properties"]
assert test_lambda["Handler"] == "sentry_sdk.integrations.init_serverless_sdk.sentry_lambda_handler"
assert test_lambda["ReservedConcurrentExecutions"] == 1
def test_triggers(stack):
queues = stack.find_resources("AWS::SQS::Queue")
lambdas = stack.find_resources("AWS::Lambda::Function")
assert len(queues) == len(lambdas)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment