| 1 | package team |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "errors" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "testing" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| 14 | func sharedFixture(t *testing.T) (*env, *sql.DB) { |
| 15 | t.Helper() |
| 16 | e := newEnv(t) |
| 17 | must(t, e.store.Close()) |
| 18 | e.store = nil |
| 19 | writer, err := sql.Open("sqlite", e.path+"?_pragma=foreign_keys(1)") |
| 20 | must(t, err) |
| 21 | writer.SetMaxOpenConns(1) |
| 22 | t.Cleanup(func() { writer.Close() }) |
| 23 | return e, writer |
| 24 | } |
| 25 | |
| 26 | func TestExecutionSharedValidation(t *testing.T) { |
| 27 | for _, scenario := range []string{"pool", "unlimited", "missing schema", "old schema", "new schema", "missing table", "missing column", "missing index", "wrong path", "missing dependencies"} { |
| 28 | t.Run(scenario, func(t *testing.T) { |
| 29 | e, writer := sharedFixture(t) |
| 30 | path, opts := e.path, e.options() |
| 31 | switch scenario { |
| 32 | case "pool": |
| 33 | writer.SetMaxOpenConns(2) |
| 34 | case "unlimited": |
| 35 | writer.SetMaxOpenConns(0) |
| 36 | case "missing schema": |
| 37 | _, err := writer.Exec(`DROP TABLE service`) |
| 38 | must(t, err) |
| 39 | case "old schema": |
| 40 | _, err := writer.Exec(`UPDATE service SET schema_version=2`) |
| 41 | must(t, err) |
| 42 | case "new schema": |
| 43 | _, err := writer.Exec(`UPDATE service SET schema_version=99`) |
| 44 | must(t, err) |
| 45 | case "missing table": |
| 46 | _, err := writer.Exec(`DROP TABLE artifacts`) |
| 47 | must(t, err) |
| 48 | case "missing column": |
| 49 | _, err := writer.Exec(`ALTER TABLE artifacts DROP COLUMN payload`) |
| 50 | must(t, err) |
| 51 | case "missing index": |
| 52 | _, err := writer.Exec(`DROP INDEX attempts_one_live`) |
| 53 | must(t, err) |
| 54 | case "wrong path": |
| 55 | path = filepath.Join(e.dir, "other.db") |
| 56 | case "missing dependencies": |
| 57 | opts.Authority = nil |
| 58 | } |
| 59 | before, err := os.ReadFile(e.path + ".incarnation") |
| 60 | must(t, err) |
| 61 | s, err := OpenShared(e.ctx, writer, path, opts) |
| 62 | if err == nil { |
| 63 | s.Close() |
| 64 | t.Fatal("invalid attachment accepted") |
| 65 | } |
| 66 | must(t, writer.Ping()) |
| 67 | after, err := os.ReadFile(e.path + ".incarnation") |
| 68 | must(t, err) |
| 69 | if string(before) != string(after) { |
| 70 | t.Fatal("failed attachment changed witness") |
| 71 | } |
| 72 | if scenario == "old schema" || scenario == "new schema" { |
| 73 | var version int |
| 74 | must(t, writer.QueryRow(`SELECT schema_version FROM service`).Scan(&version)) |
| 75 | if (scenario == "old schema" && version != 2) || (scenario == "new schema" && version != 99) { |
| 76 | t.Fatal("attachment migrated schema") |
| 77 | } |
| 78 | } |
| 79 | }) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func TestExecutionSharedCommitFailure(t *testing.T) { |
| 84 | for _, stage := range []string{"before_commit", "after_commit", "commit_error", "witness"} { |
| 85 | t.Run(stage, func(t *testing.T) { |
| 86 | e, writer := sharedFixture(t) |
| 87 | s, err := OpenShared(e.ctx, writer, e.path, e.options()) |
| 88 | must(t, err) |
| 89 | var before int64 |
| 90 | must(t, writer.QueryRow(`SELECT revision FROM service`).Scan(&before)) |
| 91 | injected := errors.New("injected commit failure") |
| 92 | if stage == "witness" { |
| 93 | must(t, os.Mkdir(s.marker+".tmp", 0700)) |
| 94 | } else { |
| 95 | s.commitHook = func(at string) error { |
| 96 | if at == stage { |
| 97 | return injected |
| 98 | } |
| 99 | return nil |
| 100 | } |
| 101 | } |
| 102 | err = s.WithTransaction(e.ctx, func(tx *sql.Tx) error { |
| 103 | if stage == "commit_error" { |
| 104 | _, err := tx.Exec(`PRAGMA defer_foreign_keys=ON; INSERT INTO events VALUES (999,1,'test','test',x'01','')`) |
| 105 | return err |
| 106 | } |
| 107 | _, err := tx.Exec(`UPDATE service SET quarantine_why='committed payload'`) |
| 108 | return err |
| 109 | }) |
| 110 | if err == nil { |
| 111 | t.Fatal("commit failure ignored") |
| 112 | } |
| 113 | s.commitHook = nil |
| 114 | if stage == "witness" { |
| 115 | must(t, os.Remove(s.marker+".tmp")) |
| 116 | } |
| 117 | var after int64 |
| 118 | must(t, writer.QueryRow(`SELECT revision FROM service`).Scan(&after)) |
| 119 | want := before |
| 120 | if stage == "after_commit" || stage == "witness" { |
| 121 | want++ |
| 122 | } |
| 123 | if after != want { |
| 124 | t.Fatalf("revision = %d, want %d", after, want) |
| 125 | } |
| 126 | if stage == "before_commit" { |
| 127 | must(t, s.Close()) |
| 128 | return |
| 129 | } |
| 130 | |
| 131 | // Repairing the witness must not revive a handle whose commit failed. |
| 132 | var incarnation int64 |
| 133 | must(t, writer.QueryRow(`SELECT incarnation FROM service`).Scan(&incarnation)) |
| 134 | must(t, writeMarker(s.marker, incarnation, after, false)) |
| 135 | wantCode(t, s.WithTransaction(e.ctx, func(*sql.Tx) error { t.Fatal("poisoned callback ran"); return nil }), CodeStaleStore) |
| 136 | wantCode(t, s.Close(), CodeStaleStore) |
| 137 | must(t, writer.Ping()) |
| 138 | reopened, err := OpenShared(e.ctx, writer, e.path, e.options()) |
| 139 | must(t, err) |
| 140 | rr, err := reopened.Resume(e.ctx) |
| 141 | must(t, err) |
| 142 | if !rr.Resumed || rr.Quarantine { |
| 143 | t.Fatalf("gap did not recover: %+v", rr) |
| 144 | } |
| 145 | must(t, reopened.Close()) |
| 146 | }) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | func TestExecutionSharedCrashHelper(t *testing.T) { |
| 151 | path := os.Getenv("EQUIPO_SHARED_CRASH_PATH") |
| 152 | if path == "" { |
| 153 | return |
| 154 | } |
| 155 | writer, err := sql.Open("sqlite", path) |
| 156 | must(t, err) |
| 157 | writer.SetMaxOpenConns(1) |
| 158 | clock := NewFakeClock(time.Now()) |
| 159 | s, err := OpenShared(context.Background(), writer, path, Options{Clock: clock, Authority: NewFakeAuthority(clock, time.Minute)}) |
| 160 | must(t, err) |
| 161 | s.commitHook = func(stage string) error { |
| 162 | if stage == "after_commit" { |
| 163 | os.Exit(73) |
| 164 | } |
| 165 | return nil |
| 166 | } |
| 167 | must(t, s.WithTransaction(context.Background(), func(tx *sql.Tx) error { |
| 168 | _, err := tx.Exec(`INSERT INTO period_budgets(account,revision,config) VALUES ('crash-payload', 1, '{}')`) |
| 169 | return err |
| 170 | })) |
| 171 | t.Fatal("crash boundary not reached") |
| 172 | } |
| 173 | |
| 174 | func TestExecutionSharedCrashRecovery(t *testing.T) { |
| 175 | e, writer := sharedFixture(t) |
| 176 | exe, err := os.Executable() |
| 177 | must(t, err) |
| 178 | ctx, cancel := context.WithTimeout(e.ctx, 10*time.Second) |
| 179 | defer cancel() |
| 180 | cmd := exec.CommandContext(ctx, exe, "-test.run=^TestExecutionSharedCrashHelper$") |
| 181 | cmd.Env = append(os.Environ(), "EQUIPO_SHARED_CRASH_PATH="+e.path) |
| 182 | out, err := cmd.CombinedOutput() |
| 183 | var exit *exec.ExitError |
| 184 | if !errors.As(err, &exit) || exit.ExitCode() != 73 { |
| 185 | t.Fatalf("crash helper: %v\n%s", err, out) |
| 186 | } |
| 187 | s, err := OpenShared(e.ctx, writer, e.path, e.options()) |
| 188 | must(t, err) |
| 189 | var n int |
| 190 | must(t, writer.QueryRow(`SELECT count(*) FROM period_budgets WHERE account='crash-payload'`).Scan(&n)) |
| 191 | if n != 1 { |
| 192 | t.Fatal("SQL commit lost across witness gap") |
| 193 | } |
| 194 | rr, err := s.Resume(e.ctx) |
| 195 | must(t, err) |
| 196 | if !rr.Resumed || rr.Quarantine { |
| 197 | t.Fatalf("crash treated as restored history: %+v", rr) |
| 198 | } |
| 199 | must(t, s.Close()) |
| 200 | must(t, writer.Ping()) |
| 201 | } |
| 202 | |
| 203 | func TestExecutionSharedClosedHandle(t *testing.T) { |
| 204 | standalone := newEnv(t) |
| 205 | owned := standalone.store.db |
| 206 | must(t, standalone.store.Close()) |
| 207 | standalone.store = nil |
| 208 | if err := owned.Ping(); err == nil { |
| 209 | t.Fatal("standalone close left owned pool open") |
| 210 | } |
| 211 | |
| 212 | e, writer := sharedFixture(t) |
| 213 | s, err := OpenShared(e.ctx, writer, e.path, e.options()) |
| 214 | must(t, err) |
| 215 | must(t, s.Close()) |
| 216 | wantCode(t, s.WithTransaction(e.ctx, func(*sql.Tx) error { t.Fatal("closed shared handle ran a write"); return nil }), CodeStaleStore) |
| 217 | must(t, writer.Ping()) |
| 218 | } |
| 219 | |
| 220 | func TestExecutionSharedGuard(t *testing.T) { |
| 221 | e, writer := sharedFixture(t) |
| 222 | s, err := OpenShared(e.ctx, writer, e.path, e.options()) |
| 223 | must(t, err) |
| 224 | committed, release, done := make(chan struct{}), make(chan struct{}), make(chan error, 1) |
| 225 | s.commitHook = func(stage string) error { |
| 226 | if stage == "after_commit" { |
| 227 | close(committed) |
| 228 | <-release |
| 229 | } |
| 230 | return nil |
| 231 | } |
| 232 | go func() { done <- s.WithTransaction(e.ctx, func(*sql.Tx) error { return nil }) }() |
| 233 | <-committed |
| 234 | ctx, cancel := context.WithTimeout(e.ctx, 50*time.Millisecond) |
| 235 | replacement, err := OpenShared(ctx, writer, e.path, e.options()) |
| 236 | cancel() |
| 237 | close(release) |
| 238 | must(t, <-done) |
| 239 | s.commitHook = nil |
| 240 | if replacement != nil { |
| 241 | replacement.Close() |
| 242 | t.Fatal("replacement entered commit/witness gap") |
| 243 | } |
| 244 | if !errors.Is(err, context.DeadlineExceeded) { |
| 245 | t.Fatalf("replacement did not wait for guard: %v", err) |
| 246 | } |
| 247 | replacement, err = OpenShared(e.ctx, writer, e.path, e.options()) |
| 248 | must(t, err) |
| 249 | wantCode(t, s.WithTransaction(e.ctx, func(*sql.Tx) error { t.Fatal("stale shared handle ran"); return nil }), CodeStaleStore) |
| 250 | must(t, replacement.Close()) |
| 251 | } |
| 252 | |