From 7cc84dcdbe9c03f8b113faa5df3e41c982d60014 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Tue, 9 Aug 2022 02:13:06 -0400 Subject: [PATCH] tests: add unit test for lambda builder --- tests/apps/lambda-python/function.py | 6 + tests/apps/lambda-python/requirements.txt | 1 + tests/unit/builder-lambda.bats | 154 ++++++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 tests/apps/lambda-python/function.py create mode 100644 tests/apps/lambda-python/requirements.txt create mode 100644 tests/unit/builder-lambda.bats diff --git a/tests/apps/lambda-python/function.py b/tests/apps/lambda-python/function.py new file mode 100644 index 000000000..2c5a5dcf5 --- /dev/null +++ b/tests/apps/lambda-python/function.py @@ -0,0 +1,6 @@ +import requests + + +def handler(event, context): + print("Hello World!") + return "Hello World!" diff --git a/tests/apps/lambda-python/requirements.txt b/tests/apps/lambda-python/requirements.txt new file mode 100644 index 000000000..9cdfca0c2 --- /dev/null +++ b/tests/apps/lambda-python/requirements.txt @@ -0,0 +1 @@ +requests==2.27.1 \ No newline at end of file diff --git a/tests/unit/builder-lambda.bats b/tests/unit/builder-lambda.bats new file mode 100644 index 000000000..fe69ae51a --- /dev/null +++ b/tests/unit/builder-lambda.bats @@ -0,0 +1,154 @@ +#!/usr/bin/env bats + +load test_helper + +setup_file() { + install_pack +} + +setup() { + create_app +} + +teardown() { + destroy_app +} + +@test "(builder-lambda:set)" { + run /bin/bash -c "dokku config:set $TEST_APP SECRET_KEY=fjdkslafjdk" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku builder:set $TEST_APP selected lambda" + echo "output: $output" + echo "status: $status" + assert_success + + run deploy_app lambda-python dokku@dokku.me:$TEST_APP inject_lambda_yml + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains 'Building app with image mlupin/docker-lambda:python3.9-build' + assert_output_contains 'Installing dependencies via pip' + + run /bin/bash -c "dokku logs $TEST_APP" + echo "output: $output" + echo "status: $status" + assert_output_contains "Hello World" 0 + assert_success + + run /bin/bash -c "curl -d {} --silent --write-out '%{http_code}\n' $(dokku url $TEST_APP)/2015-03-31/functions/function.handler/invocations | grep 200" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku logs $TEST_APP" + echo "output: $output" + echo "status: $status" + assert_output_contains "Hello World" + assert_success + + run /bin/bash -c "dokku builder-lambda:set $TEST_APP lambdayml-path nonexistent.yml" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku ps:rebuild $TEST_APP" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains 'Building app with image mlupin/docker-lambda:python3.9-build' + assert_output_contains 'Installing dependencies via pip' + + run /bin/bash -c "dokku logs $TEST_APP" + echo "output: $output" + echo "status: $status" + assert_output_contains "Hello World" 0 + assert_success + + run /bin/bash -c "curl -d {} --silent --write-out '%{http_code}\n' $(dokku url $TEST_APP)/2015-03-31/functions/function.handler/invocations | grep 200" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku logs $TEST_APP" + echo "output: $output" + echo "status: $status" + assert_output_contains "Hello World" + assert_success + + run /bin/bash -c "dokku builder-lambda:set $TEST_APP lambdayml-path lambda2.yml" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku ps:rebuild $TEST_APP" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains 'Building app with image mlupin/docker-lambda:python3.9-build' + assert_output_contains 'Installing dependencies via pip' + + run /bin/bash -c "dokku logs $TEST_APP" + echo "output: $output" + echo "status: $status" + assert_output_contains "Hello World" 0 + assert_success + + run /bin/bash -c "curl -d {} --silent --write-out '%{http_code}\n' $(dokku url $TEST_APP)/2015-03-31/functions/function.handler/invocations | grep 200" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku logs $TEST_APP" + echo "output: $output" + echo "status: $status" + assert_output_contains "Hello World" + assert_success + + run /bin/bash -c "dokku builder-lambda:set $TEST_APP lambdayml-path" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku ps:rebuild $TEST_APP" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains 'Building app with image mlupin/docker-lambda:python3.9-build' + assert_output_contains 'Installing dependencies via pip' + + run /bin/bash -c "dokku logs $TEST_APP" + echo "output: $output" + echo "status: $status" + assert_output_contains "Hello World" 0 + assert_success + + run /bin/bash -c "curl -d {} --silent --write-out '%{http_code}\n' $(dokku url $TEST_APP)/2015-03-31/functions/function.handler/invocations | grep 200" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku logs $TEST_APP" + echo "output: $output" + echo "status: $status" + assert_output_contains "Hello World" + assert_success +} + +inject_lambda_yml() { + local APP="$1" + local APP_REPO_DIR="$2" + [[ -z "$APP" ]] && local APP="$TEST_APP" + echo "-----> Injecting lambda.yml" + echo "---" >>"$APP_REPO_DIR/lambda.yml" + + echo "-----> Injecting lambda.yml" + echo "---" >>"$APP_REPO_DIR/lambda.yml" + + echo "-----> Injecting lambda2.yml" + echo "---" >>"$APP_REPO_DIR/lambda2.yml" + echo "build_image: mlupin/docker-lambda:python3.9-build" >>"$APP_REPO_DIR/lambda2.yml" + echo "run_image: mlupin/docker-lambda:python3.9" >>"$APP_REPO_DIR/lambda2.yml" +}