package runner import ( "context" "encoding/json" "fmt" "os/exec" "strings" "testing" "amadan.net/oficina/equipo/internal/equipo/team" "amadan.net/oficina/equipo/internal/runner/forge" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/runner/workspace" "amadan.net/oficina/equipo/internal/runnerproto" ) type localRead struct{ path string } func (r localRead) Resolve(context.Context, string) (workspace.Remote, error) { return workspace.Remote{URL: r.path}, nil } func wfMust(t *testing.T, err error) { t.Helper() if err != nil { t.Fatal(err) } } func wfGit(t *testing.T, dir string, args ...string) string { t.Helper() cmd := exec.Command("/usr/bin/git", args...) cmd.Dir = dir 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"} b, err := cmd.CombinedOutput() if err != nil { t.Fatalf("git %v: %v %s", args, err, b) } return string(bytesTrim(b)) } func bytesTrim(b []byte) []byte { for len(b) > 0 && (b[len(b)-1] == '\n' || b[len(b)-1] == '\r') { b = b[:len(b)-1] } return b } type coreWorkflowAPI struct { t *testing.T question *runnerproto.Question admissions int core *team.Store w *Witness assignments map[string]runnerproto.Assignment } func (a *coreWorkflowAPI) Call(ctx context.Context, op string, q runnerproto.RunnerRequest, out any) error { assignment := a.assignments[q.AssignmentID] var result any var err error switch op { case "questions": err = a.w.Question(ctx, assignment, q) if err == nil { copy := *q.Question a.question = © result = copy } case "answers": if a.question == nil { return provider.ErrIdentity } copy := *a.question copy.Answer = &runnerproto.QuestionAnswer{Behavior: "allow", UpdatedInput: []byte(`{"answers":{"answer":"Use the failing value case"}}`)} result = []runnerproto.Question{copy} case "question_resolved": err = a.w.Question(ctx, assignment, q) case "validate": err = a.core.ValidateHandle(ctx, q.Scope, q.Handle) case "publication_ready": result, err = a.core.Publication(ctx, q.Scope, q.Handle) case "publication_authorize": result, err = a.core.AuthorizePublication(ctx, q.Scope, q.Handle, q.ActionID, q.RequestID) case "receipts": var receipt team.Receipt receipt, err = a.w.Receipt(ctx, assignment, q) if err == nil { err = a.core.Record(ctx, q.Scope, q.ActionID, receipt) } case "conclude": result, err = a.core.Conclude(ctx, q.Scope, "runner") case "renew": result, err = a.core.Renew(ctx, q.Scope, q.Handle) case "evidence": if q.ArtifactID > 0 { result, err = a.core.Artifact(ctx, q.Scope, q.ArtifactID) } else { result, err = a.core.Artifacts(ctx, q.Scope) } case "intend": result, err = a.core.Intend(ctx, q.Scope, q.Handle, *q.Intent) case "admit": a.admissions++ result, err = a.core.AdmitOnce(ctx, q.Scope, q.Handle, q.ActionID, q.RequestID) case "artifacts": err = a.w.Artifact(ctx, assignment, q) if err == nil { result, err = a.core.Register(ctx, q.Scope, q.Handle, *q.Artifact) } case "events": err = a.w.Events(ctx, assignment, q) if err == nil { var e Event runnerproto.DecodeLimit(strings.NewReader(string(q.Event)), &e, runnerproto.MaxEventBytes+4096) if e.Kind == "turn_completed" || e.Kind == "turn_failed" { var receipt team.Receipt receipt, err = a.w.Receipt(ctx, assignment, q) if err == nil { err = a.core.Record(ctx, q.Scope, q.ActionID, receipt) } } } case "freeze": var f runnerproto.FreezeRequest json.Unmarshal(q.Prepared, &f) err = a.core.FreezeBugEvidence(ctx, q.Scope, q.Handle, f.ExpectedSymptom, f.DefinitionHash, f.ReproductionHash) case "complete": err = a.core.Complete(ctx, q.Scope, q.Handle, *q.Result) case "stop": err = a.core.StopAttempt(ctx, q.Scope, q.Handle, "test_stop") case "contained": if assignment.StepKey == "reproduce" { if _, e := a.core.Claim(ctx, q.Scope, "implement", "runner", "too-early"); e == nil { return fmt.Errorf("dependent claimed before containment") } } err = a.w.Containment(ctx, assignment, q) if err == nil { err = a.core.ContainUntrusted(ctx, q.Scope, q.Handle) } default: return fmt.Errorf("unexpected operation %s", op) } if err != nil { return fmt.Errorf("%s: %w", op, err) } if out != nil && result != nil { return json.Unmarshal(marshal(result), out) } return nil } func decodeCandidate(b []byte) (v team.GitCandidate) { json.Unmarshal(b, &v); return } func apiArtifacts(ctx context.Context, w *Witness, c RunClaim) ([]team.Artifact, error) { var arts []team.Artifact err := w.read(ctx, "artifacts", attemptKey(c.Scope, c.Handle), &arts) return arts, err } type workflowDescription struct{ value forge.Description } func (*workflowDescription) BeforePush() bool { return true } func (d *workflowDescription) Set(_ context.Context, _ forge.Credential, v forge.Description) error { d.value = v return nil } func (d *workflowDescription) Read(_ context.Context, _ forge.Credential, repository, ref string) (forge.Description, error) { return d.value, nil }