| 1 | package hostedctl |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "errors" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/isolatedjob" |
| 11 | ) |
| 12 | |
| 13 | // opRow is one resume_ops row. |
| 14 | type opRow struct { |
| 15 | id, status string |
| 16 | prepared int64 |
| 17 | result sql.NullInt64 |
| 18 | } |
| 19 | |
| 20 | func (e *launchEnv) resumeOps(attempt string) []opRow { |
| 21 | e.t.Helper() |
| 22 | var out []opRow |
| 23 | if err := e.store.Tx(e.ctx, func(tx *sql.Tx) error { |
| 24 | out = out[:0] |
| 25 | rows, err := tx.QueryContext(e.ctx, `SELECT operation_id, status, prepared_epoch, result_epoch FROM resume_ops |
| 26 | WHERE attempt_id = ? ORDER BY prepared_t, operation_id`, attempt) |
| 27 | if err != nil { |
| 28 | return err |
| 29 | } |
| 30 | defer rows.Close() |
| 31 | for rows.Next() { |
| 32 | var r opRow |
| 33 | if err := rows.Scan(&r.id, &r.status, &r.prepared, &r.result); err != nil { |
| 34 | return err |
| 35 | } |
| 36 | out = append(out, r) |
| 37 | } |
| 38 | return rows.Err() |
| 39 | }); err != nil { |
| 40 | e.t.Fatalf("read resume operations of %s = %v", attempt, err) |
| 41 | } |
| 42 | return out |
| 43 | } |
| 44 | |
| 45 | func (e *launchEnv) resume(op string) (int64, error) { |
| 46 | e.t.Helper() |
| 47 | return e.c.Resume(e.ctx, "inst-1", instKey, "att-1", op) |
| 48 | } |
| 49 | |
| 50 | // resumeDone wants Resume(op) to return epoch with no error. |
| 51 | func (e *launchEnv) resumeDone(op string, epoch int64) { |
| 52 | e.t.Helper() |
| 53 | if got, err := e.resume(op); err != nil || got != epoch { |
| 54 | e.t.Fatalf("Resume(%s) = (%d, %v), want (%d, nil)", op, got, err, epoch) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // wantResume checks the job's state and epoch, the one pending or settled |
| 59 | // row, and how many provider resumes were made. |
| 60 | func (e *launchEnv) wantResume(state string, epoch int64, ops []opRow, calls int) { |
| 61 | e.t.Helper() |
| 62 | p := e.pauseCols("att-1") |
| 63 | got := e.resumeOps("att-1") |
| 64 | n := e.fake.Calls(isolatedjob.OpResume) |
| 65 | ok := p.state == state && p.epoch == epoch && len(got) == len(ops) && n == calls |
| 66 | for i := 0; ok && i < len(ops); i++ { |
| 67 | ok = got[i].id == ops[i].id && got[i].status == ops[i].status && got[i].prepared == ops[i].prepared && |
| 68 | got[i].result == ops[i].result |
| 69 | } |
| 70 | if !ok { |
| 71 | e.t.Fatalf("job %q at epoch %d, ops %+v, %d provider resumes; want %q at %d, %+v, %d", |
| 72 | p.state, p.epoch, got, n, state, epoch, ops, calls) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func pending(id string, prepared int64) opRow { |
| 77 | return opRow{id: id, status: "pending", prepared: prepared} |
| 78 | } |
| 79 | |
| 80 | func done(id string, epoch int64) opRow { |
| 81 | return opRow{id: id, status: "done", prepared: epoch, result: sql.NullInt64{Int64: epoch, Valid: true}} |
| 82 | } |
| 83 | |
| 84 | // resumeProbe wraps the fake provider's Resume with a hook before the call |
| 85 | // (a non-nil error from it is returned without calling the provider) and one |
| 86 | // after it. |
| 87 | type resumeProbe struct { |
| 88 | *isolatedjob.Fake |
| 89 | before func() error |
| 90 | after func() |
| 91 | } |
| 92 | |
| 93 | func (p *resumeProbe) Resume(ctx context.Context, ref isolatedjob.Ref) error { |
| 94 | if p.before != nil { |
| 95 | if err := p.before(); err != nil { |
| 96 | return err |
| 97 | } |
| 98 | } |
| 99 | err := p.Fake.Resume(ctx, ref) |
| 100 | if p.after != nil { |
| 101 | p.after() |
| 102 | } |
| 103 | return err |
| 104 | } |
| 105 | |
| 106 | // probe puts p in front of the fake provider for a new controller. |
| 107 | func (e *launchEnv) probe(p *resumeProbe) { |
| 108 | e.t.Helper() |
| 109 | p.Fake = e.fake |
| 110 | e.run = p |
| 111 | e.newController() |
| 112 | } |
| 113 | |
| 114 | // restart is a controller restart over the same fake provider, with no |
| 115 | // probe: Close (already done by the crash) is followed by Open and New. |
| 116 | func (e *launchEnv) restart() { |
| 117 | e.t.Helper() |
| 118 | e.run = e.fake |
| 119 | e.up() |
| 120 | } |
| 121 | |
| 122 | func TestResumePreparesEpochBeforeProvider(t *testing.T) { |
| 123 | t.Parallel() |
| 124 | e := runningJob(t) |
| 125 | e.suspendJob() |
| 126 | type seen struct { |
| 127 | state string |
| 128 | epoch int64 |
| 129 | status sql.NullString |
| 130 | prep sql.NullInt64 |
| 131 | err error |
| 132 | } |
| 133 | var at []seen |
| 134 | e.probe(&resumeProbe{before: func() error { |
| 135 | // A transaction held across the call would make this read wait; |
| 136 | // the deadline turns that into an error. |
| 137 | rctx, cancel := context.WithTimeout(e.ctx, 2*time.Second) |
| 138 | defer cancel() |
| 139 | var s seen |
| 140 | s.err = e.store.Tx(rctx, func(tx *sql.Tx) error { |
| 141 | if err := tx.QueryRowContext(rctx, `SELECT state, epoch FROM jobs WHERE attempt_id = 'att-1'`).Scan(&s.state, &s.epoch); err != nil { |
| 142 | return err |
| 143 | } |
| 144 | err := tx.QueryRowContext(rctx, `SELECT status, prepared_epoch FROM resume_ops |
| 145 | WHERE attempt_id = 'att-1' AND operation_id = 'op-1'`).Scan(&s.status, &s.prep) |
| 146 | if errors.Is(err, sql.ErrNoRows) { |
| 147 | return nil |
| 148 | } |
| 149 | return err |
| 150 | }) |
| 151 | at = append(at, s) |
| 152 | return nil |
| 153 | }}) |
| 154 | if _, err := e.resume("op-1"); !errors.Is(err, ErrResumePending) { |
| 155 | t.Fatalf("Resume = %v, want ErrResumePending while the VM still reports suspended", err) |
| 156 | } |
| 157 | if len(at) != 1 { |
| 158 | t.Fatalf("provider resumes = %d, want 1", len(at)) |
| 159 | } |
| 160 | s := at[0] |
| 161 | if s.err != nil || s.state != JobResuming || s.epoch != 0 || s.status.String != "pending" || s.prep.Int64 != 1 { |
| 162 | t.Fatalf("store at the provider call: %+v; want resuming at epoch 0 with op-1 pending at epoch 1", s) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | func TestResumeCrashBeforeProvider(t *testing.T) { |
| 167 | t.Parallel() |
| 168 | e := runningJob(t) |
| 169 | e.suspendJob() |
| 170 | e.probe(&resumeProbe{before: func() error { |
| 171 | if err := e.store.Close(); err != nil { |
| 172 | t.Errorf("Close = %v", err) |
| 173 | } |
| 174 | return errors.New("the controller died before the call") |
| 175 | }}) |
| 176 | if _, err := e.resume("op-1"); err == nil { |
| 177 | t.Fatal("Resume across a crash = nil error") |
| 178 | } |
| 179 | e.restart() |
| 180 | e.wantResume(JobResuming, 0, []opRow{pending("op-1", 1)}, 0) |
| 181 | |
| 182 | e.refresh() // the first pass after the restart only re-bases the job's times |
| 183 | e.wantResume(JobResuming, 0, []opRow{pending("op-1", 1)}, 0) |
| 184 | e.refresh() // the VM is suspended: the resume is sent |
| 185 | e.wantResume(JobResuming, 0, []opRow{pending("op-1", 1)}, 1) |
| 186 | e.fake.Tick() |
| 187 | e.refresh() // the VM runs: commit |
| 188 | e.wantResume(JobRunning, 1, []opRow{done("op-1", 1)}, 1) |
| 189 | e.resumeDone("op-1", 1) |
| 190 | e.refresh() |
| 191 | e.wantResume(JobRunning, 1, []opRow{done("op-1", 1)}, 1) |
| 192 | } |
| 193 | |
| 194 | func TestResumeCrashAfterProvider(t *testing.T) { |
| 195 | t.Parallel() |
| 196 | e := runningJob(t) |
| 197 | e.suspendJob() |
| 198 | e.probe(&resumeProbe{after: func() { |
| 199 | e.fake.Tick() // the VM runs before the controller can commit |
| 200 | if err := e.store.Close(); err != nil { |
| 201 | t.Errorf("Close = %v", err) |
| 202 | } |
| 203 | }}) |
| 204 | if _, err := e.resume("op-1"); err == nil { |
| 205 | t.Fatal("Resume across a crash = nil error") |
| 206 | } |
| 207 | e.restart() |
| 208 | e.wantResume(JobResuming, 0, []opRow{pending("op-1", 1)}, 1) |
| 209 | |
| 210 | // Until the first Refresh re-bases the job's times, a retry waits, and |
| 211 | // that Refresh drives no resume (the VM already runs: no commit yet). |
| 212 | if _, err := e.resume("op-1"); !errors.Is(err, ErrResumePending) { |
| 213 | t.Fatalf("Resume before the first refresh after a restart = %v, want ErrResumePending", err) |
| 214 | } |
| 215 | e.refresh() |
| 216 | e.wantResume(JobResuming, 0, []opRow{pending("op-1", 1)}, 1) |
| 217 | |
| 218 | // The client retries: a no-op provider resume, then the commit. |
| 219 | e.resumeDone("op-1", 1) |
| 220 | e.wantResume(JobRunning, 1, []opRow{done("op-1", 1)}, 2) |
| 221 | e.refresh() |
| 222 | e.resumeDone("op-1", 1) |
| 223 | e.wantResume(JobRunning, 1, []opRow{done("op-1", 1)}, 2) |
| 224 | if got := e.fake.LiveByAttempt()["att-1"]; got != 1 { |
| 225 | t.Fatalf("live VMs for att-1 = %d, want 1", got) |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | func TestResumeLostReply(t *testing.T) { |
| 230 | t.Parallel() |
| 231 | e := runningJob(t) |
| 232 | e.suspendJob() |
| 233 | e.fake.Inject(isolatedjob.OpResume, isolatedjob.FaultLostReply) |
| 234 | _, err := e.resume("op-1") |
| 235 | if !errors.Is(err, ErrResumePending) || !errors.Is(err, isolatedjob.ErrLostReply) { |
| 236 | t.Fatalf("Resume with a lost reply = %v, want ErrResumePending and the lost reply", err) |
| 237 | } |
| 238 | e.wantResume(JobResuming, 0, []opRow{pending("op-1", 1)}, 1) |
| 239 | |
| 240 | // The resume took effect but the VM still reports suspended: neither a |
| 241 | // retry nor Refresh may commit yet. |
| 242 | if ep, err := e.resume("op-1"); !errors.Is(err, ErrResumePending) || ep != 0 { |
| 243 | t.Fatalf("retry while the VM is suspended = (%d, %v), want (0, ErrResumePending)", ep, err) |
| 244 | } |
| 245 | e.wantResume(JobResuming, 0, []opRow{pending("op-1", 1)}, 2) |
| 246 | e.refresh() |
| 247 | e.wantResume(JobResuming, 0, []opRow{pending("op-1", 1)}, 3) |
| 248 | |
| 249 | e.fake.Tick() |
| 250 | e.resumeDone("op-1", 1) |
| 251 | e.wantResume(JobRunning, 1, []opRow{done("op-1", 1)}, 4) |
| 252 | e.refresh() |
| 253 | e.resumeDone("op-1", 1) |
| 254 | e.wantResume(JobRunning, 1, []opRow{done("op-1", 1)}, 4) |
| 255 | } |
| 256 | |
| 257 | func TestResumeRetrySameOperation(t *testing.T) { |
| 258 | t.Parallel() |
| 259 | e := runningJob(t) |
| 260 | e.suspendJob() |
| 261 | for i := 0; i < 2; i++ { |
| 262 | if ep, err := e.resume("op-1"); !errors.Is(err, ErrResumePending) || ep != 0 { |
| 263 | t.Fatalf("Resume %d while suspended = (%d, %v), want (0, ErrResumePending)", i+1, ep, err) |
| 264 | } |
| 265 | } |
| 266 | e.wantResume(JobResuming, 0, []opRow{pending("op-1", 1)}, 2) |
| 267 | e.fake.Tick() |
| 268 | e.resumeDone("op-1", 1) |
| 269 | for i := 0; i < 3; i++ { |
| 270 | e.resumeDone("op-1", 1) |
| 271 | e.refresh() |
| 272 | } |
| 273 | e.wantResume(JobRunning, 1, []opRow{done("op-1", 1)}, 3) |
| 274 | if !e.hasAudit("inst-1", "att-1", actionResumeRepeated, "op-1") { |
| 275 | t.Fatal("no resume_repeated audit row for a retry of a done operation") |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | func TestResumeOperationsRemembered(t *testing.T) { |
| 280 | t.Parallel() |
| 281 | e := runningJob(t) |
| 282 | e.suspendJob() |
| 283 | if _, err := e.resume("op-a"); !errors.Is(err, ErrResumePending) { |
| 284 | t.Fatalf("Resume(op-a) = %v, want ErrResumePending", err) |
| 285 | } |
| 286 | e.fake.Tick() |
| 287 | e.resumeDone("op-a", 1) |
| 288 | |
| 289 | e.clk.AdvanceMono(time.Minute) // op-b is the latest operation by time |
| 290 | e.suspendJob() |
| 291 | if _, err := e.resume("op-b"); !errors.Is(err, ErrResumePending) { |
| 292 | t.Fatalf("Resume(op-b) = %v, want ErrResumePending", err) |
| 293 | } |
| 294 | e.fake.Tick() |
| 295 | e.resumeDone("op-b", 2) |
| 296 | calls := e.fake.Calls(isolatedjob.OpResume) |
| 297 | |
| 298 | e.resumeDone("op-a", 1) |
| 299 | e.resumeDone("op-b", 2) |
| 300 | e.wantResume(JobRunning, 2, []opRow{done("op-a", 1), done("op-b", 2)}, calls) |
| 301 | } |
| 302 | |
| 303 | func TestResumeOtherOperationWhilePending(t *testing.T) { |
| 304 | t.Parallel() |
| 305 | e := runningJob(t) |
| 306 | e.suspendJob() |
| 307 | if _, err := e.resume("op-a"); !errors.Is(err, ErrResumePending) { |
| 308 | t.Fatalf("Resume(op-a) = %v, want ErrResumePending", err) |
| 309 | } |
| 310 | if ep, err := e.resume("op-b"); !errors.Is(err, ErrResumePending) || ep != 0 { |
| 311 | t.Fatalf("Resume(op-b) while op-a is pending = (%d, %v), want (0, ErrResumePending)", ep, err) |
| 312 | } |
| 313 | e.wantResume(JobResuming, 0, []opRow{pending("op-a", 1)}, 1) |
| 314 | e.fake.Tick() |
| 315 | e.resumeDone("op-a", 1) |
| 316 | if _, err := e.resume("op-b"); !errors.Is(err, isolatedjob.ErrConflict) { |
| 317 | t.Fatalf("Resume(op-b) of a running job = %v, want ErrConflict", err) |
| 318 | } |
| 319 | e.wantResume(JobRunning, 1, []opRow{done("op-a", 1)}, 2) |
| 320 | |
| 321 | for _, bad := range []string{"", "op 1", "op_1", "op/1", string(make([]byte, 65))} { |
| 322 | if _, err := e.resume(bad); !errors.Is(err, isolatedjob.ErrInvalid) { |
| 323 | t.Fatalf("Resume(%q) = %v, want ErrInvalid", bad, err) |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | func TestResumePastDeadlineRefused(t *testing.T) { |
| 329 | t.Parallel() |
| 330 | e := runningJob(t) |
| 331 | e.suspendJob() |
| 332 | stop := e.stopCols("att-1").stop |
| 333 | e.clk.AdvanceMono(time.Duration(T(stop) - e.store.Clock().Now())) |
| 334 | if _, err := e.resume("op-1"); !errors.Is(err, ErrPastDeadline) { |
| 335 | t.Fatalf("Resume at the stop time = %v, want ErrPastDeadline", err) |
| 336 | } |
| 337 | e.wantResume(JobSuspended, 0, nil, 0) |
| 338 | if a := e.lastAudit(); a.Action != actionResumeRefused || a.Actor != "inst-1" { |
| 339 | t.Fatalf("audit = %+v, want resume_refused by inst-1", a) |
| 340 | } |
| 341 | |
| 342 | // A pending operation is failed past the stop time, with no further |
| 343 | // provider call; a retry of it is refused from then on. |
| 344 | e2 := runningJob(t) |
| 345 | e2.suspendJob() |
| 346 | e2.fake.Inject(isolatedjob.OpResume, isolatedjob.FaultThrottle) |
| 347 | if _, err := e2.resume("op-1"); !errors.Is(err, ErrResumePending) { |
| 348 | t.Fatalf("throttled Resume = %v, want ErrResumePending", err) |
| 349 | } |
| 350 | stop = e2.stopCols("att-1").stop |
| 351 | e2.clk.AdvanceMono(time.Duration(T(stop)-e2.store.Clock().Now()) + time.Second) |
| 352 | if _, err := e2.resume("op-1"); !errors.Is(err, ErrPastDeadline) { |
| 353 | t.Fatalf("retry of a pending Resume past the stop time = %v, want ErrPastDeadline", err) |
| 354 | } |
| 355 | failed := opRow{id: "op-1", status: "failed", prepared: 1} |
| 356 | e2.wantResume(JobResuming, 0, []opRow{failed}, 1) |
| 357 | if _, err := e2.resume("op-1"); !errors.Is(err, isolatedjob.ErrConflict) { |
| 358 | t.Fatalf("retry of a failed operation = %v, want ErrConflict", err) |
| 359 | } |
| 360 | e2.wantResume(JobResuming, 0, []opRow{failed}, 1) |
| 361 | |
| 362 | // Admission does not reopen past the stop time either. |
| 363 | e3 := runningJob(t) |
| 364 | g := e3.drain() |
| 365 | stop = e3.stopCols("att-1").stop |
| 366 | e3.clk.AdvanceMono(time.Duration(T(stop) - e3.store.Clock().Now())) |
| 367 | if err := e3.c.Reopen(e3.ctx, "inst-1", instKey, "att-1", g); !errors.Is(err, ErrPastDeadline) { |
| 368 | t.Fatalf("Reopen at the stop time = %v, want ErrPastDeadline", err) |
| 369 | } |
| 370 | if p := e3.pauseCols("att-1"); p.state != JobDraining { |
| 371 | t.Fatalf("after a refused Reopen the job is %q, want draining", p.state) |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | func TestResumeRefreshPastDeadline(t *testing.T) { |
| 376 | t.Parallel() |
| 377 | e := runningJob(t) |
| 378 | e.suspendJob() |
| 379 | e.fake.Inject(isolatedjob.OpResume, isolatedjob.FaultThrottle) |
| 380 | if _, err := e.resume("op-1"); !errors.Is(err, ErrResumePending) { |
| 381 | t.Fatalf("throttled Resume = %v, want ErrResumePending", err) |
| 382 | } |
| 383 | stop := e.stopCols("att-1").stop |
| 384 | e.clk.AdvanceMono(time.Duration(T(stop) - e.store.Clock().Now())) |
| 385 | e.refresh() |
| 386 | e.wantResume(JobSuspended, 0, []opRow{{id: "op-1", status: "failed", prepared: 1}}, 1) |
| 387 | e.fake.Tick() |
| 388 | e.refresh() |
| 389 | e.wantResume(JobSuspended, 0, []opRow{{id: "op-1", status: "failed", prepared: 1}}, 1) |
| 390 | } |
| 391 | |
| 392 | func TestResumeRestartRebasesFirst(t *testing.T) { |
| 393 | t.Parallel() |
| 394 | e := runningJob(t) |
| 395 | e.suspendJob() |
| 396 | e.probe(&resumeProbe{after: func() { |
| 397 | e.fake.Tick() // the VM runs before the controller can commit |
| 398 | if err := e.store.Close(); err != nil { |
| 399 | t.Errorf("Close = %v", err) |
| 400 | } |
| 401 | }}) |
| 402 | if _, err := e.resume("op-1"); err == nil { |
| 403 | t.Fatal("Resume across a crash = nil error") |
| 404 | } |
| 405 | // Down for 2 h with the wall clock stopped: the restored controller |
| 406 | // time knows nothing of it, the provider's age does. |
| 407 | e.clk.AdvanceMono(2 * time.Hour) |
| 408 | e.restart() |
| 409 | before := e.stopCols("att-1") |
| 410 | |
| 411 | e.refresh() // re-base only: no commit against the stale stop time |
| 412 | e.wantResume(JobResuming, 0, []opRow{pending("op-1", 1)}, 1) |
| 413 | if after := e.stopCols("att-1"); after.stop >= before.stop || T(after.stop) > e.store.Clock().Now() { |
| 414 | t.Fatalf("stop time %d after the first refresh (was %d, now %d); want it re-based into the past", |
| 415 | after.stop, before.stop, e.store.Clock().Now()) |
| 416 | } |
| 417 | e.refresh() // past the re-based stop time: the operation fails |
| 418 | e.wantResume(JobRunning, 0, []opRow{{id: "op-1", status: "failed", prepared: 1}}, 1) |
| 419 | } |
| 420 | |
| 421 | func TestResumeDeadlineDuringProviderCall(t *testing.T) { |
| 422 | t.Parallel() |
| 423 | e := runningJob(t) |
| 424 | e.suspendJob() |
| 425 | e.probe(&resumeProbe{after: func() { |
| 426 | stop := e.stopCols("att-1").stop |
| 427 | e.clk.AdvanceMono(time.Duration(T(stop) - e.store.Clock().Now())) |
| 428 | e.fake.Tick() // the VM runs, but only once the stop time has come |
| 429 | }}) |
| 430 | if ep, err := e.resume("op-1"); !errors.Is(err, ErrPastDeadline) || ep != 0 { |
| 431 | t.Fatalf("Resume whose provider call crossed the stop time = (%d, %v), want (0, ErrPastDeadline)", ep, err) |
| 432 | } |
| 433 | e.wantResume(JobRunning, 0, []opRow{{id: "op-1", status: "failed", prepared: 1}}, 1) |
| 434 | } |
| 435 | |
| 436 | func TestResumeLiveReadClearsNotFound(t *testing.T) { |
| 437 | t.Parallel() |
| 438 | e := runningJob(t) |
| 439 | flaky := ¬FoundOnce{Fake: e.fake} |
| 440 | e.run = flaky |
| 441 | e.newController() |
| 442 | e.suspendJob() |
| 443 | if _, err := e.resume("op-1"); !errors.Is(err, ErrResumePending) { |
| 444 | t.Fatalf("Resume = %v, want ErrResumePending", err) |
| 445 | } |
| 446 | |
| 447 | flaky.n = 1 // a stale not-found |
| 448 | e.refresh() |
| 449 | if s := e.stopCols("att-1"); !s.notFoundSince.Valid { |
| 450 | t.Fatal("no first not-found recorded") |
| 451 | } |
| 452 | // The retry reads the VM live (still suspended): the not-found is void. |
| 453 | if _, err := e.resume("op-1"); !errors.Is(err, ErrResumePending) { |
| 454 | t.Fatalf("retry = %v, want ErrResumePending", err) |
| 455 | } |
| 456 | if s := e.stopCols("att-1"); s.notFoundSince.Valid || s.state != JobResuming { |
| 457 | t.Fatalf("after a live read: not-found since %v, state %q; want cleared, resuming", s.notFoundSince, s.state) |
| 458 | } |
| 459 | e.clk.AdvanceMono(NotFoundConfirm + time.Second) |
| 460 | flaky.n = 1 // another stale not-found |
| 461 | e.refresh() |
| 462 | if s := e.stopCols("att-1"); s.proof.Valid { |
| 463 | t.Fatalf("proof %s from two not-founds with a live read between them", s.proof.String) |
| 464 | } |
| 465 | e.audit() // a fresh passing network audit, so only the replacement rule can refuse |
| 466 | if err := e.replace("att-2", "att-1"); !errors.Is(err, ErrReplacementLive) { |
| 467 | t.Fatalf("replacement of att-1 = %v, want ErrReplacementLive", err) |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | // TestResumeFailedByProof: every way the job gets stop proof while a resume |
| 472 | // is pending fails the operation with the proof, keeps its row, leaves the |
| 473 | // epoch, and sends no further resume. |
| 474 | func TestResumeFailedByProof(t *testing.T) { |
| 475 | t.Parallel() |
| 476 | for _, tc := range []struct { |
| 477 | name string |
| 478 | end func(t *testing.T, e *launchEnv, ref isolatedjob.Ref) |
| 479 | }{ |
| 480 | {"the VM ends on its own", func(t *testing.T, e *launchEnv, ref isolatedjob.Ref) { |
| 481 | if err := e.fake.Stop(e.ctx, ref, "provider ended it"); err != nil { |
| 482 | t.Fatal(err) |
| 483 | } |
| 484 | e.fake.Tick() |
| 485 | e.refresh() |
| 486 | }}, |
| 487 | {"the VM is gone, confirmed", func(t *testing.T, e *launchEnv, ref isolatedjob.Ref) { |
| 488 | e.fake.Vanish(ref) |
| 489 | e.refresh() |
| 490 | e.clk.AdvanceMono(NotFoundConfirm + time.Second) |
| 491 | e.refresh() |
| 492 | }}, |
| 493 | {"the instance stops it", func(t *testing.T, e *launchEnv, ref isolatedjob.Ref) { |
| 494 | if _, err := e.c.Stop(e.ctx, "inst-1", instKey, "att-1", "task done"); !errors.Is(err, ErrStopPending) { |
| 495 | t.Fatalf("Stop = %v, want ErrStopPending", err) |
| 496 | } |
| 497 | e.fake.Tick() |
| 498 | if _, err := e.c.Stop(e.ctx, "inst-1", instKey, "att-1", "task done"); err != nil { |
| 499 | t.Fatalf("Stop once stopped = %v, want proof", err) |
| 500 | } |
| 501 | }}, |
| 502 | } { |
| 503 | t.Run(tc.name, func(t *testing.T) { |
| 504 | t.Parallel() |
| 505 | e := runningJob(t) |
| 506 | e.suspendJob() |
| 507 | e.fake.Inject(isolatedjob.OpResume, isolatedjob.FaultLostReply) |
| 508 | if _, err := e.resume("op-1"); !errors.Is(err, ErrResumePending) { |
| 509 | t.Fatalf("Resume with a lost reply = %v, want ErrResumePending", err) |
| 510 | } |
| 511 | v, err := e.c.Get(e.ctx, "inst-1", "att-1") |
| 512 | if err != nil { |
| 513 | t.Fatal(err) |
| 514 | } |
| 515 | tc.end(t, e, v.VMRef) |
| 516 | failed := []opRow{{id: "op-1", status: "failed", prepared: 1}} |
| 517 | e.wantResume(JobStopped, 0, failed, 1) |
| 518 | if !e.stopCols("att-1").proof.Valid { |
| 519 | t.Fatal("no stop proof") |
| 520 | } |
| 521 | e.refresh() |
| 522 | if _, err := e.resume("op-1"); !errors.Is(err, isolatedjob.ErrConflict) { |
| 523 | t.Fatalf("retry after proof = %v, want ErrConflict", err) |
| 524 | } |
| 525 | e.wantResume(JobStopped, 0, failed, 1) |
| 526 | }) |
| 527 | } |
| 528 | } |
| 529 | |