diff --git a/internal/infrastructure/readiness/probes.go b/internal/infrastructure/readiness/probes.go index c0a6527ae..2192f3d88 100644 --- a/internal/infrastructure/readiness/probes.go +++ b/internal/infrastructure/readiness/probes.go @@ -165,6 +165,35 @@ func NewDiskProbeTargets(targets []DiskTarget) *DiskProbe { func (p *DiskProbe) Name() string { return "disk" } +// reduceTargets collapses the configured targets to one per path: the first +// target seen for a path wins its position, and the result keeps the strictest +// requirement any target placed on that path. +// +// Position is what makes this necessary rather than using a plain set. With +// root, an optional /var, then a required data path that resolves to /var, the +// optional /var is the target that survives dedup -- and its optional rules +// then apply to a path the daemon requires, so a vanished required path is +// skipped as "optional absent" and the probe reports OK. Choosing the required +// target (and its name) keeps the failure visible and correctly attributed. +func reduceTargets(targets []DiskTarget) []DiskTarget { + index := make(map[string]int, len(targets)) + out := make([]DiskTarget, 0, len(targets)) + for _, target := range targets { + if target.Path == "" { + continue + } + if at, ok := index[target.Path]; ok { + if !target.Optional && out[at].Optional { + out[at] = target + } + continue + } + index[target.Path] = len(out) + out = append(out, target) + } + return out +} + func (p *DiskProbe) Check(ctx context.Context) health.Result { _ = ctx targets := p.Targets @@ -180,12 +209,7 @@ func (p *DiskProbe) Check(ctx context.Context) health.Result { } var details []string degraded := false - seen := make(map[string]bool) - for _, target := range targets { - if target.Path == "" || seen[target.Path] { - continue - } - seen[target.Path] = true + for _, target := range reduceTargets(targets) { var stat syscall.Statfs_t if err := statfs(target.Path, &stat); err != nil { if target.Optional && errors.Is(err, os.ErrNotExist) { diff --git a/internal/infrastructure/readiness/probes_test.go b/internal/infrastructure/readiness/probes_test.go index 2e348b9ef..be2560642 100644 --- a/internal/infrastructure/readiness/probes_test.go +++ b/internal/infrastructure/readiness/probes_test.go @@ -3,6 +3,9 @@ package readiness import ( "context" "errors" + "os" + "strings" + "syscall" "testing" "github.com/samcharles93/archie-core/internal/config" @@ -196,3 +199,71 @@ func TestGatewayProbe_DegradedWhenNoChannelRunning(t *testing.T) { t.Fatalf("status = %q, want degraded", got.Status) } } + +// A required path must not inherit the rules of an earlier optional target on +// the same path. Dedup keeps one target per path, and when the optional target +// is the one kept, a required path that has gone missing is swallowed as +// "optional absent" -- reporting OK while a path the daemon needs is +// unavailable. Retaining required status for a path that any target requires +// is the fix. +func TestDiskProbe_DedupKeepsRequiredWhenOptionalSharesThePath(t *testing.T) { + fs := fakeStatfs(map[string]syscall.Statfs_t{ + "/": {Blocks: 100, Bavail: 100}, // a healthy target keeps the detail list non-empty + }) + // Production orders root first, then the optional /var, then the + // config-supplied data path -- which can resolve to /var and collide. + p := NewDiskProbeTargets([]DiskTarget{ + {Name: "root", Path: "/"}, + {Name: "var", Path: "/var", Optional: true}, + {Name: "data", Path: "/var"}, + }) + p.Statfs = fs.statfs + + got := p.Check(context.Background()) + if got.Status != health.StatusDegraded { + t.Fatalf("status = %q, want degraded: a required path that has vanished must not be reported OK via an optional target's rules (detail: %s)", + got.Status, got.Detail) + } + if !strings.Contains(got.Detail, "data") { + t.Fatalf("detail = %q, want the required target named", got.Detail) + } +} + +// The inverse: when the kept target is genuinely optional and only optional +// targets share the path, an absent path still degrades nothing. +func TestDiskProbe_DedupKeepsOptionalWhenNoTargetRequiresThePath(t *testing.T) { + fs := fakeStatfs(map[string]syscall.Statfs_t{ + "/": {Blocks: 100, Bavail: 100}, + }) + p := NewDiskProbeTargets([]DiskTarget{ + {Name: "root", Path: "/"}, + {Name: "var", Path: "/var", Optional: true}, + {Name: "var-dup", Path: "/var", Optional: true}, + }) + p.Statfs = fs.statfs + + got := p.Check(context.Background()) + if got.Status != health.StatusOK { + t.Fatalf("status = %q, want ok: an optional path absent entirely is not a failure (detail: %s)", got.Status, got.Detail) + } +} + +// fakeStatfs returns a statfs replacement serving sizes for known paths and +// ENOENT for anything else, so a test can drive the disk probe without +// filling a real filesystem. +func fakeStatfs(sizes map[string]syscall.Statfs_t) struct { + statfs func(string, *syscall.Statfs_t) error +} { + return struct { + statfs func(string, *syscall.Statfs_t) error + }{ + statfs: func(path string, out *syscall.Statfs_t) error { + stat, ok := sizes[path] + if !ok { + return os.ErrNotExist + } + *out = stat + return nil + }, + } +}