oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package equipotest
2
3import (
4 "context"
5 "errors"
6 "net/http/httptest"
7 "os"
8 "path/filepath"
9 "testing"
10 "time"
11
12 "amadan.net/oficina/equipo/internal/equipo"
13 "amadan.net/oficina/equipo/internal/equipo/team"
14 "amadan.net/oficina/equipo/internal/runner"
15 "amadan.net/oficina/equipo/internal/runner/provider"
16 "amadan.net/oficina/equipo/internal/runnerproto"
17)
18
19func TestCancelTaskFencesBeforeAcknowledgement(t *testing.T) {
20 f := runnerFixtureNew(t, true)
21 report, err := f.s.CancelTask(connectionContext, ownerActor, f.a.TaskID)
22 executionMust(t, err)
23 if report.Contained {
24 t.Fatal("cancel acknowledged before process verification")
25 }
26 if err := f.x.Core.ValidateHandle(connectionContext, f.claim.Scope, f.claim.Handle); err == nil {
27 t.Fatal("cancel retained authority")
28 }
29 attempts, err := f.x.Core.Attempts(connectionContext, f.claim.Scope)
30 executionMust(t, err)
31 if len(attempts) != 1 || attempts[0].Containment == team.ContainmentContained {
32 t.Fatal("cancel fabricated containment")
33 }
34 if _, err = f.s.CancelTask(connectionContext, equipo.Actor{Address: "stranger@example.com"}, f.a.TaskID); err == nil {
35 t.Fatal("stranger cancelled")
36 }
37}
38
39func TestRunnerBugFailureEndsFixedRun(t *testing.T) {
40 f := runnerFixtureNew(t, true)
41 q := f.request()
42 q.Result = &team.AttemptResult{Outcome: team.OutcomeFailure}
43 _, err := f.x.Mutate(connectionContext, f.principal, "complete", q)
44 executionMust(t, err)
45 job, err := f.x.Core.GetJob(connectionContext, f.claim.Scope)
46 executionMust(t, err)
47 if job.State != team.JobFailed {
48 t.Fatal("fixed run invented another attempt", job.State)
49 }
50}
51
52func TestCancelLostRotationReply(t *testing.T) {
53 f := runnerFixtureNew(t, true)
54 f.x.SetRunnerValidators(equipo.RunnerValidators{Containment: func(_ context.Context, _ equipo.Assignment, _ equipo.RunnerRequest) error { return nil }})
55 q := f.request()
56 q.RequestID = "lost-rotation"
57 q.Proof = flowJSON(runnerproto.ContainmentProof{Version: 2, Scope: q.Scope, Handle: q.Handle, CheckedAt: time.Now().UTC()})
58 _, err := f.x.Cleanup(connectionContext, f.claim.CleanupToken, "contained", q)
59 executionMust(t, err)
60 altered := q
61 altered.Proof = []byte(`{"changed":true}`)
62 if _, err := f.x.Cleanup(connectionContext, f.claim.CleanupToken, "contained", altered); err == nil {
63 t.Fatal("changed containment recovered credentials")
64 }
65 value, err := f.x.Cleanup(connectionContext, f.claim.CleanupToken, "contained", q)
66 executionMust(t, err)
67 rotated := value.(equipo.CredentialRotation)
68 if rotated.Bearer.Token == "" {
69 t.Fatal("lost rotation did not recover")
70 }
71 principal, err := f.s.AuthenticateRunner(connectionContext, rotated.Bearer.Token)
72 executionMust(t, err)
73 if _, err = f.x.Mutate(connectionContext, principal, "renew", q); err == nil {
74 t.Fatal("rotation opened admission before journal acknowledgement")
75 }
76 executionMust(t, f.x.AcknowledgeRotation(connectionContext, principal, q.RequestID))
77 if _, err = f.x.Cleanup(connectionContext, f.claim.CleanupToken, "contained", q); err == nil {
78 t.Fatal("old cleanup recovered after acknowledgement")
79 }
80}
81
82func TestCancelUnstartedTaskComplete(t *testing.T) {
83 f := runnerFixtureNew(t, false)
84 report, err := f.s.CancelTask(connectionContext, ownerActor, f.a.TaskID)
85 executionMust(t, err)
86 task, err := f.s.Task(connectionContext, ownerActor, f.a.TaskID)
87 executionMust(t, err)
88 if !report.Contained || task.State != "cancelled" {
89 t.Fatal("unstarted task retained stop request", report.Contained, task.State)
90 }
91}
92
93func TestRunnerRestartLostClaimReply(t *testing.T) {
94 f := runnerFixtureNew(t, true)
95 f.clock.Advance(61 * time.Second)
96 q := runnerproto.ClaimRequest{Version: 2, AssignmentID: f.a.ID, Revision: f.a.Revision, RequestID: "claim"}
97 recovered, err := f.x.RecoverClaim(connectionContext, f.principal, q)
98 executionMust(t, err)
99 if recovered.CleanupToken == "" || !recovered.LeaseDeadline.IsZero() || !recovered.AttemptDeadline.IsZero() || recovered.Handle.Attempt != f.claim.Handle.Attempt {
100 t.Fatal("recovery restored dispatch authority")
101 }
102 q.RequestID = "other"
103 if reply, e := f.x.RecoverClaim(connectionContext, f.principal, q); e != nil || !reply.NotClaimed || reply.CleanupToken != "" {
104 t.Fatal("foreign claim request recovered authority")
105 }
106}
107
108func TestRunnerHTTPExpiredLeaseDefinitive(t *testing.T) {
109 f := runnerFixtureNew(t, true)
110 token := runnerBearerFor(t, f)
111 server := httptest.NewServer(f.s.RunnerHandler())
112 defer server.Close()
113 dir := t.TempDir()
114 executionMust(t, os.Chmod(dir, 0700))
115 journal, err := runner.OpenJournal(connectionContext, filepath.Join(dir, "journal.db"))
116 executionMust(t, err)
117 defer journal.Close()
118 api, err := runner.NewHTTPInstance(server.URL, journal, runnerproto.RunnerCredential{Token: token, ExpiresAt: time.Now().Add(time.Hour)})
119 executionMust(t, err)
120 f.clock.Advance(61 * time.Second)
121 if err = api.Call(connectionContext, "validate", f.request(), nil); !errors.Is(err, provider.ErrIdentity) {
122 t.Fatal("expired lease was not a definitive denial", err)
123 }
124}
125