b0dd13154b
S1: corrupt settings.json recovers from .bak / safe-defaults+preserve, no crash-loop. F2: validStackName gates restore + export handlers (reject /,\,..,NUL traversal). S3: corrupt quiesce marker logged + quarantined, not silently dropped. Tests T-S1/F2/S3 + red-proofs. Agent/hub untouched. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
57 lines
1.7 KiB
Go
57 lines
1.7 KiB
Go
package quiesce
|
|
|
|
import (
|
|
"bytes"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// T-S3a: a corrupt marker is quarantined + logged (not silently dropped). Still returns false.
|
|
// Companion: pre-fix readMarker (silent return false) → no *.corrupt-* + no log → this fails.
|
|
func TestReadMarker_QuarantinesCorrupt(t *testing.T) {
|
|
mp := filepath.Join(t.TempDir(), "quiesce-state.json")
|
|
if err := os.WriteFile(mp, []byte(`{ "active": true, BROKEN not json`), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var buf bytes.Buffer
|
|
l := New(Options{
|
|
MarkerPath: mp, Poll: time.Hour, StatusPoll: time.Millisecond, MaxQuiesce: time.Second,
|
|
Logger: log.New(&buf, "", 0),
|
|
})
|
|
if _, ok := l.readMarker(); ok {
|
|
t.Fatal("corrupt marker must return ok=false")
|
|
}
|
|
if m, _ := filepath.Glob(mp + ".corrupt-*"); len(m) == 0 {
|
|
t.Error("corrupt marker must be quarantined as *.corrupt-*")
|
|
}
|
|
if !strings.Contains(buf.String(), "corrupt") {
|
|
t.Errorf("expected a WARN about the corrupt marker, got: %q", buf.String())
|
|
}
|
|
}
|
|
|
|
// T-S3b: valid marker → (marker, true), no quarantine (regression).
|
|
func TestReadMarker_HappyPath(t *testing.T) {
|
|
mp := filepath.Join(t.TempDir(), "quiesce-state.json")
|
|
if err := os.WriteFile(mp, []byte(`{"active":true,"stopped_stacks":["romm","komga"]}`), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
l := New(Options{
|
|
MarkerPath: mp, Poll: time.Hour, StatusPoll: time.Millisecond, MaxQuiesce: time.Second,
|
|
Logger: log.New(&bytes.Buffer{}, "", 0),
|
|
})
|
|
m, ok := l.readMarker()
|
|
if !ok {
|
|
t.Fatal("valid marker must return ok=true")
|
|
}
|
|
if len(m.StoppedStacks) != 2 {
|
|
t.Errorf("expected 2 stopped stacks, got %d", len(m.StoppedStacks))
|
|
}
|
|
if g, _ := filepath.Glob(mp + ".corrupt-*"); len(g) != 0 {
|
|
t.Error("happy path must NOT quarantine")
|
|
}
|
|
}
|