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 "bytes"
5 "context"
6 "crypto/sha256"
7 "fmt"
8 "net/http/httptest"
9 "testing"
10
11 "amadan.net/oficina/equipo/internal/equipo/team"
12 "amadan.net/oficina/equipo/internal/runnerproto"
13
14 "amadan.net/oficina/equipo/internal/equipo"
15)
16
17func TestArtifactScopeAndLimits(t *testing.T) {
18 f := runnerFixtureNew(t, true)
19 ctx := connectionContext
20 p := f.principal
21 s := f.claim.Scope
22 h := f.claim.Handle
23 b := []byte("frozen artifact")
24 digest := fmt.Sprintf("%x", sha256.Sum256(b))
25 executionMust(t, f.s.PutBlob(ctx, p, s, h, digest, b))
26 executionMust(t, f.s.PutBlob(ctx, p, s, h, digest, b))
27 got, err := f.s.Blob(ctx, p, s, digest)
28 executionMust(t, err)
29 if !bytes.Equal(got, b) {
30 t.Fatal("blob bytes changed")
31 }
32 for _, kind := range []string{"digest", "new_digest", "oversize", "fence", "person", "job", "team", "runner"} {
33 t.Run(kind, func(t *testing.T) {
34 pp := p
35 ss := s
36 hh := h
37 dd := digest
38 bb := b
39 switch kind {
40 case "digest":
41 bb = []byte("corrupt")
42 case "new_digest":
43 dd = fmt.Sprintf("%x", sha256.Sum256([]byte("different bytes")))
44 case "oversize":
45 bb = make([]byte, equipo.MaxBlobBytes+1)
46 dd = fmt.Sprintf("%x", sha256.Sum256(bb))
47 case "fence":
48 hh.Fence++
49 case "person":
50 pp.Person = "other@example.com"
51 case "job":
52 ss.Job++
53 case "team":
54 ss.Account = "other"
55 case "runner":
56 pp.RunnerID = "other"
57 }
58 if err := f.s.PutBlob(ctx, pp, ss, hh, dd, bb); err == nil {
59 t.Fatal("unsafe blob upload accepted")
60 }
61 if kind == "person" || kind == "job" || kind == "team" || kind == "runner" {
62 if _, err := f.s.Blob(ctx, pp, ss, digest); err == nil {
63 t.Fatal("foreign blob read accepted")
64 }
65 }
66 })
67 }
68 // Exercise the cumulative limit without writing 128 MiB into each fixture.
69 _, err = f.d.Writer().Exec(`UPDATE artifact_blob_uploads SET size=? WHERE attempt_id=?`, equipo.MaxAttemptBlobBytes, h.Attempt)
70 executionMust(t, err)
71 other := []byte("another")
72 if err := f.s.PutBlob(ctx, p, s, h, fmt.Sprintf("%x", sha256.Sum256(other)), other); err == nil {
73 t.Fatal("attempt upload limit ignored")
74 }
75 _, err = f.d.Writer().Exec(`UPDATE artifact_blobs SET bytes=? WHERE digest=?`, []byte("corrupt"), digest)
76 executionMust(t, err)
77 if _, err := f.s.Blob(ctx, p, s, digest); err == nil {
78 t.Fatal("corrupt stored blob returned")
79 }
80}
81
82func TestArtifactProtocol(t *testing.T) {
83 f := runnerFixtureNew(t, true)
84 payload := []byte("protocol artifact")
85 digest := fmt.Sprintf("%x", sha256.Sum256(payload))
86 body, _ := runnerproto.Marshal(runnerproto.BlobRequest{Version: 2, Scope: f.claim.Scope, Handle: f.claim.Handle, Digest: digest, Bytes: payload})
87 req := httptest.NewRequest("POST", "/blobs", bytes.NewReader(body))
88 req.Header.Set("Content-Type", "application/json")
89 req.Header.Set("Authorization", "Bearer "+runnerBearerFor(t, f))
90 rec := httptest.NewRecorder()
91 f.s.RunnerHandler().ServeHTTP(rec, req)
92 if rec.Code != 200 {
93 t.Fatalf("blob upload: %d %s", rec.Code, rec.Body.String())
94 }
95 body, _ = runnerproto.Marshal(runnerproto.BlobReadRequest{Version: 2, Scope: f.claim.Scope, Digest: digest})
96 req = httptest.NewRequest("POST", "/blobs/read", bytes.NewReader(body))
97 req.Header.Set("Content-Type", "application/json")
98 req.Header.Set("Authorization", "Bearer "+runnerBearerFor(t, f))
99 rec = httptest.NewRecorder()
100 f.s.RunnerHandler().ServeHTTP(rec, req)
101 if rec.Code != 200 {
102 t.Fatalf("blob read: %d %s", rec.Code, rec.Body.String())
103 }
104}
105
106func TestArtifactRegistrationStaysCore(t *testing.T) {
107 f := runnerFixtureNew(t, true)
108 b := []byte("blob only")
109 digest := fmt.Sprintf("%x", sha256.Sum256(b))
110 executionMust(t, f.s.PutBlob(connectionContext, f.principal, f.claim.Scope, f.claim.Handle, digest, b))
111 var count int
112 executionMust(t, f.d.Writer().QueryRow(`SELECT count(*) FROM artifacts`).Scan(&count))
113 if count != 0 {
114 t.Fatal("blob upload bypassed core registration")
115 }
116 f.x.SetRunnerValidators(equipo.RunnerValidators{Artifact: func(context.Context, equipo.Assignment, equipo.RunnerRequest) error { return nil }})
117 q := f.request()
118 q.Artifact = &team.ArtifactSpec{Role: team.RoleBaseResult, MediaType: "application/json", Payload: []byte(`{"outcome":"fail","tests_run":1,"tests_failed":1}`)}
119 if _, err := f.x.Mutate(connectionContext, f.principal, "artifacts", q); err == nil {
120 t.Fatal("agent submitted executor evidence")
121 }
122 executionMust(t, f.s.RevokeRunner(connectionContext, ownerActor, f.principal.RunnerID, 1))
123 if _, err := f.s.Blob(connectionContext, f.principal, f.claim.Scope, digest); err == nil {
124 t.Fatal("revoked runner read blob")
125 }
126}
127
128func TestArtifactReadRequiresCurrentClaim(t *testing.T) {
129 for _, change := range []string{"state", "kind", "fence", "expired"} {
130 t.Run(change, func(t *testing.T) {
131 f := runnerFixtureNew(t, true)
132 b := []byte("current claim")
133 digest := fmt.Sprintf("%x", sha256.Sum256(b))
134 executionMust(t, f.s.PutBlob(connectionContext, f.principal, f.claim.Scope, f.claim.Handle, digest, b))
135 query := map[string]string{
136 "state": "UPDATE runner_assignments SET state='contained' WHERE attempt_id=?",
137 "kind": "UPDATE runner_assignments SET kind='prepare' WHERE attempt_id=?",
138 "fence": "UPDATE runner_assignments SET fence=fence+1 WHERE attempt_id=?",
139 "expired": "UPDATE attempts SET lease_deadline='2000-01-01T00:00:00Z' WHERE id=?",
140 }[change]
141 _, err := f.d.Writer().Exec(query, f.claim.Handle.Attempt)
142 executionMust(t, err)
143 if _, err := f.s.Blob(connectionContext, f.principal, f.claim.Scope, digest); err == nil {
144 t.Fatal("blob read without current claimed attempt")
145 }
146 })
147 }
148}
149
150func TestArtifactReadAssignmentOrder(t *testing.T) {
151 f := runnerFixtureNew(t, true)
152 b := []byte("ordered claim")
153 digest := fmt.Sprintf("%x", sha256.Sum256(b))
154 executionMust(t, f.s.PutBlob(connectionContext, f.principal, f.claim.Scope, f.claim.Handle, digest, b))
155 _, err := f.d.Writer().Exec(`INSERT INTO runner_assignments
156 (id,task_id,step_key,team_id,runner_id,person,selection,isolation,kind,state,revision,job_id,attempt_id,fence)
157 SELECT '000-old-late-row',task_id,step_key,team_id,runner_id,person,'{}',isolation,kind,state,revision+100,job_id,attempt_id,fence-1
158 FROM runner_assignments WHERE attempt_id=?`, f.claim.Handle.Attempt)
159 executionMust(t, err)
160 got, err := f.s.Blob(connectionContext, f.principal, f.claim.Scope, digest)
161 executionMust(t, err)
162 if !bytes.Equal(got, b) {
163 t.Fatal("current claim did not read blob")
164 }
165}
166