Skip to content

Commit 786ac41

Browse files
authored
Merge pull request #6618 from NHSDigital/end-to-end-redis-db
Use redis db 2 in end_to_end env
2 parents 78af693 + bf990fc commit 786ac41

1 file changed

Lines changed: 69 additions & 63 deletions

File tree

bin/e2e

Lines changed: 69 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
set -euo pipefail
33

44
# Rails
5-
RAILS_ENV="end_to_end"
6-
RAILS_PORT="4100"
7-
HEALTH_CHECK_URL="http://localhost:${RAILS_PORT}/up"
8-
MAVIS_TEST_REPO="${MAVIS_TEST_REPO:-"../manage-vaccinations-in-schools-testing"}"
9-
PYTEST_ARGS=("-m" "not accessibility and not reporting and not imms_api and not pds_api")
5+
rails_port="4100"
6+
health_check_url="http://localhost:${rails_port}/up"
7+
mavis_test_repo="${mavis_test_repo:-"../manage-vaccinations-in-schools-testing"}"
8+
pytest_args=("-m" "not accessibility and not reporting and not imms_api and not pds_api")
9+
10+
export RAILS_ENV="end_to_end"
11+
export REDIS_URL="redis://localhost:6379/2"
12+
export BASE_URL="http://localhost:${rails_port}"
1013

1114
# Argument handling
1215

@@ -17,7 +20,7 @@ function print_help() {
1720
echo "run by default."
1821
echo ""
1922
echo "The pytest CLI command defaults to:"
20-
echo " uv run pytest ${PYTEST_ARGS[*]} <pytest-target>"
23+
echo " uv run pytest ${pytest_args[*]} <pytest-target>"
2124
echo ""
2225
echo "Options:"
2326
echo " --main Force sync testing repo to latest main branch. WARNING: this"
@@ -36,35 +39,35 @@ if ! command -v uv >/dev/null 2>&1; then
3639
exit 1
3740
fi
3841

39-
if [ ! -d "$MAVIS_TEST_REPO/.git" ]; then
40-
echo "[e2e] ERROR: Testing repo not found at: $MAVIS_TEST_REPO" >&2
41-
echo " Clone the repo there or set the MAVIS_TEST_REPO" >&2
42+
if [ ! -d "$mavis_test_repo/.git" ]; then
43+
echo "[e2e] ERROR: Testing repo not found at: $mavis_test_repo" >&2
44+
echo " Clone the repo there or set the mavis_test_repo" >&2
4245
echo " environment variable to the path of your repo" >&2
4346
exit 1
4447
fi
4548

4649
# Consume --main flag if present
47-
USE_MAVIS_BRANCH=false
50+
use_mavis_branch=false
4851

4952
for arg in "$@"; do
5053
case "$arg" in
5154
--main)
52-
USE_MAVIS_BRANCH=true
55+
use_mavis_branch=true
5356
;;
5457
-h|--help)
5558
print_help
5659
exit 0
5760
;;
5861
*)
59-
PYTEST_ARGS+=("$arg")
62+
pytest_args+=("$arg")
6063
;;
6164
esac
6265
done
6366

6467
# Update mavis testing repo
65-
if [ "${USE_MAVIS_BRANCH:-false}" = true ]; then
68+
if [ "${use_mavis_branch:-false}" = true ]; then
6669
echo "[e2e] Using latest main branch of testing repo."
67-
pushd "$MAVIS_TEST_REPO" > /dev/null
70+
pushd "$mavis_test_repo" > /dev/null
6871

6972
git fetch origin
7073
git checkout main
@@ -78,48 +81,48 @@ else
7881
echo "[e2e] Checking branch alignment with testing repo..."
7982

8083
# Service (main) repo branch
81-
MAVIS_REPO="$(git rev-parse --show-toplevel 2>/dev/null || echo "")"
82-
MAVIS_BRANCH="unknown"
83-
if [ -n "$MAVIS_REPO" ]; then
84-
MAVIS_BRANCH="$(git -C "$MAVIS_REPO" rev-parse --abbrev-ref HEAD 2>/dev/null || echo "HEAD")"
84+
mavis_repo="$(git rev-parse --show-toplevel 2>/dev/null || echo "")"
85+
mavis_branch="unknown"
86+
if [ -n "$mavis_repo" ]; then
87+
mavis_branch="$(git -C "$mavis_repo" rev-parse --abbrev-ref HEAD 2>/dev/null || echo "HEAD")"
8588
fi
8689

8790
# Testing repo branch
88-
MAVIS_TEST_BRANCH="unknown"
89-
if git -C "$MAVIS_TEST_REPO" rev-parse --git-dir >/dev/null 2>&1; then
90-
MAVIS_TEST_BRANCH="$(git -C "$MAVIS_TEST_REPO" rev-parse --abbrev-ref HEAD 2>/dev/null || echo "HEAD")"
91+
mavis_test_branch="unknown"
92+
if git -C "$mavis_test_repo" rev-parse --git-dir >/dev/null 2>&1; then
93+
mavis_test_branch="$(git -C "$mavis_test_repo" rev-parse --abbrev-ref HEAD 2>/dev/null || echo "HEAD")"
9194
fi
9295

