Skip to content

Commit a057e78

Browse files
committed
test(e2e): read the opensearch release history through the pinned name
The opensearch suite wrote the history jsonpath out as a literal, so the contract test that runs HELMRELEASE_HISTORY_JSONPATH against the upstream Flux type never saw the expression this suite hands kubectl. A field rename upstream would have reached it only as an empty read. The suite now takes the shared expression, and the contract test reads every Chainsaw suite that calls the guard, so a suite copying the literal, or assigning the shared name after sourcing the library, fails in the unit lane instead of going unchecked. The read is matched inside the script step that calls the guard, because every step runs in a shell of its own and a read in another step never reaches the call. Assisted-by: LLM Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent 309875a commit a057e78

2 files changed

Lines changed: 145 additions & 8 deletions

File tree

‎hack/e2e-chainsaw/opensearch/chainsaw-test.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ spec:
551551
cd ../../..
552552
. hack/e2e-chainsaw/_lib/remediation-guard.sh
553553
statuses=$(kubectl -n "$NAMESPACE" get helmrelease opensearch-test \
554-
-o jsonpath='{range .status.history[*]}{.status}{"\n"}{end}')
554+
-o "jsonpath=${HELMRELEASE_HISTORY_JSONPATH}")
555555
# Always emit the raw value so a silent future-Flux field rename shows
556556
# up as "empty history on a Ready HR" in CI logs rather than vanishing.
557557
# The helper reads empty input as "no cycle observed", so without this

‎internal/fluxcontract/snapshot_shape_test.go‎

Lines changed: 144 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
package fluxcontract
2525

