Skip to content

Commit

Permalink
tests: add a teardown target for cleaning up docker resources
Browse files Browse the repository at this point in the history
  • Loading branch information
flrgh committed Apr 1, 2024
1 parent 673f0f1 commit 485b6fc
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
9 changes: 7 additions & 2 deletions .ci/_common.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

readonly DOCKER_LABEL=com.konghq.deck.ci=1

# Usage: waitContainer "PostgreSQL" 5432 0.2
function waitContainer()
{
Expand All @@ -9,7 +11,7 @@ function waitContainer()

for try in {1..100}; do
echo "waiting for ${container}.."
nc localhost ${port} && break;
nc localhost ${port} </dev/null && break;
sleep ${sleep_time}
done
}
Expand All @@ -18,7 +20,8 @@ function create_network()
{
# create docker network if it doesn't exist
if [[ -z $(docker network ls --filter name=${NETWORK_NAME} -q) ]]; then
docker network create ${NETWORK_NAME}
docker network create "${NETWORK_NAME}" \
--label "$DOCKER_LABEL"
fi
}

Expand All @@ -41,6 +44,7 @@ function deploy_pg()
-e "POSTGRES_USER=$DATABASE_USER" \
-e "POSTGRES_DB=$DATABASE_NAME" \
-e "POSTGRES_PASSWORD=$KONG_DB_PASSWORD" \
--label "$DOCKER_LABEL" \
postgres:9.6

waitContainer "PostgreSQL" 5432 0.2
Expand All @@ -56,6 +60,7 @@ function perform_migrations()
-e "KONG_PG_PASSWORD=$KONG_DB_PASSWORD" \
-e "KONG_PASSWORD=$KONG_DB_PASSWORD" \
-e "KONG_LICENSE_DATA=$KONG_LICENSE_DATA" \
--label "$DOCKER_LABEL" \
$KONG_IMAGE kong migrations bootstrap && break
done
}
3 changes: 3 additions & 0 deletions .ci/setup_kong.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function deploy_kong_postgres()
-e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \
-e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" \
-e "KONG_ADMIN_GUI_AUTH=basic-auth" \
-e "KONG_ADMIN_GUI_SESSION_CONF={}" \
-e "KONG_ENFORCE_RBAC=on" \
-e "KONG_PORTAL=on" \
-e "KONG_ROUTER_FLAVOR=${KONG_ROUTER_FLAVOR}" \
Expand All @@ -43,6 +44,7 @@ function deploy_kong_postgres()
-p 8443:8443 \
-p 127.0.0.1:8001:8001 \
-p 127.0.0.1:8444:8444 \
--label "$DOCKER_LABEL" \
$KONG_IMAGE
waitContainer "Kong" 8001 0.2
}
Expand All @@ -68,6 +70,7 @@ function deploy_kong_dbless()
-p 8443:8443 \
-p 127.0.0.1:8001:8001 \
-p 127.0.0.1:8444:8444 \
--label "$DOCKER_LABEL" \
$KONG_IMAGE
waitContainer "Kong" 8001 0.2
}
Expand Down
1 change: 1 addition & 0 deletions .ci/setup_kong_ee.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function deploy_kong_ee()
-p 8445:8445 \
-p 8003:8003 \
-p 8004:8004 \
--label "$DOCKER_LABEL" \
$KONG_IMAGE
}

Expand Down
23 changes: 23 additions & 0 deletions .ci/teardown.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

set -e

source $(dirname "$0")/_common.sh

cleanup() {
local -r resource=${1?resource type required}
shift

docker "$resource" ls "$@" \
--filter "label=$DOCKER_LABEL" \
--quiet \
| while read -r id; do
docker "$resource" rm \
--force \
"$id"
done
}

cleanup container --all
cleanup volume
cleanup network
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ setup-kong-postgres:
setup-kong-ee:
bash .ci/setup_kong_ee.sh

.PHONY: teardown
teardown:
bash .ci/teardown.sh

.PHONY: test-coverage-enterprise
test-coverage-enterprise:
go test -tags=enterprise -race -v -count=1 -coverprofile=coverage.out.tmp ./...
Expand Down

0 comments on commit 485b6fc

Please sign in to comment.