package thintest import ( "net/url" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/wire" ) // Cancel after a withdrawal: the withdrawn attempt's proof finishes the // cancellation; no next attempt is placed or may start. func TestContractCancelAfterWithdrawal(t *testing.T) { r := newRunnersRig(t) task, step, w, _ := r.startedOnWorker("Withdrawn, then cancelled") if _, err := r.client.CancelTask(r.ctx, task); err != nil { t.Fatal(err) } w.send(wire.FrameStep, wire.StepReport{TaskID: task, StepID: step, Attempt: 1, State: wire.StepCancelled, Reason: "cancelled", Termination: true, TerminationKind: wire.TerminationProcessGroup, Findings: []wire.Finding{}}) k, _ := r.client.Task(r.ctx, task) s := r.checksStep(task) if k.Stage != wire.StageCancelled || s.Attempt != 1 || s.State != wire.StepCancelled || !s.Terminated { t.Fatalf("after the proof: task %s step %+v", k.Stage, s) } w.none(wire.FrameDispatch, 100*time.Millisecond) if w.lease(step, 2) { t.Fatal("a next attempt may start after the cancel") } } // remove asks for a runner's removal as the owner: ordinary, or now. func (r *runnersRig) remove(id string, now bool) { r.t.Helper() var revision int64 if err := r.inst.DB.Writer().QueryRow("SELECT revision FROM runners WHERE id = ?", id).Scan(&revision); err != nil { r.t.Fatal(err) } remove := r.inst.Store.RevokeRunner if now { remove = r.inst.Store.RemoveRunnerNow } if err := remove(r.ctx, equipo.Actor{Address: Owner}, id, revision); err != nil { r.t.Fatal(err) } } func (r *runnersRig) revokedAt(id string) bool { r.t.Helper() var n int if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runners WHERE id = ? AND revoked_at IS NOT NULL", id).Scan(&n); err != nil { r.t.Fatal(err) } return n == 1 } // Ordinary removal is two-phase. An attempt that never started is // withdrawn at once and placed elsewhere; one that may have started is // cancelled, and the runner reads Removing (still able to report, given // no new work) until its proof arrives; only then is it revoked. func TestContractRemovalWaitsForProof(t *testing.T) { r := newRunnersRig(t) a, _ := r.newWorker("Mac A") b, _ := r.newWorker("Mac B") started, _ := r.candidate("iOS started", true) _, body := a.expect(wire.FrameDispatch) d1 := body.(*wire.DispatchCommand) if !a.lease(d1.StepID, 1) { t.Fatal("start refused") } waiting, _ := r.candidate("iOS not started", true) _, body = b.expect(wire.FrameDispatch) // fewest running steps: B d2 := body.(*wire.DispatchCommand) if d2.TaskID != waiting { t.Fatalf("second dispatch %+v", d2) } r.remove(b.fr.Welcome.RunnerID, false) // Never started: withdrawn in the same transaction, and B (no // uncertain attempt) is revoked at once. if !r.revokedAt(b.fr.Welcome.RunnerID) { t.Fatal("a runner with nothing uncertain was not revoked") } if s := r.checksStep(waiting); s.Attempt != 2 || s.RunnerID == b.fr.Welcome.RunnerID { t.Fatalf("never-started attempt not withdrawn: %+v", s) } r.remove(a.fr.Welcome.RunnerID, false) if r.revokedAt(a.fr.Welcome.RunnerID) { t.Fatal("revoked before its attempt's termination proof") } runners, _ := r.client.Runners(r.ctx) removing := false for _, x := range runners.Items { if x.ID == a.fr.Welcome.RunnerID && x.Removing { removing = true } } if !removing { t.Fatalf("runner not shown as removing: %+v", runners.Items) } _, body = a.expect(wire.FrameCancelStep) if c := body.(*wire.CancelStepCommand); c.StepID != d1.StepID { t.Fatalf("cancel %+v", c) } // The primary's empty sync and time passing prove nothing. r.primary.send(wire.FrameSync, wire.Sync{Sessions: []wire.SessionState{}, Runs: []wire.Run{}, Steps: []wire.StepRun{}, Asks: []wire.AskRun{}}) if r.revokedAt(a.fr.Welcome.RunnerID) || r.checksStep(started).Attempt != 1 { t.Fatal("something other than the proof settled it") } a.send(wire.FrameStep, wire.StepReport{TaskID: started, StepID: d1.StepID, Attempt: 1, State: wire.StepCancelled, Reason: "cancelled", Termination: true, TerminationKind: wire.TerminationProcessGroup, Findings: []wire.Finding{}}) if !r.revokedAt(a.fr.Welcome.RunnerID) { t.Fatal("not revoked after the proof") } if s := r.checksStep(started); s.Attempt != 2 || s.RunnerID == a.fr.Welcome.RunnerID { t.Fatalf("next attempt %+v", s) } } // Remove now revokes at once and quarantines what may still run: the // step stops (machine_removed, no termination proof) and Retry is // refused until the task's creator recovers it (audited); then Retry. func TestContractRemoveNowQuarantines(t *testing.T) { r := newRunnersRig(t) w, _ := r.newWorker("Mac A") task, _ := r.candidate("iOS lost machine", true) _, body := w.expect(wire.FrameDispatch) d := body.(*wire.DispatchCommand) if !w.lease(d.StepID, 1) { t.Fatal("start refused") } r.remove(w.fr.Welcome.RunnerID, true) if !r.revokedAt(w.fr.Welcome.RunnerID) { t.Fatal("remove now did not revoke at once") } k, _ := r.client.Task(r.ctx, task) s := r.checksStep(task) if s.State != wire.StepFailed || s.Reason != "machine_removed" || s.Terminated || k.NeedsYou != wire.NeedsMachineRemoved { t.Fatalf("not quarantined: %+v needs %q", s, k.NeedsYou) } if _, err := r.client.RetryTask(r.ctx, task); Status(err) != 409 { t.Fatalf("retry before recovery: %v", err) } mate := personToolRig(t, r.toolRig, "mate@example.com", equipo.RoleOwner) if _, err := mate.client.RecoverTask(r.ctx, task); Status(err) != 403 { t.Fatalf("a teammate recovered: %v", err) } if _, err := r.client.RecoverTask(r.ctx, task); err != nil { t.Fatal(err) } s = r.checksStep(task) if !s.Terminated || s.TerminationKind != wire.TerminationUnverified { t.Fatalf("recovered %+v", s) } var audits int if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM audit_events WHERE kind = 'task.recover'").Scan(&audits); err != nil || audits != 1 { t.Fatalf("recovery not audited: %d %v", audits, err) } if acc, err := r.client.RetryTask(r.ctx, task); err != nil || acc.Attempt != 2 { t.Fatalf("retry after recovery %+v %v", acc, err) } } // The instance tells a worker its role with its credential at enrolment // (a primary's credential says nothing, as an older instance's does). func TestContractEnrolmentNamesAWorker(t *testing.T) { r := newRunnersRig(t) if cred := r.inst.EnrolRunner(t); cred.Role != wire.RoleWorker { t.Fatalf("second runner's credential role %q", cred.Role) } } // The pages say what happened and offer the way out: a runner removing // shows so, with Remove now; a quarantined step's card says to make sure // the machine is off, then Recover, and Recover there settles it. func TestPagesRemovalAndRecover(t *testing.T) { r := newRunnersRig(t) owner := r.inst.Cookie() w, _ := r.newWorker("Mac A") task, _ := r.candidate("iOS removed", true) _, body := w.expect(wire.FrameDispatch) d := body.(*wire.DispatchCommand) if !w.lease(d.StepID, 1) { t.Fatal("start refused") } r.remove(w.fr.Welcome.RunnerID, false) page := r.get(owner, "/runners/enrol", 200) if !strings.Contains(page, "Removing: waiting for it to stop its work") || !strings.Contains(page, `action="/runners/`+w.fr.Welcome.RunnerID+`/remove-now"`) { t.Fatalf("runners page while removing: %s", page) } var revision string if err := r.inst.DB.Writer().QueryRow("SELECT revision FROM runners WHERE id = ?", w.fr.Welcome.RunnerID).Scan(&revision); err != nil { t.Fatal(err) } if status, _ := r.post(owner, "/runners/"+w.fr.Welcome.RunnerID+"/remove-now", url.Values{"revision": {revision}}); status != 303 || !r.revokedAt(w.fr.Welcome.RunnerID) { t.Fatalf("remove now from the page: %d", status) } page = r.get(owner, "/tasks/"+task, 200) if !strings.Contains(page, "Stopped: the machine was removed mid-step. Make sure it") || !strings.Contains(page, "s switched off, then Recover") || !strings.Contains(page, "data-task-recover") || strings.Contains(page, "data-task-retry") { t.Fatalf("quarantined task page: %s", page) } if status, _ := r.post(owner, "/conversation/tasks/"+task+"/recover", url.Values{"from": {"task"}}); status != 303 { t.Fatalf("recover from the page: %d", status) } if s := r.checksStep(task); !s.Terminated || s.TerminationKind != wire.TerminationUnverified { t.Fatalf("not recovered: %+v", s) } }