| index e2128f9b3..4dbcac681 100644 |
| --- a/internal/host/namedunitsfetch_test.go |
| +++ b/internal/host/namedunitsfetch_test.go |
| @@ -471,42 +471,74 @@ func p(a rollbackAttempt) *rollbackAttempt { return &a } |
| // the invariant. The step runs on full and config-only passes (after the |
| // -restart-only return), its record is written before the release key |
| // parse can kill the pass, and it sits outside the CARLOS_SECRETS_KEY |
| -// branch, so a box without the key still installs. |
| +// branch, so a box without the key still installs. runNamedUnitsStep |
| +// holds the step and the write; runAdoptUnderRootRelease calls it. |
| func TestAdoptRunsNamedUnitsStepBeforeConfigSync(t *testing.T) { |
| - raw, err := os.ReadFile("agent.go") |
| + helper := funcSource(t, "namedunitsfetch.go", "runNamedUnitsStep") |
| + hat := sourceAt(t, "runNamedUnitsStep", helper) |
| + for _, s := range []string{"adoptNamedUnitsStep(", "writeNamedUnitsStatus(", "return "} { |
| + if n := strings.Count(helper, s); n != 1 { |
| + t.Fatalf("%s appears %d times in runNamedUnitsStep, want once", s, n) |
| + } |
| + } |
| + if step, write, ret := hat("adoptNamedUnitsStep("), hat("writeNamedUnitsStatus("), hat("return "); !(step < write && write < ret) { |
| + t.Fatalf("runNamedUnitsStep order: step %d, write %d, return %d", step, write, ret) |
| + } |
| + |
| + body := funcSource(t, "agent.go", "runAdoptUnderRootRelease") |
| + at := sourceAt(t, "runAdoptUnderRootRelease", body) |
| + restart := at("if *restartOnly {") |
| + restartEnd := restart + strings.Index(body[restart:], "\n\t}\n") |
| + step := at("runNamedUnitsStep(") |
| + pub := at("release.ParsePublicKey(") |
| + secrets := at(`os.Getenv("CARLOS_SECRETS_KEY")`) |
| + sync := at("appconfig.SyncUnits(") |
| + for _, s := range []string{"runNamedUnitsStep(", "adoptNamedUnitsStep(", "writeNamedUnitsStatus("} { |
| + want := 0 |
| + if s == "runNamedUnitsStep(" { |
| + want = 1 |
| + } |
| + if n := strings.Count(body, s); n != want { |
| + t.Fatalf("%s appears %d times in runAdoptUnderRootRelease, want %d", s, n, want) |
| + } |
| + } |
| + // Before the key is read, so outside any branch on it. |
| + if !(restartEnd < step && step < pub && pub < secrets && secrets < sync) { |
| + t.Fatalf("order: restart-only end %d, step %d, ParsePublicKey %d, secrets %d, SyncUnits %d", restartEnd, step, pub, secrets, sync) |
| + } |
| +} |
| + |
| +// funcSource is the source of func name in file, up to its closing brace |
| +// at column one. |
| +func funcSource(t *testing.T, file, name string) string { |
| + t.Helper() |
| + raw, err := os.ReadFile(file) |
| if err != nil { |
| t.Fatal(err) |
| } |
| src := string(raw) |
| - start := strings.Index(src, "\nfunc runAdoptUnderRootRelease(") |
| + start := strings.Index(src, "\nfunc "+name+"(") |
| if start < 0 { |
| - t.Fatal("runAdoptUnderRootRelease is no longer in agent.go, move this test with it") |
| + t.Fatalf("%s is no longer in %s, move this test with it", name, file) |
| } |
| body := src[start+1:] |
| if end := strings.Index(body, "\n}\n"); end >= 0 { |
| body = body[:end] |
| } |
| - at := func(s string) int { |
| + return body |
| +} |
| + |
| +// sourceAt returns a lookup of s's offset in body, failing when s is |
| +// missing. |
| +func sourceAt(t *testing.T, name, body string) func(s string) int { |
| + return func(s string) int { |
| t.Helper() |
| i := strings.Index(body, s) |
| if i < 0 { |
| - t.Fatalf("runAdoptUnderRootRelease no longer contains %q", s) |
| + t.Fatalf("%s no longer contains %q", name, s) |
| } |
| return i |
| } |
| - restart := at("if *restartOnly {") |
| - restartEnd := restart + strings.Index(body[restart:], "\n\t}\n") |
| - step := at("adoptNamedUnitsStep(") |
| - write := at("writeNamedUnitsStatus(") |
| - pub := at("release.ParsePublicKey(") |
| - secrets := at(`os.Getenv("CARLOS_SECRETS_KEY")`) |
| - sync := at("appconfig.SyncUnits(") |
| - if n := strings.Count(body, "adoptNamedUnitsStep("); n != 1 { |
| - t.Fatalf("adoptNamedUnitsStep( appears %d times, want once", n) |
| - } |
| - if !(restartEnd < step && step < write && write < pub && pub < secrets && secrets < sync) { |
| - t.Fatalf("order: restart-only end %d, step %d, write %d, ParsePublicKey %d, secrets %d, SyncUnits %d", restartEnd, step, write, pub, secrets, sync) |
| - } |
| } |
| |
| func TestProductionNamedUnitsFetchIsWired(t *testing.T) { |
| @@ -612,40 +644,24 @@ func TestAdoptWritesNamedUnitsStatusAfterSync(t *testing.T) { |
| } |
| |
| // The real pass calls exactly that, in that order, after the |
| - // -restart-only return and before the -config-only one. |
| - raw, err := os.ReadFile("agent.go") |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - src := string(raw) |
| - start := strings.Index(src, "\nfunc runAdoptUnderRootRelease(") |
| - if start < 0 { |
| - t.Fatal("runAdoptUnderRootRelease is no longer in agent.go, move this test with it") |
| - } |
| - body := src[start+1:] |
| - if end := strings.Index(body, "\n}\n"); end >= 0 { |
| - body = body[:end] |
| - } |
| - at := func(s string) int { |
| - t.Helper() |
| - i := strings.Index(body, s) |
| - if i < 0 { |
| - t.Fatalf("runAdoptUnderRootRelease no longer contains %q", s) |
| - } |
| - return i |
| - } |
| - for _, s := range []string{"adoptNamedUnitsStep(", "writeNamedUnitsStatus(", "adoptConfigSync(", "appconfig.SyncUnits("} { |
| + // -restart-only return and before the -config-only one: the step and |
| + // its write through runNamedUnitsStep (whose own order |
| + // TestAdoptRunsNamedUnitsStepBeforeConfigSync pins), then |
| + // adoptConfigSync with SyncUnits. |
| + body := funcSource(t, "agent.go", "runAdoptUnderRootRelease") |
| + at := sourceAt(t, "runAdoptUnderRootRelease", body) |
| + for _, s := range []string{"runNamedUnitsStep(", "adoptConfigSync(", "appconfig.SyncUnits("} { |
| if n := strings.Count(body, s); n != 1 { |
| t.Fatalf("%s appears %d times in runAdoptUnderRootRelease, want once", s, n) |
| } |
| } |
| restart := at("if *restartOnly {") |
| restartEnd := restart + strings.Index(body[restart:], "\n\t}\n") |
| - step0, write0 := at("adoptNamedUnitsStep("), at("writeNamedUnitsStatus(") |
| + step0 := at("runNamedUnitsStep(") |
| pub, cfg := at("release.ParsePublicKey("), at("adoptConfigSync(") |
| sync, configOnly := at("appconfig.SyncUnits("), at("if *configOnly {\n\t\t// Everything below") |
| - if !(restartEnd < step0 && step0 < write0 && write0 < pub && pub < cfg && cfg < sync && sync < configOnly) { |
| - t.Fatalf("order: restart-only end %d, step %d, write %d, ParsePublicKey %d, adoptConfigSync %d, SyncUnits %d, config-only return %d", |
| - restartEnd, step0, write0, pub, cfg, sync, configOnly) |
| + if !(restartEnd < step0 && step0 < pub && pub < cfg && cfg < sync && sync < configOnly) { |
| + t.Fatalf("order: restart-only end %d, step %d, ParsePublicKey %d, adoptConfigSync %d, SyncUnits %d, config-only return %d", |
| + restartEnd, step0, pub, cfg, sync, configOnly) |
| } |
| } |