|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Container helper functions for bats tests |
| 3 | + |
| 4 | +# Default container names |
| 5 | +POSTGRES_CONTAINER_NAME="${POSTGRES_CONTAINER_NAME:-dynflow-test-postgres}" |
| 6 | +REDIS_CONTAINER_NAME="${REDIS_CONTAINER_NAME:-dynflow-test-redis}" |
| 7 | + |
| 8 | +# Default ports |
| 9 | +POSTGRES_PORT="${POSTGRES_PORT:-15432}" |
| 10 | +REDIS_PORT="${REDIS_PORT:-16379}" |
| 11 | + |
| 12 | +# Database credentials |
| 13 | +POSTGRES_USER="${POSTGRES_USER:-dynflow_test}" |
| 14 | +POSTGRES_PASSWORD="${POSTGRES_PASSWORD:-dynflow_test_pass}" |
| 15 | +POSTGRES_DB="${POSTGRES_DB:-dynflow_test}" |
| 16 | + |
| 17 | +# Container images |
| 18 | +POSTGRES_IMAGE="${POSTGRES_IMAGE:-docker.io/library/postgres:15}" |
| 19 | +REDIS_IMAGE="${REDIS_IMAGE:-docker.io/library/redis:7-alpine}" |
| 20 | + |
| 21 | +# Start PostgreSQL container |
| 22 | +start_postgres() { |
| 23 | + echo "Starting PostgreSQL container: ${POSTGRES_CONTAINER_NAME}" >&2 |
| 24 | + |
| 25 | + podman run -d \ |
| 26 | + --name "${POSTGRES_CONTAINER_NAME}" \ |
| 27 | + -e POSTGRES_USER="${POSTGRES_USER}" \ |
| 28 | + -e POSTGRES_PASSWORD="${POSTGRES_PASSWORD}" \ |
| 29 | + -e POSTGRES_DB="${POSTGRES_DB}" \ |
| 30 | + -p "${POSTGRES_PORT}:5432" \ |
| 31 | + "${POSTGRES_IMAGE}" \ |
| 32 | + postgres -c fsync=off -c synchronous_commit=off -c full_page_writes=off |
| 33 | + |
| 34 | + # Wait for PostgreSQL to be ready |
| 35 | + echo "Waiting for PostgreSQL to be ready..." >&2 |
| 36 | + local max_attempts=30 |
| 37 | + local attempt=0 |
| 38 | + |
| 39 | + while [ $attempt -lt $max_attempts ]; do |
| 40 | + if podman exec "${POSTGRES_CONTAINER_NAME}" pg_isready -U "${POSTGRES_USER}" > /dev/null 2>&1; then |
| 41 | + echo "PostgreSQL is ready" >&2 |
| 42 | + return 0 |
| 43 | + fi |
| 44 | + attempt=$((attempt + 1)) |
| 45 | + sleep 1 |
| 46 | + done |
| 47 | + |
| 48 | + echo "ERROR: PostgreSQL failed to start within ${max_attempts} seconds" >&2 |
| 49 | + return 1 |
| 50 | +} |
| 51 | + |
| 52 | +stop_container() { |
| 53 | + local container="$1" |
| 54 | + local with_volumes="$2" |
| 55 | + |
| 56 | + echo "Stopping container: ${container}" >&2 |
| 57 | + if podman ps -a --format "{{.Names}}" | grep -q "^${container}$"; then |
| 58 | + podman stop -t 2 "${container}" > /dev/null 2>&1 || true |
| 59 | + if [ "$with_volumes" = "1" ]; then |
| 60 | + podman rm -v -f "${container}" > /dev/null 2>&1 || true |
| 61 | + else |
| 62 | + podman rm -f "${container}" > /dev/null 2>&1 || true |
| 63 | + fi |
| 64 | + fi |
| 65 | +} |
| 66 | + |
| 67 | +# Stop PostgreSQL container |
| 68 | +stop_postgres() { |
| 69 | + stop_container "$POSTGRES_CONTAINER_NAME" "$1" |
| 70 | +} |
| 71 | + |
| 72 | +# Start Redis container |
| 73 | +start_redis() { |
| 74 | + echo "Starting Redis container: ${REDIS_CONTAINER_NAME}" >&2 |
| 75 | + |
| 76 | + podman run -d \ |
| 77 | + --name "${REDIS_CONTAINER_NAME}" \ |
| 78 | + -p "${REDIS_PORT}:6379" \ |
| 79 | + "${REDIS_IMAGE}" |
| 80 | + |
| 81 | + # Wait for Redis to be ready |
| 82 | + echo "Waiting for Redis to be ready..." >&2 |
| 83 | + local max_attempts=30 |
| 84 | + local attempt=0 |
| 85 | + |
| 86 | + while [ $attempt -lt $max_attempts ]; do |
| 87 | + if podman exec "${REDIS_CONTAINER_NAME}" redis-cli ping > /dev/null 2>&1; then |
| 88 | + echo "Redis is ready" >&2 |
| 89 | + return 0 |
| 90 | + fi |
| 91 | + attempt=$((attempt + 1)) |
| 92 | + sleep 1 |
| 93 | + done |
| 94 | + |
| 95 | + echo "ERROR: Redis failed to start within ${max_attempts} seconds" >&2 |
| 96 | + return 1 |
| 97 | +} |
| 98 | + |
| 99 | +# Stop Redis container |
| 100 | +stop_redis() { |
| 101 | + stop_container "$REDIS_CONTAINER_NAME" "$1" |
| 102 | +} |
| 103 | + |
| 104 | +# Check if PostgreSQL container is running |
| 105 | +is_postgres_running() { |
| 106 | + podman ps --format "{{.Names}}" | grep -q "^${POSTGRES_CONTAINER_NAME}$" |
| 107 | +} |
| 108 | + |
| 109 | +# Check if Redis container is running |
| 110 | +is_redis_running() { |
| 111 | + podman ps --format "{{.Names}}" | grep -q "^${REDIS_CONTAINER_NAME}$" |
| 112 | +} |
| 113 | + |
| 114 | +# Get PostgreSQL connection string |
| 115 | +get_postgres_url() { |
| 116 | + echo "postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost:${POSTGRES_PORT}/${POSTGRES_DB}" |
| 117 | +} |
| 118 | + |
| 119 | +# Get Redis URL |
| 120 | +get_redis_url() { |
| 121 | + echo "redis://localhost:${REDIS_PORT}/0" |
| 122 | +} |
| 123 | + |
| 124 | +# Execute SQL in PostgreSQL container |
| 125 | +exec_sql() { |
| 126 | + local sql="$1" |
| 127 | + podman exec "${POSTGRES_CONTAINER_NAME}" \ |
| 128 | + psql -U "${POSTGRES_USER}" -d "${POSTGRES_DB}" -c "${sql}" |
| 129 | +} |
| 130 | + |
| 131 | +# Execute Redis command |
| 132 | +exec_redis() { |
| 133 | + podman exec "${REDIS_CONTAINER_NAME}" redis-cli "$@" |
| 134 | +} |
| 135 | + |
| 136 | +# Clean up all test containers |
| 137 | +cleanup_containers() { |
| 138 | + stop_postgres 1 |
| 139 | + stop_redis 1 |
| 140 | +} |
| 141 | + |
| 142 | +# Start all test containers |
| 143 | +start_containers() { |
| 144 | + start_postgres || return 1 |
| 145 | + start_redis || return 1 |
| 146 | +} |
0 commit comments