2626
import (
27+
"bufio"
28+
"bytes"
2729
"encoding/json"
2830
"errors"
31+
"io"
2932
"os"
3033
"os/exec"
3134
"path/filepath"
@@ -34,14 +37,20 @@ import (
3437
"testing"
3538

3639
helmv2 "github.com/fluxcd/helm-controller/api/v2"
40+
utilyaml "k8s.io/apimachinery/pkg/util/yaml"
3741
"k8s.io/client-go/util/jsonpath"
42+
"sigs.k8s.io/yaml"
3843
)
3944

4045
const (
4146
// The shell library that owns the expression, and the script that runs it.
4247
guardLibrary = "../../hack/e2e-chainsaw/_lib/remediation-guard.sh"
4348
guardCaller = "../../hack/e2e-chainsaw/_lib/run-kubernetes.sh"
4449

50+
// The Chainsaw suites, one directory each, which may call the guard from a
51+
// script step.
52+
chainsawSuites = "../../hack/e2e-chainsaw"
53+
4554
sharedName = "HELMRELEASE_HISTORY_JSONPATH"
4655
)
4756

@@ -73,11 +82,12 @@ var nameFollowedByEquals = regexp.MustCompile(regexp.QuoteMeta(sharedName) + `=`
7382
// free, because this script mixes forms that behave identically. Each is a
7483
// case in the accept table below.
7584
//
76-
// The reach is one named caller and calls that pass a local. A second script
77-
// calling the guard, or this one passing a substitution written out in the
78-
// argument, is outside what this sees. Neither exists today, and the rule for
79-
// authors is prose rather than a pattern: docs/agents/e2e-testing.md tells them
80-
// to take the expression from the shared name.
85+
// The reach is one named caller, the Chainsaw suites, and calls that pass a
86+
// local. Any other script calling the guard, or a caller passing a substitution
87+
// written out in the argument, is outside what this sees. Neither exists today,
88+
// and the rule for authors is prose rather than a pattern:
89+
// docs/agents/e2e-testing.md tells them to take the expression from the shared
90+
// name.
8191
var guardConsumers = regexp.MustCompile(
8292
`(?m)^[ \t]*(?:if[ \t]+)?(?:![ \t]*)?helmrelease_has_remediation_cycle[ \t]+"?\$\{?([A-Za-z_][A-Za-z0-9_]*)`)
8393

@@ -478,6 +488,11 @@ func TestGuardCallerReferencesTheSharedExpansion(t *testing.T) {
478488
"calls it in a shape %s cannot see.", guardCaller, guardConsumers)
479489
}
480490

491+
assertConsumedLocalsTakeTheSharedRead(t, guardCaller, src, consumers)
492+
}
493+
494+
func assertConsumedLocalsTakeTheSharedRead(t *testing.T, path string, src []byte, consumers [][][]byte) {
495+
t.Helper()
481496
for _, m := range consumers {
482497
local := string(m[1])
483498
n := len(localFollowedByEquals(local).FindAll(src, -1))
@@ -486,16 +501,138 @@ func TestGuardCallerReferencesTheSharedExpansion(t *testing.T) {
486501
"exactly one. The count is of occurrences, so a comment mentioning one counts too; "+
487502
"what it looks for is a second assignment, which the shell would take instead of "+
488503
"the one this pin matched, putting an unpinned expression in front of the guard.",
489-
guardCaller, local, n)
504+
path, local, n)
490505
continue
491506
}
492507
if !sharedReadBy(local).Match(src) {
493508
t.Errorf("%s fills %s, the local it hands the guard, in a shape that does not take its "+
494509
"expression from %s. The pattern is the exact form accepted: %s. Either the read "+
495510
"stopped using the shared assignment, or it uses it in a shape the pattern cannot "+
496-
"see.", guardCaller, local, sharedName, sharedReadBy(local))
511+
"see.", path, local, sharedName, sharedReadBy(local))
512+
}
513+
}
514+
}
515+
516+
// A Chainsaw suite that sources the guard library from a script step is a
517+
// caller too, and one this file would otherwise never read: the pin above names
518+
// one script, so a suite writing the jsonpath out as a literal runs an
519+
// expression nothing here checked.
520+
//
521+
// Every suite is read rather than a named one, because the next suite to adopt
522+
// the guard is the likeliest place for a copied literal and naming suites here
523+
// would need the same edit that introduces it. A suite cannot assign the shared
524+
// name either: its script sources the library first, so its own assignment
525+
// lands afterwards and replaces the value the upstream test resolved.
526+
//
527+
// Both sets are asserted non-empty. A glob that stops matching, or suites that
528+
// all stop calling the guard, leave the loop with nothing to check, and that
529+
// reads exactly like a pass.
530+
func TestChainsawSuitesReadHistoryThroughTheSharedName(t *testing.T) {
531+
var suites []string
532+
for _, pattern := range []string{"chainsaw-test.yaml", "chainsaw-test.yml"} {
533+
found, err := filepath.Glob(filepath.Join(chainsawSuites, "*", pattern))
534+
if err != nil {
535+
t.Fatalf("globbing the Chainsaw suites: %v", err)
536+
}
537+
suites = append(suites, found...)
538+
}
539+
if len(suites) == 0 {
540+
t.Fatalf("no Chainsaw suites found under %s, so this test asserted nothing", chainsawSuites)
541+
}
542+
543+
// Each script step runs in a shell of its own, so the read the guard sees is
544+
// the one in its own step. Checked against the whole file, a read in one
545+
// step would satisfy a call in another, and an unrelated assignment of the
546+
// same local elsewhere would fail a correct one.
547+
t.Run("a read in another step does not count", func(t *testing.T) {
548+
steps := scriptSteps(t, "fixture", []byte(`spec:
549+
steps:
550+
- try:
551+
- script:
552+
content: |
553+
statuses=$(kubectl get hr -o "jsonpath=${HELMRELEASE_HISTORY_JSONPATH}")
554+
--- # a separator may carry a comment
555+
spec:
556+
steps:
557+
- try:
558+
- script:
559+
content: |
560+
helmrelease_has_remediation_cycle "$statuses"
561+
`))
562+
if len(steps) != 2 {
563+
t.Fatalf("split the fixture into %d script steps, want 2", len(steps))
564+
}
565+
if n := len(localFollowedByEquals("statuses").FindAll([]byte(steps[1]), -1)); n != 0 {
566+
t.Errorf("the calling step holds %d assignments of the local, want 0", n)
567+
}
568+
})
569+
570+
callers := 0
571+
for _, path := range suites {
572+
src := readGuardFile(t, path)
573+
if n := len(nameFollowedByEquals.FindAll(src, -1)); n != 0 {
574+
t.Errorf("%s writes %s followed by = %d times; only %s may assign it. A suite sources "+
575+
"the library before its own lines run, so its assignment replaces the value the "+
576+
"upstream test checked.", path, sharedName, n, guardLibrary)
577+
}
578+
for _, step := range scriptSteps(t, path, src) {
579+
consumers := guardConsumers.FindAllSubmatch([]byte(step), -1)
580+
if len(consumers) == 0 {
581+
continue
582+
}
583+
callers++
584+
assertConsumedLocalsTakeTheSharedRead(t, path, []byte(step), consumers)
585+
}
586+
}
587+
if callers == 0 {
588+
t.Fatalf("no Chainsaw suite under %s calls helmrelease_has_remediation_cycle with an "+
589+
"expanded local, so this test asserted nothing. If no suite runs the guard any more, "+
590+
"this test goes with it.", chainsawSuites)
591+
}
592+
}
593+
594+
// The content of every script step in a Chainsaw file, wherever it sits: try,
595+
// catch, finally, or a nested operation.
596+
func scriptSteps(t *testing.T, path string, src []byte) []string {
597+
t.Helper()
598+
var steps []string
599+
var walk func(any)
600+
walk = func(node any) {
601+
switch v := node.(type) {
602+
case map[string]any:
603+
if script, ok := v["script"].(map[string]any); ok {
604+
if content, ok := script["content"].(string); ok {
605+
steps = append(steps, content)
606+
}
607+
}
608+
for _, child := range v {
609+
walk(child)
610+
}
611+
case []any:
612+
for _, child := range v {
613+
walk(child)
614+
}
615+
}
616+
}
617+
// The reader splits on a separator line that carries a comment too, which a
618+
// pattern for a bare `---` would leave inside one document, and Unmarshal
619+
// reads only the first document it is given.
620+
docs := utilyaml.NewYAMLReader(bufio.NewReader(bytes.NewReader(src)))
621+
for {
622+
doc, err := docs.Read()
623+
if errors.Is(err, io.EOF) {
624+
break
625+
}
626+
if err != nil {
627+
t.Fatalf("splitting %s into documents: %v", path, err)
628+
}
629+
var tree any
630+
if err := yaml.Unmarshal(doc, &tree); err != nil {
631+
t.Fatalf("parsing %s: %v", path, err)
497632
}
633+
walk(tree)
498634
}
635+
return steps
499636
}
500637

501638
// No shell library but the one that owns the name assigns it. Sourcing the

0 commit comments

Comments
 (0)