|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Start Bifrost with merged config (providers from env + Postgres) and run E2E API tests (Newman). |
| 5 | +# Requires: Docker with Postgres already running (docker compose up -d) |
| 6 | +# All provider API keys in environment (OPENAI_API_KEY, ANTHROPIC_API_KEY, etc.) |
| 7 | +# Usage: ./start-bifrost-and-run-e2e-api-tests.sh <bifrost-binary-path> [port] |
| 8 | + |
| 9 | +if command -v readlink >/dev/null 2>&1 && readlink -f "$0" >/dev/null 2>&1; then |
| 10 | + SCRIPT_DIR="$(dirname "$(readlink -f "$0")")" |
| 11 | +else |
| 12 | + SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)" |
| 13 | +fi |
| 14 | + |
| 15 | +REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd -P)" |
| 16 | + |
| 17 | +if [ "${1:-}" = "" ]; then |
| 18 | + echo "Usage: $0 <bifrost-binary-path> [port]" >&2 |
| 19 | + exit 1 |
| 20 | +fi |
| 21 | + |
| 22 | +BIFROST_BINARY="$1" |
| 23 | +PORT="${2:-8080}" |
| 24 | + |
| 25 | +POSTGRES_HOST="${POSTGRES_HOST:-localhost}" |
| 26 | +POSTGRES_PORT="${POSTGRES_PORT:-5432}" |
| 27 | +POSTGRES_USER="${POSTGRES_USER:-bifrost}" |
| 28 | +POSTGRES_PASSWORD="${POSTGRES_PASSWORD:-bifrost_password}" |
| 29 | +POSTGRES_DB="${POSTGRES_DB:-bifrost}" |
| 30 | +POSTGRES_SSLMODE="${POSTGRES_SSLMODE:-disable}" |
| 31 | + |
| 32 | +if [ ! -f "$BIFROST_BINARY" ] || [ ! -x "$BIFROST_BINARY" ]; then |
| 33 | + echo "❌ Bifrost binary not found or not executable: $BIFROST_BINARY" >&2 |
| 34 | + exit 1 |
| 35 | +fi |
| 36 | + |
| 37 | +# Base config has providers with env.OPENAI_API_KEY etc.; we merge Postgres for stores |
| 38 | +BASE_CONFIG="$REPO_ROOT/tests/integrations/python/config.json" |
| 39 | +if [ ! -f "$BASE_CONFIG" ]; then |
| 40 | + echo "❌ Base config not found: $BASE_CONFIG" >&2 |
| 41 | + exit 1 |
| 42 | +fi |
| 43 | + |
| 44 | +TEMP_DIR=$(mktemp -d) |
| 45 | +MERGED_CONFIG="$TEMP_DIR/config.json" |
| 46 | +SERVER_LOG="$TEMP_DIR/server.log" |
| 47 | +BIFROST_PID="" |
| 48 | + |
| 49 | +cleanup() { |
| 50 | + local exit_code=$? |
| 51 | + if [ -n "${BIFROST_PID:-}" ] && kill -0 "$BIFROST_PID" 2>/dev/null; then |
| 52 | + kill "$BIFROST_PID" 2>/dev/null || true |
| 53 | + wait "$BIFROST_PID" 2>/dev/null || true |
| 54 | + fi |
| 55 | + rm -rf "$TEMP_DIR" |
| 56 | + exit $exit_code |
| 57 | +} |
| 58 | +trap cleanup EXIT |
| 59 | + |
| 60 | +echo "📝 Merged config (providers + Postgres)..." |
| 61 | +if command -v jq >/dev/null 2>&1; then |
| 62 | + jq --arg host "$POSTGRES_HOST" --arg port "$POSTGRES_PORT" --arg user "$POSTGRES_USER" \ |
| 63 | + --arg pass "$POSTGRES_PASSWORD" --arg db "$POSTGRES_DB" --arg ssl "$POSTGRES_SSLMODE" \ |
| 64 | + '. + { |
| 65 | + "config_store": {"enabled": true, "type": "postgres", "config": {"host": $host, "port": $port, "user": $user, "password": $pass, "db_name": $db, "ssl_mode": $ssl}}, |
| 66 | + "logs_store": {"enabled": true, "type": "postgres", "config": {"host": $host, "port": $port, "user": $user, "password": $pass, "db_name": $db, "ssl_mode": $ssl}} |
| 67 | + }' "$BASE_CONFIG" > "$MERGED_CONFIG" |
| 68 | +else |
| 69 | + python3 - "$BASE_CONFIG" "$MERGED_CONFIG" << EOF |
| 70 | +import sys, json, os |
| 71 | +with open(sys.argv[1]) as f: c = json.load(f) |
| 72 | +pg = {"host": os.environ.get("POSTGRES_HOST", "localhost"), "port": os.environ.get("POSTGRES_PORT", "5432"), "user": os.environ.get("POSTGRES_USER", "bifrost"), "password": os.environ.get("POSTGRES_PASSWORD", "bifrost_password"), "db_name": os.environ.get("POSTGRES_DB", "bifrost"), "ssl_mode": os.environ.get("POSTGRES_SSLMODE", "disable")} |
| 73 | +c["config_store"] = {"enabled": True, "type": "postgres", "config": pg} |
| 74 | +c["logs_store"] = {"enabled": True, "type": "postgres", "config": dict(pg)} |
| 75 | +with open(sys.argv[2], "w") as f: json.dump(c, f, indent=2) |
| 76 | +EOF |
| 77 | +fi |
| 78 | + |
| 79 | +echo "🔄 Resetting PostgreSQL database..." |
| 80 | +DOCKER_COMPOSE_FILE="$REPO_ROOT/.github/workflows/configs/docker-compose.yml" |
| 81 | +if [ -f "$DOCKER_COMPOSE_FILE" ]; then |
| 82 | + POSTGRES_CONTAINER=$(docker compose -f "$DOCKER_COMPOSE_FILE" ps -q postgres 2>/dev/null || true) |
| 83 | + if [ -n "$POSTGRES_CONTAINER" ]; then |
| 84 | + docker exec "$POSTGRES_CONTAINER" psql -U "$POSTGRES_USER" -d postgres -c "DROP DATABASE IF EXISTS $POSTGRES_DB; CREATE DATABASE $POSTGRES_DB;" 2>/dev/null || true |
| 85 | + fi |
| 86 | +fi |
| 87 | + |
| 88 | +echo "🚀 Starting Bifrost on port $PORT..." |
| 89 | +"$BIFROST_BINARY" --app-dir "$TEMP_DIR" --port "$PORT" --log-level debug > "$SERVER_LOG" 2>&1 & |
| 90 | +BIFROST_PID=$! |
| 91 | + |
| 92 | +MAX_WAIT=60 |
| 93 | +ELAPSED=0 |
| 94 | +while [ $ELAPSED -lt $MAX_WAIT ]; do |
| 95 | + if grep -q "successfully started bifrost" "$SERVER_LOG" 2>/dev/null; then |
| 96 | + echo " ✅ Bifrost started" |
| 97 | + break |
| 98 | + fi |
| 99 | + if ! kill -0 "$BIFROST_PID" 2>/dev/null; then |
| 100 | + echo " ❌ Bifrost process exited" |
| 101 | + cat "$SERVER_LOG" |
| 102 | + exit 1 |
| 103 | + fi |
| 104 | + sleep 1 |
| 105 | + ELAPSED=$((ELAPSED + 1)) |
| 106 | +done |
| 107 | + |
| 108 | +if [ $ELAPSED -ge $MAX_WAIT ]; then |
| 109 | + echo " ❌ Bifrost did not start within ${MAX_WAIT}s" |
| 110 | + cat "$SERVER_LOG" |
| 111 | + exit 1 |
| 112 | +fi |
| 113 | + |
| 114 | +export BIFROST_BASE_URL="http://localhost:$PORT" |
| 115 | +"$SCRIPT_DIR/run-e2e-api-tests.sh" |
0 commit comments