Skip navigation links

Package software.amazon.awscdk.integtests

integ-tests

See: Description

Package software.amazon.awscdk.integtests Description

integ-tests

Usage

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();
 
 Stack stack = new Stack(app, "stack");
 
 IntegTestCase differentArchsCase = IntegTestCase.Builder.create(stack, "DifferentArchitectures")
         .stacks(List.of(
             new StackUnderTest(app, "Stack1", new StackUnderTestProps()
                     .architecture(Architecture.ARM_64)
                     ),
             new StackUnderTest(app, "Stack2", new StackUnderTestProps()
                     .architecture(Architecture.X86_64)
                     )))
         .build();
 
 // There must be exactly one instance of TestCase per file
 // There must be exactly one instance of TestCase per file
 IntegTest.Builder.create(app, "integ-test")
 
         // Register as many test cases as you want here
         .testCases(List.of(differentArchsCase))
         .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");
 
 IntegTestCase testCase = IntegTestCase.Builder.create(stack, "CustomizedDeploymentWorkflow")
         .stacks(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();
 
 IntegTest.Builder.create(app, "integ-test")
         .testCases(List.of(testCase))
         .build();
 
Skip navigation links

Copyright © 2022. All rights reserved.