Skip to content

Commit 7a59b2e

Browse files
test: fix flaky force_renew test (detect renewal by serial) (#1269)
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 4900882 commit 7a59b2e

2 files changed

Lines changed: 24 additions & 26 deletions

File tree

test/tests/force_renew/run.sh

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,42 +26,32 @@ trap cleanup EXIT
2626
# Run a nginx container for ${domains[0]}.
2727
run_nginx_container --hosts "${domains[0]}"
2828

29-
# Wait for a symlink at /etc/nginx/certs/${domains[0]}.crt
30-
# Grab the expiration time of the certificate
29+
# Wait for the certificate to be issued, then record its serial number.
3130
wait_for_symlink "${domains[0]}" "$le_container_name"
32-
first_cert_expire="$(get_cert_date_epoch expiration "${domains[0]}" "$le_container_name")"
31+
first_serial="$(get_cert_serial "${domains[0]}" "$le_container_name")"
3332

3433
# Just to be sure
3534
sleep 5
3635

37-
# Issue a forced renewal
38-
docker exec "$le_container_name" /app/force_renew &> /dev/null
36+
# Issue a forced renewal (capture the output so a failure is diagnosable).
37+
renew_output="$(docker exec "$le_container_name" /app/force_renew 2>&1)"
3938

40-
# Poll until expiration date changes or timeout
41-
# Use a longer sleep and add error handling for transient states
39+
# A renewal re-issues the cert, so its serial must change.
4240
timeout=$(($(date +%s) + 30))
43-
second_cert_expire="$first_cert_expire"
41+
second_serial="$first_serial"
4442
while [[ $(date +%s) -lt $timeout ]]; do
45-
# Try to get the new expiration date, but handle errors gracefully
46-
new_expire="$(get_cert_date_epoch expiration "${domains[0]}" "$le_container_name" 2>/dev/null || echo "$first_cert_expire")"
47-
48-
# Only update if we got a valid value (not empty and numeric)
49-
if [[ -n "$new_expire" ]] && [[ "$new_expire" =~ ^[0-9]+$ ]]; then
50-
second_cert_expire="$new_expire"
51-
52-
# If the new certificate has a later expiration, renewal succeeded
53-
if [[ $second_cert_expire -gt $first_cert_expire ]]; then
54-
[[ "${DRY_RUN:-}" == 1 ]] && echo "Certificate for ${domains[0]} was correctly renewed."
55-
break
56-
fi
43+
new_serial="$(get_cert_serial "${domains[0]}" "$le_container_name" 2>/dev/null || true)"
44+
if [[ -n "$new_serial" && "$new_serial" != "$first_serial" ]]; then
45+
second_serial="$new_serial"
46+
[[ "${DRY_RUN:-}" == 1 ]] && echo "Certificate for ${domains[0]} was correctly renewed."
47+
break
5748
fi
58-
5949
sleep 2
6050
done
6151

62-
# Final check - verify renewal actually happened
63-
if ! [[ $second_cert_expire -gt $first_cert_expire ]]; then
64-
echo "Certificate for ${domains[0]} was not correctly renewed within 30s."
65-
echo "First certificate expiration epoch : $first_cert_expire."
66-
echo "Second certificate expiration epoch : $second_cert_expire."
52+
# Final check - verify the certificate was actually re-issued.
53+
if [[ "$second_serial" == "$first_serial" ]]; then
54+
echo "Certificate for ${domains[0]} was not correctly renewed within 30s (serial unchanged: $first_serial)."
55+
echo "force_renew output:"
56+
echo "$renew_output"
6757
fi

test/tests/test-functions.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,14 @@ function get_cert_date_epoch {
348348
}
349349
export -f get_cert_date_epoch
350350

351+
# Get the serial number of the certificate for domain $1 inside container $2.
352+
function get_cert_serial {
353+
local domain="${1:?}"
354+
local name="${2:?}"
355+
docker exec "$name" openssl x509 -noout -serial -in "/etc/nginx/certs/$domain.crt" | cut -d '=' -f 2
356+
}
357+
export -f get_cert_serial
358+
351359
# Get the certificate validity period in seconds of domain $1 inside container $2
352360
function get_cert_validity_seconds {
353361
local domain="${1:?}"

0 commit comments

Comments
 (0)