diff --git a/tests/test.sh b/tests/test.sh index 0a2f1ba..95bd9dc 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -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 @@ -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 @@ -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 @@ -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 } @@ -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- . < /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 @@ -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