Skip to content

Commit a7ab8c2

Browse files
authored
Merge pull request #27 from jerusdp/jerusdp-test-aws-lambda
- test aws lambda
2 parents 8cd6503 + b4d0712 commit a7ab8c2

File tree

1 file changed

+75
-25
lines changed

1 file changed

+75
-25
lines changed

tests/test.sh

Lines changed: 75 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

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

97
source "${HERE}"/bashtest.sh
@@ -12,12 +10,12 @@ source "${HERE}"/bashtest.sh
1210
package_bin() {
1311
rm -rf target/lambda/release > /dev/null 2>&1
1412
docker run --rm \
15-
-u $(id -u):$(id -g) \
13+
-u "$(id -u)":"$(id -g)" \
1614
-e BIN="$1" \
1715
-v "${PWD}":/code \
1816
-v "${HOME}"/.cargo/registry:/cargo/registry \
1917
-v "${HOME}"/.cargo/git:/cargo/git \
20-
${IMAGE} && \
18+
"${IMAGE}" && \
2119
ls target/lambda/release/"${1}".zip > /dev/null 2>&1 &&
2220
ls target/lambda/release/output/"${1}"/bootstrap 2>&1 &&
2321
ls target/lambda/release/output/"${1}"/bootstrap.debug 2>&1
@@ -27,11 +25,11 @@ package_bin() {
2725
package_all() {
2826
rm -rf target/lambda/release > /dev/null 2>&1
2927
docker run --rm \
30-
-u $(id -u):$(id -g) \
28+
-u "$(id -u)":"$(id -g)" \
3129
-v "${PWD}":/code \
3230
-v "${HOME}"/.cargo/registry:/cargo/registry \
3331
-v "${HOME}"/.cargo/git:/cargo/git \
34-
${IMAGE} && \
32+
"${IMAGE}" && \
3533
ls target/lambda/release/"${1}".zip > /dev/null 2>&1 &&
3634
ls target/lambda/release/output/"${1}"/bootstrap 2>&1 &&
3735
ls target/lambda/release/output/"${1}"/bootstrap.debug 2>&1
@@ -41,13 +39,13 @@ package_all() {
4139
compile_without_packaging() {
4240
rm -rf target/lambda/release > /dev/null 2>&1
4341
docker run --rm \
44-
-u $(id -u):$(id -g) \
42+
-u "$(id -u)":"$(id -g)" \
4543
-e PACKAGE=false \
4644
-v "${PWD}":/code \
4745
-v "${HOME}"/.cargo/registry:/cargo/registry \
4846
-v "${HOME}"/.cargo/git:/cargo/git \
49-
${IMAGE} &&
50-
!(ls target/lambda/release/"${1}".zip > /dev/null 2>&1) &&
47+
"${IMAGE}" &&
48+
! (ls target/lambda/release/"${1}".zip > /dev/null 2>&1) &&
5149
ls target/lambda/release/output/"${1}"/bootstrap 2>&1 &&
5250
ls target/lambda/release/output/"${1}"/bootstrap.debug 2>&1
5351
}
@@ -56,19 +54,82 @@ compile_without_packaging() {
5654
package_all_dev_profile() {
5755
rm -rf target/lambda/debug > /dev/null 2>&1
5856
docker run --rm \
59-
-u $(id -u):$(id -g) \
57+
-u "$(id -u)":"$(id -g)" \
6058
-e PROFILE=dev \
6159
-v "${PWD}":/code \
6260
-v "${HOME}"/.cargo/registry:/cargo/registry \
6361
-v "${HOME}"/.cargo/git:/cargo/git \
64-
${IMAGE} && \
62+
"${IMAGE}" && \
6563
ls target/lambda/debug/"${1}".zip > /dev/null 2>&1 &&
6664
ls target/lambda/release/output/"${1}"/bootstrap 2>&1 &&
6765
ls target/lambda/release/output/"${1}"/bootstrap.debug 2>&1
6866
}
6967

68+
verify_packaged_application() {
69+
LAMBDA_RUNTIME_DIR="/var/runtime"
70+
LAMBDA_TASK_DIR="/var/task"
71+
HOOKS="test-func-with-hooks"
72+
TRY=1
73+
MAX_TRY=10
74+
TRIES_EXCEEDED=10
75+
SLEEP=1
76+
PACKAGE="${1}"
77+
PROJECT="${2}"
78+
TSFRACTION=$(date +%M%S)
79+
80+
clean_up() {
81+
docker container stop lamb > /dev/null 2>&1
82+
rm -f bootstrap > /dev/null 2>&1
83+
rm -f output.log > /dev/null 2>&1
84+
}
85+
86+
clean_up
87+
rm -f test-out.log > /dev/null 2>&1
88+
89+
unzip -o \
90+
target/lambda/release/"${PACKAGE}".zip
91+
92+
if [ "$PROJECT" = "${HOOKS}" ]; then
93+
docker build -t mylambda:"${TSFRACTION}" -f- . <<EOF
94+
FROM public.ecr.aws/lambda/provided:al2
95+
COPY bootstrap ${LAMBDA_RUNTIME_DIR}
96+
COPY output.log ${LAMBDA_TASK_DIR}
97+
CMD [ "function.handler" ]
98+
EOF
99+
else
100+
docker build -t mylambda:"${TSFRACTION}" -f- . <<EOF
101+
FROM public.ecr.aws/lambda/provided:al2
102+
COPY bootstrap ${LAMBDA_RUNTIME_DIR}
103+
CMD [ "function.handler" ]
104+
EOF
105+
fi
106+
# rm -f bootstrap > /dev/null 2>&1
107+
docker run \
108+
--name lamb \
109+
--rm \
110+
-p 9000:8080 \
111+
-d mylambda:"${TSFRACTION}"
112+
113+
until curl -X POST \
114+
-H "Content-Type: application/json" \
115+
-d "@test-event.json" \
116+
"http://localhost:9000/2015-03-31/functions/function/invocations" | \
117+
grep -v RequestId | \
118+
grep -v '^\W*$' > test-out.log; do
119+
>&2 echo "waiting for service to spin up"
120+
sleep ${SLEEP}
121+
if [ ${TRY} = ${MAX_TRY} ]; then
122+
exit ${TRIES_EXCEEDED}
123+
else
124+
TRY=$((TRY + 1))
125+
fi
126+
done
127+
128+
clean_up
129+
}
130+
70131
for project in test-func test-multi-func test-func-with-hooks; do
71-
cd "${HERE}"/"${project}"
132+
cd "${HERE}"/"${project}" || exit 2
72133
echo "👩‍🔬 Running tests for $project with image $IMAGE"
73134

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

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

89-
# verify packaged artifact by invoking it using the lambdaci "provided.al2" docker image
90-
rm -f output.log > /dev/null 2>&1
91-
rm -f test-out.log > /dev/null 2>&1
92-
rm -rf /tmp/lambda > /dev/null 2>&1
93-
unzip -o \
94-
target/lambda/release/"${bin_name}".zip \
95-
-d /tmp/lambda > /dev/null 2>&1 && \
96-
docker run \
97-
-i -e DOCKER_LAMBDA_USE_STDIN=1 \
98-
--rm \
99-
-v /tmp/lambda:/var/task \
100-
lambci/lambda:provided.al2 < test-event.json | grep -v RequestId | grep -v '^\W*$' > test-out.log
101-
150+
# verify packaged artifact by invoking it using the aws lambda "provided.al2" docker image
151+
verify_packaged_application "${bin_name}" "${project}"
102152
assert "when invoked, it produces expected output" diff expected-output.json test-out.log
103153
done
104154

0 commit comments

Comments
 (0)