Skip to content

Commit

Permalink
Test multiple stacks
Browse files Browse the repository at this point in the history
  • Loading branch information
exoego committed Feb 26, 2024
1 parent bacb3e8 commit e764489
Show file tree
Hide file tree
Showing 2 changed files with 213 additions and 0 deletions.
171 changes: 171 additions & 0 deletions src/__tests__/__snapshots__/stacks.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Multiple stacks in a single app dependent stacks 1`] = `
{
"Resources": {
"BarA6AB415C": {
"DependsOn": [
"BarServiceRole3E5F94C9",
],
"Properties": {
"Code": {
"S3Bucket": {
"Fn::Sub": "cdk-hnb659fds-assets-\${AWS::AccountId}-\${AWS::Region}",
},
"S3Key": "7ecc77636deefb31954ea2e6aadf6d2c361a2943535a678d65f46a8de5e1f503.zip",
},
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"BarServiceRole3E5F94C9",
"Arn",
],
},
"Runtime": "nodejs20.x",
},
"Type": "AWS::Lambda::Function",
},
"BarServiceRole3E5F94C9": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com",
},
},
],
"Version": "2012-10-17",
},
"ManagedPolicyArns": [
{
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition",
},
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
],
],
},
],
},
"Type": "AWS::IAM::Role",
},
},
}
`;

exports[`Multiple stacks in a single app dependent stacks 2`] = `
{
"Resources": {
"BarA6AB415C": {
"DependsOn": [
"BarServiceRole3E5F94C9",
],
"Properties": {
"Code": {
"S3Bucket": {
"Fn::Sub": "cdk-hnb659fds-assets-\${AWS::AccountId}-\${AWS::Region}",
},
"S3Key": "7ecc77636deefb31954ea2e6aadf6d2c361a2943535a678d65f46a8de5e1f503.zip",
},
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"BarServiceRole3E5F94C9",
"Arn",
],
},
"Runtime": "nodejs20.x",
},
"Type": "AWS::Lambda::Function",
},
"BarServiceRole3E5F94C9": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com",
},
},
],
"Version": "2012-10-17",
},
"ManagedPolicyArns": [
{
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition",
},
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
],
],
},
],
},
"Type": "AWS::IAM::Role",
},
},
}
`;

exports[`Multiple stacks in a single app one stack referencing the other 1`] = `
{
"Outputs": {
"ExportsOutputFnGetAttFooDFE0DD70Arn2A3BEF77": {
"Export": {
"Name": "S3:ExportsOutputFnGetAttFooDFE0DD70Arn2A3BEF77",
},
"Value": {
"Fn::GetAtt": [
"FooDFE0DD70",
"Arn",
],
},
},
},
"Resources": {
"FooDFE0DD70": {
"DeletionPolicy": "Retain",
"Type": "AWS::S3::Bucket",
"UpdateReplacePolicy": "Retain",
},
},
}
`;

exports[`Multiple stacks in a single app one stack referencing the other 2`] = `
{
"Outputs": {
"ExportsOutputFnGetAttFooDFE0DD70Arn2A3BEF77": {
"Export": {
"Name": "S3:ExportsOutputFnGetAttFooDFE0DD70Arn2A3BEF77",
},
"Value": {
"Fn::GetAtt": [
"FooDFE0DD70",
"Arn",
],
},
},
},
"Resources": {
"FooDFE0DD70": {
"DeletionPolicy": "Retain",
"Type": "AWS::S3::Bucket",
"UpdateReplacePolicy": "Retain",
},
},
}
`;
42 changes: 42 additions & 0 deletions src/__tests__/stacks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Stack, App } from "aws-cdk-lib";
import { Bucket } from "aws-cdk-lib/aws-s3";
import { Function, Code, Runtime } from "aws-cdk-lib/aws-lambda";
import "../index";
import path from "path";

describe("Multiple stacks in a single app", () => {
test("dependent stacks", () => {
const app = new App();
const s3Stack = new Stack(app, "S3");
new Bucket(s3Stack, "Foo");

const lambdaStack = new Stack(app, "Lambda");
new Function(lambdaStack, "Bar", {
code: Code.fromAsset(path.join(__dirname, "fixtures", "lambda")),
handler: "index.handler",
runtime: Runtime.NODEJS_20_X,
});

expect(s3Stack).toMatchCdkSnapshot();
expect(lambdaStack).toMatchCdkSnapshot();
});

test("one stack referencing the other", () => {
const app = new App();
const s3Stack = new Stack(app, "S3");
const bucket = new Bucket(s3Stack, "Foo");

const lambdaStack = new Stack(app, "Lambda");
new Function(lambdaStack, "Bar", {
code: Code.fromAsset(path.join(__dirname, "fixtures", "lambda")),
handler: "index.handler",
runtime: Runtime.NODEJS_20_X,
environment: {
bucket: bucket.bucketArn,
},
});

expect(s3Stack).toMatchCdkSnapshot();
expect(lambdaStack).toMatchCdkSnapshot();
});
});

0 comments on commit e764489

Please sign in to comment.