| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "io" |
| 7 | "net/http" |
| 8 | "slices" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 14 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 15 | ) |
| 16 | |
| 17 | func TestInfrastructureFailureReleasesAttempt(t *testing.T) { |
| 18 | for _, cause := range []error{context.Canceled, context.DeadlineExceeded, ErrTransient, team.ErrEvidence} { |
| 19 | t.Run(cause.Error(), func(t *testing.T) { |
| 20 | s, r, _, order := cleanupFixture(t) |
| 21 | r.ServiceInterrupted = errors.Is(cause, context.Canceled) || errors.Is(cause, context.DeadlineExceeded) |
| 22 | _ = s.finishAttempt(context.Background(), r, cause) |
| 23 | if errors.Is(cause, team.ErrEvidence) { |
| 24 | if !slices.Contains(*order, "complete") { |
| 25 | t.Fatal("task outcome not failed", *order) |
| 26 | } |
| 27 | } else if slices.Contains(*order, "complete") || !slices.Contains(*order, "release") { |
| 28 | t.Fatal("infrastructure permanently failed task", *order) |
| 29 | } |
| 30 | }) |
| 31 | } |
| 32 | } |
| 33 | func TestReportPostsRetryExactRequest(t *testing.T) { |
| 34 | for _, op := range []string{"evidence", "artifacts", "freeze", "complete", "questions", "prepare", "publication_ready", "publication_authorize"} { |
| 35 | t.Run(op, func(t *testing.T) { |
| 36 | calls := 0 |
| 37 | var first string |
| 38 | c := &HTTPInstance{origin: "https://equipo.example", bearer: runnerproto.RunnerCredential{Token: "test", ExpiresAt: time.Now().Add(time.Hour)}, client: &http.Client{Transport: transportFunc(func(r *http.Request) (*http.Response, error) { |
| 39 | raw, _ := io.ReadAll(r.Body) |
| 40 | calls++ |
| 41 | if calls == 1 { |
| 42 | first = string(raw) |
| 43 | return nil, io.ErrUnexpectedEOF |
| 44 | } |
| 45 | if string(raw) != first { |
| 46 | t.Error("retry changed request") |
| 47 | } |
| 48 | return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(`{"version":2,"result":null}`))}, nil |
| 49 | })}} |
| 50 | ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 51 | defer cancel() |
| 52 | if err := c.Call(ctx, op, runnerproto.RunnerRequest{Version: 2, RequestID: "exact"}, nil); err != nil || calls != 2 { |
| 53 | t.Fatal("post was not retried", calls, err) |
| 54 | } |
| 55 | }) |
| 56 | } |
| 57 | } |
| 58 | |