oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package runner
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "os/exec"
8 "strings"
9 "testing"
10
11 "amadan.net/oficina/equipo/internal/equipo/team"
12 "amadan.net/oficina/equipo/internal/runner/forge"
13 "amadan.net/oficina/equipo/internal/runner/provider"
14 "amadan.net/oficina/equipo/internal/runner/workspace"
15 "amadan.net/oficina/equipo/internal/runnerproto"
16)
17
18type localRead struct{ path string }
19
20func (r localRead) Resolve(context.Context, string) (workspace.Remote, error) {
21 return workspace.Remote{URL: r.path}, nil
22}
23func wfMust(t *testing.T, err error) {
24 t.Helper()
25 if err != nil {
26 t.Fatal(err)
27 }
28}
29func wfGit(t *testing.T, dir string, args ...string) string {
30 t.Helper()
31 cmd := exec.Command("/usr/bin/git", args...)
32 cmd.Dir = dir
33 cmd.Env = []string{"PATH=/usr/bin:/bin", "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null", "GIT_AUTHOR_NAME=test", "GIT_AUTHOR_EMAIL=test@example.test", "GIT_COMMITTER_NAME=test", "GIT_COMMITTER_EMAIL=test@example.test"}
34 b, err := cmd.CombinedOutput()
35 if err != nil {
36 t.Fatalf("git %v: %v %s", args, err, b)
37 }
38 return string(bytesTrim(b))
39}
40func bytesTrim(b []byte) []byte {
41 for len(b) > 0 && (b[len(b)-1] == '\n' || b[len(b)-1] == '\r') {
42 b = b[:len(b)-1]
43 }
44 return b
45}
46
47type coreWorkflowAPI struct {
48 t *testing.T
49 question *runnerproto.Question
50 admissions int
51 core *team.Store
52 w *Witness
53 assignments map[string]runnerproto.Assignment
54}
55
56func (a *coreWorkflowAPI) Call(ctx context.Context, op string, q runnerproto.RunnerRequest, out any) error {
57 assignment := a.assignments[q.AssignmentID]
58 var result any
59 var err error
60 switch op {
61 case "questions":
62 err = a.w.Question(ctx, assignment, q)
63 if err == nil {
64 copy := *q.Question
65 a.question = &copy
66 result = copy
67 }
68 case "answers":
69 if a.question == nil {
70 return provider.ErrIdentity
71 }
72 copy := *a.question
73 copy.Answer = &runnerproto.QuestionAnswer{Behavior: "allow", UpdatedInput: []byte(`{"answers":{"answer":"Use the failing value case"}}`)}
74 result = []runnerproto.Question{copy}
75 case "question_resolved":
76 err = a.w.Question(ctx, assignment, q)
77 case "validate":
78 err = a.core.ValidateHandle(ctx, q.Scope, q.Handle)
79 case "publication_ready":
80 result, err = a.core.Publication(ctx, q.Scope, q.Handle)
81 case "publication_authorize":
82 result, err = a.core.AuthorizePublication(ctx, q.Scope, q.Handle, q.ActionID, q.RequestID)
83 case "receipts":
84 var receipt team.Receipt
85 receipt, err = a.w.Receipt(ctx, assignment, q)
86 if err == nil {
87 err = a.core.Record(ctx, q.Scope, q.ActionID, receipt)
88 }
89 case "conclude":
90 result, err = a.core.Conclude(ctx, q.Scope, "runner")
91 case "renew":
92 result, err = a.core.Renew(ctx, q.Scope, q.Handle)
93 case "evidence":
94 if q.ArtifactID > 0 {
95 result, err = a.core.Artifact(ctx, q.Scope, q.ArtifactID)
96 } else {
97 result, err = a.core.Artifacts(ctx, q.Scope)
98 }
99 case "intend":
100 result, err = a.core.Intend(ctx, q.Scope, q.Handle, *q.Intent)
101 case "admit":
102 a.admissions++
103 result, err = a.core.AdmitOnce(ctx, q.Scope, q.Handle, q.ActionID, q.RequestID)
104 case "artifacts":
105 err = a.w.Artifact(ctx, assignment, q)
106 if err == nil {
107 result, err = a.core.Register(ctx, q.Scope, q.Handle, *q.Artifact)
108 }
109 case "events":
110 err = a.w.Events(ctx, assignment, q)
111 if err == nil {
112 var e Event
113 runnerproto.DecodeLimit(strings.NewReader(string(q.Event)), &e, runnerproto.MaxEventBytes+4096)
114 if e.Kind == "turn_completed" || e.Kind == "turn_failed" {
115 var receipt team.Receipt
116 receipt, err = a.w.Receipt(ctx, assignment, q)
117 if err == nil {
118 err = a.core.Record(ctx, q.Scope, q.ActionID, receipt)
119 }
120 }
121 }
122 case "freeze":
123 var f runnerproto.FreezeRequest
124 json.Unmarshal(q.Prepared, &f)
125 err = a.core.FreezeBugEvidence(ctx, q.Scope, q.Handle, f.ExpectedSymptom, f.DefinitionHash, f.ReproductionHash)
126 case "complete":
127 err = a.core.Complete(ctx, q.Scope, q.Handle, *q.Result)
128 case "stop":
129 err = a.core.StopAttempt(ctx, q.Scope, q.Handle, "test_stop")
130 case "contained":
131 if assignment.StepKey == "reproduce" {
132 if _, e := a.core.Claim(ctx, q.Scope, "implement", "runner", "too-early"); e == nil {
133 return fmt.Errorf("dependent claimed before containment")
134 }
135 }
136 err = a.w.Containment(ctx, assignment, q)
137 if err == nil {
138 err = a.core.ContainUntrusted(ctx, q.Scope, q.Handle)
139 }
140 default:
141 return fmt.Errorf("unexpected operation %s", op)
142 }
143 if err != nil {
144 return fmt.Errorf("%s: %w", op, err)
145 }
146 if out != nil && result != nil {
147 return json.Unmarshal(marshal(result), out)
148 }
149 return nil
150}
151func decodeCandidate(b []byte) (v team.GitCandidate) { json.Unmarshal(b, &v); return }
152func apiArtifacts(ctx context.Context, w *Witness, c RunClaim) ([]team.Artifact, error) {
153 var arts []team.Artifact
154 err := w.read(ctx, "artifacts", attemptKey(c.Scope, c.Handle), &arts)
155 return arts, err
156}
157
158type workflowDescription struct{ value forge.Description }
159
160func (*workflowDescription) BeforePush() bool { return true }
161func (d *workflowDescription) Set(_ context.Context, _ forge.Credential, v forge.Description) error {
162 d.value = v
163 return nil
164}
165func (d *workflowDescription) Read(_ context.Context, _ forge.Credential, repository, ref string) (forge.Description, error) {
166 return d.value, nil
167}
168