Package: aws-cdk.assertions
Assertions---AWS CDK v1 has reached End-of-Support on 2023-06-01.
This package is no longer being updated, and users should migrate to AWS CDK v2.For more information on how to migrate, see theMigrating to AWS CDK v2guide.If you're migrating from the oldassertlibrary, the migration guide can be found inour GitHub repository.Functions for writing test asserting against CDK applications, with focus on CloudFormation templates.TheTemplateclass includes a set of methods for writing assertions against CloudFormation templates. Use one of theTemplate.fromXxx()static methods to create an instance of this class.To createTemplatefrom CDK stack, start off with:fromaws_cdk.coreimportStackfromaws_cdk.assertionsimportTemplatestack=Stack()# ...template=Template.from_stack(stack)Alternatively, assertions can be run on an existing CloudFormation template -template_json="{\"Resources\": ... }"# The CloudFormation template as JSON serialized string.template=Template.from_string(template_json)Full Template MatchThe simplest assertion would be to assert that the template matches a given
template.template.template_matches({"Resources":{"BarLogicalId":{"Type":"Foo::Bar","Properties":{"Baz":"Qux"}}}})By default, thetemplateMatches()API will use the an 'object-like' comparison,
which means that it will allow for the actual template to be a superset of the
given expectation. SeeSpecial Matchersfor details on how
to change this.Snapshot testing is a common technique to store a snapshot of the output and
compare it during future changes. Since CloudFormation templates are human readable,
they are a good target for snapshot testing.ThetoJSON()method on theTemplatecan be used to produce a well formatted JSON
of the CloudFormation template that can be used as a snapshot.SeeSnapshot Testing in JestandSnapshot
Testing in Java.Counting ResourcesThis module allows asserting the number of resources of a specific type found
in a template.template.resource_count_is("Foo::Bar",2)Resource Matching & RetrievalBeyond resource counting, the module also allows asserting that a resource with
specific properties are present.The following code asserts that thePropertiessection of a resource of typeFoo::Barcontains the specified properties -template.has_resource_properties("Foo::Bar",{"Foo":"Bar","Baz":5,"Qux":["Waldo","Fred"]})Alternatively, if you would like to assert the entire resource definition, you
can use thehasResource()API.template.has_resource("Foo::Bar",{"Properties":{"Foo":"Bar"},"DependsOn":["Waldo","Fred"]})Beyond assertions, the module provides APIs to retrieve matching resources.
ThefindResources()API is complementary to thehasResource()API, except,
instead of asserting its presence, it returns the set of matching resources.By default, thehasResource()andhasResourceProperties()APIs perform deep
partial object matching. This behavior can be configured using matchers.
See subsequent section onspecial matchers.Output and Mapping sectionsThe module allows you to assert that the CloudFormation template contains an Output
that matches specific properties. The following code asserts that a template contains
an Output with alogicalIdofFooand the specified properties -expected={"Value":"Bar","Export":{"Name":"ExportBaz"}}template.has_output("Foo",expected)If you want to match against all Outputs in the template, use*as thelogicalId.template.has_output("*",{"Value":"Bar","Export":{"Name":"ExportBaz"}})findOutputs()will return a set of outputs that match thelogicalIdandprops,
and you can use the'*'special case as well.result=template.find_outputs("*",{"Value":"Fred"})expect(result.Foo).to_equal({"Value":"Fred","Description":"FooFred"})expect(result.Bar).to_equal({"Value":"Fred","Description":"BarFred"})The APIshasMapping(),findMappings(),hasCondition(), andhasCondtions()provide similar functionalities.Special MatchersThe expectation provided to thehasXxx(),findXxx()andtemplateMatches()APIs, besides carrying literal values, as seen in the above examples, also accept
special matchers.They are available as part of theMatchclass.Object MatchersTheMatch.objectLike()API can be used to assert that the target is a superset
object of the provided pattern.
This API will perform a deep partial match on the target.
Deep partial matching is where objects are matched partially recursively. At each
level, the list of keys in the target is a subset of the provided pattern.# Given a template -# {#   "Resources": {#     "MyBar": {#       "Type": "Foo::Bar",#       "Properties": {#         "Fred": {#           "Wobble": "Flob",#           "Bob": "Cat"#         }#       }#     }#   }# }# The following will NOT throw an assertion errortemplate.has_resource_properties("Foo::Bar",{"Fred":Match.object_like({"Wobble":"Flob"})})# The following will throw an assertion errortemplate.has_resource_properties("Foo::Bar",{"Fred":Match.object_like({"Brew":"Coffee"})})TheMatch.objectEquals()API can be used to assert a target as a deep exact
match.Presence and AbsenceTheMatch.absent()matcher can be used to specify that a specific
value should not exist on the target. This can be used withinMatch.objectLike()or outside of any matchers.# Given a template -# {#   "Resources": {#     "MyBar": {#       "Type": "Foo::Bar",#       "Properties": {#         "Fred": {#           "Wobble": "Flob",#         }#       }#     }#   }# }# The following will NOT throw an assertion errortemplate.has_resource_properties("Foo::Bar",{"Fred":Match.object_like({"Bob":Match.absent()})})# The following will throw an assertion errortemplate.has_resource_properties("Foo::Bar",{"Fred":Match.object_like({"Wobble":Match.absent()})})TheMatch.anyValue()matcher can be used to specify that a specific value should be found
at the location. This matcher will fail if when the target location has null-ish values
(i.e.,nullorundefined).This matcher can be combined with any of the other matchers.# Given a template -# {#   "Resources": {#     "MyBar": {#       "Type": "Foo::Bar",#       "Properties": {#         "Fred": {#           "Wobble": ["Flob", "Flib"],#         }#       }#     }#   }# }# The following will NOT throw an assertion errortemplate.has_resource_properties("Foo::Bar",{"Fred":{"Wobble":[Match.any_value(),Match.any_value()]}})# The following will throw an assertion errortemplate.has_resource_properties("Foo::Bar",{"Fred":{"Wimble":Match.any_value()}})Array MatchersTheMatch.arrayWith()API can be used to assert that the target is equal to or a subset
of the provided pattern array.
This API will perform subset match on the target.# Given a template -# {#   "Resources": {#     "MyBar": {#       "Type": "Foo::Bar",#       "Properties": {#         "Fred": ["Flob", "Cat"]#       }#     }#   }# }# The following will NOT throw an assertion errortemplate.has_resource_properties("Foo::Bar",{"Fred":Match.array_with(["Flob"])})# The following will throw an assertion errortemplate.has_resource_properties("Foo::Bar",Match.object_like({"Fred":Match.array_with(["Wobble"])}))Note:The list of items in the pattern array should be in order as they appear in the
target array. Out of order will be recorded as a match failure.Alternatively, theMatch.arrayEquals()API can be used to assert that the target is
exactly equal to the pattern array.String MatchersTheMatch.stringLikeRegexp()API can be used to assert that the target matches the
provided regular expression.# Given a template -# {#   "Resources": {#     "MyBar": {#       "Type": "Foo::Bar",#       "Properties": {#         "Template": "const includeHeaders = true;"#       }#     }#   }# }# The following will NOT throw an assertion errortemplate.has_resource_properties("Foo::Bar",{"Template":Match.string_like_regexp("includeHeaders = (true|false)")})# The following will throw an assertion errortemplate.has_resource_properties("Foo::Bar",{"Template":Match.string_like_regexp("includeHeaders = null")})Not MatcherThe not matcher inverts the search pattern and matches all patterns in the path that does
not match the pattern specified.# Given a template -# {#   "Resources": {#     "MyBar": {#       "Type": "Foo::Bar",#       "Properties": {#         "Fred": ["Flob", "Cat"]#       }#     }#   }# }# The following will NOT throw an assertion errortemplate.has_resource_properties("Foo::Bar",{"Fred":Match.not(["Flob"])})# The following will throw an assertion errortemplate.has_resource_properties("Foo::Bar",Match.object_like({"Fred":Match.not(["Flob","Cat"])}))Serialized JSONOften, we find that some CloudFormation Resource types declare properties as a string,
but actually expect JSON serialized as a string.
For example, theBuildSpecproperty ofAWS::CodeBuild::Project,
theDefinitionproperty ofAWS::StepFunctions::StateMachine,
to name a couple.TheMatch.serializedJson()matcher allows deep matching within a stringified JSON.# Given a template -# {#   "Resources": {#     "MyBar": {#       "Type": "Foo::Bar",#       "Properties": {#         "Baz": "{ \"Fred\": [\"Waldo\", \"Willow\"] }"#       }#     }#   }# }# The following will NOT throw an assertion errortemplate.has_resource_properties("Foo::Bar",{"Baz":Match.serialized_json({"Fred":Match.array_with(["Waldo"])})})# The following will throw an assertion errortemplate.has_resource_properties("Foo::Bar",{"Baz":Match.serialized_json({"Fred":["Waldo","Johnny"]})})Capturing ValuesThe matcher APIs documented above allow capturing values in the matching entry
(Resource, Output, Mapping, etc.). The following code captures a string from a
matching resource.# Given a template -# {#   "Resources": {#     "MyBar": {#       "Type": "Foo::Bar",#       "Properties": {#         "Fred": ["Flob", "Cat"],#         "Waldo": ["Qix", "Qux"],#       }#     }#   }# }fred_capture=Capture()waldo_capture=Capture()template.has_resource_properties("Foo::Bar",{"Fred":fred_capture,"Waldo":["Qix",waldo_capture]})fred_capture.as_array()# returns ["Flob", "Cat"]waldo_capture.as_string()With captures, a nested pattern can also be specified, so that only targets
that match the nested pattern will be captured. This pattern can be literals or
further Matchers.# Given a template -# {#   "Resources": {#     "MyBar1": {#       "Type": "Foo::Bar",#       "Properties": {#         "Fred": ["Flob", "Cat"],#       }#     }#     "MyBar2": {#       "Type": "Foo::Bar",#       "Properties": {#         "Fred": ["Qix", "Qux"],#       }#     }#   }# }capture=Capture(Match.array_with(["Cat"]))template.has_resource_properties("Foo::Bar",{"Fred":capture})capture.as_array()When multiple resources match the given condition, eachCapturedefined in
the condition will capture all matching values. They can be paged through using
thenext()API. The following example illustrates this -# Given a template -# {#   "Resources": {#     "MyBar": {#       "Type": "Foo::Bar",#       "Properties": {#         "Fred": "Flob",#       }#     },#     "MyBaz": {#       "Type": "Foo::Bar",#       "Properties": {#         "Fred": "Quib",#       }#     }#   }# }fred_capture=Capture()template.has_resource_properties("Foo::Bar",{"Fred":fred_capture})fred_capture.as_string()# returns "Flob"fred_capture.next()# returns truefred_capture.as_string()Asserting AnnotationsIn addition to template matching, we provide an API for annotation matching.Annotationscan be added via theAspectsAPI. You can learn more about Aspectshere.Say you have aMyAspectand aMyStackthat usesMyAspect:importaws_cdk.coreascdkfromconstructsimportConstruct,IConstructclassMyAspect(cdk.IAspect):defvisit(self,node):ifnodeinstanceofcdk.CfnResource&&node.cfn_resource_type=="Foo::Bar":self.error(node,"we do not want a Foo::Bar resource")deferror(self,node,message):cdk.Annotations.of(node).add_error(message)classMyStack(cdk.Stack):def__init__(self,scope,id):super().__init__(scope,id)stack=cdk.Stack()cdk.CfnResource(stack,"Foo",type="Foo::Bar",properties={"Fred":"Thud"})cdk.Aspects.of(stack).add(MyAspect())We can then assert that the stack contains the expected Error:# import { Annotations } from '@aws-cdk/assertions';Annotations.from_stack(stack).has_error("/Default/Foo","we do not want a Foo::Bar resource")Here are the available APIs forAnnotations:hasError(),hasNoError(), andfindError()hasWarning(),hasNoWarning(), andfindWarning()hasInfo(),hasNoInfo(), andfindInfo()The correspondingfindXxx()API is complementary to thehasXxx()API, except instead
of asserting its presence, it returns the set of matching messages.In addition, this suite of APIs is compatable withMatchersfor more fine-grained control.
For example, the following assertion works as well:Annotations.from_stack(stack).has_error("/Default/Foo",Match.string_like_regexp(".*Foo::Bar.*"))