Skip to content

Commit 21a1ca5

Browse files
committed
Refactor run-tests.sh to build and run test image
Replace docker-compose based test orchestration with a simpler build-and-run flow. Switch to /usr/bin/env bash and enable strict flags (set -euo pipefail). Read PYTHON_VERSION from environment (default 3.11), require BASE_URL and API_TOKEN (with API_TOKEN_SUPERUSER fallback), normalize container host URLs, build a pydataverse-tests image with PYTHON_VERSION as a build-arg, and run it passing necessary env vars and host.docker.internal mapping. Remove argument parsing, compose files, container polling/logging, and make the script executable.
1 parent 6e70a5b commit 21a1ca5

1 file changed

Lines changed: 27 additions & 74 deletions

File tree

run-tests.sh

100644100755
Lines changed: 27 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,40 @@
1-
#!bin/bash
1+
#!/usr/bin/env bash
2+
set -euo pipefail
23

3-
# Parse arguments
4-
usage() {
5-
echo "Usage: $0 [-p Python version (e.g. 3.10, 3.11, ...)]" 1>&2
6-
exit 1
7-
}
8-
9-
while getopts ":p:d:" o; do
10-
case "${o}" in
11-
p)
12-
p=${OPTARG}
13-
;;
14-
*) ;;
15-
esac
16-
done
17-
shift $((OPTIND - 1))
4+
PYTHON_VERSION="${PYTHON_VERSION:-3.11}"
185

19-
# Fall back to Python 3.11 if no Python version is specified
20-
if [ -z "${p}" ]; then
21-
printf "\n⚠️ No Python version specified falling back to '3.11'\n"
22-
p=3.11
23-
fi
24-
25-
# Validate Python version
26-
if [[ ! "${p}" =~ ^3\.[0-9]+$ ]]; then
27-
echo "\n❌ Invalid Python version. Please specify a valid Python version (e.g. 3.10, 3.11, ...)\n"
6+
if [[ -z "${API_TOKEN:-}" ]]; then
7+
echo "API_TOKEN is required."
288
exit 1
299
fi
3010

31-
# Check if Docker is installed
32-
if ! command -v docker &>/dev/null; then
33-
echo "✋ Docker is not installed. Please install Docker before running this script."
11+
if [[ -z "${BASE_URL:-}" ]]; then
12+
echo "BASE_URL is required."
3413
exit 1
3514
fi
3615

37-
# Prepare the environment for the test
38-
mkdir dv >>/dev/null 2>&1
39-
touch dv/bootstrap.exposed.env >>/dev/null 2>&1
40-
41-
# Add python version to the environment
42-
export PYTHON_VERSION=${p}
16+
# Usually identical to API_TOKEN, but can be overridden.
17+
API_TOKEN_SUPERUSER="${API_TOKEN_SUPERUSER:-$API_TOKEN}"
4318

44-
printf "\n🚀 Preparing containers\n"
45-
printf " Using PYTHON_VERSION=${p}\n\n"
19+
CONTAINER_BASE_URL="$BASE_URL"
20+
CONTAINER_BASE_URL="${CONTAINER_BASE_URL//localhost/host.docker.internal}"
21+
CONTAINER_BASE_URL="${CONTAINER_BASE_URL//127.0.0.1/host.docker.internal}"
4622

47-
# Run all containers
48-
docker compose \
49-
-f docker/docker-compose-base.yml \
50-
-f ./docker/docker-compose-test-all.yml \
51-
--env-file local-test.env \
52-
up -d
23+
echo "Building test image with Python ${PYTHON_VERSION}..."
24+
docker build \
25+
--build-arg PYTHON_VERSION="${PYTHON_VERSION}" \
26+
-t pydataverse-tests .
5327

54-
printf "\n🔎 Running pyDataverse tests\n"
55-
printf " Logs will be printed once finished...\n\n"
56-
57-
# Check if "unit-test" container has finished
58-
while [ -n "$(docker ps -f "name=unit-tests" -f "status=running" -q)" ]; do
59-
printf " Waiting for unit-tests container to finish...\n"
60-
sleep 5
61-
done
62-
63-
# Check if "unit-test" container has failed
64-
if [ "$(docker inspect -f '{{.State.ExitCode}}' unit-tests)" -ne 0 ]; then
65-
printf "\n❌ Unit tests failed. Printing logs...\n"
66-
docker logs unit-tests
67-
printf "\n Stopping containers\n"
68-
docker compose \
69-
-f docker/docker-compose-base.yml \
70-
-f ./docker/docker-compose-test-all.yml \
71-
--env-file local-test.env \
72-
down
73-
exit 1
28+
echo "Running pytest in container..."
29+
DOCKER_TTY_ARGS=()
30+
if [[ -t 1 ]]; then
31+
DOCKER_TTY_ARGS=(-it)
7432
fi
7533

76-
# Print test results
77-
printf "\n"
78-
cat dv/unit-tests.log
79-
printf "\n\n✅ Unit tests passed\n\n"
80-
81-
# Stop all containers
82-
docker compose \
83-
-f docker/docker-compose-base.yml \
84-
-f ./docker/docker-compose-test-all.yml \
85-
--env-file local-test.env \
86-
down
87-
printf "\n🎉 Done\n\n"
34+
docker run --rm \
35+
"${DOCKER_TTY_ARGS[@]}" \
36+
--add-host=host.docker.internal:host-gateway \
37+
-e BASE_URL="${CONTAINER_BASE_URL}" \
38+
-e API_TOKEN="${API_TOKEN}" \
39+
-e API_TOKEN_SUPERUSER="${API_TOKEN_SUPERUSER}" \
40+
pydataverse-tests

0 commit comments

Comments
 (0)