|
1 | | -#!bin/bash |
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
2 | 3 |
|
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}" |
18 | 5 |
|
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." |
28 | 8 | exit 1 |
29 | 9 | fi |
30 | 10 |
|
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." |
34 | 13 | exit 1 |
35 | 14 | fi |
36 | 15 |
|
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}" |
43 | 18 |
|
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}" |
46 | 22 |
|
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 . |
53 | 27 |
|
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) |
74 | 32 | fi |
75 | 33 |
|
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