restic: RunCheck with subset% + lock-state sniffing

Add CheckResult (LockPresent, ErrorsFound) and RunCheck.  subsetPct>0
passes --read-data-subset N% to limit data reads.  Stderr is sniffed
for "Found stale lock"/"locked" to set LockPresent; a non-zero exit
from restic is absorbed as ErrorsFound=true rather than an error so
the caller can always persist last_check_status.  Tests cover lock
detection, exit-1 absorption, and subset-arg plumbing.
This commit is contained in:
2026-05-03 22:21:48 +01:00
parent 9b790bbade
commit b24faf6de7
2 changed files with 99 additions and 1 deletions
+46 -1
View File
@@ -34,7 +34,7 @@ func captureLines() (*[]string, LineHandler) {
return &lines, h
}
// --- B1: RunPrune ---
// --- B1: RunPrune + B2: RunCheck ---
func TestRunPruneInvokesPrune(t *testing.T) {
// Shell script that echoes its args; "prune" should appear in output.
@@ -51,3 +51,48 @@ func TestRunPruneInvokesPrune(t *testing.T) {
}
t.Fatalf("expected 'prune' in captured output; got: %v", *lines)
}
// --- B2: RunCheck ---
func TestRunCheckParsesLock(t *testing.T) {
bin := setupScriptBin(t, `echo "Found stale lock" >&2`)
env := Env{Bin: bin}
res, err := env.RunCheck(context.Background(), 0, nil)
if err != nil {
t.Fatalf("RunCheck returned unexpected error: %v", err)
}
if !res.LockPresent {
t.Fatal("expected LockPresent=true")
}
if res.ErrorsFound {
t.Fatal("expected ErrorsFound=false")
}
}
func TestRunCheckErrorsFoundOnExit1(t *testing.T) {
bin := setupScriptBin(t, `exit 1`)
env := Env{Bin: bin}
res, err := env.RunCheck(context.Background(), 0, nil)
if err != nil {
t.Fatalf("RunCheck returned unexpected error (should have absorbed exit 1): %v", err)
}
if !res.ErrorsFound {
t.Fatal("expected ErrorsFound=true for exit 1")
}
}
func TestRunCheckSubsetArg(t *testing.T) {
bin := setupScriptBin(t, `echo "$@"`)
env := Env{Bin: bin}
lines, h := captureLines()
if _, err := env.RunCheck(context.Background(), 25, h); err != nil {
t.Fatalf("RunCheck: %v", err)
}
want := "--read-data-subset 25%"
for _, l := range *lines {
if strings.Contains(l, want) {
return
}
}
t.Fatalf("expected %q in captured output; got: %v", want, *lines)
}