Skip to main content

An assertion library for use with CDK Apps

Project description

Assertions

---

cdk-constructs: Experimental

The APIs of higher level constructs in this module are experimental and under active development. They are subject to non-backward compatible changes or removal in any future version. These are not subject to the Semantic Versioning model and breaking changes will be announced in the release notes. This means that while you may use them, you may need to update your source code when upgrading to a newer version of this package.


Functions for writing test asserting against CDK applications, with focus on CloudFormation templates.

The TemplateAssertions class includes a set of methods for writing assertions against CloudFormation templates. Use one of the TemplateAssertions.fromXxx() static methods to create an instance of this class.

To create TemplateAssertions from CDK stack, start off with:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
from aws_cdk.core import Stack
from aws_cdk.assertions import TemplateAssertions

stack = Stack(...)
assert = TemplateAssertions.from_stack(stack)

Alternatively, assertions can be run on an existing CloudFormation template -

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
template = fs.read_file_sync("/path/to/template/file")
assert = TemplateAssertions.from_string(template)

Full Template Match

The simplest assertion would be to assert that the template matches a given template.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
assert.template_matches(
    Resources={
        "Type": "Foo::Bar",
        "Properties": {
            "Baz": "Qux"
        }
    }
)

Counting Resources

This module allows asserting the number of resources of a specific type found in a template.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
assert.resource_count_is("Foo::Bar", 2)

Resource Matching

Beyond resource counting, the module also allows asserting that a resource with specific properties are present.

The following code asserts that the Properties section of a resource of type Foo::Bar contains the specified properties -

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
assert.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 the hasResource() API.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
assert.has_resource("Foo::Bar",
    Properties={"Foo": "Bar"},
    DependsOn=["Waldo", "Fred"]
)

By default, the hasResource() and hasResourceProperties() APIs perform deep partial object matching. This behavior can be configured using matchers. See subsequent section on special matchers.

Special Matchers

The expectation provided to the hasResourceXXX() methods, besides carrying literal values, as seen in the above examples, can also have special matchers encoded. They are available as part of the Match class.

Object Matchers

The Match.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.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
# Given a template -
# {
#   "Resources": {
#     "MyBar": {
#       "Type": "Foo::Bar",
#       "Properties": {
#         "Fred": {
#           "Wobble": "Flob",
#           "Bob": "Cat"
#         }
#       }
#     }
#   }
# }

# The following will NOT throw an assertion error
assert.has_resource_properties("Foo::Bar",
    Fred=Match.object_like(
        Wobble="Flob"
    )
)

# The following will throw an assertion error
assert.has_resource_properties("Foo::Bar",
    Fred=Match.object_like(
        Brew="Coffee"
    )
)

The Match.objectEquals() API can be used to assert a target as a deep exact match.

In addition, the Match.absentProperty() can be used to specify that a specific property should not exist on the target. This can be used within Match.objectLike() or outside of any matchers.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
# Given a template -
# {
#   "Resources": {
#     "MyBar": {
#       "Type": "Foo::Bar",
#       "Properties": {
#         "Fred": {
#           "Wobble": "Flob",
#         }
#       }
#     }
#   }
# }

# The following will NOT throw an assertion error
assert.has_resource_properties("Foo::Bar",
    Fred=Match.object_like(
        Bob=Match.absent_property()
    )
)

# The following will throw an assertion error
assert.has_resource_properties("Foo::Bar",
    Fred=Match.object_like(
        Wobble=Match.absent_property()
    )
)

Array Matchers

The Match.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.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
# Given a template -
# {
#   "Resources": {
#     "MyBar": {
#       "Type": "Foo::Bar",
#       "Properties": {
#         "Fred": ["Flob", "Cat"]
#       }
#     }
#   }
# }

# The following will NOT throw an assertion error
assert.has_resource_properties("Foo::Bar",
    Fred=Match.array_with(["Flob"])
)

# The following will throw an assertion error
assert.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, the Match.arrayEquals() API can be used to assert that the target is exactly equal to the pattern array.

Strongly typed languages

Some of the APIs documented above, such as templateMatches() and hasResourceProperties() accept fluently an arbitrary JSON (like) structure its parameter. This fluency is available only in dynamically typed languages like javascript and Python.

For strongly typed languages, like Java, you can achieve similar fluency using any popular JSON deserializer. The following Java example uses Gson -

// In Java, using text blocks and Gson
import com.google.gson.Gson;

String json = """
  {
    "Foo": "Bar",
    "Baz": 5,
    "Qux": [ "Waldo", "Fred" ],
  } """;

Map expected = new Gson().fromJson(json, Map.class);
assert.hasResourceProperties("Foo::Bar", expected);

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aws-cdk.assertions-1.112.0.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aws_cdk.assertions-1.112.0-py3-none-any.whl (1.1 MB view details)

Uploaded Python 3

File details

Details for the file aws-cdk.assertions-1.112.0.tar.gz.

File metadata

  • Download URL: aws-cdk.assertions-1.112.0.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.6.5

File hashes

Hashes for aws-cdk.assertions-1.112.0.tar.gz
Algorithm Hash digest
SHA256 bbff6c4a0c4f6d68bd380241fe2a05f67f0324c70cd80de0639e553ab7bb72c0
MD5 0899f41a187837862abd72bd538eb195
BLAKE2b-256 295629f57f7569fee8d955b610aeb7f1a4ab5167f4e42f33c3d98b10b396d54d

See more details on using hashes here.

File details

Details for the file aws_cdk.assertions-1.112.0-py3-none-any.whl.

File metadata

  • Download URL: aws_cdk.assertions-1.112.0-py3-none-any.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.6.5

File hashes

Hashes for aws_cdk.assertions-1.112.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c7bb46fb7da3add8fd37ea69871fcb4d3d6698364933e119b2a67d69239f9f0d
MD5 cddd9642ba387fc40d951b3d41583610
BLAKE2b-256 7458aee0c843f3f539a30f206b2f7ac0959335209c227a919ab7805f4b08ea02

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page