|
|
|
@@ -1,19 +1,18 @@
|
|
|
|
|
// Package main tests the offline recovery subcommands of archie-state-store:
|
|
|
|
|
// backup, restore, validate and rollback against the task database file
|
|
|
|
|
// directly. They are the operator's only path back when the control plane's
|
|
|
|
|
// stored settings will not validate, because the daemon fails closed: the
|
|
|
|
|
// in-band remedy (replay an earlier revision while the State Store is up) is
|
|
|
|
|
// unavailable in exactly the case that needs it.
|
|
|
|
|
// Package main tests the offline recovery subcommands of archie-state-store
|
|
|
|
|
// against the PostgreSQL database the configuration names. They are the
|
|
|
|
|
// operator's only path back when the control plane's stored settings will not
|
|
|
|
|
// validate, because the daemon fails closed: the in-band remedy (replay an
|
|
|
|
|
// earlier revision while the State Store is up) is unavailable in exactly the
|
|
|
|
|
// case that needs it.
|
|
|
|
|
//
|
|
|
|
|
// The tests drive the subcommands the way an operator does -- through
|
|
|
|
|
// runRecovery, against real files on disk -- rather than through the internal
|
|
|
|
|
// helpers, so the flag surface and the exit codes are part of what is verified.
|
|
|
|
|
// runRecovery -- rather than through the internal helpers, so the flag surface
|
|
|
|
|
// and the exit codes are part of what is verified.
|
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"database/sql"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
@@ -22,13 +21,14 @@ import (
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
_ "modernc.org/sqlite"
|
|
|
|
|
|
|
|
|
|
"github.com/samcharles93/archie-core/internal/app/archied"
|
|
|
|
|
"github.com/samcharles93/archie-core/internal/app/controlplane"
|
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
|
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
|
|
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
|
|
|
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
|
|
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
|
|
|
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/workflowsteps"
|
|
|
|
|
"github.com/samcharles93/archie-core/internal/store"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// runRecoveryCmd runs one subcommand the way main does and returns its exit
|
|
|
|
@@ -40,240 +40,38 @@ func runRecoveryCmd(t *testing.T, args ...string) (int, string, string) {
|
|
|
|
|
return code, stdout.String(), stderr.String()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// openTaskStore opens the task database the subcommands operate on. It does not
|
|
|
|
|
// take the ownership lock: only the serving process does that, and a test that
|
|
|
|
|
// wants a "live" store holds it explicitly with holdStoreOwnership.
|
|
|
|
|
func openTaskStore(t *testing.T, path string) *store.Store {
|
|
|
|
|
// recoveryFixture writes a config naming a fresh migrated database and returns
|
|
|
|
|
// it with the task database on that database.
|
|
|
|
|
func recoveryFixture(t *testing.T) (string, *pgstore.TaskDB) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
st, err := store.Open(t.Context(), path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("open task store %s: %v", path, err)
|
|
|
|
|
}
|
|
|
|
|
t.Cleanup(func() { _ = st.Close() })
|
|
|
|
|
return st
|
|
|
|
|
url := pgtest.URL(t)
|
|
|
|
|
configPath := writeConfigFor(t, t.TempDir(), url)
|
|
|
|
|
return configPath, pgstore.On(pgstore.PoolAt(t, url))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// holdStoreOwnership takes the ownership lock the running State Store holds for
|
|
|
|
|
// its whole life, so a test can prove a command refuses to touch a live store.
|
|
|
|
|
func holdStoreOwnership(t *testing.T, path string) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
ownership, err := store.AcquireOwnership(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("acquire store ownership: %v", err)
|
|
|
|
|
}
|
|
|
|
|
t.Cleanup(func() { _ = ownership.Release() })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// seedTask writes one task so a snapshot can be distinguished from the live
|
|
|
|
|
// database by what it contains, not merely by its existence.
|
|
|
|
|
func seedTask(t *testing.T, st *store.Store, title string) {
|
|
|
|
|
// seedTask writes one task so the database holds more than its settings.
|
|
|
|
|
func seedTask(t *testing.T, st *pgstore.TaskDB, title string) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
if _, err := st.EnqueueChatTask(t.Context(), "acme", "widget", title, "body", "implement", ""); err != nil {
|
|
|
|
|
t.Fatalf("seed task %q: %v", title, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func taskTitles(t *testing.T, path string) []string {
|
|
|
|
|
// execRaw runs one statement against the database the way an operator editing
|
|
|
|
|
// a broken store would, bypassing every check the store applies.
|
|
|
|
|
func execRaw(t *testing.T, st *pgstore.TaskDB, statement string) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
st := openTaskStore(t, path)
|
|
|
|
|
tasks, err := st.Tasks(t.Context(), 100)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("list tasks in %s: %v", path, err)
|
|
|
|
|
}
|
|
|
|
|
titles := make([]string, 0, len(tasks))
|
|
|
|
|
for _, task := range tasks {
|
|
|
|
|
titles = append(titles, task.Title)
|
|
|
|
|
}
|
|
|
|
|
return titles
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// backupPath is the snapshot every restore test restores from.
|
|
|
|
|
func backupPath(t *testing.T, dir string) string { return filepath.Join(dir, "snapshot.sqlite") }
|
|
|
|
|
|
|
|
|
|
// backup takes the snapshot the caller then restores, through the subcommand
|
|
|
|
|
// under test rather than through the store directly, so a broken backup fails
|
|
|
|
|
// the restore test too instead of silently passing an empty file.
|
|
|
|
|
func backup(t *testing.T, db, out string) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "backup", "-db", db, "-out", out)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("backup exited %d: %s", code, stderr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The update installer cannot stop the process that runs it, so it backs the
|
|
|
|
|
// task database up while the State Store is serving. VACUUM INTO is safe
|
|
|
|
|
// against a live writer; a backup that insisted on an exclusively-owned file
|
|
|
|
|
// would break the one caller it has, and the update would refuse to start.
|
|
|
|
|
func TestRecoveryBackupSnapshotsAStoreItsStateStoreIsServing(t *testing.T) {
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
db := filepath.Join(dir, "archie.db-tasks.sqlite")
|
|
|
|
|
out := backupPath(t, dir)
|
|
|
|
|
seedTask(t, openTaskStore(t, db), "before")
|
|
|
|
|
holdStoreOwnership(t, db)
|
|
|
|
|
if err := os.WriteFile(out, []byte("stale snapshot"), 0o600); err != nil {
|
|
|
|
|
if _, err := st.Pool.Exec(t.Context(), statement); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
code, stdout, stderr := runRecoveryCmd(t, "backup", "-db", db, "-out", out)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("backup exited %d: %s", code, stderr)
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(stdout, out) || !strings.Contains(stdout, db) {
|
|
|
|
|
t.Errorf("backup must report the database and the snapshot it wrote; stdout = %q", stdout)
|
|
|
|
|
}
|
|
|
|
|
// The stale file was replaced by a real snapshot, and the snapshot is a
|
|
|
|
|
// store a restore can put back: same rows, readable through the store.
|
|
|
|
|
if got := taskTitles(t, out); len(got) != 1 || got[0] != "before" {
|
|
|
|
|
t.Fatalf("snapshot tasks = %v, want the one seeded task", got)
|
|
|
|
|
}
|
|
|
|
|
if entries, err := filepath.Glob(out + ".tmp*"); err != nil || len(entries) != 0 {
|
|
|
|
|
t.Errorf("backup left scratch files beside the snapshot: %v (%v)", entries, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The snapshot is the rollback state, so an interrupted backup must never
|
|
|
|
|
// leave a truncated file where a good snapshot was.
|
|
|
|
|
func TestRecoveryBackupRefusesAMissingDatabase(t *testing.T) {
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "backup", "-db", filepath.Join(dir, "absent.sqlite"), "-out", backupPath(t, dir))
|
|
|
|
|
if code == 0 {
|
|
|
|
|
t.Fatal("backup of a missing database succeeded")
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(stderr, "absent.sqlite") {
|
|
|
|
|
t.Errorf("refusal must name the database it could not read; stderr = %q", stderr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// backup is the one recovery command allowed to run against a serving store,
|
|
|
|
|
// and that exemption is only sound while it never replaces the database. With
|
|
|
|
|
// -out naming the database it renames the snapshot over the live file and
|
|
|
|
|
// leaves the WAL of the database it just unlinked -- the state the restore path
|
|
|
|
|
// refuses up front and the code elsewhere calls unrecoverable.
|
|
|
|
|
func TestRecoveryBackupRefusesTheDatabaseAsItsOwnSnapshot(t *testing.T) {
|
|
|
|
|
db := filepath.Join(t.TempDir(), "archie.db-tasks.sqlite")
|
|
|
|
|
st := openTaskStore(t, db)
|
|
|
|
|
seedTask(t, st, "live")
|
|
|
|
|
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "backup", "-db", db, "-out", db)
|
|
|
|
|
if code == 0 {
|
|
|
|
|
t.Fatal("backup over the database itself succeeded")
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(stderr, "is the database itself") {
|
|
|
|
|
t.Errorf("refusal must say the snapshot is the database; stderr = %q", stderr)
|
|
|
|
|
}
|
|
|
|
|
if got := taskTitles(t, db); len(got) != 1 || got[0] != "live" {
|
|
|
|
|
t.Fatalf("refused backup changed the database: tasks = %v", got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Restoring is the forward-migration escape hatch: the previous release
|
|
|
|
|
// reads this file, so it must be exactly the snapshot, with the WAL that
|
|
|
|
|
// belongs to the replaced database gone rather than replayed over it.
|
|
|
|
|
func TestRecoveryRestorePutsTheSnapshotBackAndDropsItsWAL(t *testing.T) {
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
db := filepath.Join(dir, "archie.db-tasks.sqlite")
|
|
|
|
|
out := backupPath(t, dir)
|
|
|
|
|
st := openTaskStore(t, db)
|
|
|
|
|
seedTask(t, st, "before")
|
|
|
|
|
backup(t, db, out)
|
|
|
|
|
seedTask(t, st, "after")
|
|
|
|
|
|
|
|
|
|
code, stdout, stderr := runRecoveryCmd(t, "restore", "-db", db, "-from", out)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("restore exited %d: %s", code, stderr)
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(stdout, out) {
|
|
|
|
|
t.Errorf("restore must report the snapshot it restored from; stdout = %q", stdout)
|
|
|
|
|
}
|
|
|
|
|
// Checked before anything reopens the database: opening a store sets WAL
|
|
|
|
|
// mode, which creates these files, so the point is that the restore did not
|
|
|
|
|
// leave the replaced database's log behind -- not that a later reader
|
|
|
|
|
// cannot recreate its own.
|
|
|
|
|
for _, suffix := range []string{"-wal", "-shm"} {
|
|
|
|
|
if _, err := os.Stat(db + suffix); err == nil {
|
|
|
|
|
t.Errorf("%s survived the restore: the replaced database's write-ahead log must not be replayed over the snapshot", db+suffix)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if got := taskTitles(t, db); len(got) != 1 || got[0] != "before" {
|
|
|
|
|
t.Fatalf("restored tasks = %v, want only the snapshot's task", got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Overwriting a database a running State Store still owns leaves that process
|
|
|
|
|
// writing to an unlinked inode while the store serves a file nobody owns --
|
|
|
|
|
// the one way an offline command can destroy a task store.
|
|
|
|
|
func TestRecoveryRestoreRefusesAStoreTheStateStoreIsRunning(t *testing.T) {
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
db := filepath.Join(dir, "archie.db-tasks.sqlite")
|
|
|
|
|
out := backupPath(t, dir)
|
|
|
|
|
st := openTaskStore(t, db)
|
|
|
|
|
seedTask(t, st, "before")
|
|
|
|
|
backup(t, db, out)
|
|
|
|
|
seedTask(t, st, "after")
|
|
|
|
|
holdStoreOwnership(t, db)
|
|
|
|
|
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "restore", "-db", db, "-from", out)
|
|
|
|
|
if code == 0 {
|
|
|
|
|
t.Fatal("restore against a live State Store succeeded")
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(stderr, "owned by another process") {
|
|
|
|
|
t.Errorf("refusal must say the database is owned by another process; stderr = %q", stderr)
|
|
|
|
|
}
|
|
|
|
|
if got := taskTitles(t, db); len(got) != 2 {
|
|
|
|
|
t.Fatalf("refused restore changed the database: tasks = %v", got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRecoveryRestoreRefusesAFileThatIsNotAStore(t *testing.T) {
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
db := filepath.Join(dir, "archie.db-tasks.sqlite")
|
|
|
|
|
seedTask(t, openTaskStore(t, db), "live")
|
|
|
|
|
notAStore := filepath.Join(dir, "notes.txt")
|
|
|
|
|
if err := os.WriteFile(notAStore, []byte("this is not a database"), 0o600); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "restore", "-db", db, "-from", notAStore)
|
|
|
|
|
if code == 0 {
|
|
|
|
|
t.Fatal("restore from a file that is not a store succeeded")
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(stderr, notAStore) {
|
|
|
|
|
t.Errorf("refusal must name the snapshot it could not read; stderr = %q", stderr)
|
|
|
|
|
}
|
|
|
|
|
if got := taskTitles(t, db); len(got) != 1 {
|
|
|
|
|
t.Fatalf("refused restore changed the database: tasks = %v", got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRecoveryRestoreRefusesTheDatabaseAsItsOwnSnapshot(t *testing.T) {
|
|
|
|
|
// Restoring a database from itself would delete the write-ahead log of the
|
|
|
|
|
// file it is about to copy, so the one case that must never reach the swap
|
|
|
|
|
// is refused up front.
|
|
|
|
|
db := filepath.Join(t.TempDir(), "archie.db-tasks.sqlite")
|
|
|
|
|
seedTask(t, openTaskStore(t, db), "live")
|
|
|
|
|
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "restore", "-db", db, "-from", db)
|
|
|
|
|
if code == 0 {
|
|
|
|
|
t.Fatal("restore from the database itself succeeded")
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(stderr, "is the database itself") {
|
|
|
|
|
t.Errorf("refusal must say the snapshot is the database; stderr = %q", stderr)
|
|
|
|
|
}
|
|
|
|
|
if got := taskTitles(t, db); len(got) != 1 {
|
|
|
|
|
t.Fatalf("refused restore changed the database: tasks = %v", got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRecoveryValidateAcceptsAHealthyStore(t *testing.T) {
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
configPath := writeMinimalConfig(t, dir)
|
|
|
|
|
db := filepath.Join(dir, "archie.db-tasks.sqlite")
|
|
|
|
|
st := openTaskStore(t, db)
|
|
|
|
|
configPath, st := recoveryFixture(t)
|
|
|
|
|
seedTask(t, st, "healthy")
|
|
|
|
|
seedStoreResources(t, st, configPath)
|
|
|
|
|
|
|
|
|
|
code, stdout, stderr := runRecoveryCmd(t, "validate", "-db", db, "-config", configPath)
|
|
|
|
|
code, stdout, stderr := runRecoveryCmd(t, "validate", "-config", configPath)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("validate exited %d: %s", code, stderr)
|
|
|
|
|
}
|
|
|
|
@@ -293,63 +91,54 @@ func TestRecoveryValidateAcceptsAHealthyStore(t *testing.T) {
|
|
|
|
|
// path's own per-resource decode, and the two disagree: the write path accepts
|
|
|
|
|
// a scheduling policy boot rejects.
|
|
|
|
|
func TestRecoveryValidateRunsTheGateBootRuns(t *testing.T) {
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
configPath := writeMinimalConfig(t, dir)
|
|
|
|
|
db := filepath.Join(dir, "archie.db-tasks.sqlite")
|
|
|
|
|
seedStoreResources(t, openTaskStore(t, db), configPath)
|
|
|
|
|
configPath, st := recoveryFixture(t)
|
|
|
|
|
seedStoreResources(t, st, configPath)
|
|
|
|
|
|
|
|
|
|
// The control: a store seeded from this config is one the daemon boots on.
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "validate", "-db", db, "-config", configPath)
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "validate", "-config", configPath)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("validate exited %d on a store the daemon starts on: %s", code, stderr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The upgrade case: a release defines a kind the store does not hold yet,
|
|
|
|
|
// and validate runs before the State Store has started again. The State
|
|
|
|
|
// Store seeds the kind it is missing, so the store is not the reason the
|
|
|
|
|
// daemon would refuse to start.
|
|
|
|
|
execRaw(t, st, `DELETE FROM resources WHERE kind='provider-settings'`)
|
|
|
|
|
code, stdout, stderr := runRecoveryCmd(t, "validate", "-config", configPath)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("validate refused a store missing a kind the State Store seeds; stderr = %q", stderr)
|
|
|
|
|
}
|
|
|
|
|
if count := storedResourceCount(t, stdout); count == 0 {
|
|
|
|
|
t.Errorf("validate must still check the kinds the store does hold; stdout = %q", stdout)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A hand-edited value, or an older revision of one: the write path's own
|
|
|
|
|
// validator never inspects dispatch.trigger, so only the config gate
|
|
|
|
|
// catches it, and the daemon exits 1 on it (validateDispatch).
|
|
|
|
|
if err := execRaw(t, db, `UPDATE resources SET value='{"poll_interval":"1m","max_retries":3,"dispatch":{"trigger":"bogus"}}' WHERE kind='scheduling-policy'`); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
code, stdout, stderr := runRecoveryCmd(t, "validate", "-db", db, "-config", configPath)
|
|
|
|
|
execRaw(t, st, `UPDATE resources SET value='{"poll_interval":"1m","max_retries":3,"dispatch":{"trigger":"bogus"}}' WHERE kind='scheduling-policy'`)
|
|
|
|
|
code, stdout, stderr = runRecoveryCmd(t, "validate", "-config", configPath)
|
|
|
|
|
if code == 0 {
|
|
|
|
|
t.Fatalf("validate accepted a stored policy the daemon refuses to boot with; stdout = %q", stdout)
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(stderr, "dispatch.trigger") {
|
|
|
|
|
t.Errorf("refusal must name what boot rejects; stderr = %q", stderr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A store the State Store never seeded holds no kinds at all. Its next start
|
|
|
|
|
// creates the resources table and seeds every kind from this same config
|
|
|
|
|
// (internal/app/archied/state_store.go), so archied layers those seeds and
|
|
|
|
|
// starts: refusing the file here would send an operator to restore a
|
|
|
|
|
// snapshot for a store the daemon boots on perfectly well.
|
|
|
|
|
unseeded := filepath.Join(dir, "unseeded.db-tasks.sqlite")
|
|
|
|
|
openTaskStore(t, unseeded)
|
|
|
|
|
code, stdout, stderr = runRecoveryCmd(t, "validate", "-db", unseeded, "-config", configPath)
|
|
|
|
|
// A database the State Store never seeded holds no kinds at all. Its next
|
|
|
|
|
// start seeds every kind from this same config, so archied layers those seeds
|
|
|
|
|
// and starts: refusing it would send an operator to restore a snapshot for a
|
|
|
|
|
// database the daemon boots on perfectly well.
|
|
|
|
|
func TestRecoveryValidateAcceptsAnUnseededDatabase(t *testing.T) {
|
|
|
|
|
configPath, _ := recoveryFixture(t)
|
|
|
|
|
code, stdout, stderr := runRecoveryCmd(t, "validate", "-config", configPath)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("validate refused a store the State Store would seed on its next start; stderr = %q", stderr)
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(stdout, "0 stored resources validate") {
|
|
|
|
|
t.Errorf("validate must report that there was nothing stored to check; stdout = %q", stdout)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The upgrade case: a release defines a kind the store does not hold yet,
|
|
|
|
|
// and validate runs before the State Store has started again. The State
|
|
|
|
|
// Store seeds the kind it is missing, so this is the same stance as the
|
|
|
|
|
// store with nothing stored at all -- the file is not the reason the daemon
|
|
|
|
|
// would refuse to start, and saying otherwise costs the operator a snapshot.
|
|
|
|
|
partial := filepath.Join(dir, "partial.db-tasks.sqlite")
|
|
|
|
|
seedStoreResources(t, openTaskStore(t, partial), configPath)
|
|
|
|
|
if err := execRaw(t, partial, `DELETE FROM resources WHERE kind='provider-settings'`); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
code, stdout, stderr = runRecoveryCmd(t, "validate", "-db", partial, "-config", configPath)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("validate refused a store missing a kind the State Store seeds; stderr = %q", stderr)
|
|
|
|
|
}
|
|
|
|
|
if count := storedResourceCount(t, stdout); count == 0 {
|
|
|
|
|
t.Errorf("validate must still check the kinds the store does hold; stdout = %q", stdout)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The subcommands advertise -h as the way to read their flags, and the serve
|
|
|
|
@@ -372,10 +161,7 @@ func TestRecoveryHelpExitsZero(t *testing.T) {
|
|
|
|
|
func TestRecoveryValidateLeavesTheDaemonLogAlone(t *testing.T) {
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
configPath, logFile := writeConfigWithLogFile(t, dir)
|
|
|
|
|
db := filepath.Join(dir, "archie.db-tasks.sqlite")
|
|
|
|
|
seedStoreResources(t, openTaskStore(t, db), configPath)
|
|
|
|
|
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "validate", "-db", db, "-config", configPath)
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "validate", "-config", configPath)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("validate exited %d: %s", code, stderr)
|
|
|
|
|
}
|
|
|
|
@@ -419,11 +205,10 @@ func TestRecoveryDefaultConfigResolvesWhereTheDaemonReads(t *testing.T) {
|
|
|
|
|
if err := os.MkdirAll(configDir, 0o700); err != nil {
|
|
|
|
|
t.Fatalf("create %s: %v", configDir, err)
|
|
|
|
|
}
|
|
|
|
|
configPath := writeMinimalConfig(t, configDir)
|
|
|
|
|
db := filepath.Join(root, "archie.db-tasks.sqlite")
|
|
|
|
|
seedStoreResources(t, openTaskStore(t, db), configPath)
|
|
|
|
|
|
|
|
|
|
code, stdout, stderr := runRecoveryCmd(t, "validate", "-db", db)
|
|
|
|
|
url := pgtest.URL(t)
|
|
|
|
|
pgstore.PoolAt(t, url)
|
|
|
|
|
configPath := writeConfigFor(t, configDir, url)
|
|
|
|
|
code, stdout, stderr := runRecoveryCmd(t, "validate")
|
|
|
|
|
if !tc.wantOK {
|
|
|
|
|
if code == 0 {
|
|
|
|
|
t.Fatalf("validate exited 0 with no config at the resolved path %s: %s", configPath, stdout)
|
|
|
|
@@ -434,125 +219,28 @@ func TestRecoveryDefaultConfigResolvesWhereTheDaemonReads(t *testing.T) {
|
|
|
|
|
t.Fatalf("validate with no -config exited %d, so it did not read %s: %s", code, configPath, stderr)
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(stdout, "stored resources validate") {
|
|
|
|
|
t.Errorf("validate reported %q, want the verdict on the store seeded from the resolved config", stdout)
|
|
|
|
|
t.Errorf("validate reported %q, want the verdict on the database the resolved config names", stdout)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// validate exists to answer "would archied start against this file". Each case
|
|
|
|
|
// below is a state the daemon fails closed on, or a file it cannot read at all.
|
|
|
|
|
// validate exists to answer "would archied start against this database". A
|
|
|
|
|
// stored value that both gates refuse -- the write path stops it at Decode
|
|
|
|
|
// (workflow.ExecutionSettings.Validate rejects a negative limit) and boot stops
|
|
|
|
|
// on it too -- can only get here by bypassing the write path, which is exactly
|
|
|
|
|
// the state the operator has no path back from.
|
|
|
|
|
func TestRecoveryValidateRefusesWhatTheDaemonRefuses(t *testing.T) {
|
|
|
|
|
newStore := func(t *testing.T) string {
|
|
|
|
|
t.Helper()
|
|
|
|
|
db := filepath.Join(t.TempDir(), "archie.db-tasks.sqlite")
|
|
|
|
|
openTaskStore(t, db)
|
|
|
|
|
return db
|
|
|
|
|
configPath, st := recoveryFixture(t)
|
|
|
|
|
seedResource(t, st, controlplane.WorkflowExecutionSettingsKind,
|
|
|
|
|
`{"max_model_tool_steps":-1,"max_runtime_seconds":60,"max_consecutive_gate_failures":3}`, "hand-edited")
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "validate", "-config", configPath)
|
|
|
|
|
if code == 0 {
|
|
|
|
|
t.Fatal("validate accepted a stored resource the write path would refuse")
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(stderr, controlplane.WorkflowExecutionSettingsKind) {
|
|
|
|
|
t.Errorf("refusal must name the resource kind; stderr = %q", stderr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.Run("forward_migrated_schema", func(t *testing.T) {
|
|
|
|
|
// A database written by a newer release is the state a rollback has to
|
|
|
|
|
// recognise before it hands the file to an older binary.
|
|
|
|
|
db := newStore(t)
|
|
|
|
|
if err := execRaw(t, db, `PRAGMA user_version = 99`); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "validate", "-db", db)
|
|
|
|
|
if code == 0 {
|
|
|
|
|
t.Fatal("validate accepted a forward-migrated database")
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(stderr, "newer than supported version") {
|
|
|
|
|
t.Errorf("refusal must name the schema version; stderr = %q", stderr)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("corrupt_file", func(t *testing.T) {
|
|
|
|
|
// A real database header over pages that are not a database. The magic
|
|
|
|
|
// is right, so this fails on the content rather than on the format, which
|
|
|
|
|
// is the shape a half-written or truncated store has.
|
|
|
|
|
db := filepath.Join(t.TempDir(), "archie.db-tasks.sqlite")
|
|
|
|
|
corrupt := append([]byte("SQLite format 3\x00"), bytes.Repeat([]byte{0xff}, 4096)...)
|
|
|
|
|
if err := os.WriteFile(db, corrupt, 0o600); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "validate", "-db", db)
|
|
|
|
|
if code == 0 {
|
|
|
|
|
t.Fatal("validate accepted a corrupt database")
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(stderr, db) {
|
|
|
|
|
t.Errorf("refusal must name the file; stderr = %q", stderr)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("not_a_database", func(t *testing.T) {
|
|
|
|
|
// Pointing -db at the wrong file is the common operator mistake, and the
|
|
|
|
|
// configured db_path is a different file from the task database the
|
|
|
|
|
// State Store owns.
|
|
|
|
|
db := filepath.Join(t.TempDir(), "archie.db")
|
|
|
|
|
if err := os.WriteFile(db, []byte("db_path, not the task database"), 0o600); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "validate", "-db", db)
|
|
|
|
|
if code == 0 {
|
|
|
|
|
t.Fatal("validate accepted a file that is not a database")
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(stderr, db) {
|
|
|
|
|
t.Errorf("refusal must name the file; stderr = %q", stderr)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("stored_resource_that_will_not_validate", func(t *testing.T) {
|
|
|
|
|
// A stored value that both gates refuse: the write path stops it at
|
|
|
|
|
// Decode (workflow.ExecutionSettings.Validate rejects a negative limit),
|
|
|
|
|
// and boot stops on it too -- startWorkflowExecutionSettings decodes the
|
|
|
|
|
// same document and exits 1. It can only get here by bypassing the write
|
|
|
|
|
// path, which is exactly the state the operator has no path back from.
|
|
|
|
|
db := newStore(t)
|
|
|
|
|
seedResource(t, openTaskStore(t, db), controlplane.WorkflowExecutionSettingsKind,
|
|
|
|
|
`{"max_model_tool_steps":-1,"max_runtime_seconds":60,"max_consecutive_gate_failures":3}`, "hand-edited")
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "validate", "-db", db)
|
|
|
|
|
if code == 0 {
|
|
|
|
|
t.Fatal("validate accepted a stored resource the write path would refuse")
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(stderr, controlplane.WorkflowExecutionSettingsKind) {
|
|
|
|
|
t.Errorf("refusal must name the resource kind; stderr = %q", stderr)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("missing_database", func(t *testing.T) {
|
|
|
|
|
db := newStore(t)
|
|
|
|
|
if err := os.Remove(db); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "validate", "-db", db)
|
|
|
|
|
if code == 0 {
|
|
|
|
|
t.Fatal("validate accepted a database that does not exist")
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(stderr, db) {
|
|
|
|
|
t.Errorf("refusal must name the file; stderr = %q", stderr)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("store_without_the_control_plane_table", func(t *testing.T) {
|
|
|
|
|
// A store written before the control plane existed holds no stored
|
|
|
|
|
// settings. The serving process creates the table on its next start, so
|
|
|
|
|
// refusing this file would send an operator to restore a snapshot for a
|
|
|
|
|
// database that would have started perfectly well.
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
configPath := writeMinimalConfig(t, dir)
|
|
|
|
|
db := filepath.Join(dir, "archie.db-tasks.sqlite")
|
|
|
|
|
openTaskStore(t, db)
|
|
|
|
|
if err := execRaw(t, db, `DROP TABLE resources`); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
code, stdout, stderr := runRecoveryCmd(t, "validate", "-db", db, "-config", configPath)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("validate exited %d: %s", code, stderr)
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(stdout, "0 stored resources validate") {
|
|
|
|
|
t.Errorf("validate must report that there was nothing stored to check; stdout = %q", stdout)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// rollback is the one operation that closes the documented gap: with the State
|
|
|
|
@@ -560,13 +248,11 @@ func TestRecoveryValidateRefusesWhatTheDaemonRefuses(t *testing.T) {
|
|
|
|
|
// ordinary replace, so no rollback RPC is needed and the rollback itself is
|
|
|
|
|
// audited as one more revision.
|
|
|
|
|
func TestRecoveryRollbackReplaysTheRevisionThroughReplace(t *testing.T) {
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
db := filepath.Join(dir, "archie.db-tasks.sqlite")
|
|
|
|
|
st := openTaskStore(t, db)
|
|
|
|
|
configPath, st := recoveryFixture(t)
|
|
|
|
|
seedResource(t, st, controlplane.ModelRoleAssignmentsKind, `{"implement":"anthropic/claude"}`, "first")
|
|
|
|
|
seedResource(t, st, controlplane.ModelRoleAssignmentsKind, `{"implement":"openai/gpt"}`, "second")
|
|
|
|
|
|
|
|
|
|
code, stdout, stderr := runRecoveryCmd(t, "rollback", "-db", db, "-kind", controlplane.ModelRoleAssignmentsKind)
|
|
|
|
|
code, stdout, stderr := runRecoveryCmd(t, "rollback", "-config", configPath, "-kind", controlplane.ModelRoleAssignmentsKind)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("rollback exited %d: %s", code, stderr)
|
|
|
|
|
}
|
|
|
|
@@ -598,14 +284,12 @@ func TestRecoveryRollbackReplaysTheRevisionThroughReplace(t *testing.T) {
|
|
|
|
|
// The revision to restore may be named, which is how an operator reaches past
|
|
|
|
|
// the most recent change without replaying it first.
|
|
|
|
|
func TestRecoveryRollbackRestoresANamedRevision(t *testing.T) {
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
db := filepath.Join(dir, "archie.db-tasks.sqlite")
|
|
|
|
|
st := openTaskStore(t, db)
|
|
|
|
|
configPath, st := recoveryFixture(t)
|
|
|
|
|
seedResource(t, st, controlplane.ModelRoleAssignmentsKind, `{"implement":"anthropic/claude"}`, "first")
|
|
|
|
|
seedResource(t, st, controlplane.ModelRoleAssignmentsKind, `{"implement":"openai/gpt"}`, "second")
|
|
|
|
|
seedResource(t, st, controlplane.ModelRoleAssignmentsKind, `{"implement":"google/gemini"}`, "third")
|
|
|
|
|
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "rollback", "-db", db, "-kind", controlplane.ModelRoleAssignmentsKind, "-revision", "1")
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "rollback", "-config", configPath, "-kind", controlplane.ModelRoleAssignmentsKind, "-revision", "1")
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("rollback exited %d: %s", code, stderr)
|
|
|
|
|
}
|
|
|
|
@@ -619,10 +303,9 @@ func TestRecoveryRollbackRestoresANamedRevision(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRecoveryRollbackRefusesWhatItCannotReplay(t *testing.T) {
|
|
|
|
|
newStore := func(t *testing.T, revisions int) string {
|
|
|
|
|
newStore := func(t *testing.T, revisions int) (string, *pgstore.TaskDB) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
db := filepath.Join(t.TempDir(), "archie.db-tasks.sqlite")
|
|
|
|
|
st := openTaskStore(t, db)
|
|
|
|
|
configPath, st := recoveryFixture(t)
|
|
|
|
|
for i := range revisions {
|
|
|
|
|
value := `{"implement":"anthropic/claude"}`
|
|
|
|
|
if i > 0 {
|
|
|
|
@@ -630,12 +313,12 @@ func TestRecoveryRollbackRefusesWhatItCannotReplay(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
seedResource(t, st, controlplane.ModelRoleAssignmentsKind, value, "seed")
|
|
|
|
|
}
|
|
|
|
|
return db
|
|
|
|
|
return configPath, st
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.Run("unknown_kind", func(t *testing.T) {
|
|
|
|
|
db := newStore(t, 2)
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "rollback", "-db", db, "-kind", "not-a-resource")
|
|
|
|
|
configPath, _ := newStore(t, 2)
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "rollback", "-config", configPath, "-kind", "not-a-resource")
|
|
|
|
|
if code == 0 {
|
|
|
|
|
t.Fatal("rollback of an unknown resource kind succeeded")
|
|
|
|
|
}
|
|
|
|
@@ -645,8 +328,8 @@ func TestRecoveryRollbackRefusesWhatItCannotReplay(t *testing.T) {
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("no_earlier_revision", func(t *testing.T) {
|
|
|
|
|
db := newStore(t, 1)
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "rollback", "-db", db, "-kind", controlplane.ModelRoleAssignmentsKind)
|
|
|
|
|
configPath, _ := newStore(t, 1)
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "rollback", "-config", configPath, "-kind", controlplane.ModelRoleAssignmentsKind)
|
|
|
|
|
if code == 0 {
|
|
|
|
|
t.Fatal("rollback with no earlier revision succeeded")
|
|
|
|
|
}
|
|
|
|
@@ -656,8 +339,8 @@ func TestRecoveryRollbackRefusesWhatItCannotReplay(t *testing.T) {
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("current_revision", func(t *testing.T) {
|
|
|
|
|
db := newStore(t, 2)
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "rollback", "-db", db, "-kind", controlplane.ModelRoleAssignmentsKind, "-revision", "2")
|
|
|
|
|
configPath, _ := newStore(t, 2)
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "rollback", "-config", configPath, "-kind", controlplane.ModelRoleAssignmentsKind, "-revision", "2")
|
|
|
|
|
if code == 0 {
|
|
|
|
|
t.Fatal("rollback of the current revision succeeded")
|
|
|
|
|
}
|
|
|
|
@@ -667,14 +350,18 @@ func TestRecoveryRollbackRefusesWhatItCannotReplay(t *testing.T) {
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("live_state_store", func(t *testing.T) {
|
|
|
|
|
db := newStore(t, 2)
|
|
|
|
|
holdStoreOwnership(t, db)
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "rollback", "-db", db, "-kind", controlplane.ModelRoleAssignmentsKind)
|
|
|
|
|
configPath, st := newStore(t, 2)
|
|
|
|
|
claim, err := postgres.AcquireOwnership(t.Context(), st.Pool, postgres.OwnerStateStore)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
t.Cleanup(func() { _ = claim.Release(t.Context()) })
|
|
|
|
|
code, _, stderr := runRecoveryCmd(t, "rollback", "-config", configPath, "-kind", controlplane.ModelRoleAssignmentsKind)
|
|
|
|
|
if code == 0 {
|
|
|
|
|
t.Fatal("rollback against a live State Store succeeded")
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(stderr, "owned by another process") {
|
|
|
|
|
t.Errorf("refusal must say the database is owned by another process; stderr = %q", stderr)
|
|
|
|
|
if !strings.Contains(stderr, "stop the State Store") {
|
|
|
|
|
t.Errorf("refusal must say to stop the State Store; stderr = %q", stderr)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
@@ -683,7 +370,7 @@ func TestRecoveryRollbackRefusesWhatItCannotReplay(t *testing.T) {
|
|
|
|
|
// an operator who mistyped a command watching a server start instead.
|
|
|
|
|
func TestRecoveryRejectsAnUnknownSubcommand(t *testing.T) {
|
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
|
code := runRecovery([]string{"backp", "-db", "x"}, &stdout, &stderr)
|
|
|
|
|
code := runRecovery([]string{"backp", "-out", "x"}, &stdout, &stderr)
|
|
|
|
|
if code == 0 {
|
|
|
|
|
t.Fatal("an unknown subcommand exited 0")
|
|
|
|
|
}
|
|
|
|
@@ -696,7 +383,9 @@ func TestRecoveryRejectsAnUnknownSubcommand(t *testing.T) {
|
|
|
|
|
// destination, which only the daemon may bring into existence.
|
|
|
|
|
func writeConfigWithLogFile(t *testing.T, dir string) (string, string) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
configPath := writeMinimalConfig(t, dir)
|
|
|
|
|
url := pgtest.URL(t)
|
|
|
|
|
pgstore.PoolAt(t, url)
|
|
|
|
|
configPath := writeConfigFor(t, dir, url)
|
|
|
|
|
logFile := filepath.Join(dir, "logs", "archied.log")
|
|
|
|
|
f, err := os.OpenFile(configPath, os.O_APPEND|os.O_WRONLY, 0o600)
|
|
|
|
|
if err != nil {
|
|
|
|
@@ -725,7 +414,7 @@ func writeConfigWithLogFile(t *testing.T, dir string) (string, string) {
|
|
|
|
|
// registers the same set itself rather than an empty manager: the definitions
|
|
|
|
|
// are decoded with the vocabulary the served path validates against, which is
|
|
|
|
|
// what makes the seeded values the ones validate is asked about.
|
|
|
|
|
func seedStoreResources(t *testing.T, st *store.Store, configPath string) {
|
|
|
|
|
func seedStoreResources(t *testing.T, st *pgstore.TaskDB, configPath string) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
doc, err := configuration.New(nil).Resolve(configPath, "")
|
|
|
|
|
if err != nil {
|
|
|
|
@@ -772,7 +461,7 @@ func storedResourceCount(t *testing.T, stdout string) int {
|
|
|
|
|
// from the label and the revision it produces: the store deduplicates a
|
|
|
|
|
// repeated request ID, so a fixed one would make the second write of a series
|
|
|
|
|
// a silent no-op.
|
|
|
|
|
func seedResource(t *testing.T, st *store.Store, kind, value, label string) {
|
|
|
|
|
func seedResource(t *testing.T, st *pgstore.TaskDB, kind, value, label string) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
current, err := st.Resource(t.Context(), kind)
|
|
|
|
|
expected := int64(0)
|
|
|
|
@@ -787,23 +476,10 @@ func seedResource(t *testing.T, st *store.Store, kind, value, label string) {
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if _, err := st.PutResource(t.Context(), store.ResourceWrite{
|
|
|
|
|
if _, err := st.PutResource(t.Context(), storecontract.ResourceWrite{
|
|
|
|
|
Kind: kind, Value: encoded, Actor: label, Source: "test",
|
|
|
|
|
RequestID: fmt.Sprintf("%s-%d", label, expected+1), ExpectedVersion: expected,
|
|
|
|
|
}); err != nil {
|
|
|
|
|
t.Fatalf("seed resource %s: %v", kind, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// execRaw runs one statement against the database file the way an operator
|
|
|
|
|
// editing a broken store would, bypassing every check the store applies.
|
|
|
|
|
func execRaw(t *testing.T, path, statement string) error {
|
|
|
|
|
t.Helper()
|
|
|
|
|
db, err := sql.Open("sqlite", path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer func() { _ = db.Close() }()
|
|
|
|
|
_, err = db.ExecContext(t.Context(), statement)
|
|
|
|
|
return err
|
|
|
|
|
}
|