Files
felhom-agent/internal/poweroff/state_test.go
T
admin 3403c87c80
gates / gates (push) Successful in 34s
Host power-off for the household (R-998): POST /host/poweroff
The guest's controller asks; the agent refuses in the first 10 min after boot, a second time in
one boot (persisted, keyed by /proc/stat btime), and while a backup, restore test, OS step,
controller update, drive format, NAS verify, escrow ceremony or vzdump runs (each a named 409
reason). On acceptance: the record first, a host report carrying deliberate_off accepted by the
hub (bounded 60 s), then pct shutdown --timeout 120, then systemctl poweroff.

Sudoers: two exact vectors (pct ^shutdown N --timeout N$, systemctl poweroff) in FELHOM_POWEROFF;
nine injection cases. New TestEveryAliasIsGranted: a defined alias missing from the grant line
passed the coverage check before. Every new test red-proofed.
2026-10-13 19:29:52 +02:00

42 lines
1.3 KiB
Go

package poweroff
import (
"errors"
"path/filepath"
"testing"
"time"
)
// One accepted request per boot; the record is "current" only in the boot it was made in.
func TestOnePerBootAndCurrentFollowsTheBoot(t *testing.T) {
boot := "1000"
s := New(filepath.Join(t.TempDir(), "poweroff.json"), func() string { return boot })
if s.Current() != nil {
t.Fatal("no record yet, Current must be nil")
}
now := time.Date(2026, 10, 13, 18, 0, 0, 0, time.UTC)
if _, err := s.Mark(now, 9201); err != nil {
t.Fatalf("first Mark: %v", err)
}
if r := s.Current(); r == nil || !r.RequestedAt.Equal(now) || r.VMID != 9201 {
t.Fatalf("Current after Mark = %+v", r)
}
if _, err := s.Mark(now.Add(time.Minute), 9201); !errors.Is(err, ErrAlreadyThisBoot) {
t.Fatalf("second Mark in the same boot: err = %v, want ErrAlreadyThisBoot", err)
}
boot = "2000" // the machine was switched on again
if s.Current() != nil {
t.Fatal("a record from the previous boot must not be current")
}
if _, err := s.Mark(now.Add(time.Hour), 9201); err != nil {
t.Fatalf("Mark in a new boot: %v", err)
}
}
func TestUnknownBootFailsClosed(t *testing.T) {
s := New(filepath.Join(t.TempDir(), "p.json"), func() string { return "" })
if _, err := s.Mark(time.Now(), 1); !errors.Is(err, ErrBootUnknown) {
t.Fatalf("err = %v, want ErrBootUnknown", err)
}
}