9396
# If testing repo is on main, warn if it's behind origin/main
94-
if [ "$MAVIS_TEST_BRANCH" = "main" ]; then
97+
if [ "$mavis_test_branch" = "main" ]; then
9598
# Fetch without failing the whole script if it goes wrong
96-
git -C "$MAVIS_TEST_REPO" fetch origin main >/dev/null 2>&1 || true
99+
git -C "$mavis_test_repo" fetch origin main >/dev/null 2>&1 || true
97100

98-
LOCAL_MAIN_SHA="$(git -C "$MAVIS_TEST_REPO" rev-parse main 2>/dev/null || echo "")"
99-
REMOTE_MAIN_SHA="$(git -C "$MAVIS_TEST_REPO" rev-parse origin/main 2>/dev/null || echo "")"
101+
local_main_sha="$(git -C "$mavis_test_repo" rev-parse main 2>/dev/null || echo "")"
102+
remote_main_sha="$(git -C "$mavis_test_repo" rev-parse origin/main 2>/dev/null || echo "")"
100103

101-
if [ -n "$LOCAL_MAIN_SHA" ] && [ -n "$REMOTE_MAIN_SHA" ] && [ "$LOCAL_MAIN_SHA" != "$REMOTE_MAIN_SHA" ]; then
102-
AHEAD_BEHIND="$(git -C "$MAVIS_TEST_REPO" rev-list --left-right --count main...origin/main 2>/dev/null || echo "")"
103-
set -- $AHEAD_BEHIND
104-
LOCAL_AHEAD="$1"
105-
LOCAL_BEHIND="$2"
104+
if [ -n "$local_main_sha" ] && [ -n "$remote_main_sha" ] && [ "$local_main_sha" != "$remote_main_sha" ]; then
105+
ahead_behind="$(git -C "$mavis_test_repo" rev-list --left-right --count main...origin/main 2>/dev/null || echo "")"
106+
set -- $ahead_behind
107+
local_ahead="$1"
108+
local_behind="$2"
106109

107110
# Warn if local main differs from remote
108-
if [ -n "$LOCAL_BEHIND" ] && [ "$LOCAL_BEHIND" -gt 0 ] 2>/dev/null; then
109-
echo "[e2e] !!! WARNING !!! Testing repo 'main' is behind origin/main by $LOCAL_BEHIND commit(s)."
110-
echo "[e2e] Run 'git -C \"$MAVIS_TEST_REPO\" pull --ff-only origin main' "
111+
if [ -n "$local_behind" ] && [ "$local_behind" -gt 0 ] 2>/dev/null; then
112+
echo "[e2e] !!! WARNING !!! Testing repo 'main' is behind origin/main by $local_behind commit(s)."
113+
echo "[e2e] Run 'git -C \"$mavis_test_repo\" pull --ff-only origin main' "
111114
echo "[e2e] or use --main to force-sync."
112-
elif [ -n "$LOCAL_AHEAD" ] && [ "$LOCAL_AHEAD" -gt 0 ] 2>/dev/null; then
113-
echo "[e2e] !!! WARNING !!! Testing repo 'main' is ahead of origin/main by $LOCAL_AHEAD commit(s)."
114-
echo "[e2e] Run 'git -C \"$MAVIS_TEST_REPO\" pull --ff-only origin main' "
115+
elif [ -n "$local_ahead" ] && [ "$local_ahead" -gt 0 ] 2>/dev/null; then
116+
echo "[e2e] !!! WARNING !!! Testing repo 'main' is ahead of origin/main by $local_ahead commit(s)."
117+
echo "[e2e] Run 'git -C \"$mavis_test_repo\" pull --ff-only origin main' "
115118
echo "[e2e] or use --main to force-sync."
116119
fi
117120
else
118121
echo "[e2e] Testing repo 'main' branch is up to date with origin/main."
119122
fi
120-
elif [ "$MAVIS_BRANCH" != "$MAVIS_TEST_BRANCH" ]; then
121-
echo "[e2e] !!! WARNING !!! Service repo branch ($MAVIS_BRANCH) "
122-
echo "[e2e] does not match testing repo branch ($MAVIS_TEST_BRANCH)."
123+
elif [ "$mavis_branch" != "$mavis_test_branch" ]; then
124+
echo "[e2e] !!! WARNING !!! Service repo branch ($mavis_branch) "
125+
echo "[e2e] does not match testing repo branch ($mavis_test_branch)."
123126
echo "[e2e] You may be running tests from a different branch than you desire."
124127
fi
125128
fi
@@ -128,7 +131,7 @@ fi
128131

129132
echo "[e2e] Preparing Rails test DB schema (RAILS_ENV=$RAILS_ENV)..."
130133
bin/bundle install --quiet
131-
RAILS_ENV="$RAILS_ENV" bin/rails db:prepare
134+
bin/rails db:prepare
132135

133136
# Start Rails server in end_to_end environment
134137

@@ -137,43 +140,43 @@ gem install --silent foreman
137140

