Add pause resume and abort controls
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/backupper-unraid/backupper/internal/model"
|
||||
)
|
||||
@@ -88,3 +89,60 @@ func TestResticErrorExplainsCiphertextVerification(t *testing.T) {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerPausesResumesAndCancelsProcessGroup(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
ticksPath := filepath.Join(dir, "ticks")
|
||||
script := filepath.Join(dir, "restic")
|
||||
body := fmt.Sprintf("#!/bin/sh\nwhile :; do printf x >> '%s'; sleep 0.05; done\n", ticksPath)
|
||||
if err := os.WriteFile(script, []byte(body), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runner := &Runner{Binary: script, RuntimeDir: dir, Secrets: fakeSecrets{"password": "secret"}}
|
||||
repo := model.Repository{Type: model.RepositoryLocal, Location: "/repo", PasswordRef: "password"}
|
||||
ctx, cancel := context.WithCancel(WithRunID(context.Background(), "run"))
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- runner.run(ctx, repo, []string{"backup"}, nil, nil) }()
|
||||
|
||||
waitForSize := func(minimum int64) int64 {
|
||||
deadline := time.Now().Add(2 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
if info, err := os.Stat(ticksPath); err == nil && info.Size() >= minimum {
|
||||
return info.Size()
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
t.Fatalf("tick file did not reach %d bytes", minimum)
|
||||
return 0
|
||||
}
|
||||
waitForSize(2)
|
||||
if err := runner.Pause("run"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
pausedSize := waitForSize(1)
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
info, err := os.Stat(ticksPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if info.Size() != pausedSize {
|
||||
t.Fatalf("process continued while paused: size=%d, expected=%d", info.Size(), pausedSize)
|
||||
}
|
||||
if err := runner.Resume("run"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
waitForSize(pausedSize + 2)
|
||||
if err := runner.Pause("run"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cancel()
|
||||
select {
|
||||
case err := <-done:
|
||||
if err == nil || !strings.Contains(err.Error(), "cancelled") {
|
||||
t.Fatalf("cancel result = %v", err)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("paused process group was not cancelled")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user