Skip to content

Commit 41046f4

Browse files
test(monitoring-agents): check the first patch lands before the Job deadline
The grace-period test keeps sleep free of fake time so a shortened wait shows, which also let a poll interval long enough to outlast activeDeadlineSeconds pass. A separate test now charges each sleep to the fake clock and checks the first finalizer patch against the deadline rendered from the chart. Assisted-by: LLM Signed-off-by: Mattia Eleuteri <[email protected]>
1 parent 1f79524 commit 41046f4

1 file changed

Lines changed: 37 additions & 2 deletions

File tree

‎hack/monitoring-agents-vm-cleanup-hook.bats‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ render_hook() {
5656
# only when the payload actually clears the finalizers. Every call goes to $KLOG.
5757
#
5858
# date advances 1s per call from $CLOCK, so the 60s wait ends after sixty
59-
# of polls, and records each time it returns in $KLOG; sleep only records
60-
# itself. The log thus shows how much fake time passed before each patch.
59+
# of polls, and records each time it returns in $KLOG; sleep records itself,
60+
# and with $SLEEP_ADVANCES=1 also moves $CLOCK by its argument. The log thus
61+
# shows how much fake time passed before each patch.
6162
# No line below is a bare column-0 `}`, so cozytest.sh's parser leaves it alone.
6263
write_fakes() {
6364
cat > "$1/kubectl" <<'KEOF'
@@ -104,6 +105,10 @@ KEOF
104105
cat > "$1/sleep" <<'KEOF'
105106
#!/bin/sh
106107
echo "sleep $*" >> "$KLOG"
108+
if [ "${SLEEP_ADVANCES:-0}" = 1 ]; then
109+
c=$(cat "$CLOCK" 2>/dev/null || echo 0)
110+
echo $((c + $1)) > "$CLOCK"
111+
fi
107112
KEOF
108113
chmod +x "$1/kubectl" "$1/date" "$1/sleep"
109114
}
@@ -202,6 +207,36 @@ run_hook() {
202207
rm -rf "$tmp"
203208
}
204209

210+
@test "vm cleanup hook clears the finalizers before its Job's deadline" {
211+
# The grace-period test above keeps sleep free so a shortened wait shows; here
212+
# sleep costs its argument, so a poll interval long enough to overshoot the
213+
# Job's activeDeadlineSeconds fails instead of passing.
214+
tmp=$(mktemp -d)
215+
write_fakes "$tmp"
216+
write_state "$tmp/state"
217+
render_hook vm-objects-cleanup "$tmp/hook.sh"
218+
job_deadline=$(helm template "$MA_RELEASE" "$MA_CHART" --namespace "$MA_NAMESPACE" \
219+
| yq eval 'select(.kind == "Job" and .metadata.name == "vm-objects-cleanup") | .spec.activeDeadlineSeconds' -)
220+
export OPERATOR_LIVE=0 MISSING_KINDS="" SLEEP_ADVANCES=1
221+
run_hook "$tmp"
222+
unset SLEEP_ADVANCES
223+
224+
if [ "$rc" -ne 0 ]; then
225+
echo "FAIL: hook exited $rc"
226+
cat "$tmp/out" "$tmp/err"
227+
false
228+
fi
229+
first_patch=$(grep -n ' patch ' "$tmp/calls" | head -1 | cut -d: -f1)
230+
start=$(grep '^date ' "$tmp/calls" | head -1 | cut -d' ' -f2)
231+
waited_until=$(head -n "${first_patch:-0}" "$tmp/calls" | grep '^date ' | tail -1 | cut -d' ' -f2)
232+
if [ -z "$first_patch" ] || [ -z "$job_deadline" ] || [ $((waited_until - start)) -ge "$job_deadline" ]; then
233+
echo "FAIL: first finalizer patch at $((waited_until - start))s, Job deadline ${job_deadline}s"
234+
cat "$tmp/calls"
235+
false
236+
fi
237+
rm -rf "$tmp"
238+
}
239+
205240
@test "vm cleanup hook leaves the finalizers to a live operator" {
206241
tmp=$(mktemp -d)
207242
write_fakes "$tmp"

0 commit comments

Comments
 (0)