appconfig: a failed reload still refuses drifted units
On a daemon-reload failure applyStamped refused the pending units and returned, so an unchanged unit whose planning probe showed drift kept its provisional delivered outcome. The drift check is now refuseDrifted, run on that exit too, silently: the exit's printed output is unchanged. A two-unit test pins one Outcome per host and the drifted one refused.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+65
−7
internal/appconfig/unitsync.go+22 −7internal/appconfig/unitsync_test.go+43 −0
diff --git a/internal/appconfig/unitsync.go b/internal/appconfig/unitsync.go| index f27c30b5c..852bd2b04 100644 |
| --- a/internal/appconfig/unitsync.go |
| +++ b/internal/appconfig/unitsync.go |
| @@ -514,6 +514,10 @@ func applyStamped(envDir string, ops *UnitOps, plan stampedPlan, work map[string |
| fmt.Printf(" %s\n", line) |
| } |
| pending := listPending(ops.DropInRoot) |
| + marked := map[string]bool{} |
| + for _, unit := range pending { |
| + marked[unit] = true |
| + } |
| if len(pending) > 0 { |
| if err := ops.Reload(); err != nil { |
| for _, unit := range pending { |
| @@ -522,12 +526,14 @@ func applyStamped(envDir string, ops *UnitOps, plan stampedPlan, work map[string |
| plan.outcomes.refuse(w.host, msg) |
| } |
| } |
| + // The unmarked units still end on their planning probes, so a |
| + // drifted one is never reported delivered; this exit has never |
| + // printed drift, and still does not. |
| + refuseDrifted(envDir, ops, plan, work, marked, false) |
| return |
| } |
| } |
| - marked := map[string]bool{} |
| for _, unit := range pending { |
| - marked[unit] = true |
| w, prepared := work[unit] |
| _, exists, isMarked, err := readDropIn(ops, unit) |
| if err != nil { |
| @@ -592,9 +598,14 @@ func applyStamped(envDir string, ops *UnitOps, plan stampedPlan, work map[string |
| fmt.Printf(" %s: config updated, %s restarted\n", w.host, unit) |
| plan.outcomes.deliver(w.host) |
| } |
| - // Drift (step 7): an unmarked, delivered unit whose pre-mutation probe |
| - // shows anything but carlos's file is running what the operator |
| - // configured. Say so every pass; never fight it. |
| + refuseDrifted(envDir, ops, plan, work, marked, true) |
| +} |
| + |
| +// refuseDrifted is drift (step 7): an unmarked, delivered unit whose |
| +// pre-mutation probe shows anything but carlos's file is running what the |
| +// operator configured. Its host is refused, and with report the pass says |
| +// so, every pass; it never fights it. |
| +func refuseDrifted(envDir string, ops *UnitOps, plan stampedPlan, work map[string]unitWork, marked map[string]bool, report bool) { |
| units := make([]string, 0, len(work)) |
| for unit := range work { |
| units = append(units, unit) |
| @@ -607,8 +618,12 @@ func applyStamped(envDir string, ops *UnitOps, plan stampedPlan, work map[string |
| } |
| base, want := baseEnvPath(ops.BaseEnvDir, unit), filepath.Join(envDir, w.host+".env") |
| if st, ok := plan.probe[unit]; ok && !envFilesMatch(st.EnvFiles, base, want) { |
| - plan.outcomes.refuse(w.host, say(w.host, "delivery blocked: %s reads %v, want exactly [%s %s]; a later drop-in owns the EnvironmentFile list", |
| - unit, st.EnvFiles, base, want)) |
| + const format = "delivery blocked: %s reads %v, want exactly [%s %s]; a later drop-in owns the EnvironmentFile list" |
| + msg := fmt.Sprintf(format, unit, st.EnvFiles, base, want) |
| + if report { |
| + msg = say(w.host, format, unit, st.EnvFiles, base, want) |
| + } |
| + plan.outcomes.refuse(w.host, msg) |
| } |
| } |
| } |
diff --git a/internal/appconfig/unitsync_test.go b/internal/appconfig/unitsync_test.go| index 78b7ac4a7..b0e90d9d1 100644 |
| --- a/internal/appconfig/unitsync_test.go |
| +++ b/internal/appconfig/unitsync_test.go |
| @@ -1738,6 +1738,49 @@ func TestSyncUnitsOutcomeLaterRefusalWins(t *testing.T) { |
| printedReason(t, out, c) |
| } |
| +// TestSyncUnitsOutcomeReloadFailureStillRefusesDrift: a failed reload ends |
| +// the apply phase early, and an unchanged unit whose planning probe shows |
| +// drift is still refused, not left delivered (branch review R1 P2). The |
| +// printed output is as before: that exit never printed drift. |
| +func TestSyncUnitsOutcomeReloadFailureStillRefusesDrift(t *testing.T) { |
| + ctx, s, reg, envDir, priv := unitKit(t) |
| + f := newFakeUnits(t) |
| + f.loaded("relay.service", envDir, "relay.example.com") |
| + f.loaded("b.service", envDir, "b.example.com") |
| + stampDeclared(t, f, reg, "relay.example.com", "relay.service", nil) |
| + stampDeclared(t, f, reg, "b.example.com", "b.service", nil) |
| + syncOnce(t, ctx, reg, s, priv, envDir, f) // both delivered, markers cleared |
| + f.restarted = nil |
| + |
| + // A needs a reload (its env file is gone, so it is rewritten), and the |
| + // reload fails. B's files are unchanged, but a later drop-in has taken |
| + // its EnvironmentFile list. |
| + if err := os.Remove(filepath.Join(envDir, "relay.example.com.env")); err != nil { |
| + t.Fatal(err) |
| + } |
| + f.reloadErr = errors.New("busy") |
| + f.states["b.service"] = UnitState{ID: "b.service", LoadState: "loaded", EnvFiles: []string{"/etc/other.env"}} |
| + |
| + out, calls := syncWithOutcomes(t, ctx, reg, s, priv, envDir, f) |
| + if len(calls) != 2 { |
| + t.Fatalf("Outcome calls = %+v, want exactly two", calls) |
| + } |
| + a := outcomeFor(t, calls, "relay.example.com") |
| + if a.state != "refused" || !strings.Contains(a.reason, "daemon-reload failed") { |
| + t.Fatalf("A: %+v, want refused for the failed reload", a) |
| + } |
| + b := outcomeFor(t, calls, "b.example.com") |
| + if b.unit != "b.service" || b.state != "refused" || !strings.Contains(b.reason, "/etc/other.env") { |
| + t.Fatalf("B: %+v, want refused on b.service naming the list it reads", b) |
| + } |
| + if strings.Contains(out, "/etc/other.env") { |
| + t.Fatalf("the reload-failure exit printed drift:\n%s", out) |
| + } |
| + if len(f.restarted) != 0 || !hasPending(f.root, "relay.service") { |
| + t.Fatalf("restarted=%v pending=%v, want no restart and A's marker kept", f.restarted, hasPending(f.root, "relay.service")) |
| + } |
| +} |
| + |
| func TestSyncUnitsOutcomeNoRouteAndUnresolved(t *testing.T) { |
| ctx, s, reg, envDir, priv := unitKit(t) |
| f := newFakeUnits(t) |