Interface | Description |
---|---|
AssertionRequest |
(experimental) A request to make an assertion that the actual value matches the expected.
|
AssertionResult |
(experimental) The result of an Assertion wrapping the actual result data in another struct.
|
AssertionResultData |
(experimental) The result of an assertion.
|
AwsApiCallOptions |
(experimental) Options to perform an AWS JavaScript V2 API call.
|
AwsApiCallProps |
(experimental) Options for creating an SDKQuery provider.
|
AwsApiCallRequest |
(experimental) A AWS JavaScript SDK V2 request.
|
AwsApiCallResult |
(experimental) The result from a SdkQuery.
|
EqualsAssertionProps |
(experimental) Options for an EqualsAssertion.
|
IAwsApiCall |
(experimental) Interface for creating a custom resource that will perform an API call using the AWS SDK.
|
IAwsApiCall.Jsii$Default |
Internal default implementation for
IAwsApiCall . |
IDeployAssert |
(experimental) Interface that allows for registering a list of assertions that should be performed on a construct.
|
IDeployAssert.Jsii$Default |
Internal default implementation for
IDeployAssert . |
IntegTestCaseProps |
(experimental) Properties of an integration test case.
|
IntegTestCaseStackProps |
(experimental) Properties of an integration test case stack.
|
IntegTestProps |
(experimental) Integration test properties.
|
LambdaInvokeFunctionProps |
(experimental) Options to pass to the Lambda invokeFunction API call.
|
Class | Description |
---|---|
ActualResult |
(experimental) Represents the "actual" results to compare.
|
AssertionRequest.Builder |
A builder for
AssertionRequest |
AssertionRequest.Jsii$Proxy |
An implementation for
AssertionRequest |
AssertionResult.Builder |
A builder for
AssertionResult |
AssertionResult.Jsii$Proxy |
An implementation for
AssertionResult |
AssertionResultData.Builder |
A builder for
AssertionResultData |
AssertionResultData.Jsii$Proxy |
An implementation for
AssertionResultData |
AssertionsProvider |
(experimental) Represents an assertions provider.
|
AwsApiCall |
(experimental) Construct that creates a custom resource that will perform a query using the AWS SDK.
|
AwsApiCall.Builder |
(experimental) A fluent builder for
AwsApiCall . |
AwsApiCallOptions.Builder |
A builder for
AwsApiCallOptions |
AwsApiCallOptions.Jsii$Proxy |
An implementation for
AwsApiCallOptions |
AwsApiCallProps.Builder |
A builder for
AwsApiCallProps |
AwsApiCallProps.Jsii$Proxy |
An implementation for
AwsApiCallProps |
AwsApiCallRequest.Builder |
A builder for
AwsApiCallRequest |
AwsApiCallRequest.Jsii$Proxy |
An implementation for
AwsApiCallRequest |
AwsApiCallResult.Builder |
A builder for
AwsApiCallResult |
AwsApiCallResult.Jsii$Proxy |
An implementation for
AwsApiCallResult |
EqualsAssertion |
(experimental) Construct that creates a CustomResource to assert that two values are equal.
|
EqualsAssertion.Builder |
(experimental) A fluent builder for
EqualsAssertion . |
EqualsAssertionProps.Builder |
A builder for
EqualsAssertionProps |
EqualsAssertionProps.Jsii$Proxy |
An implementation for
EqualsAssertionProps |
ExpectedResult |
(experimental) Represents the "expected" results to compare.
|
IAwsApiCall.Jsii$Proxy |
A proxy class which represents a concrete javascript instance of this type.
|
IDeployAssert.Jsii$Proxy |
A proxy class which represents a concrete javascript instance of this type.
|
IntegTest |
(experimental) A collection of test cases.
|
IntegTest.Builder |
(experimental) A fluent builder for
IntegTest . |
IntegTestCase |
(experimental) An integration test case.
|
IntegTestCase.Builder |
(experimental) A fluent builder for
IntegTestCase . |
IntegTestCaseProps.Builder |
A builder for
IntegTestCaseProps |
IntegTestCaseProps.Jsii$Proxy |
An implementation for
IntegTestCaseProps |
IntegTestCaseStack |
(experimental) An integration test case stack.
|
IntegTestCaseStack.Builder |
(experimental) A fluent builder for
IntegTestCaseStack . |
IntegTestCaseStackProps.Builder |
A builder for
IntegTestCaseStackProps |
IntegTestCaseStackProps.Jsii$Proxy |
An implementation for
IntegTestCaseStackProps |
IntegTestProps.Builder |
A builder for
IntegTestProps |
IntegTestProps.Jsii$Proxy |
An implementation for
IntegTestProps |
LambdaInvokeFunction |
(experimental) An AWS Lambda Invoke function API call.
|
LambdaInvokeFunction.Builder |
(experimental) A fluent builder for
LambdaInvokeFunction . |
LambdaInvokeFunctionProps.Builder |
A builder for
LambdaInvokeFunctionProps |
LambdaInvokeFunctionProps.Jsii$Proxy |
An implementation for
LambdaInvokeFunctionProps |
Match |
(experimental) Partial and special matching during assertions.
|
Enum | Description |
---|---|
AssertionType |
(experimental) The type of assertion to perform.
|
InvocationType |
(experimental) The type of invocation.
|
LogType |
(experimental) Set to Tail to include the execution log in the response.
|
Status |
(experimental) The status of the assertion.
|
---
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.
This library is meant to be used in combination with the integ-runner CLI to enable users to write and execute integration tests for AWS CDK Constructs.
An integration test should be defined as a CDK application, and there should be a 1:1 relationship between an integration test and a CDK application.
So for example, in order to create an integration test called my-function
we would need to create a file to contain our integration test application.
test/integ.my-function.ts
App app = new App(); Stack stack = new Stack(); Function.Builder.create(stack, "MyFunction") .runtime(Runtime.NODEJS_12_X) .handler("index.handler") .code(Code.fromAsset(join(__dirname, "lambda-handler"))) .build();
This is a self contained CDK application which we could deploy by running
cdk deploy --app 'node test/integ.my-function.js'
In order to turn this into an integration test, all that is needed is to
use the IntegTest
construct.
App app; Stack stack; IntegTest.Builder.create(app, "Integ").testCases(List.of(stack)).build();
You will notice that the stack
is registered to the IntegTest
as a test case.
Each integration test can contain multiple test cases, which are just instances
of a stack. See the Usage section for more details.
Suppose you have a simple stack, that only encapsulates a Lambda function with a certain handler:
public class StackUnderTestProps extends StackProps { private Architecture architecture; public Architecture getArchitecture() { return this.architecture; } public StackUnderTestProps architecture(Architecture architecture) { this.architecture = architecture; return this; } } public class StackUnderTest extends Stack { public StackUnderTest(Construct scope, String id, StackUnderTestProps props) { super(scope, id, props); Function.Builder.create(this, "Handler") .runtime(Runtime.NODEJS_12_X) .handler("index.handler") .code(Code.fromAsset(join(__dirname, "lambda-handler"))) .architecture(props.getArchitecture()) .build(); } }
You may want to test this stack under different conditions. For example, we want
this stack to be deployed correctly, regardless of the architecture we choose
for the Lambda function. In particular, it should work for both ARM_64
and
X86_64
. So you can create an IntegTestCase
that exercises both scenarios:
public class StackUnderTestProps extends StackProps { private Architecture architecture; public Architecture getArchitecture() { return this.architecture; } public StackUnderTestProps architecture(Architecture architecture) { this.architecture = architecture; return this; } } public class StackUnderTest extends Stack { public StackUnderTest(Construct scope, String id, StackUnderTestProps props) { super(scope, id, props); Function.Builder.create(this, "Handler") .runtime(Runtime.NODEJS_12_X) .handler("index.handler") .code(Code.fromAsset(join(__dirname, "lambda-handler"))) .architecture(props.getArchitecture()) .build(); } } // Beginning of the test suite App app = new App(); IntegTest.Builder.create(app, "DifferentArchitectures") .testCases(List.of( new StackUnderTest(app, "Stack1", new StackUnderTestProps() .architecture(Architecture.ARM_64) ), new StackUnderTest(app, "Stack2", new StackUnderTestProps() .architecture(Architecture.X86_64) ))) .build();
This is all the instruction you need for the integration test runner to know which stacks to synthesize, deploy and destroy. But you may also need to customize the behavior of the runner by changing its parameters. For example:
App app = new App(); Stack stackUnderTest = new Stack(app, "StackUnderTest"); Stack stack = new Stack(app, "stack"); IntegTest testCase = IntegTest.Builder.create(app, "CustomizedDeploymentWorkflow") .testCases(List.of(stackUnderTest)) .diffAssets(true) .stackUpdateWorkflow(true) .cdkCommandOptions(CdkCommands.builder() .deploy(DeployCommand.builder() .args(DeployOptions.builder() .requireApproval(RequireApproval.NEVER) .json(true) .build()) .build()) .destroy(DestroyCommand.builder() .args(DestroyOptions.builder() .force(true) .build()) .build()) .build()) .build();
In the majority of cases an integration test will contain a single IntegTestCase
.
By default when you create an IntegTest
an IntegTestCase
is created for you
and all of your test cases are registered to this IntegTestCase
. The IntegTestCase
and IntegTestCaseStack
constructs are only needed when it is necessary to
defined different options for individual test cases.
For example, you might want to have one test case where diffAssets
is enabled.
App app; Stack stackUnderTest; IntegTestCaseStack testCaseWithAssets = IntegTestCaseStack.Builder.create(app, "TestCaseAssets") .diffAssets(true) .build(); IntegTest.Builder.create(app, "Integ").testCases(List.of(stackUnderTest, testCaseWithAssets)).build();
This library also provides a utility to make assertions against the infrastructure that the integration test deploys.
There are two main scenarios in which assertions are created.
integ-runner
In this case you would create an integration test using the IntegTest
construct and then make assertions using the assert
property.
You should not utilize the assertion constructs directly, but should instead use the methods
on IntegTest.assert
.
App app; Stack stack; IntegTest integ = IntegTest.Builder.create(app, "Integ").testCases(List.of(stack)).build(); integ.assertions.awsApiCall("S3", "getObject");
In this case you may be using assertions as part of a normal CDK deployment in order to make an assertion on the infrastructure before the deployment is considered successful. In this case you can utilize the assertions constructs directly.
Stack myAppStack; AwsApiCall.Builder.create(myAppStack, "GetObject") .service("S3") .api("getObject") .build();
Assertions are created by using the DeployAssert
construct. This construct creates it's own Stack
separate from
any stacks that you create as part of your integration tests. This Stack
is treated differently from other stacks
by the integ-runner
tool. For example, this stack will not be diffed by the integ-runner
.
DeployAssert
also provides utilities to register your own assertions.
CustomResource myCustomResource; Stack stack; App app; IntegTest integ = IntegTest.Builder.create(app, "Integ").testCases(List.of(stack)).build(); integ.assertions.expect("CustomAssertion", ExpectedResult.objectLike(Map.of("foo", "bar")), ActualResult.fromCustomResource(myCustomResource, "data"));
In the above example an assertion is created that will trigger a user defined CustomResource
and assert that the data
attribute is equal to { foo: 'bar' }
.
A common method to retrieve the "actual" results to compare with what is expected is to make an AWS API call to receive some data. This library does this by utilizing CloudFormation custom resources which means that CloudFormation will call out to a Lambda Function which will use the AWS JavaScript SDK to make the API call.
This can be done by using the class directory (in the case of a normal deployment):
Stack stack; AwsApiCall.Builder.create(stack, "MyAssertion") .service("SQS") .api("receiveMessage") .parameters(Map.of( "QueueUrl", "url")) .build();
Or by using the awsApiCall
method on DeployAssert
(when writing integration tests):
App app; Stack stack; IntegTest integ = IntegTest.Builder.create(app, "Integ") .testCases(List.of(stack)) .build(); integ.assertions.awsApiCall("SQS", "receiveMessage", Map.of( "QueueUrl", "url"));
This library currently provides the ability to assert that two values are equal
to one another by utilizing the EqualsAssertion
class. This utilizes a Lambda
backed CustomResource
which in tern uses the Match utility from the
@aws-cdk/assertions library.
App app; Stack stack; Queue queue; IFunction fn; IntegTest integ = IntegTest.Builder.create(app, "Integ") .testCases(List.of(stack)) .build(); integ.assertions.invokeFunction(LambdaInvokeFunctionProps.builder() .functionName(fn.getFunctionName()) .invocationType(InvocationType.EVENT) .payload(JSON.stringify(Map.of("status", "OK"))) .build()); IAwsApiCall message = integ.assertions.awsApiCall("SQS", "receiveMessage", Map.of( "QueueUrl", queue.getQueueUrl(), "WaitTimeSeconds", 20)); message.assertAtPath("Messages.0.Body", ExpectedResult.objectLike(Map.of( "requestContext", Map.of( "condition", "Success"), "requestPayload", Map.of( "status", "OK"), "responseContext", Map.of( "statusCode", 200), "responsePayload", "success")));
integ-tests
also provides a Match
utility similar to the @aws-cdk/assertions
module. Match
can be used to construct the ExpectedResult
.
AwsApiCall message; message.expect(ExpectedResult.objectLike(Map.of( "Messages", Match.arrayWith(List.of(Map.of( "Body", Map.of( "Values", Match.arrayWith(List.of(Map.of("Asdf", 3))), "Message", Match.stringLikeRegexp("message"))))))));
In this example there is a Lambda Function that is invoked and we assert that the payload that is returned is equal to '200'.
IFunction lambdaFunction; App app; Stack stack = new Stack(app, "cdk-integ-lambda-bundling"); IntegTest integ = IntegTest.Builder.create(app, "IntegTest") .testCases(List.of(stack)) .build(); IAwsApiCall invoke = integ.assertions.invokeFunction(LambdaInvokeFunctionProps.builder() .functionName(lambdaFunction.getFunctionName()) .build()); invoke.expect(ExpectedResult.objectLike(Map.of( "Payload", "200")));
In this example there is a StepFunctions state machine that is executed and then we assert that the result of the execution is successful.
App app; Stack stack; IStateMachine sm; IntegTest testCase = IntegTest.Builder.create(app, "IntegTest") .testCases(List.of(stack)) .build(); // Start an execution IAwsApiCall start = testCase.assertions.awsApiCall("StepFunctions", "startExecution", Map.of( "stateMachineArn", sm.getStateMachineArn())); // describe the results of the execution IAwsApiCall describe = testCase.assertions.awsApiCall("StepFunctions", "describeExecution", Map.of( "executionArn", start.getAttString("executionArn"))); // assert the results describe.expect(ExpectedResult.objectLike(Map.of( "status", "SUCCEEDED")));