@@ -42,6 +42,10 @@ const (
4242 guardLibrary = "../../hack/e2e-chainsaw/_lib/remediation-guard.sh"
4343 guardCaller = "../../hack/e2e-chainsaw/_lib/run-kubernetes.sh"
4444
45+ // The Chainsaw suites, one directory each, which may call the guard from a
46+ // script step.
47+ chainsawSuites = "../../hack/e2e-chainsaw"
48+
4549 sharedName = "HELMRELEASE_HISTORY_JSONPATH"
4650)
4751
@@ -73,11 +77,12 @@ var nameFollowedByEquals = regexp.MustCompile(regexp.QuoteMeta(sharedName) + `=`
7377// free, because this script mixes forms that behave identically. Each is a
7478// case in the accept table below.
7579//
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.
80+ // The reach is one named caller, the Chainsaw suites, and calls that pass a
81+ // local. Any other script calling the guard, or a caller passing a substitution
82+ // written out in the argument, is outside what this sees. Neither exists today,
83+ // and the rule for authors is prose rather than a pattern:
84+ // docs/agents/e2e-testing.md tells them to take the expression from the shared
85+ // name.
8186var guardConsumers = regexp .MustCompile (
8287 `(?m)^[ \t]*(?:if[ \t]+)?(?:![ \t]*)?helmrelease_has_remediation_cycle[ \t]+"?\$\{?([A-Za-z_][A-Za-z0-9_]*)` )
8388
@@ -478,6 +483,11 @@ func TestGuardCallerReferencesTheSharedExpansion(t *testing.T) {
478483 "calls it in a shape %s cannot see." , guardCaller , guardConsumers )
479484 }
480485
486+ assertConsumedLocalsTakeTheSharedRead (t , guardCaller , src , consumers )
487+ }
488+
489+ func assertConsumedLocalsTakeTheSharedRead (t * testing.T , path string , src []byte , consumers [][][]byte ) {
490+ t .Helper ()
481491 for _ , m := range consumers {
482492 local := string (m [1 ])
483493 n := len (localFollowedByEquals (local ).FindAll (src , - 1 ))
@@ -486,18 +496,67 @@ func TestGuardCallerReferencesTheSharedExpansion(t *testing.T) {
486496 "exactly one. The count is of occurrences, so a comment mentioning one counts too; " +
487497 "what it looks for is a second assignment, which the shell would take instead of " +
488498 "the one this pin matched, putting an unpinned expression in front of the guard." ,
489- guardCaller , local , n )
499+ path , local , n )
490500 continue
491501 }
492502 if ! sharedReadBy (local ).Match (src ) {
493503 t .Errorf ("%s fills %s, the local it hands the guard, in a shape that does not take its " +
494504 "expression from %s. The pattern is the exact form accepted: %s. Either the read " +
495505 "stopped using the shared assignment, or it uses it in a shape the pattern cannot " +
496- "see." , guardCaller , local , sharedName , sharedReadBy (local ))
506+ "see." , path , local , sharedName , sharedReadBy (local ))
497507 }
498508 }
499509}
500510
511+ // A Chainsaw suite that sources the guard library from a script step is a
512+ // caller too, and one this file would otherwise never read: the pin above names
513+ // one script, so a suite writing the jsonpath out as a literal runs an
514+ // expression nothing here checked.
515+ //
516+ // Every suite is read rather than a named one, because the next suite to adopt
517+ // the guard is the likeliest place for a copied literal and naming suites here
518+ // would need the same edit that introduces it. A suite cannot assign the shared
519+ // name either: its script sources the library first, so its own assignment
520+ // lands afterwards and replaces the value the upstream test resolved.
521+ //
522+ // Both sets are asserted non-empty. A glob that stops matching, or suites that
523+ // all stop calling the guard, leave the loop with nothing to check, and that
524+ // reads exactly like a pass.
525+ func TestChainsawSuitesReadHistoryThroughTheSharedName (t * testing.T ) {
526+ var suites []string
527+ for _ , pattern := range []string {"chainsaw-test.yaml" , "chainsaw-test.yml" } {
528+ found , err := filepath .Glob (filepath .Join (chainsawSuites , "*" , pattern ))
529+ if err != nil {
530+ t .Fatalf ("globbing the Chainsaw suites: %v" , err )
531+ }
532+ suites = append (suites , found ... )
533+ }
534+ if len (suites ) == 0 {
535+ t .Fatalf ("no Chainsaw suites found under %s, so this test asserted nothing" , chainsawSuites )
536+ }
537+
538+ callers := 0
539+ for _ , path := range suites {
540+ src := readGuardFile (t , path )
541+ if n := len (nameFollowedByEquals .FindAll (src , - 1 )); n != 0 {
542+ t .Errorf ("%s writes %s followed by = %d times; only %s may assign it. A suite sources " +
543+ "the library before its own lines run, so its assignment replaces the value the " +
544+ "upstream test checked." , path , sharedName , n , guardLibrary )
545+ }
546+ consumers := guardConsumers .FindAllSubmatch (src , - 1 )
547+ if len (consumers ) == 0 {
548+ continue
549+ }
550+ callers ++
551+ assertConsumedLocalsTakeTheSharedRead (t , path , src , consumers )
552+ }
553+ if callers == 0 {
554+ t .Fatalf ("no Chainsaw suite under %s calls helmrelease_has_remediation_cycle with an " +
555+ "expanded local, so this test asserted nothing. If no suite runs the guard any more, " +
556+ "this test goes with it." , chainsawSuites )
557+ }
558+ }
559+
501560// No shell library but the one that owns the name assigns it. Sourcing the
502561// owning library resolves what that library ends up with, and cannot see a
503562// later one overwriting the name: the caller sources the owning library first,
0 commit comments