138141
echo "[e2e] Starting end_to_end stack with foreman..."
139142
if command -v setsid >/dev/null 2>&1; then
140-
START_FOREMAN=(setsid foreman start -f Procfile.e2e)
143+
start_foreman=(setsid foreman start -f Procfile.e2e)
141144
else
142145
# macOS does not ship a setsid binary; use Perl to create a new session.
143-
START_FOREMAN=(perl -MPOSIX -e 'POSIX::setsid() or die "setsid failed: $!"; exec @ARGV' foreman start -f Procfile.e2e)
146+
start_foreman=(perl -MPOSIX -e 'POSIX::setsid() or die "setsid failed: $!"; exec @ARGV' foreman start -f Procfile.e2e)
144147
fi
145148

146149
echo "[e2e] Starting rails server with foreman. Logs will be written to /tmp/e2e-foreman.log"
147-
RAILS_ENV="$RAILS_ENV" "${START_FOREMAN[@]}" >/tmp/e2e-foreman.log 2>&1 &
148-
E2E_PGID=$!
150+
"${start_foreman[@]}" >/tmp/e2e-foreman.log 2>&1 &
151+
e2e_pgid=$!
149152

150153

151154
cleanup() {
152155
echo "[e2e] Cleaning up end_to_end stack..."
153156

154-
if [[ -n "${E2E_PGID:-}" ]]; then
155-
echo "[e2e] Stopping process group PGID=$E2E_PGID..."
157+
if [[ -n "${e2e_pgid:-}" ]]; then
158+
echo "[e2e] Stopping process group PGID=$e2e_pgid..."
156159

157160
# Try INT (like Ctrl‑C) then TERM then KILL for the whole group
158-
for SIG in INT TERM KILL; do
161+
for sig in INT TERM KILL; do
159162
# If nothing in the group, stop
160-
if ! kill -0 "-$E2E_PGID" 2>/dev/null; then
161-
echo "[e2e] Process group $E2E_PGID already gone."
163+
if ! kill -0 "-$e2e_pgid" 2>/dev/null; then
164+
echo "[e2e] Process group $e2e_pgid already gone."
162165
break
163166
fi
164167

165-
echo "[e2e] Sending $SIG to process group $E2E_PGID..."
166-
kill "-$SIG" "-$E2E_PGID" 2>/dev/null || true
168+
echo "[e2e] Sending $sig to process group $e2e_pgid..."
169+
kill "-$sig" "-$e2e_pgid" 2>/dev/null || true
167170
sleep 3
168171
done
169172
fi
170173

171-
# Final safety net: ensure nothing is listening on $RAILS_PORT
174+
# Final safety net: ensure nothing is listening on $rails_port
172175
if command -v lsof >/dev/null 2>&1; then
173-
PIDS="$(lsof -ti tcp:"$RAILS_PORT" || true)"
174-
if [ -n "$PIDS" ]; then
175-
echo "[e2e] Forcing kill of processes on port $RAILS_PORT: $PIDS"
176-
kill -KILL $PIDS 2>/dev/null || true
176+
pids="$(lsof -ti tcp:"$rails_port" || true)"
177+
if [ -n "$pids" ]; then
178+
echo "[e2e] Forcing kill of processes on port $rails_port: $pids"
179+
kill -KILL $pids 2>/dev/null || true
177180
fi
178181
fi
179182
}
@@ -182,10 +185,10 @@ trap cleanup EXIT INT TERM
182185

183186
# Wait for Rails health
184187

185-
echo "[e2e] Waiting for Rails to become healthy on $HEALTH_CHECK_URL"
188+
echo "[e2e] Waiting for Rails to become healthy on $health_check_url"
186189
for i in {1..10}; do
187190
printf "."
188-
if curl -fsS "$HEALTH_CHECK_URL" > /dev/null 2>&1; then
191+
if curl -fsS "$health_check_url" > /dev/null 2>&1; then
189192
printf "\n[e2e] Rails is up.\n"
190193
break
191194
fi
@@ -201,13 +204,16 @@ done
201204

202205
# Run pytest via uv
203206

204-
pushd "$MAVIS_TEST_REPO" > /dev/null
207+
pushd "$mavis_test_repo" > /dev/null
205208

206209
echo "[e2e] Running end-to-end tests:"
207-
echo "[e2e] BASE_URL=\"http://localhost:${RAILS_PORT}\" uv run pytest ${PYTEST_ARGS[*]}"
208-
BASE_URL="http://localhost:${RAILS_PORT}" uv run pytest -m "not accessibility and not reporting and not imms_api and not pds_api" "${PYTEST_ARGS[@]}"
209-
PYTEST_EXIT_CODE=$?
210+
echo " export RAILS_ENV=\"$RAILS_ENV\""
211+
echo " export REDIS_URL=\"$REDIS_URL\""
212+
echo " export BASE_URL=\"$BASE_URL\""
213+
echo " uv run pytest ${pytest_args[*]}"
214+
uv run pytest "${pytest_args[@]}"
215+
pytest_exit_code=$?
210216

211217
popd > /dev/null
212218

213-
exit $PYTEST_EXIT_CODE
219+
exit $pytest_exit_code

0 commit comments

Comments
 (0)