carlos / platform Public

test: named-units show age is clock-independent

show read the wall clock for the ages it prints, and the tests built
their heartbeat and record times from a separate time.Now(), so the
exact "1m30s ago" assertion could flake on a slow runner. show's clock
is now namedUnitsNow, which the tests pin to a fixed time; the record's
age is asserted too.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 0108032b74827f93ab083f441bca5fc2e7c66b5d parent 9dadcb3
2 files changed, +22 −6
  • system_namedunits.go +4 −1
  • system_namedunits_test.go +18 −5
diff --git a/system_namedunits.go b/system_namedunits.go
index 07e337628..d31d6da84 100644
--- a/system_namedunits.go
+++ b/system_namedunits.go
@@ -89,6 +89,9 @@ func systemNamedUnitsCommand() *clireg.Node {
}
}
+// namedUnitsNow is show's clock, for the ages it prints; tests fix it.
+var namedUnitsNow = time.Now
+
// errNamedUnitsUsage is a flag or argument error; the flag set has already
// printed the problem and the usage.
var errNamedUnitsUsage = errors.New("usage")
@@ -243,7 +246,7 @@ func runNamedUnitsShow(ctx context.Context, args []string, out io.Writer, open f
if err != nil {
fmt.Fprintf(out, "box report unavailable: %v\n", err)
} else {
- printNamedUnitsReport(out, iid, rows, region, haveValue, version, time.Now())
+ printNamedUnitsReport(out, iid, rows, region, haveValue, version, namedUnitsNow())
}
if readErr != nil && !errors.Is(readErr, hostenv.ErrNoParameter) {
return errors.New("the parameter could not be read")
diff --git a/system_namedunits_test.go b/system_namedunits_test.go
index 0cd50f49c..6c6ea9edd 100644
--- a/system_namedunits_test.go
+++ b/system_namedunits_test.go
@@ -43,6 +43,17 @@ func (f *fakeNamedUnitsStore) Region() string { return f.region }
const testIID = "i-0abc1234def567890"
+// fixNamedUnitsClock pins show's clock for the test, so the ages it
+// prints do not depend on how long the test takes.
+func fixNamedUnitsClock(t *testing.T) time.Time {
+ t.Helper()
+ now := time.Date(2026, 11, 19, 9, 0, 0, 0, time.UTC)
+ prev := namedUnitsNow
+ namedUnitsNow = func() time.Time { return now }
+ t.Cleanup(func() { namedUnitsNow = prev })
+ return now
+}
+
// validDeclarations passes both decoders: addr and health path set.
const validDeclarations = `{"schema":1,"units":[{"host":"shop.example.com","app":"shop","unit":"shop.service","addr":"127.0.0.1:8080","health":{"path":"/up"}}]}`
@@ -130,14 +141,15 @@ func TestNamedUnitsPutPrintsPathVersionAndPickup(t *testing.T) {
// region and the box reads its own. Both regions are printed, and the
// mismatch is named, because that is the whole diagnosis.
func TestNamedUnitsShowNamesBothRegions(t *testing.T) {
+ now := fixNamedUnitsClock(t)
store := &fakeNamedUnitsStore{region: "eu-west-1", raw: []byte(validDeclarations), version: 4}
open := func(context.Context, string) (hostenv.NamedUnitsStore, error) { return store, nil }
boxes := func(context.Context, string) ([]systemBoxRow, error) {
return []systemBoxRow{
{Label: "other", InstanceID: "i-0fff1234def567890"},
- {Label: "box-1", InstanceID: testIID, HeartbeatAt: time.Now().Add(-2 * time.Minute), NamedUnits: &status.NamedUnitsStatus{
+ {Label: "box-1", InstanceID: testIID, HeartbeatAt: now.Add(-2 * time.Minute), NamedUnits: &status.NamedUnitsStatus{
Parameter: "/carlos/host/named-units/" + testIID, InstanceID: testIID, Region: "eu-west-2",
- Outcome: "not-found", At: time.Now().Add(-3 * time.Minute),
+ Outcome: "not-found", At: now.Add(-3 * time.Minute),
}},
}, nil
}
@@ -198,11 +210,12 @@ func TestNamedUnitsShowConsoleUnavailable(t *testing.T) {
// version says so with its heartbeat's age, and each unit's delivery
// state is listed.
func TestNamedUnitsShowBehindAndUnits(t *testing.T) {
+ now := fixNamedUnitsClock(t)
store := &fakeNamedUnitsStore{region: "eu-west-1", raw: []byte(validDeclarations), version: 5}
open := func(context.Context, string) (hostenv.NamedUnitsStore, error) { return store, nil }
boxes := func(context.Context, string) ([]systemBoxRow, error) {
- return []systemBoxRow{{Label: "box-1", InstanceID: testIID, HeartbeatAt: time.Now().Add(-90 * time.Second), NamedUnits: &status.NamedUnitsStatus{
- Region: "eu-west-1", Version: 4, Outcome: "installed", At: time.Now().Add(-time.Hour),
+ return []systemBoxRow{{Label: "box-1", InstanceID: testIID, HeartbeatAt: now.Add(-90 * time.Second), NamedUnits: &status.NamedUnitsStatus{
+ Region: "eu-west-1", Version: 4, Outcome: "installed", At: now.Add(-time.Hour),
Units: []status.NamedUnitDelivery{
{Host: "shop.example.com", Unit: "shop.service", State: "delivered"},
{Host: "blog.example.com", Unit: "blog.service", State: "refused", Reason: "unit not loaded"},
@@ -214,7 +227,7 @@ func TestNamedUnitsShowBehindAndUnits(t *testing.T) {
t.Fatal(err)
}
s := out.String()
- for _, want := range []string{"has not taken version 5", "1m30s ago", "shop.service", "delivered", "blog.service", "refused: unit not loaded"} {
+ for _, want := range []string{"has not taken version 5", "last heartbeat was 1m30s ago", "at 2026-11-19T08:00:00Z (1h00m ago)", "shop.service", "delivered", "blog.service", "refused: unit not loaded"} {
if !strings.Contains(s, want) {
t.Errorf("output lacks %q:\n%s", want, s)
}