Skip to content

Commit

Permalink
chore: Add integration test for Python
Browse files Browse the repository at this point in the history
  • Loading branch information
lym953 committed Oct 9, 2024
1 parent 0415606 commit 4eb7d86
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .github/workflows/integration_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ jobs:
if: steps.cache-node-modules.outputs.cache-hit != 'true'
run: yarn install

- name: Build
run: yarn build

- name: Install CDK CLI
run: sudo yarn global add aws-cdk --prefix /usr/local

Expand Down
Empty file.
Empty file.
33 changes: 33 additions & 0 deletions integration_tests/stacks/python/lambda_python_stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from constructs import Construct
from datadog_cdk_constructs_v2 import Datadog, DatadogProps
from aws_cdk import App

from lambda_python_stack_base import LambdaPythonStackBase

class LambdaPythonStack(LambdaPythonStackBase):
def __init__(self, scope: Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)

datadog = Datadog(
self,
"Datadog",
dotnet_layer_version=15,
node_layer_version=107,
python_layer_version=89,
extension_layer_version=55,
add_layers=True,
api_key="1234",
enable_datadog_tracing=True,
enable_datadog_asm=True,
flush_metrics_to_logs=True,
site="datadoghq.com",
)

# Ensure DatadogProps can be imported properly
props = DatadogProps()
datadog.add_lambda_functions(self.lambdaFunctions)

app = App()
LambdaPythonStack(app, "LambdaPythonStack")
app.synth()

92 changes: 92 additions & 0 deletions integration_tests/stacks/python/lambda_python_stack_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from aws_cdk import (
aws_lambda as _lambda,
aws_lambda_go_alpha as go,
BundlingOptions,
BundlingOutput,
Duration,
Stack,
)
from constructs import Construct


class LambdaPythonStackBase(Stack):
def __init__(self, scope: Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)

hello_node = _lambda.Function(
self,
"hello-node",
runtime=_lambda.Runtime.NODEJS_20_X,
timeout=Duration.seconds(10),
memory_size=256,
code=_lambda.Code.from_asset(
"../examples/lambda/node",
bundling=BundlingOptions(
image=_lambda.Runtime.NODEJS_20_X.bundling_image,
command=[
"bash",
"-c",
"cp -aT . /asset-output && npm install --prefix /asset-output",
],
user="root",
),
),
handler="hello.lambda_handler",
)

hello_python = _lambda.Function(
self,
"hello-python",
runtime=_lambda.Runtime.PYTHON_3_11,
timeout=Duration.seconds(10),
memory_size=256,
code=_lambda.Code.from_asset(
"../examples/lambda/python",
bundling=BundlingOptions(
image=_lambda.Runtime.PYTHON_3_11.bundling_image,
command=[
"bash",
"-c",
"pip install -r requirements.txt -t /asset-output && cp -aT . /asset-output",
],
),
),
handler="hello.lambda_handler",
)

hello_go = go.GoFunction(
self,
"hello-go",
entry="../examples/lambda/go/hello.go",
runtime=_lambda.Runtime.PROVIDED_AL2,
timeout=Duration.seconds(10),
bundling=go.BundlingOptions(
go_build_flags=['-ldflags "-s -w"'],
),
)

hello_dotnet = _lambda.Function(
self,
"hello-dotnet",
runtime=_lambda.Runtime.DOTNET_8,
handler="HelloWorld::HelloWorld.Handler::SayHi",
timeout=Duration.seconds(10),
memory_size=256,
code=_lambda.Code.from_asset(
"../examples/lambda/dotnet",
bundling=BundlingOptions(
image=_lambda.Runtime.DOTNET_8.bundling_image,
command=[
'/bin/sh',
'-c',
' dotnet tool install -g Amazon.Lambda.Tools' +
' && dotnet build' +
' && dotnet lambda package --output-package /asset-output/function.zip'
],
user="root",
output_type=BundlingOutput.ARCHIVED
),
),
)

self.lambdaFunctions = [hello_node, hello_python, hello_go, hello_dotnet]
4 changes: 4 additions & 0 deletions integration_tests/stacks/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
aws-cdk-lib~=2.134.0
aws-cdk.aws-lambda-go-alpha~=2.134.0a0
constructs~=10.0.5
datadog_cdk_constructs_v2~=1.13.0
21 changes: 20 additions & 1 deletion scripts/run_integration_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ STACK_CONFIG_PATHS=(
"typescript/lambda-nodejs-function-stack.ts"
"typescript/lambda-python-function-stack.ts"
"typescript/lambda-java-function-stack.ts"
"python/lambda_python_stack.py"
)

SCRIPT_PATH=${BASH_SOURCE[0]}
Expand Down Expand Up @@ -73,15 +74,33 @@ printOutputAndExit() {
fi
}

echo "Setting up for Python"
VERSION=$(jq -r '.version' ../version.json)
cp "$ROOT_DIR/dist/python/datadog-cdk-constructs-v2-$VERSION.tar.gz" stacks/python
cd stacks/python
virtualenv env && source env/bin/activate
pip install -r requirements.txt
pip install datadog-cdk-constructs-v2-$VERSION.tar.gz
cd ../..

for ((i = 0; i < ${#STACK_CONFIG_PATHS[@]}; i++)); do
npx tsc --project tsconfig.json
if [[ ${STACK_CONFIG_PATHS[i]} =~ ^typescript/ && ${STACK_CONFIG_PATHS[i]} =~ \.ts$ ]]; then
# Case 1. TypeScript
# Strip the ".ts" suffix
STACK_CONFIG_PATH_NO_EXT="${STACK_CONFIG_PATHS[i]%.ts}"
# Strip the "typescript/" suffix
# Strip the "typescript/" prefix
STACK_CONFIG_NAME="${STACK_CONFIG_PATH_NO_EXT#typescript/}"

cdk synth --app testlib/integration_tests/stacks/$STACK_CONFIG_PATH_NO_EXT.js --json --quiet
elif [[ ${STACK_CONFIG_PATHS[i]} =~ ^python/ && ${STACK_CONFIG_PATHS[i]} =~ \.py$ ]]; then
# Case 2. Python
# Strip the ".py" suffix
STACK_CONFIG_PATH_NO_EXT="${STACK_CONFIG_PATHS[i]%.py}"
# Strip the "python/" prefix
STACK_CONFIG_NAME="${STACK_CONFIG_PATH_NO_EXT#python/}"

python3 "stacks/${STACK_CONFIG_PATHS[i]}"
else
echo "Invalid stack config path: ${STACK_CONFIG_PATHS[i]}"
exit 1
Expand Down

0 comments on commit 4eb7d86

Please sign in to comment.