92 lines
2.5 KiB
Bash
92 lines
2.5 KiB
Bash
|
|
#!/usr/bin/env bash
|
||
|
|
# Shared helpers for e2e-run / e2e-up / e2e-down.
|
||
|
|
# shellcheck disable=SC2034
|
||
|
|
|
||
|
|
e2e_root_dir() {
|
||
|
|
cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd
|
||
|
|
}
|
||
|
|
|
||
|
|
# Stop gateway started for E2E: pid file → port listeners → stale go run orphans.
|
||
|
|
e2e_stop_gateway() {
|
||
|
|
local port="${1:-18888}"
|
||
|
|
local pid_file="${2:-}"
|
||
|
|
|
||
|
|
if [[ -n "${pid_file}" && -f "${pid_file}" ]]; then
|
||
|
|
local pid
|
||
|
|
pid="$(cat "${pid_file}")"
|
||
|
|
if [[ -n "${pid}" ]] && kill -0 "${pid}" 2>/dev/null; then
|
||
|
|
echo ">> stopping gateway pid=${pid}"
|
||
|
|
kill "${pid}" 2>/dev/null || true
|
||
|
|
for _ in $(seq 1 10); do
|
||
|
|
kill -0 "${pid}" 2>/dev/null || break
|
||
|
|
sleep 0.2
|
||
|
|
done
|
||
|
|
if kill -0 "${pid}" 2>/dev/null; then
|
||
|
|
kill -9 "${pid}" 2>/dev/null || true
|
||
|
|
fi
|
||
|
|
wait "${pid}" 2>/dev/null || true
|
||
|
|
fi
|
||
|
|
rm -f "${pid_file}"
|
||
|
|
fi
|
||
|
|
|
||
|
|
if command -v lsof >/dev/null 2>&1; then
|
||
|
|
local pids
|
||
|
|
pids="$(lsof -ti tcp:"${port}" 2>/dev/null | tr '\n' ' ' || true)"
|
||
|
|
if [[ -n "${pids// /}" ]]; then
|
||
|
|
echo ">> stopping listener(s) on :${port} (${pids})"
|
||
|
|
# shellcheck disable=SC2086
|
||
|
|
kill ${pids} 2>/dev/null || true
|
||
|
|
sleep 0.5
|
||
|
|
pids="$(lsof -ti tcp:"${port}" 2>/dev/null | tr '\n' ' ' || true)"
|
||
|
|
if [[ -n "${pids// /}" ]]; then
|
||
|
|
# shellcheck disable=SC2086
|
||
|
|
kill -9 ${pids} 2>/dev/null || true
|
||
|
|
fi
|
||
|
|
fi
|
||
|
|
fi
|
||
|
|
|
||
|
|
# go run leaves a compiled binary under $TMPDIR; kill by e2e config path if still up.
|
||
|
|
if command -v pgrep >/dev/null 2>&1; then
|
||
|
|
while IFS= read -r orphan; do
|
||
|
|
[[ -z "${orphan}" ]] && continue
|
||
|
|
echo ">> stopping orphan gateway pid=${orphan}"
|
||
|
|
kill "${orphan}" 2>/dev/null || true
|
||
|
|
done < <(pgrep -f "gateway(-e2e)? .*${port}|gateway.go -f .*e2e.yaml" 2>/dev/null || true)
|
||
|
|
fi
|
||
|
|
}
|
||
|
|
|
||
|
|
# Build a real binary so $! is the server PID (go run only tracks the wrapper).
|
||
|
|
e2e_start_gateway() {
|
||
|
|
local root="$1"
|
||
|
|
local config="$2"
|
||
|
|
local port="$3"
|
||
|
|
local pid_file="$4"
|
||
|
|
|
||
|
|
local bin="${root}/.cache/e2e-gateway"
|
||
|
|
mkdir -p "${root}/.cache"
|
||
|
|
|
||
|
|
e2e_stop_gateway "${port}" "${pid_file}"
|
||
|
|
|
||
|
|
echo ">> building e2e gateway binary"
|
||
|
|
(cd "${root}" && go build -o "${bin}" gateway.go)
|
||
|
|
|
||
|
|
echo ">> starting gateway on :${port}"
|
||
|
|
GATEWAY_E2E=1 "${bin}" -f "${config}" &
|
||
|
|
local pid=$!
|
||
|
|
echo "${pid}" > "${pid_file}"
|
||
|
|
echo "${pid}"
|
||
|
|
}
|
||
|
|
|
||
|
|
e2e_wait_gateway() {
|
||
|
|
local port="$1"
|
||
|
|
local url="http://127.0.0.1:${port}/api/v1/health"
|
||
|
|
for i in $(seq 1 60); do
|
||
|
|
if curl -sf "${url}" >/dev/null; then
|
||
|
|
return 0
|
||
|
|
fi
|
||
|
|
sleep 1
|
||
|
|
done
|
||
|
|
echo "timeout waiting for gateway ${url}" >&2
|
||
|
|
return 1
|
||
|
|
}
|