mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-22 04:33:36 +09:00
The test that claimed to pin the HelmRelease v2 history shape wrote a HelmRelease document containing the field names it expected, then read that same document back with the same jsonpath. It established that yq can read a fixture the test itself composed. Its own comment described the failure it was meant to catch: a future Flux release renames the field, the jsonpath matches nothing, the guard reports no remediation cycle, and real cycles pass the e2e assertion. A rename moves neither the fixture nor the expression, so the test stays green through exactly that event. The comment also named where the truth lives, github.com/fluxcd/helm-controller/api/v2, and pinned a copy of the author's expectations instead. Replace it with a Go test that supplies neither half. The expression is read out of the shell library that ships it, and the object is built from the upstream type at the version go.mod holds, then serialized and matched the way kubectl does. A renamed or retyped field is a compile error or a failed match; a guard that stops reading history fails the lookup with a message saying so. Both directions were confirmed by mutation. Locating the expression by searching the file is not enough on its own. Inline in a long script, locating it means deciding which occurrences are code and which are prose, and every way of getting that wrong fails green: an example quoted near a broken read satisfies the pin while the guard reads nothing. So the expression moves into a named assignment in remediation-guard.sh, which the guard already sources, and the test sources that library and takes the value the shell is left holding. Deciding which text is the assignment is then not the test's problem: an `export`, an indent, a second assignment later in the file all resolve the way the guard will resolve them, because the thing resolving them is the shell. Sourcing borrows the environment along with the shell, so the snippet unsets the name before it sources. `go test` hands the child its own environment, and a shell that had exported this name would answer in place of a library that stopped assigning it: the value comes back non-empty, the empty check stays quiet, and the pin passes over a guard that reads nothing. That is this file's own subject reaching it through the environment rather than through the source. Moving the expression out of the call site puts a step between what is pinned and what runs, so the call site is checked too, and the check starts from the call rather than from the text: the local the guard is handed must be assigned exactly once, by an anchored assignment whose jsonpath is the expansion of the shared name. That a well-formed read exists somewhere in the file is a weaker claim than it reads as, since a second assignment to the same local, or a read filling another one, satisfies it while the guard runs a literal nothing here pins. The expansion and not the name: kubectl echoes a braceless template back verbatim, so a read that dropped the `$` would return its own text, clear the script's empty-history backstop, and carry no status the cycle check matches. The library is checked by sourcing rather than by reading, so an assignment the shell would run is resolved by the shell instead of by a guess about which text the shell would pick. Each rule is pinned by its own test, so a regression names which one it opened rather than only that extraction broke. How kubectl's printer treats an absent path is its own configuration rather than part of this contract, so it is left unasserted: guessing at it would put the expectation and the fixture back in the same hands. Assisted-by: LLM Signed-off-by: Aleksei Sviridkin <f@lex.la>
83 lines
3.2 KiB
Bash
83 lines
3.2 KiB
Bash
#!/usr/bin/env bats
|
|
# -----------------------------------------------------------------------------
|
|
# Unit tests for hack/e2e-chainsaw/_lib/remediation-guard.sh
|
|
#
|
|
# helmrelease_has_remediation_cycle takes a newline-delimited list of
|
|
# HelmRelease history snapshot status values (deployed/superseded/failed/
|
|
# uninstalled/...) and returns 0 when any entry is "failed" or "uninstalled"
|
|
# (meaning flux helm-controller performed install/upgrade remediation).
|
|
#
|
|
# This is used by the e2e script after the HelmRelease reaches Ready. The
|
|
# failure/upgrade counters (.status.installFailures / .status.upgradeFailures)
|
|
# are useless there because flux's ClearFailures zeroes them on successful
|
|
# reconciliation; .status.history retains the snapshot trail.
|
|
#
|
|
# cozytest.sh's awk parser recognizes only @test blocks and a bare `}` on
|
|
# its own line; there is no bats `run` or `$status`. Assertions are
|
|
# expressed as direct shell tests that exit non-zero on failure.
|
|
#
|
|
# Run with: hack/cozytest.sh hack/remediation-guard.bats
|
|
# -----------------------------------------------------------------------------
|
|
|
|
@test "empty history returns not-detected" {
|
|
. hack/e2e-chainsaw/_lib/remediation-guard.sh
|
|
if helmrelease_has_remediation_cycle ""; then
|
|
echo "expected not-detected for empty history" >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
@test "single deployed snapshot returns not-detected" {
|
|
. hack/e2e-chainsaw/_lib/remediation-guard.sh
|
|
if helmrelease_has_remediation_cycle "deployed"; then
|
|
echo "expected not-detected for deployed-only history" >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
@test "deployed then superseded returns not-detected" {
|
|
. hack/e2e-chainsaw/_lib/remediation-guard.sh
|
|
statuses=$(printf 'deployed\nsuperseded\n')
|
|
if helmrelease_has_remediation_cycle "${statuses}"; then
|
|
echo "expected not-detected for deployed+superseded history" >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
@test "single failed snapshot returns detected" {
|
|
. hack/e2e-chainsaw/_lib/remediation-guard.sh
|
|
if ! helmrelease_has_remediation_cycle "failed"; then
|
|
echo "expected detected when history contains failed snapshot" >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
@test "single uninstalled snapshot returns detected" {
|
|
# The exact signature of the install-remediation race: the first install
|
|
# exceeded flux's wait budget, remediation uninstalled, the next retry
|
|
# eventually succeeded. History still carries the uninstalled snapshot.
|
|
. hack/e2e-chainsaw/_lib/remediation-guard.sh
|
|
if ! helmrelease_has_remediation_cycle "uninstalled"; then
|
|
echo "expected detected when history contains uninstalled snapshot" >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
@test "uninstalled then deployed still returns detected" {
|
|
. hack/e2e-chainsaw/_lib/remediation-guard.sh
|
|
statuses=$(printf 'uninstalled\ndeployed\n')
|
|
if ! helmrelease_has_remediation_cycle "${statuses}"; then
|
|
echo "expected detected despite later successful deploy" >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
@test "deployed then failed still returns detected" {
|
|
. hack/e2e-chainsaw/_lib/remediation-guard.sh
|
|
statuses=$(printf 'deployed\nfailed\n')
|
|
if ! helmrelease_has_remediation_cycle "${statuses}"; then
|
|
echo "expected detected when any entry is failed" >&2
|
|
exit 1
|
|
fi
|
|
}
|