Skip to content

Commit 6879b11

Browse files
committed
fixup! feat(dev): add local api test runner
1 parent cf5e83c commit 6879b11

1 file changed

Lines changed: 41 additions & 56 deletions

File tree

run-api-tests-stack.sh

Lines changed: 41 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ set -euo pipefail
33

44
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
55
COMPOSE="docker compose -f ${ROOT_DIR}/docker-compose.yml -f ${ROOT_DIR}/docker-compose.test-api.yml"
6-
BUILD_IMAGES_STACK=(api dbapi nslord nsmaster dblord dbmaster)
76
BUILD_IMAGES_DB=(dbapi)
87
BUILD=1
9-
MODE="host"
108
PROD_DB=0
119
PROD_USER="root"
1210
PROD_HOST="digga.desec.io"
@@ -16,9 +14,8 @@ CACHE_FILE="${CACHE_DIR}/dbapi.sql.gz"
1614

1715
usage() {
1816
cat <<'EOF'
19-
Usage: ./run-api-tests-stack.sh [--no-build] [--docker] [--prod-db] [--prod-user USER] [--prod-host HOST] [--refresh-prod-db]
17+
Usage: ./run-api-tests-stack.sh [--no-build] [--prod-db] [--prod-user USER] [--prod-host HOST] [--refresh-prod-db]
2018
--no-build Skip docker image build step
21-
--docker Run API tests inside the api container (CI-style)
2219
--prod-db Download a logical dbapi dump from production and load it locally
2320
--prod-user SSH username for prod (default: root)
2421
--prod-host SSH hostname for prod (default: desec.io)
@@ -29,7 +26,6 @@ EOF
2926
while [[ $# -gt 0 ]]; do
3027
case "$1" in
3128
--no-build) BUILD=0 ;;
32-
--docker) MODE="docker" ;;
3329
--prod-db) PROD_DB=1 ;;
3430
--prod-user)
3531
shift
@@ -106,59 +102,48 @@ cleanup() {
106102

107103
trap cleanup EXIT
108104

109-
if [[ "$MODE" == "docker" ]]; then
110-
if [[ "$PROD_DB" -eq 1 ]]; then
111-
echo "--prod-db is only supported for host-mode tests." >&2
105+
if [[ "$BUILD" -eq 1 ]]; then
106+
$COMPOSE build "${BUILD_IMAGES_DB[@]}"
107+
fi
108+
$COMPOSE up -d dbapi
109+
(
110+
set -a
111+
source "${ROOT_DIR}/.env"
112+
set +a
113+
export DJANGO_SETTINGS_MODULE=api.settings_quick_test
114+
export DESECSTACK_DJANGO_TEST=1
115+
cd "${ROOT_DIR}/api"
116+
if [[ -x "./venv/bin/python" ]]; then
117+
# Use project venv when present.
118+
source "./venv/bin/activate"
119+
else
120+
echo "Missing venv at ${ROOT_DIR}/api/venv. Create it with: cd api && python3 -m venv venv && source venv/bin/activate && pip install -r requirements.txt" >&2
112121
exit 1
113122
fi
114-
if [[ "$BUILD" -eq 1 ]]; then
115-
$COMPOSE build "${BUILD_IMAGES_STACK[@]}"
116-
fi
117-
$COMPOSE run --rm api bash -c "./entrypoint-tests.sh"
118-
else
119-
if [[ "$BUILD" -eq 1 ]]; then
120-
$COMPOSE build "${BUILD_IMAGES_DB[@]}"
121-
fi
122-
$COMPOSE up -d dbapi
123-
(
124-
set -a
125-
source "${ROOT_DIR}/.env"
126-
set +a
127-
export DJANGO_SETTINGS_MODULE=api.settings_quick_test
128-
export DESECSTACK_DJANGO_TEST=1
129-
cd "${ROOT_DIR}/api"
130-
if [[ -x "./venv/bin/python" ]]; then
131-
# Use project venv when present.
132-
source "./venv/bin/activate"
133-
else
134-
echo "Missing venv at ${ROOT_DIR}/api/venv. Create it with: cd api && python3 -m venv venv && source venv/bin/activate && pip install -r requirements.txt" >&2
135-
exit 1
123+
# Avoid local psql dependency by checking readiness inside the DB container.
124+
wait_seconds=120
125+
start_ts=$(date +%s)
126+
while true; do
127+
if $COMPOSE exec -T dbapi pg_isready >/dev/null 2>&1; then
128+
break
136129
fi
137-
# Avoid local psql dependency by checking readiness inside the DB container.
138-
wait_seconds=120
139-
start_ts=$(date +%s)
140-
while true; do
141-
if $COMPOSE exec -T dbapi pg_isready >/dev/null 2>&1; then
142-
break
143-
fi
144130

145-
now_ts=$(date +%s)
146-
if (( now_ts - start_ts > wait_seconds )); then
147-
echo "Timed out waiting for Postgres to become ready."
148-
$COMPOSE ps
149-
$COMPOSE logs --tail 80 dbapi || true
150-
exit 1
151-
fi
152-
echo "Postgres is unavailable - sleeping"
153-
sleep 2
154-
done
155-
test_args=()
156-
if [[ "$PROD_DB" -eq 1 ]]; then
157-
download_prod_dbapi
158-
restore_dbapi_from_cache
159-
export DESECSTACK_DJANGO_TEST_DB_NAME=desec
160-
test_args+=(--keepdb)
131+
now_ts=$(date +%s)
132+
if (( now_ts - start_ts > wait_seconds )); then
133+
echo "Timed out waiting for Postgres to become ready."
134+
$COMPOSE ps
135+
$COMPOSE logs --tail 80 dbapi || true
136+
exit 1
161137
fi
162-
python3 manage.py test "${test_args[@]}"
163-
)
164-
fi
138+
echo "Postgres is unavailable - sleeping"
139+
sleep 2
140+
done
141+
test_args=()
142+
if [[ "$PROD_DB" -eq 1 ]]; then
143+
download_prod_dbapi
144+
restore_dbapi_from_cache
145+
export DESECSTACK_DJANGO_TEST_DB_NAME=desec
146+
test_args+=(--keepdb)
147+
fi
148+
python3 manage.py test "${test_args[@]}"
149+
)

0 commit comments

Comments
 (0)