Files
archie/internal/app/archied/drain_monitor_test.go
T
sam 21a1aa6ab3 feat(drain): honour external drain marker with instantiation epoch (#718)
Implement drain control via an external marker file. The daemon polls
$XDG_CONFIG_HOME/archie/.drain_request.json (the repo's config-home
convention) for a drain request, validates it against the current
instantiation epoch (kernel boot id + PID 1 start time) and, only when
the marker is live, cancels the boot context to trigger the same
graceful shutdown an operator SIGTERM does.

A marker written against a prior instantiation (reboot or PID 1/container
restart) is stale and ignored, so a leftover request never drains a
daemon that came up after it was written. An unreadable marker or epoch
fails closed to no drain. The daemon never invents a new shutdown path.

domain/drain owns the pure decision (Epoch, Marker, Decide);
infrastructure/drain reads the marker file and /proc boot facts;
app/archied wires a poller that cancels the root context on a valid
request.

Path note: the epic named ~/.archie/.drain_request.json, but archied
resolves every per-user path under ${XDG_CONFIG_HOME:-~/.config}/archie
(see AGENTS.md 'Config path'), so the marker lives there too rather than
at a path this daemon never uses.

Refs bead archie-core-1786637496817-266-06b64a72 (drain control: marker
file and instantiation epoch), part of the operations epic
archie-core-1786637496703-262-1e49aef1.

Tests: table-driven Decide (epoch mismatch -> stale, matching -> valid,
nil -> none, unreadable -> stale), marker round-trip from operator JSON,
and monitor (valid -> shutdown, stale/none/error -> no shutdown).
2026-09-05 13:38:02 +10:00

154 lines
3.5 KiB
Go

package archied
import (
"context"
"errors"
"log/slog"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/domain/drain"
)
const testInterval = 5 * time.Millisecond
// countShutdown returns a shutdown func plus a channel signalled on each call.
func countShutdown(t *testing.T) (func(), *chan struct{}) {
t.Helper()
calls := make(chan struct{}, 1)
return func() {
select {
case calls <- struct{}{}:
default:
}
}, &calls
}
func TestMonitorTriggersShutdownOnValidDecision(t *testing.T) {
shutdown, calls := countShutdown(t)
err := monitorDrainRequests(
context.Background(),
func() (drain.Decision, error) { return drain.DecisionValid, nil },
shutdown,
slog.New(slog.DiscardHandler),
testInterval,
)
if err != nil {
t.Fatalf("monitorDrainRequests() error = %v, want nil", err)
}
select {
case <-*calls:
default:
t.Fatal("shutdown was not invoked on a valid drain decision")
}
}
func TestMonitorDoesNotShutdownOnStaleDecision(t *testing.T) {
shutdown, calls := countShutdown(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// First poll returns stale; the monitor waits one interval. Cancel the
// context before the next poll, then assert shutdown was never invoked and
// the monitor returned the cancellation.
go func() {
time.Sleep(testInterval * 3)
cancel()
}()
err := monitorDrainRequests(
ctx,
func() (drain.Decision, error) { return drain.DecisionStale, nil },
shutdown,
slog.New(slog.DiscardHandler),
testInterval,
)
if !errors.Is(err, context.Canceled) {
t.Fatalf("monitorDrainRequests() error = %v, want context.Canceled", err)
}
select {
case <-*calls:
t.Fatal("shutdown was invoked on a stale drain decision")
default:
}
}
func TestMonitorDoesNotShutdownOnNoMarker(t *testing.T) {
shutdown, calls := countShutdown(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
time.Sleep(testInterval * 3)
cancel()
}()
err := monitorDrainRequests(
ctx,
func() (drain.Decision, error) { return drain.DecisionNone, nil },
shutdown,
slog.New(slog.DiscardHandler),
testInterval,
)
if !errors.Is(err, context.Canceled) {
t.Fatalf("monitorDrainRequests() error = %v, want context.Canceled", err)
}
select {
case <-*calls:
t.Fatal("shutdown was invoked with no drain marker")
default:
}
}
func TestMonitorDoesNotShutdownOnCheckError(t *testing.T) {
shutdown, calls := countShutdown(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
time.Sleep(testInterval * 3)
cancel()
}()
boom := errors.New("marker unreadable")
err := monitorDrainRequests(
ctx,
func() (drain.Decision, error) { return drain.DecisionStale, boom },
shutdown,
slog.New(slog.DiscardHandler),
testInterval,
)
if !errors.Is(err, context.Canceled) {
t.Fatalf("monitorDrainRequests() error = %v, want context.Canceled", err)
}
select {
case <-*calls:
t.Fatal("shutdown was invoked despite a check error")
default:
}
}
func TestMonitorShutdownAfterStaleThenValid(t *testing.T) {
shutdown, calls := countShutdown(t)
callsMade := 0
err := monitorDrainRequests(
context.Background(),
func() (drain.Decision, error) {
callsMade++
if callsMade == 1 {
return drain.DecisionStale, nil
}
return drain.DecisionValid, nil
},
shutdown,
slog.New(slog.DiscardHandler),
testInterval,
)
if err != nil {
t.Fatalf("monitorDrainRequests() error = %v, want nil", err)
}
select {
case <-*calls:
default:
t.Fatal("shutdown was not invoked after a marker became valid")
}
}