|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Runs the server stack with Docker, then runs the moderation service |
| 4 | +# end-to-end CSAM test using cargo. |
| 5 | +# |
| 6 | +# Env: |
| 7 | +# KEEP_STACK=1 leave the Docker stack running on exit (default: tear down) |
| 8 | +set -euo pipefail |
| 9 | + |
| 10 | +cd "$(dirname "$0")/../../.." |
| 11 | + |
| 12 | +# A fixed project name keeps the network name (<project>_default) independent |
| 13 | +# of the checkout directory, for both the connect below and teardown. |
| 14 | +export COMPOSE_PROJECT_NAME=polycentric |
| 15 | +NETWORK="${COMPOSE_PROJECT_NAME}_default" |
| 16 | + |
| 17 | +# Host:port the readiness probe waits on (and, locally, the test connects to). |
| 18 | +SERVER_HOST=localhost |
| 19 | +SERVER_PORT=3000 |
| 20 | + |
| 21 | +# The job's own container ID. GitLab sets the build container's hostname to a |
| 22 | +# short predefined name (runner-…-concurrent-N) that the daemon does not know |
| 23 | +# it by, so `docker network connect <hostname>` fails. Recover the real 64-hex |
| 24 | +# ID from the bind mounts Docker sets up for /etc/hostname, /etc/hosts, etc. |
| 25 | +# (sourced from /var/lib/docker/containers/<id>/…), falling back to hostname. |
| 26 | +self_container() { |
| 27 | + local id |
| 28 | + id=$(grep -oE 'containers/[0-9a-f]{64}' /proc/self/mountinfo | head -1 | cut -d/ -f2) |
| 29 | + echo "${id:-$(cat /etc/hostname)}" |
| 30 | +} |
| 31 | + |
| 32 | +cleanup() { |
| 33 | + if [[ "${CI:-}" == "true" ]]; then |
| 34 | + docker network disconnect "$NETWORK" "$(self_container)" >/dev/null 2>&1 || true |
| 35 | + fi |
| 36 | + if [[ "${KEEP_STACK:-0}" != "1" ]]; then |
| 37 | + docker compose down -v >/dev/null 2>&1 || true |
| 38 | + fi |
| 39 | +} |
| 40 | +trap cleanup EXIT |
| 41 | + |
| 42 | +echo "==> Bringing up the server stack…" |
| 43 | +docker compose up -d --build --wait postgres rustfs kafka server |
| 44 | + |
| 45 | +if [[ "${CI:-}" == "true" ]]; then |
| 46 | + echo "==> Joining the job container to the stack network ($NETWORK)…" |
| 47 | + docker network connect "$NETWORK" "$(self_container)" |
| 48 | + # Reach the services by their in-network names instead of localhost. Kafka |
| 49 | + # is reached on its INTERNAL listener, which advertises kafka:19092 on this |
| 50 | + # network (the EXTERNAL listener advertises localhost:9092, for local use). |
| 51 | + SERVER_HOST=server |
| 52 | + export POLYCENTRIC_TEST_SERVER="http://server:3000" |
| 53 | + export POLYCENTRIC_TEST_DATABASE_URL="postgres://postgres:testing@postgres:5432" |
| 54 | + export POLYCENTRIC_TEST_OS_ENDPOINT="http://rustfs:9000" |
| 55 | + export POLYCENTRIC_TEST_KAFKA_BROKERS="kafka:19092" |
| 56 | +fi |
| 57 | + |
| 58 | +echo "==> Waiting for the server gRPC port ($SERVER_HOST:$SERVER_PORT)…" |
| 59 | +for _ in $(seq 1 60); do |
| 60 | + if (exec 3<>"/dev/tcp/$SERVER_HOST/$SERVER_PORT") 2>/dev/null; then |
| 61 | + exec 3>&- 3<&- |
| 62 | + echo " server is accepting connections" |
| 63 | + break |
| 64 | + fi |
| 65 | + sleep 1 |
| 66 | +done |
| 67 | + |
| 68 | +echo "==> Applying server database migrations…" |
| 69 | +docker compose exec -T server /app/migration up |
| 70 | + |
| 71 | +echo "==> Running the moderation CSAM pipeline test…" |
| 72 | +cargo test -p moderation-service --test csam_pipeline -- --ignored --nocapture |
0 commit comments