oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package thintest
2
3import (
4 "net/url"
5 "strings"
6 "testing"
7 "time"
8
9 "amadan.net/oficina/equipo/internal/equipo"
10 "amadan.net/oficina/equipo/internal/wire"
11)
12
13// Cancel after a withdrawal: the withdrawn attempt's proof finishes the
14// cancellation; no next attempt is placed or may start.
15func TestContractCancelAfterWithdrawal(t *testing.T) {
16 r := newRunnersRig(t)
17 task, step, w, _ := r.startedOnWorker("Withdrawn, then cancelled")
18 if _, err := r.client.CancelTask(r.ctx, task); err != nil {
19 t.Fatal(err)
20 }
21 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{}})
22 k, _ := r.client.Task(r.ctx, task)
23 s := r.checksStep(task)
24 if k.Stage != wire.StageCancelled || s.Attempt != 1 || s.State != wire.StepCancelled || !s.Terminated {
25 t.Fatalf("after the proof: task %s step %+v", k.Stage, s)
26 }
27 w.none(wire.FrameDispatch, 100*time.Millisecond)
28 if w.lease(step, 2) {
29 t.Fatal("a next attempt may start after the cancel")
30 }
31}
32
33// remove asks for a runner's removal as the owner: ordinary, or now.
34func (r *runnersRig) remove(id string, now bool) {
35 r.t.Helper()
36 var revision int64
37 if err := r.inst.DB.Writer().QueryRow("SELECT revision FROM runners WHERE id = ?", id).Scan(&revision); err != nil {
38 r.t.Fatal(err)
39 }
40 remove := r.inst.Store.RevokeRunner
41 if now {
42 remove = r.inst.Store.RemoveRunnerNow
43 }
44 if err := remove(r.ctx, equipo.Actor{Address: Owner}, id, revision); err != nil {
45 r.t.Fatal(err)
46 }
47}
48
49func (r *runnersRig) revokedAt(id string) bool {
50 r.t.Helper()
51 var n int
52 if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runners WHERE id = ? AND revoked_at IS NOT NULL", id).Scan(&n); err != nil {
53 r.t.Fatal(err)
54 }
55 return n == 1
56}
57
58// Ordinary removal is two-phase. An attempt that never started is
59// withdrawn at once and placed elsewhere; one that may have started is
60// cancelled, and the runner reads Removing (still able to report, given
61// no new work) until its proof arrives; only then is it revoked.
62func TestContractRemovalWaitsForProof(t *testing.T) {
63 r := newRunnersRig(t)
64 a, _ := r.newWorker("Mac A")
65 b, _ := r.newWorker("Mac B")
66 started, _ := r.candidate("iOS started", true)
67 _, body := a.expect(wire.FrameDispatch)
68 d1 := body.(*wire.DispatchCommand)
69 if !a.lease(d1.StepID, 1) {
70 t.Fatal("start refused")
71 }
72 waiting, _ := r.candidate("iOS not started", true)
73 _, body = b.expect(wire.FrameDispatch) // fewest running steps: B
74 d2 := body.(*wire.DispatchCommand)
75 if d2.TaskID != waiting {
76 t.Fatalf("second dispatch %+v", d2)
77 }
78 r.remove(b.fr.Welcome.RunnerID, false)
79 // Never started: withdrawn in the same transaction, and B (no
80 // uncertain attempt) is revoked at once.
81 if !r.revokedAt(b.fr.Welcome.RunnerID) {
82 t.Fatal("a runner with nothing uncertain was not revoked")
83 }
84 if s := r.checksStep(waiting); s.Attempt != 2 || s.RunnerID == b.fr.Welcome.RunnerID {
85 t.Fatalf("never-started attempt not withdrawn: %+v", s)
86 }
87 r.remove(a.fr.Welcome.RunnerID, false)
88 if r.revokedAt(a.fr.Welcome.RunnerID) {
89 t.Fatal("revoked before its attempt's termination proof")
90 }
91 runners, _ := r.client.Runners(r.ctx)
92 removing := false
93 for _, x := range runners.Items {
94 if x.ID == a.fr.Welcome.RunnerID && x.Removing {
95 removing = true
96 }
97 }
98 if !removing {
99 t.Fatalf("runner not shown as removing: %+v", runners.Items)
100 }
101 _, body = a.expect(wire.FrameCancelStep)
102 if c := body.(*wire.CancelStepCommand); c.StepID != d1.StepID {
103 t.Fatalf("cancel %+v", c)
104 }
105 // The primary's empty sync and time passing prove nothing.
106 r.primary.send(wire.FrameSync, wire.Sync{Sessions: []wire.SessionState{}, Runs: []wire.Run{}, Steps: []wire.StepRun{}, Asks: []wire.AskRun{}})
107 if r.revokedAt(a.fr.Welcome.RunnerID) || r.checksStep(started).Attempt != 1 {
108 t.Fatal("something other than the proof settled it")
109 }
110 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{}})
111 if !r.revokedAt(a.fr.Welcome.RunnerID) {
112 t.Fatal("not revoked after the proof")
113 }
114 if s := r.checksStep(started); s.Attempt != 2 || s.RunnerID == a.fr.Welcome.RunnerID {
115 t.Fatalf("next attempt %+v", s)
116 }
117}
118
119// Remove now revokes at once and quarantines what may still run: the
120// step stops (machine_removed, no termination proof) and Retry is
121// refused until the task's creator recovers it (audited); then Retry.
122func TestContractRemoveNowQuarantines(t *testing.T) {
123 r := newRunnersRig(t)
124 w, _ := r.newWorker("Mac A")
125 task, _ := r.candidate("iOS lost machine", true)
126 _, body := w.expect(wire.FrameDispatch)
127 d := body.(*wire.DispatchCommand)
128 if !w.lease(d.StepID, 1) {
129 t.Fatal("start refused")
130 }
131 r.remove(w.fr.Welcome.RunnerID, true)
132 if !r.revokedAt(w.fr.Welcome.RunnerID) {
133 t.Fatal("remove now did not revoke at once")
134 }
135 k, _ := r.client.Task(r.ctx, task)
136 s := r.checksStep(task)
137 if s.State != wire.StepFailed || s.Reason != "machine_removed" || s.Terminated || k.NeedsYou != wire.NeedsMachineRemoved {
138 t.Fatalf("not quarantined: %+v needs %q", s, k.NeedsYou)
139 }
140 if _, err := r.client.RetryTask(r.ctx, task); Status(err) != 409 {
141 t.Fatalf("retry before recovery: %v", err)
142 }
143 mate := personToolRig(t, r.toolRig, "mate@example.com", equipo.RoleOwner)
144 if _, err := mate.client.RecoverTask(r.ctx, task); Status(err) != 403 {
145 t.Fatalf("a teammate recovered: %v", err)
146 }
147 if _, err := r.client.RecoverTask(r.ctx, task); err != nil {
148 t.Fatal(err)
149 }
150 s = r.checksStep(task)
151 if !s.Terminated || s.TerminationKind != wire.TerminationUnverified {
152 t.Fatalf("recovered %+v", s)
153 }
154 var audits int
155 if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM audit_events WHERE kind = 'task.recover'").Scan(&audits); err != nil || audits != 1 {
156 t.Fatalf("recovery not audited: %d %v", audits, err)
157 }
158 if acc, err := r.client.RetryTask(r.ctx, task); err != nil || acc.Attempt != 2 {
159 t.Fatalf("retry after recovery %+v %v", acc, err)
160 }
161}
162
163// The instance tells a worker its role with its credential at enrolment
164// (a primary's credential says nothing, as an older instance's does).
165func TestContractEnrolmentNamesAWorker(t *testing.T) {
166 r := newRunnersRig(t)
167 if cred := r.inst.EnrolRunner(t); cred.Role != wire.RoleWorker {
168 t.Fatalf("second runner's credential role %q", cred.Role)
169 }
170}
171
172// The pages say what happened and offer the way out: a runner removing
173// shows so, with Remove now; a quarantined step's card says to make sure
174// the machine is off, then Recover, and Recover there settles it.
175func TestPagesRemovalAndRecover(t *testing.T) {
176 r := newRunnersRig(t)
177 owner := r.inst.Cookie()
178 w, _ := r.newWorker("Mac A")
179 task, _ := r.candidate("iOS removed", true)
180 _, body := w.expect(wire.FrameDispatch)
181 d := body.(*wire.DispatchCommand)
182 if !w.lease(d.StepID, 1) {
183 t.Fatal("start refused")
184 }
185 r.remove(w.fr.Welcome.RunnerID, false)
186 page := r.get(owner, "/runners/enrol", 200)
187 if !strings.Contains(page, "Removing: waiting for it to stop its work") || !strings.Contains(page, `action="/runners/`+w.fr.Welcome.RunnerID+`/remove-now"`) {
188 t.Fatalf("runners page while removing: %s", page)
189 }
190 var revision string
191 if err := r.inst.DB.Writer().QueryRow("SELECT revision FROM runners WHERE id = ?", w.fr.Welcome.RunnerID).Scan(&revision); err != nil {
192 t.Fatal(err)
193 }
194 if status, _ := r.post(owner, "/runners/"+w.fr.Welcome.RunnerID+"/remove-now", url.Values{"revision": {revision}}); status != 303 || !r.revokedAt(w.fr.Welcome.RunnerID) {
195 t.Fatalf("remove now from the page: %d", status)
196 }
197 page = r.get(owner, "/tasks/"+task, 200)
198 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") {
199 t.Fatalf("quarantined task page: %s", page)
200 }
201 if status, _ := r.post(owner, "/conversation/tasks/"+task+"/recover", url.Values{"from": {"task"}}); status != 303 {
202 t.Fatalf("recover from the page: %d", status)
203 }
204 if s := r.checksStep(task); !s.Terminated || s.TerminationKind != wire.TerminationUnverified {
205 t.Fatalf("not recovered: %+v", s)
206 }
207}
208