fix(readiness): keep required status when an optional target shares a path

Targets are deduplicated by path so a shared filesystem is probed once, but
the first target seen won. With root, then an optional /var, then a required
data path that resolves to /var, the optional target is the one that survived
-- so its rules were applied to a path the daemon requires. An absent /var is
skipped as "optional absent", and with another healthy target present the
probe reported OK while a required path was unavailable.

Deduplication now keeps the strictest requirement any target places on a
path, and its name, so the failure is both visible and correctly attributed
rather than reported against the optional target that happened to come first.

Red first: the new test drives the reported scenario through an injected
statfs and failed with `status = "ok", want degraded` before the fix.
Verified by mutation -- restoring first-wins dedup fails it again.
This commit is contained in:
sam committed 2026-09-17 11:48:23 +00:00
1 parent 8378c92739
commit 51a05c0f33
2 files changed
+101 -6

No files matched your search

+30 -6
View File
@@ -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) {
@@ -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
},
}
}