Skip to content

Commit 8bac54d

Browse files
committed
test(e2e): pin the release-history read against the upstream type
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 script 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. 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: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent 677d80a commit 8bac54d

3 files changed

Lines changed: 137 additions & 47 deletions

File tree

‎hack/e2e-chainsaw/_lib/run-kubernetes.sh‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2280,8 +2280,11 @@ EOF
22802280
# leaves a "failed" or "uninstalled" entry behind that survives a later
22812281
# successful reinstall, unlike the installFailures/upgradeFailures
22822282
# counters (which ClearFailures zeroes on every successful reconcile).
2283-
# The shape is pinned by hack/remediation-guard.bats; the upstream
2284-
# types are github.com/fluxcd/helm-controller/api v2 Snapshot.
2283+
# The expression below is pinned by internal/fluxcontract, which reads it
2284+
# out of this file and runs it over a HelmRelease built from the upstream
2285+
# types (github.com/fluxcd/helm-controller/api v2, at the version go.mod
2286+
# holds). Renaming a field upstream fails that test rather than turning this
2287+
# read into a silent empty string.
22852288
history_statuses=$(kubectl get hr -n tenant-test "kubernetes-${test_name}" \
22862289
-ojsonpath='{range .status.history[*]}{.status}{"\n"}{end}')
22872290
# Always emit the raw value so a silent future-Flux field rename shows

