Skip to content

Commit

Permalink
Aj/local tests with custom bundles (#80)
Browse files Browse the repository at this point in the history
* feat: Support nodejs as well as local linking to dd-lambda-js/py

* feat: Node dockerfile, genericized script, and node handler

* feat: newlines
  • Loading branch information
astuyve authored Oct 3, 2022
1 parent 91848db commit ecaafef
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 55 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ local_tests/python
local_tests/datadog-agent
local_tests/recorder-extension
local_tests/layer-*.zip
local_tests/nodejs
30 changes: 30 additions & 0 deletions local_tests/Dockerfile.Node
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
FROM public.ecr.aws/lambda/nodejs:16

# Add Datadog library
COPY nodejs /opt/nodejs

# Copy customer code
COPY func.js /var/task/

# Copy both the datadog extension and the recorder one
RUN mkdir -p /opt/extensions
COPY recorder-extension /opt/extensions/
COPY datadog-agent /opt/extensions/

# Make sure that the extension will send the payload to the man in the middle
# (recorder extension is listenning on 3333)
ENV DD_API_KEY=NO_NEED_TO_BE_VALID
ENV DD_APM_DD_URL=http://127.0.0.1:3333
ENV DD_DD_URL=http://127.0.0.1:3333
ENV DD_LAMBDA_HANDLER=func.hello
ENV DD_LOGS_CONFIG_LOGS_DD_URL=127.0.0.1:3333
ENV DD_LOGS_CONFIG_LOGS_NO_SSL=true
ENV DD_LOGS_ENABLED=false
ENV DD_LOG_LEVEL=DEBUG
ENV DD_MERGE_XRAY_TRACES=false
ENV DD_SERVERLESS_LOGS_ENABLED=false
ENV DD_SERVICE=integration-test-service
ENV DD_TRACE_ENABLED=true
ENV DD_LOCAL_TEST=1

CMD ["/opt/nodejs/node_modules/datadog-lambda-js/handler.handler"]
53 changes: 0 additions & 53 deletions local_tests/build-docker-python.sh

This file was deleted.

75 changes: 75 additions & 0 deletions local_tests/build-docker-runtime.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/bin/bash

set -e

if [ -z $RUNTIME ]; then
echo "Runtime not specified, using python 39"
RUNTIME=python
fi

# Determine architecture, M1 requires arm64 while Intel chip requires amd64
if [ `uname -m` == "arm64" ]; then
ARCHITECTURE=arm64
else
ARCHITECTURE=amd64
fi

if [ "$RUNTIME" == "python" ]; then
if [ "$ARCHITECTURE" == "amd64" ]; then
LAYER_NAME=Datadog-Python39-ARM
else
LAYER_NAME=Datadog-Python39
fi
DOCKERFILE=Dockerfile.Python
else
LAYER_NAME=Datadog-Node16-x
DOCKERFILE=Dockerfile.Node
fi

# Save the current path
CURRENT_PATH=$(pwd)

# Build the extension
ARCHITECTURE=$ARCHITECTURE VERSION=1 ./scripts/build_binary_and_layer_dockerized.sh

# Move to the local_tests repo
cd ./local_tests

# Copy the newly built extension in the same folder as the Dockerfile
cp ../.layers/datadog_extension-$ARCHITECTURE/extensions/datadog-agent .

# Build the recorder extension which will act as a man-in-a-middle to intercept payloads sent to Datadog
cd ../../datadog-agent/test/integration/serverless/recorder-extension
GOOS=linux GOARCH=$ARCHITECTURE go build -o "$CURRENT_PATH/local_tests/recorder-extension" main.go
cd "$CURRENT_PATH/local_tests"
if [ -z "$LAYER_PATH" ]; then
# Get the latest available version
LATEST_AVAILABLE_VERSION=$(aws-vault exec sandbox-account-admin \
-- aws lambda list-layer-versions --layer-name $LAYER_NAME --region sa-east-1 --max-items 1 \
| jq -r ".LayerVersions | .[0] | .Version")

# If not yet downloaded, download and unzip
LAYER="$CURRENT_PATH/local_tests/layer-$LATEST_AVAILABLE_VERSION.zip"

if test -f "$LAYER"; then
echo "The layer has already been downloaded, skipping"
else
echo "Downloading the latest $RUNTIME layer (version $LATEST_AVAILABLE_VERSION)"
URL=$(aws-vault exec sandbox-account-admin \
-- aws lambda get-layer-version --layer-name $LAYER_NAME --version-number $LATEST_AVAILABLE_VERSION \
--query Content.Location --region sa-east-1 --output text)
curl $URL -o "$LAYER"
rm -rf $CURRENT_PATH/local_tests/META_INF
rm -rf $CURRENT_PATH/local_tests/python
unzip "$LAYER"
fi
else
echo "Using $LAYER_PATH instead of fetching from AWS"
if test -d "$CURRENT_PATH/local_tests/$RUNTIME"; then
echo "Removing and rebuilding from local path"
rm -rf $CURRENT_PATH/local_tests/$RUNTIME
fi
unzip $LAYER_PATH -d $CURRENT_PATH/local_tests/
fi
# Build the image
docker build -t datadog/extension-local-tests --no-cache -f $DOCKERFILE .
4 changes: 4 additions & 0 deletions local_tests/func.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports.hello = async (event, context) => {
console.log('Function received event!!!', event)
return
}
4 changes: 2 additions & 2 deletions local_tests/invoke.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ dockerId=$(docker run -d -p 9000:8080 datadog/extension-local-tests)

# Curl it!
i=0
while true; do
while true; do
i=$((i+1))
echo "Invoke # $i"
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}' ; echo
if [[ $i -gt $NB_INVOKE ]]
then
echo "Saving logs to logs.txt"
docker logs $dockerId >./local_tests/logs.txt 2>&1
echo "Stoping"
echo "Stopping"
docker stop $dockerId
exit 0
fi
Expand Down
Binary file removed local_tests/recorder-extension
Binary file not shown.

0 comments on commit ecaafef

Please sign in to comment.