Skip to content

Commit

Permalink
chore: Refactor run_integration_tests.sh by adding stack language check
Browse files Browse the repository at this point in the history
  • Loading branch information
lym953 committed Oct 8, 2024
1 parent a73aea6 commit 7370866
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as lambda from "aws-cdk-lib/aws-lambda";
import { Stack, StackProps, App } from "aws-cdk-lib";
import { LambdaRestApi, LogGroupLogDestination } from "aws-cdk-lib/aws-apigateway";
import { LogGroup } from "aws-cdk-lib/aws-logs";
import { DatadogLambda } from "../../src/index";
import { DatadogLambda } from "../../../src/index";
import { Architecture } from "aws-cdk-lib/aws-lambda";

export class ExampleStack extends Stack {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Stack, StackProps, App } from "aws-cdk-lib";
import { LambdaRestApi, LogGroupLogDestination } from "aws-cdk-lib/aws-apigateway";
import { LogGroup } from "aws-cdk-lib/aws-logs";
// use the legacy Datadog class instead of the new DatadogLambda
import { Datadog } from "../../src/index";
import { Datadog } from "../../../src/index";

export class ExampleStack extends Stack {
constructor(scope: App, id: string, props?: StackProps) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as lambda from "aws-cdk-lib/aws-lambda";
import { Stack, StackProps, App } from "aws-cdk-lib";
import { LambdaRestApi, LogGroupLogDestination } from "aws-cdk-lib/aws-apigateway";
import { LogGroup } from "aws-cdk-lib/aws-logs";
import { DatadogLambda } from "../../src/index";
import { DatadogLambda } from "../../../src/index";

export class ExampleStack extends Stack {
constructor(scope: App, id: string, props?: StackProps) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import * as lambda from "aws-cdk-lib/aws-lambda";
import { LambdaRestApi, LogGroupLogDestination } from "aws-cdk-lib/aws-apigateway";
import { LogGroup } from "aws-cdk-lib/aws-logs";
import { Stack, StackProps, App } from "aws-cdk-lib";
import { DatadogLambda } from "../../src/index";
import { DatadogLambda } from "../../../src/index";

export class ExampleStack extends Stack {
constructor(scope: App, id: string, props?: StackProps) {
super(scope, id, props);

const lambdaJavaFunction = new lambda.Function(this, "HelloHandler", {
runtime: lambda.Runtime.JAVA_11,
code: lambda.Code.fromAsset(__dirname + "/../lambda"),
code: lambda.Code.fromAsset(__dirname + "/../../lambda"),
handler: "handleRequest",
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
import { LambdaRestApi, LogGroupLogDestination } from "aws-cdk-lib/aws-apigateway";
import { LogGroup } from "aws-cdk-lib/aws-logs";
import { Stack, StackProps, App } from "aws-cdk-lib";
import { DatadogLambda } from "../../src/index";
import { DatadogLambda } from "../../../src/index";

export class ExampleStack extends Stack {
constructor(scope: App, id: string, props?: StackProps) {
super(scope, id, props);

const lambdaNodejsFunction = new NodejsFunction(this, "HelloHandler", {
runtime: lambda.Runtime.NODEJS_14_X,
entry: __dirname + "/../lambda/example-lambda.js",
entry: __dirname + "/../../lambda/example-lambda.js",
handler: "handler",
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as s3 from "aws-cdk-lib/aws-s3";
import { LambdaRestApi, LogGroupLogDestination } from "aws-cdk-lib/aws-apigateway";
import { LogGroup } from "aws-cdk-lib/aws-logs";
import { Stack, StackProps, App } from "aws-cdk-lib";
import { DatadogLambda } from "../../src/index";
import { DatadogLambda } from "../../../src/index";

export class ExampleStack extends Stack {
constructor(scope: App, id: string, props?: StackProps) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as s3 from "aws-cdk-lib/aws-s3";
import { LambdaRestApi, LogGroupLogDestination } from "aws-cdk-lib/aws-apigateway";
import { LogGroup } from "aws-cdk-lib/aws-logs";
import { Stack, StackProps, App } from "aws-cdk-lib";
import { DatadogLambda } from "../../src/index";
import { DatadogLambda } from "../../../src/index";

export class ExampleStack extends Stack {
constructor(scope: App, id: string, props?: StackProps) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import { PythonFunction } from "@aws-cdk/aws-lambda-python-alpha";
import { LambdaRestApi, LogGroupLogDestination } from "aws-cdk-lib/aws-apigateway";
import { LogGroup } from "aws-cdk-lib/aws-logs";
import { Stack, StackProps, App } from "aws-cdk-lib";
import { DatadogLambda } from "../../src/index";
import { DatadogLambda } from "../../../src/index";

export class ExampleStack extends Stack {
constructor(scope: App, id: string, props?: StackProps) {
super(scope, id, props);

const lambdaPythonFunction = new PythonFunction(this, "HelloHandler", {
runtime: lambda.Runtime.PYTHON_3_7,
entry: __dirname + "/../../../lambda",
entry: __dirname + "/../../../../lambda",
index: "example-python.py",
handler: "handler",
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as lambda from "aws-cdk-lib/aws-lambda";
import { Stack, StackProps, App } from "aws-cdk-lib";
import { LambdaRestApi, LogGroupLogDestination } from "aws-cdk-lib/aws-apigateway";
import { LogGroup } from "aws-cdk-lib/aws-logs";
import { DatadogLambda } from "../../src/index";
import { DatadogLambda } from "../../../src/index";

export class ExampleStack extends Stack {
constructor(scope: App, id: string, props?: StackProps) {
Expand Down
41 changes: 30 additions & 11 deletions scripts/run_integration_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@

set -e

# To add new tests create a new ts file in the 'integration_tests/stacks' directory, append its file name to the STACK_CONFIGS array.
# To add new tests create a new ts file in the 'integration_tests/stacks' directory, append its file path to the STACK_CONFIG_PATHS array.
# Note: Each ts file will have its respective snapshot built in the snapshots directory, e.g. lambda-function-stack.ts
# will generate both snapshots/test-lambda-function-stack-snapshot.json and snapshots/correct-lambda-function-stack-snapshot.json
STACK_CONFIGS=("lambda-provided-stack" "lambda-provided-arm-stack" "lambda-singleton-function-stack" "lambda-function-arm-stack" "lambda-function-stack" "lambda-nodejs-function-stack" "lambda-python-function-stack" "lambda-java-function-stack")
STACK_CONFIG_PATHS=(
"typescript/lambda-provided-stack.ts"
"typescript/lambda-provided-arm-stack.ts"
"typescript/lambda-singleton-function-stack.ts"
"typescript/lambda-function-arm-stack.ts"
"typescript/lambda-function-stack.ts"
"typescript/lambda-nodejs-function-stack.ts"
"typescript/lambda-python-function-stack.ts"
"typescript/lambda-java-function-stack.ts"
)

SCRIPT_PATH=${BASH_SOURCE[0]}
SCRIPTS_DIR=$(dirname $SCRIPT_PATH)
Expand Down Expand Up @@ -54,20 +63,30 @@ printOutputAndExit() {
done

NEWLINE=$'\n'
if [ $ALL_TESTS_PASSED -eq ${#STACK_CONFIGS[@]} ]; then
echo "${NEWLINE}SUCCESS: ${ALL_TESTS_PASSED} out of ${#STACK_CONFIGS[@]} snapshot tests passed."
if [ $ALL_TESTS_PASSED -eq ${#STACK_CONFIG_PATHS[@]} ]; then
echo "${NEWLINE}SUCCESS: ${ALL_TESTS_PASSED} out of ${#STACK_CONFIG_PATHS[@]} snapshot tests passed."
exit 0
else
echo "${NEWLINE}FAIL: ${ALL_TESTS_PASSED} out of ${#STACK_CONFIGS[@]} snapshot tests passed."
echo "${NEWLINE}FAIL: ${ALL_TESTS_PASSED} out of ${#STACK_CONFIG_PATHS[@]} snapshot tests passed."
exit 1
fi
}

for ((i = 0; i < ${#STACK_CONFIGS[@]}; i++)); do
for ((i = 0; i < ${#STACK_CONFIG_PATHS[@]}; i++)); do
npx tsc --project tsconfig.json
cdk synth --app testlib/integration_tests/stacks/${STACK_CONFIGS[i]}.js --json --quiet
if [[ ${STACK_CONFIG_PATHS[i]} =~ ^typescript/ && ${STACK_CONFIG_PATHS[i]} =~ \.ts$ ]]; then
# Strip the ".ts" suffix
STACK_CONFIG_PATH_NO_EXT="${STACK_CONFIG_PATHS[i]%.ts}"
# Strip the "typescript/" suffix
STACK_CONFIG_NAME="${STACK_CONFIG_PATH_NO_EXT#typescript/}"

cdk synth --app testlib/integration_tests/stacks/$STACK_CONFIG_PATH_NO_EXT.js --json --quiet
else
echo "Invalid stack config path: ${STACK_CONFIG_PATHS[i]}"
exit 1
fi

RAW_CFN_TEMPLATE="cdk.out/${STACK_CONFIGS[i]}.template.json"
RAW_CFN_TEMPLATE="cdk.out/$STACK_CONFIG_NAME.template.json"
if [ ! -e "$RAW_CFN_TEMPLATE" ]; then
touch "$RAW_CFN_TEMPLATE"
fi
Expand Down Expand Up @@ -113,8 +132,8 @@ for ((i = 0; i < ${#STACK_CONFIGS[@]}; i++)); do
perl -p -i -e 's/("resttest(?!.*(Deployment|Endpoint|Cloud|Account|XXXXXXXX)).*")/"resttestXXXXXXXX"/g' ${RAW_CFN_TEMPLATE}
perl -p -i -e 's/("resttestEndpoint.*")/"resttestEndpointXXXXXXXX"/g' ${RAW_CFN_TEMPLATE}

TEST_SNAPSHOT="snapshots/test-${STACK_CONFIGS[i]}-snapshot.json"
CORRECT_SNAPSHOT="snapshots/correct-${STACK_CONFIGS[i]}-snapshot.json"
TEST_SNAPSHOT="snapshots/test-$STACK_CONFIG_NAME-snapshot.json"
CORRECT_SNAPSHOT="snapshots/correct-$STACK_CONFIG_NAME-snapshot.json"

cp ${RAW_CFN_TEMPLATE} ${TEST_SNAPSHOT}
if [ "$UPDATE_SNAPSHOTS" = "true" ]; then
Expand All @@ -127,7 +146,7 @@ for ((i = 0; i < ${#STACK_CONFIGS[@]}; i++)); do
diff ${TEST_SNAPSHOT} ${CORRECT_SNAPSHOT}
RETURN_CODE=$?
set -e
compose_output $RETURN_CODE ${STACK_CONFIGS[i]}
compose_output $RETURN_CODE $STACK_CONFIG_NAME
done

printOutputAndExit

0 comments on commit 7370866

Please sign in to comment.