Skip to content

Jerusdp test aws lambda #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 75 additions & 25 deletions tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

# Directory of the integration test
HERE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# Root directory of the repository
DIST=$(cd "$HERE"/..; pwd)
: "${IMAGE:=rustserverless/lambda-rust}"

source "${HERE}"/bashtest.sh
Expand All @@ -12,12 +10,12 @@ source "${HERE}"/bashtest.sh
package_bin() {
rm -rf target/lambda/release > /dev/null 2>&1
docker run --rm \
-u $(id -u):$(id -g) \
-u "$(id -u)":"$(id -g)" \
-e BIN="$1" \
-v "${PWD}":/code \
-v "${HOME}"/.cargo/registry:/cargo/registry \
-v "${HOME}"/.cargo/git:/cargo/git \
${IMAGE} && \
"${IMAGE}" && \
ls target/lambda/release/"${1}".zip > /dev/null 2>&1 &&
ls target/lambda/release/output/"${1}"/bootstrap 2>&1 &&
ls target/lambda/release/output/"${1}"/bootstrap.debug 2>&1
Expand All @@ -27,11 +25,11 @@ package_bin() {
package_all() {
rm -rf target/lambda/release > /dev/null 2>&1
docker run --rm \
-u $(id -u):$(id -g) \
-u "$(id -u)":"$(id -g)" \
-v "${PWD}":/code \
-v "${HOME}"/.cargo/registry:/cargo/registry \
-v "${HOME}"/.cargo/git:/cargo/git \
${IMAGE} && \
"${IMAGE}" && \
ls target/lambda/release/"${1}".zip > /dev/null 2>&1 &&
ls target/lambda/release/output/"${1}"/bootstrap 2>&1 &&
ls target/lambda/release/output/"${1}"/bootstrap.debug 2>&1
Expand All @@ -41,13 +39,13 @@ package_all() {
compile_without_packaging() {
rm -rf target/lambda/release > /dev/null 2>&1
docker run --rm \
-u $(id -u):$(id -g) \
-u "$(id -u)":"$(id -g)" \
-e PACKAGE=false \
-v "${PWD}":/code \
-v "${HOME}"/.cargo/registry:/cargo/registry \
-v "${HOME}"/.cargo/git:/cargo/git \
${IMAGE} &&
!(ls target/lambda/release/"${1}".zip > /dev/null 2>&1) &&
"${IMAGE}" &&
! (ls target/lambda/release/"${1}".zip > /dev/null 2>&1) &&
ls target/lambda/release/output/"${1}"/bootstrap 2>&1 &&
ls target/lambda/release/output/"${1}"/bootstrap.debug 2>&1
}
Expand All @@ -56,19 +54,82 @@ compile_without_packaging() {
package_all_dev_profile() {
rm -rf target/lambda/debug > /dev/null 2>&1
docker run --rm \
-u $(id -u):$(id -g) \
-u "$(id -u)":"$(id -g)" \
-e PROFILE=dev \
-v "${PWD}":/code \
-v "${HOME}"/.cargo/registry:/cargo/registry \
-v "${HOME}"/.cargo/git:/cargo/git \
${IMAGE} && \
"${IMAGE}" && \
ls target/lambda/debug/"${1}".zip > /dev/null 2>&1 &&
ls target/lambda/release/output/"${1}"/bootstrap 2>&1 &&
ls target/lambda/release/output/"${1}"/bootstrap.debug 2>&1
}

verify_packaged_application() {
LAMBDA_RUNTIME_DIR="/var/runtime"
LAMBDA_TASK_DIR="/var/task"
HOOKS="test-func-with-hooks"
TRY=1
MAX_TRY=10
TRIES_EXCEEDED=10
SLEEP=1
PACKAGE="${1}"
PROJECT="${2}"
TSFRACTION=$(date +%M%S)

clean_up() {
docker container stop lamb > /dev/null 2>&1
rm -f bootstrap > /dev/null 2>&1
rm -f output.log > /dev/null 2>&1
}

clean_up
rm -f test-out.log > /dev/null 2>&1

unzip -o \
target/lambda/release/"${PACKAGE}".zip

if [ "$PROJECT" = "${HOOKS}" ]; then
docker build -t mylambda:"${TSFRACTION}" -f- . <<EOF
FROM public.ecr.aws/lambda/provided:al2
COPY bootstrap ${LAMBDA_RUNTIME_DIR}
COPY output.log ${LAMBDA_TASK_DIR}
CMD [ "function.handler" ]
EOF
else
docker build -t mylambda:"${TSFRACTION}" -f- . <<EOF
FROM public.ecr.aws/lambda/provided:al2
COPY bootstrap ${LAMBDA_RUNTIME_DIR}
CMD [ "function.handler" ]
EOF
fi
# rm -f bootstrap > /dev/null 2>&1
docker run \
--name lamb \
--rm \
-p 9000:8080 \
-d mylambda:"${TSFRACTION}"

until curl -X POST \
-H "Content-Type: application/json" \
-d "@test-event.json" \
"http://localhost:9000/2015-03-31/functions/function/invocations" | \
grep -v RequestId | \
grep -v '^\W*$' > test-out.log; do
>&2 echo "waiting for service to spin up"
sleep ${SLEEP}
if [ ${TRY} = ${MAX_TRY} ]; then
exit ${TRIES_EXCEEDED}
else
TRY=$((TRY + 1))
fi
done

clean_up
}

for project in test-func test-multi-func test-func-with-hooks; do
cd "${HERE}"/"${project}"
cd "${HERE}"/"${project}" || exit 2
echo "👩‍🔬 Running tests for $project with image $IMAGE"

if [[ "$project" == test-multi-func ]]; then
Expand All @@ -86,19 +147,8 @@ for project in test-func test-multi-func test-func-with-hooks; do

assert "it packages all bins" package_all "${bin_name}"

# verify packaged artifact by invoking it using the lambdaci "provided.al2" docker image
rm -f output.log > /dev/null 2>&1
rm -f test-out.log > /dev/null 2>&1
rm -rf /tmp/lambda > /dev/null 2>&1
unzip -o \
target/lambda/release/"${bin_name}".zip \
-d /tmp/lambda > /dev/null 2>&1 && \
docker run \
-i -e DOCKER_LAMBDA_USE_STDIN=1 \
--rm \
-v /tmp/lambda:/var/task \
lambci/lambda:provided.al2 < test-event.json | grep -v RequestId | grep -v '^\W*$' > test-out.log

# verify packaged artifact by invoking it using the aws lambda "provided.al2" docker image
verify_packaged_application "${bin_name}" "${project}"
assert "when invoked, it produces expected output" diff expected-output.json test-out.log
done

Expand Down