Skip to content

Commit 2f9e62b

Browse files
committed
Fix flaky fetch-jwt-svids integration test by waiting for the restarted server
Signed-off-by: Agustín Martínez Fayó <amartinezfayo@gmail.com>
1 parent 12c8be5 commit 2f9e62b

2 files changed

Lines changed: 56 additions & 8 deletions

File tree

test/integration/common

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,37 @@ check-server-started() {
144144
fail-now "timed out waiting for server to start"
145145
}
146146

147+
# Counts how many times the server has logged that it started its APIs. The
148+
# server emits the "Starting Server APIs" line twice per boot (TCP and UDS
149+
# listeners).
150+
count-server-api-starts() {
151+
# grep -c exits non-zero when there are no matches, which would trip the
152+
# step's errexit/pipefail, so swallow that into a zero count.
153+
docker compose logs "$1" | grep -c "Starting Server APIs" || true
154+
}
155+
156+
# Like check-server-started but safe to use after "docker compose restart".
157+
# A restart reuses the same container, so its log still contains the
158+
# "Starting Server APIs" markers from the pre-restart boot. Callers capture the
159+
# marker count before restarting and pass it here so we wait for the restarted
160+
# server to emit its own two markers rather than returning on the stale ones.
161+
wait-for-server-restarted() {
162+
local container="$1"
163+
local baseline="$2"
164+
local want=$((baseline + 2))
165+
MAXCHECKS=30
166+
CHECKINTERVAL=1
167+
for ((i=1;i<=MAXCHECKS;i++)); do
168+
log-info "checking for restarted server APIs ($i of $MAXCHECKS max)..."
169+
if [ "$(count-server-api-starts "$container")" -ge "$want" ]; then
170+
return 0
171+
fi
172+
sleep "${CHECKINTERVAL}"
173+
done
174+
175+
fail-now "timed out waiting for server to restart"
176+
}
177+
147178
check-log-line() {
148179
# Check at most 30 times (with one second in between) that the agent has
149180
# successfully synced down the workload entry.

test/integration/suites/fetch-jwt-svids/08-test-disabled-jwt

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,40 @@ log-info "testing disabled JWT functionality..."
44

55
# Replace with disable_jwt_svids = true configuration
66
sed -i.bak "s#disable_jwt_svids = false#disable_jwt_svids = true#g" conf/server/server.conf
7+
8+
# The restart reuses the container, so its log keeps the "Starting Server APIs"
9+
# markers from the original boot. Capture that count first and wait for the
10+
# restarted server to emit its own markers, otherwise the readiness check would
11+
# return on the stale ones before the server is actually listening again.
12+
baseline=$(count-server-api-starts spire-server)
713
docker compose restart spire-server
8-
check-server-started spire-server
14+
wait-for-server-restarted spire-server "$baseline"
915

1016
test-jwt-disabled-command() {
1117
local test_name="$1"
1218
shift 1
1319
local command=("$@")
14-
15-
if ! ERROR_OUTPUT=$("${command[@]}" 2>&1); then
20+
21+
# Retry to ride out the brief window right after the restart where the
22+
# server (or the agent's connection to it) is not ready yet and the command
23+
# fails with a transient transport error instead of the expected JWT
24+
# disabled error. The agent JWT fetch reaches the server over its TCP API,
25+
# so it is the check most prone to racing the restart.
26+
local MAXCHECKS=15
27+
local CHECKINTERVAL=1
28+
for ((i=1;i<=MAXCHECKS;i++)); do
29+
if ERROR_OUTPUT=$("${command[@]}" 2>&1); then
30+
fail-now "$test_name should have failed when JWT is disabled"
31+
fi
1632
if echo "$ERROR_OUTPUT" | grep -q "JWT functionality is disabled"; then
1733
log-info "$test_name correctly failed with JWT disabled error"
18-
else
19-
fail-now "$test_name expected JWT disabled error, but got: $ERROR_OUTPUT"
34+
return 0
2035
fi
21-
else
22-
fail-now "$test_name should have failed when JWT is disabled"
23-
fi
36+
log-info "$test_name not ready yet (attempt $i of $MAXCHECKS): $ERROR_OUTPUT"
37+
sleep "${CHECKINTERVAL}"
38+
done
39+
40+
fail-now "$test_name expected JWT disabled error, but got: $ERROR_OUTPUT"
2441
}
2542

2643
test-jwt-disabled-command "JWT authority prepare" \

0 commit comments

Comments
 (0)