‎hack/remediation-guard.bats‎

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -80,48 +80,3 @@
8080
exit 1
8181
fi
8282
}
83-
84-
@test "status.history extraction pins HR v2 status.history shape" {
85-
# Pins the Flux HelmRelease v2 .status.history[].status shape that
86-
# run-kubernetes.sh relies on. If a future flux release renames the
87-
# field, the jsonpath returns nothing, the guard reports no cycle,
88-
# and real remediation loops slip past the e2e assertion. This test
89-
# uses yq to read the exact path used in the e2e script; the upstream
90-
# Snapshot type lives at
91-
# github.com/fluxcd/helm-controller/api/v2.Snapshot (via go.mod).
92-
tmp=$(mktemp -d)
93-
94-
cat > "$tmp/hr.yaml" <<'YAML'
95-
apiVersion: helm.toolkit.fluxcd.io/v2
96-
kind: HelmRelease
97-
metadata:
98-
name: kubernetes-test
99-
namespace: tenant-test
100-
status:
101-
history:
102-
- name: kubernetes-test
103-
namespace: tenant-test
104-
version: 1
105-
status: uninstalled
106-
- name: kubernetes-test
107-
namespace: tenant-test
108-
version: 2
109-
status: deployed
110-
YAML
111-
112-
# Default yq output is yaml scalar format, which for string values emits
113-
# bare unquoted tokens - matching what kubectl -o jsonpath produces in
114-
# e2e. Do not switch to JSON output here; that would quote the values
115-
# and break the loop in helmrelease_has_remediation_cycle.
116-
statuses=$(yq '.status.history[].status' "$tmp/hr.yaml")
117-
118-
[ -n "$statuses" ]
119-
echo "$statuses" | grep --quiet '^uninstalled$'
120-
121-
. hack/e2e-chainsaw/_lib/remediation-guard.sh
122-
if ! helmrelease_has_remediation_cycle "$statuses"; then
123-
echo "expected detected for pinned HR snippet with uninstalled + deployed history" >&2
124-
exit 1
125-
fi
126-
rm -rf "$tmp"
127-
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Package fluxcontract holds contract tests for upstream Flux types this
2+
// repository reads through kubectl rather than through the Go API.
3+
//
4+
// A shell script that pulls a field out of a Kubernetes object with
5+
// `kubectl -o jsonpath` depends on the serialized shape of an upstream Go
6+
// struct, and nothing in the shell layer can observe that dependency. Rename
7+
// the field upstream and the expression stops matching: kubectl prints
8+
// nothing, exits zero, and the caller reads the empty output as an answer
9+
// about the object rather than as a question that no longer parses.
10+
//
11+
// The test for that cannot live in the shell layer either. Pointing an
12+
// expression at a document written by the same test proves the reader can read
13+
// that document and nothing else, because a rename moves neither side.
14+
//
15+
// So this file supplies neither half. The expression is read out of the script
16+
// that ships it, and the object is built from the upstream type at the version
17+
// go.mod pins. What is left to assert is that the one still finds the other.
18+
package fluxcontract
19+
20+
import (
21+
"encoding/json"
22+
"os"
23+
"path/filepath"
24+
"regexp"
25+
"strings"
26+
"testing"
27+
28+
helmv2 "github.com/fluxcd/helm-controller/api/v2"
29+
"k8s.io/client-go/util/jsonpath"
30+
)
31+
32+
// The script the e2e remediation guard runs, read at test time so the
33+
// expression under test is the one that ships rather than a copy of it.
34+
const guardScript = "../../hack/e2e-chainsaw/_lib/run-kubernetes.sh"
35+
36+
// The `-o jsonpath` argument the guard passes to kubectl, in the spellings
37+
// kubectl accepts and both quote placements this script already mixes:
38+
// `-ojsonpath='{range …}'` here, `-o 'jsonpath={range …}'` a few hundred lines
39+
// up. Anchored on the range over .status.history so an unrelated jsonpath
40+
// elsewhere in the script cannot be picked up instead.
41+
//
42+
// Worth covering both, because a miss is reported as "the guard stopped
43+
// reading history" and would send the next reader to the wrong file over what
44+
// was only a change of quoting.
45+
//
46+
// Single quotes only, deliberately. The expression contains `{"\n"}`, so a
47+
// double-quoted shell argument would have to escape its way around its own
48+
// delimiter; nobody writes it that way, and matching it would mean giving up
49+
// the delimiter as a terminator.
50+
var historyExpr = regexp.MustCompile(
51+
`--?o(?:utput)?[= ]?'?jsonpath='?(\{range \.status\.history\[\*\]\}[^']*)'`)
52+
53+
// readGuardExpression pulls the release-history jsonpath out of the guard. A
54+
// miss fails rather than skips: the guard losing this read is the change this
55+
// test exists to notice.
56+
func readGuardExpression(t *testing.T) string {
57+
t.Helper()
58+
src, err := os.ReadFile(filepath.Clean(guardScript))
59+
if err != nil {
60+
t.Fatalf("reading %s: %v", guardScript, err)
61+
}
62+
m := historyExpr.FindSubmatch(src)
63+
if m == nil {
64+
t.Fatalf("no `-o jsonpath` over .status.history found in %s. "+
65+
"If the guard reads release history another way now, this test has to follow it there. "+
66+
"If it stopped reading history at all, the e2e remediation guard asserts nothing.",
67+
guardScript)
68+
}
69+
return string(m[1])
70+
}
71+
72+
// A HelmRelease whose history carries the statuses the guard reads. Built from
73+
// the upstream types, so a renamed or retyped field is a compile error here
74+
// before it is a silent empty read against a live cluster.
75+
func helmReleaseWithHistory() *helmv2.HelmRelease {
76+
return &helmv2.HelmRelease{
77+
Status: helmv2.HelmReleaseStatus{
78+
History: helmv2.Snapshots{
79+
{Version: 2, Status: "deployed"},
80+
{Version: 1, Status: "uninstalled"},
81+
},
82+
},
83+
}
84+
}
85+
86+
// The guard's expression, run over the upstream type through JSON, the way
87+
// kubectl runs it. Serialization is the whole point: the struct's json tags
88+
// are what the expression actually sees.
89+
//
90+
// Only the match is asserted. How kubectl's printer behaves on a path that is
91+
// absent is kubectl's own configuration, not part of the Flux contract, and
92+
// guessing at it here would put the expectation and the fixture back in the
93+
// same hands.
94+
func TestGuardExpressionReadsUpstreamHistoryStatuses(t *testing.T) {
95+
expr := readGuardExpression(t)
96+
97+
raw, err := json.Marshal(helmReleaseWithHistory())
98+
if err != nil {
99+
t.Fatalf("marshalling HelmRelease: %v", err)
100+
}
101+
var generic any
102+
if err := json.Unmarshal(raw, &generic); err != nil {
103+
t.Fatalf("unmarshalling HelmRelease: %v", err)
104+
}
105+
106+
jp := jsonpath.New("guard")
107+
if err := jp.Parse(expr); err != nil {
108+
t.Fatalf("parsing the guard's expression %q: %v", expr, err)
109+
}
110+
var out strings.Builder
111+
if err := jp.Execute(&out, generic); err != nil {
112+
t.Fatalf("running the guard's expression %q over an upstream HelmRelease: %v. "+
113+
"The serialized shape no longer satisfies the expression.", expr, err)
114+
}
115+
116+
var got []string
117+
for _, line := range strings.Split(out.String(), "\n") {
118+
if line != "" {
119+
got = append(got, line)
120+
}
121+
}
122+
want := []string{"deployed", "uninstalled"}
123+
if len(got) != len(want) {
124+
t.Fatalf("expression %q returned %q, want one line per Snapshot status %q",
125+
expr, out.String(), want)
126+
}
127+
for i := range want {
128+
if got[i] != want[i] {
129+
t.Errorf("status %d: got %q, want %q", i, got[i], want[i])
130+
}
131+
}
132+
}

0 commit comments

Comments
 (0)