| 1 | #!/usr/bin/env bash |
| 2 | set -euo pipefail |
| 3 | |
| 4 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 5 | # shellcheck disable=SC1091 |
| 6 | . "$SCRIPT_DIR/lib.sh" |
| 7 | |
| 8 | init_workspace "postgres" |
| 9 | trap cleanup EXIT |
| 10 | |
| 11 | POSTGRES_PORT="$(reserve_port)" |
| 12 | write_env "POSTGRES_PORT" "$POSTGRES_PORT" |
| 13 | replace_in_file "$WORKDIR/config/go.d/postgres.conf" "127.0.0.1:5432" "127.0.0.1:${POSTGRES_PORT}" |
| 14 | |
| 15 | POSTGRES_VARIANT_LABEL="${POSTGRES_VARIANT:-postgres}" |
| 16 | if [ -n "${POSTGRES_IMAGE:-}" ]; then |
| 17 | write_env "POSTGRES_IMAGE" "$POSTGRES_IMAGE" |
| 18 | fi |
| 19 | |
| 20 | # Support pg_stat_monitor testing with Percona distribution |
| 21 | # POSTGRES_STATS_EXT can be "pg_stat_statements" (default) or "pg_stat_monitor" |
| 22 | POSTGRES_STATS_EXT="${POSTGRES_STATS_EXT:-pg_stat_statements}" |
| 23 | write_env "POSTGRES_PRELOAD_LIBRARIES" "$POSTGRES_STATS_EXT" |
| 24 | |
| 25 | # Use the appropriate init script based on extension |
| 26 | if [ "$POSTGRES_STATS_EXT" = "pg_stat_monitor" ]; then |
| 27 | cp "$WORKDIR/seed/postgres/init-pgsm.sql" "$WORKDIR/seed/postgres/init.sql" |
| 28 | fi |
| 29 | |
| 30 | compose_up postgres |
| 31 | wait_healthy postgres 90 |
| 32 | |
| 33 | build_plugin |
| 34 | |
| 35 | # Test top-queries (pg_stat_statements or pg_stat_monitor) |
| 36 | run_info postgres |
| 37 | run_top_queries postgres |
| 38 | assert_column_visibility "$WORKDIR/postgres-top-queries.json" "top-queries" |
| 39 | |
| 40 | # Verify the correct extension is being used |
| 41 | if [ "$POSTGRES_STATS_EXT" = "pg_stat_monitor" ]; then |
| 42 | # pg_stat_monitor should have applicationName and cpuUserTime columns |
| 43 | if ! jq -e '.columns | has("applicationName")' "$WORKDIR/postgres-top-queries.json" > /dev/null 2>&1; then |
| 44 | echo "ERROR: pg_stat_monitor expected but applicationName column not found" >&2 |
| 45 | exit 1 |
| 46 | fi |
| 47 | echo "Verified: pg_stat_monitor is being used (applicationName column present)" >&2 |
| 48 | else |
| 49 | # pg_stat_statements should NOT have applicationName column |
| 50 | if jq -e '.columns | has("applicationName")' "$WORKDIR/postgres-top-queries.json" > /dev/null 2>&1; then |
| 51 | echo "ERROR: pg_stat_statements expected but applicationName column found" >&2 |
| 52 | exit 1 |
| 53 | fi |
| 54 | echo "Verified: pg_stat_statements is being used" >&2 |
| 55 | fi |
| 56 | |
| 57 | # Test running-queries (pg_stat_activity) |
| 58 | # Note: running-queries may return 0 rows if no active queries at test time |
| 59 | run_info_method postgres running-queries |
| 60 | run_running_queries postgres 0 |
| 61 | assert_column_visibility "$WORKDIR/postgres-running-queries.json" "running-queries" |
| 62 | |
| 63 | echo "E2E checks passed for ${POSTGRES_VARIANT_LABEL} with ${POSTGRES_STATS_EXT}." >&2 |