test: verify reset CLI and restored startup preserve local work (#7)
This commit is contained in:
1 parent
ffdbe4fbd0
commit
2d60fcfb39
2 files changed
+142
-1
No files matched your search
@@ -0,0 +1,130 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"context"
|
||||
"flag"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.mchus.pro/mchus/quoteforge/internal/localdb"
|
||||
)
|
||||
|
||||
func TestResetCLIHelper(t *testing.T) {
|
||||
if os.Getenv("QFS_RESET_CLI_HELPER") != "1" {
|
||||
return
|
||||
}
|
||||
for i, arg := range os.Args {
|
||||
if arg == "--" {
|
||||
os.Args = append([]string{"qfs"}, os.Args[i+1:]...)
|
||||
flag.CommandLine = flag.NewFlagSet("qfs", flag.ExitOnError)
|
||||
main()
|
||||
return
|
||||
}
|
||||
}
|
||||
t.Fatal("missing CLI arguments")
|
||||
}
|
||||
|
||||
func TestResetCLIAndRestore(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
t.Setenv("QFS_STATE_DIR", dir)
|
||||
t.Setenv("QFS_BACKUP_DISABLE", "1")
|
||||
t.Setenv("QFS_RESET_LOCAL_DB", "yes")
|
||||
t.Setenv("QFS_BACKUP_DIR", filepath.Join(dir, "backups"))
|
||||
dbPath := filepath.Join(dir, "qfs.db")
|
||||
configPath := filepath.Join(dir, "config.yaml")
|
||||
if err := os.WriteFile(configPath, []byte("server:\n host: 127.0.0.1\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
local, err := localdb.New(dbPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := local.SaveSettings("127.0.0.1", 1, "example", "example", "example"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := local.AddPendingChange("configuration", "example", "update", "{}"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := local.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
run := func(args ...string) string {
|
||||
t.Helper()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer cancel()
|
||||
argv := append([]string{"-test.run=^TestResetCLIHelper$", "--", "-localdb", dbPath, "-config", configPath, "-migrate-status"}, args...)
|
||||
cmd := exec.CommandContext(ctx, os.Args[0], argv...)
|
||||
cmd.Env = append(os.Environ(), "QFS_RESET_CLI_HELPER=1")
|
||||
output, err := cmd.CombinedOutput()
|
||||
if ctx.Err() != nil {
|
||||
t.Fatalf("CLI timed out: %s", output)
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatalf("expected reset refusal or offline migration-status exit: %s", output)
|
||||
}
|
||||
return string(output)
|
||||
}
|
||||
assertPending := func(want int64) {
|
||||
t.Helper()
|
||||
local, err := localdb.New(dbPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer local.Close()
|
||||
if got := local.CountPendingChanges(); got != want {
|
||||
t.Fatalf("pending=%d want=%d", got, want)
|
||||
}
|
||||
if !local.HasSettings() {
|
||||
t.Fatal("connection settings lost")
|
||||
}
|
||||
}
|
||||
if output := run(); !strings.Contains(output, "QFS_RESET_LOCAL_DB is ignored") {
|
||||
t.Fatalf("environment reset not diagnosed: %s", output)
|
||||
}
|
||||
assertPending(1)
|
||||
if output := run("-reset-localdb"); !strings.Contains(output, "reset refused") {
|
||||
t.Fatalf("pending reset not refused: %s", output)
|
||||
}
|
||||
assertPending(1)
|
||||
if output := run("-reset-localdb", "-force-reset-localdb"); !strings.Contains(output, "verified pre-reset backup") {
|
||||
t.Fatalf("missing safety backup: %s", output)
|
||||
}
|
||||
assertPending(0)
|
||||
archives, err := filepath.Glob(filepath.Join(dir, "backups", "pre-reset", "*.zip"))
|
||||
if err != nil || len(archives) != 1 {
|
||||
t.Fatalf("archives=%v err=%v", archives, err)
|
||||
}
|
||||
archive, err := zip.OpenReader(archives[0])
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer archive.Close()
|
||||
for _, entry := range archive.File {
|
||||
if entry.Name != "qfs.db" && entry.Name != "config.yaml" {
|
||||
t.Fatalf("unexpected archive entry: %s", entry.Name)
|
||||
}
|
||||
in, err := entry.Open()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, readErr := io.ReadAll(in)
|
||||
closeErr := in.Close()
|
||||
if readErr != nil || closeErr != nil {
|
||||
t.Fatalf("restore entry: read=%v close=%v", readErr, closeErr)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, entry.Name), data, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
assertPending(1)
|
||||
if output := run(); !strings.Contains(output, "cannot run migrations: database not available") {
|
||||
t.Fatalf("restored startup did not reach migration status: %s", output)
|
||||
}
|
||||
assertPending(1)
|
||||
}
|
||||
Reference in new issue
Block a user