| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "io" |
| 7 | "net/http" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 13 | ) |
| 14 | |
| 15 | func TestCoordinatorHTTPRefusalClassification(t *testing.T) { |
| 16 | for _, status := range []int{401, 403, 404, 409, 422, 429, 500, 503} { |
| 17 | for _, code := range []string{"coordinator.fenced", "coordinator.reset", "coordinator.superseded", "coordinator.revoked", "form.forbidden", "unknown"} { |
| 18 | c := &HTTPInstance{origin: "https://equipo.example", bearer: runnerproto.RunnerCredential{Token: "fixture", ExpiresAt: time.Now().Add(time.Hour)}, client: &http.Client{Transport: transportFunc(func(*http.Request) (*http.Response, error) { |
| 19 | return &http.Response{StatusCode: status, Body: io.NopCloser(strings.NewReader(`{"version":2,"error":"` + code + `"}`))}, nil |
| 20 | })}} |
| 21 | s := CoordinatorService{API: c} |
| 22 | err := s.call(context.Background(), "recover", runnerproto.CoordinatorRequest{}, nil) |
| 23 | var refusal *RefusalError |
| 24 | final := errors.As(err, &refusal) && refusal.CoordinatorDefinitive() |
| 25 | want := (status >= 400 && status < 500) && strings.HasPrefix(code, "coordinator.") |
| 26 | if final != want || !final && !errors.Is(err, ErrTransient) { |
| 27 | t.Fatalf("status=%d code=%s final=%v error=%v", status, code, final, err) |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |