485f4322cb
Add RunUnlock (delegates straight to runWithPump) and RunStats which runs `restic stats --json --mode raw-data`, captures the single JSON line from stdout into RepoStats, and returns an error if no JSON arrives. Tests cover arg plumbing for unlock, JSON parsing, and the no-JSON error path.
152 lines
4.2 KiB
Go
152 lines
4.2 KiB
Go
package restic
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// setupScriptBin writes a small shell script to a temp directory,
|
|
// makes it executable, and returns its path. scriptBody is the
|
|
// complete script content (without the shebang line — that's added
|
|
// automatically).
|
|
func setupScriptBin(t *testing.T, scriptBody string) string {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
p := filepath.Join(dir, "restic")
|
|
content := "#!/bin/sh\n" + scriptBody + "\n"
|
|
if err := os.WriteFile(p, []byte(content), 0o755); err != nil {
|
|
t.Fatalf("setupScriptBin: %v", err)
|
|
}
|
|
return p
|
|
}
|
|
|
|
// captureLines returns a LineHandler that appends "stream:line" into
|
|
// the returned slice pointer (safe for single-goroutine test use).
|
|
func captureLines() (*[]string, LineHandler) {
|
|
var lines []string
|
|
h := func(stream, line string, _ any) {
|
|
lines = append(lines, fmt.Sprintf("%s:%s", stream, line))
|
|
}
|
|
return &lines, h
|
|
}
|
|
|
|
// --- B1: RunPrune + B2: RunCheck ---
|
|
|
|
func TestRunPruneInvokesPrune(t *testing.T) {
|
|
// Shell script that echoes its args; "prune" should appear in output.
|
|
bin := setupScriptBin(t, `echo "$@"`)
|
|
env := Env{Bin: bin}
|
|
lines, h := captureLines()
|
|
if err := env.RunPrune(context.Background(), h); err != nil {
|
|
t.Fatalf("RunPrune returned error: %v", err)
|
|
}
|
|
for _, l := range *lines {
|
|
if strings.Contains(l, "prune") {
|
|
return
|
|
}
|
|
}
|
|
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)
|
|
}
|
|
|
|
// --- B3: RunUnlock + RunStats ---
|
|
|
|
func TestRunUnlockInvokesUnlock(t *testing.T) {
|
|
bin := setupScriptBin(t, `echo "$@"`)
|
|
env := Env{Bin: bin}
|
|
lines, h := captureLines()
|
|
if err := env.RunUnlock(context.Background(), h); err != nil {
|
|
t.Fatalf("RunUnlock: %v", err)
|
|
}
|
|
for _, l := range *lines {
|
|
if strings.Contains(l, "unlock") {
|
|
return
|
|
}
|
|
}
|
|
t.Fatalf("expected 'unlock' in captured output; got: %v", *lines)
|
|
}
|
|
|
|
func TestRunStatsParsesJSON(t *testing.T) {
|
|
bin := setupScriptBin(t, `echo '{"total_size":1234,"total_uncompressed_size":5678,"snapshots_count":3,"total_file_count":100,"total_blob_count":50}'`)
|
|
env := Env{Bin: bin}
|
|
stats, err := env.RunStats(context.Background(), nil)
|
|
if err != nil {
|
|
t.Fatalf("RunStats: %v", err)
|
|
}
|
|
if stats.TotalSize != 1234 {
|
|
t.Fatalf("TotalSize: got %d, want 1234", stats.TotalSize)
|
|
}
|
|
if stats.TotalUncompressed != 5678 {
|
|
t.Fatalf("TotalUncompressed: got %d, want 5678", stats.TotalUncompressed)
|
|
}
|
|
if stats.SnapshotsCount != 3 {
|
|
t.Fatalf("SnapshotsCount: got %d, want 3", stats.SnapshotsCount)
|
|
}
|
|
if stats.TotalFileCount != 100 {
|
|
t.Fatalf("TotalFileCount: got %d, want 100", stats.TotalFileCount)
|
|
}
|
|
if stats.TotalBlobCount != 50 {
|
|
t.Fatalf("TotalBlobCount: got %d, want 50", stats.TotalBlobCount)
|
|
}
|
|
}
|
|
|
|
func TestRunStatsErrorsWithoutJSON(t *testing.T) {
|
|
bin := setupScriptBin(t, `echo "no json here"`)
|
|
env := Env{Bin: bin}
|
|
_, err := env.RunStats(context.Background(), nil)
|
|
if err == nil {
|
|
t.Fatal("expected error when no JSON in output")
|
|
}
|
|
if !strings.Contains(err.Error(), "no JSON in output") {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
}
|