cloudflare/pint
Publicmirrored fromhttps://github.com/cloudflare/pintAvailable
internal/promapi/flags_test.go
112lines · modecode
| 1 | package promapi_test |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/http/httptest" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | v1 "github.com/prometheus/client_golang/api/prometheus/v1" |
| 11 | "github.com/prometheus/client_golang/prometheus" |
| 12 | "github.com/stretchr/testify/require" |
| 13 | |
| 14 | "github.com/cloudflare/pint/internal/promapi" |
| 15 | ) |
| 16 | |
| 17 | func TestFlags(t *testing.T) { |
| 18 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 19 | switch r.URL.Path { |
| 20 | case "/default" + promapi.APIPathFlags: |
| 21 | w.WriteHeader(http.StatusOK) |
| 22 | w.Header().Set("Content-Type", "application/json") |
| 23 | _, _ = w.Write([]byte(`{"status":"success","data":{}}`)) |
| 24 | case "/foo" + promapi.APIPathFlags: |
| 25 | w.WriteHeader(http.StatusOK) |
| 26 | w.Header().Set("Content-Type", "application/json") |
| 27 | _, _ = w.Write([]byte(`{"status":"success","data":{"foo":"bar"}}`)) |
| 28 | case "/once" + promapi.APIPathFlags: |
| 29 | w.WriteHeader(http.StatusOK) |
| 30 | w.Header().Set("Content-Type", "application/json") |
| 31 | _, _ = w.Write([]byte(`{"status":"success","data":{}}`)) |
| 32 | case "/slow" + promapi.APIPathFlags: |
| 33 | w.WriteHeader(http.StatusOK) |
| 34 | w.Header().Set("Content-Type", "application/json") |
| 35 | time.Sleep(time.Second * 2) |
| 36 | _, _ = w.Write([]byte(`{"status":"success","data":{}}`)) |
| 37 | case "/error" + promapi.APIPathFlags: |
| 38 | w.WriteHeader(http.StatusInternalServerError) |
| 39 | _, _ = w.Write([]byte("fake error\n")) |
| 40 | case "/badYaml" + promapi.APIPathFlags: |
| 41 | w.WriteHeader(http.StatusOK) |
| 42 | w.Header().Set("Content-Type", "application/json") |
| 43 | _, _ = w.Write([]byte(`{"status":"success","data":{"xxx"}}`)) |
| 44 | default: |
| 45 | w.WriteHeader(http.StatusBadRequest) |
| 46 | w.Header().Set("Content-Type", "application/json") |
| 47 | _, _ = w.Write([]byte(`{"status":"error","errorType":"bad_data","error":"unhandled path"}`)) |
| 48 | } |
| 49 | })) |
| 50 | t.Cleanup(srv.Close) |
| 51 | |
| 52 | type testCaseT struct { |
| 53 | flags promapi.FlagsResult |
| 54 | prefix string |
| 55 | err string |
| 56 | timeout time.Duration |
| 57 | } |
| 58 | |
| 59 | testCases := []testCaseT{ |
| 60 | { |
| 61 | prefix: "/default", |
| 62 | timeout: time.Second, |
| 63 | flags: promapi.FlagsResult{ |
| 64 | URI: srv.URL + "/default", |
| 65 | Flags: v1.FlagsResult{}, |
| 66 | }, |
| 67 | }, |
| 68 | { |
| 69 | prefix: "/foo", |
| 70 | timeout: time.Second, |
| 71 | flags: promapi.FlagsResult{ |
| 72 | URI: srv.URL + "/foo", |
| 73 | Flags: v1.FlagsResult{"foo": "bar"}, |
| 74 | }, |
| 75 | }, |
| 76 | { |
| 77 | prefix: "/slow", |
| 78 | timeout: time.Millisecond * 10, |
| 79 | err: "connection timeout", |
| 80 | }, |
| 81 | { |
| 82 | prefix: "/error", |
| 83 | timeout: time.Second, |
| 84 | err: "server_error: 500 Internal Server Error", |
| 85 | }, |
| 86 | { |
| 87 | prefix: "/badYaml", |
| 88 | timeout: time.Second, |
| 89 | err: `bad_response: JSON parse error: expected colon after object key`, |
| 90 | }, |
| 91 | } |
| 92 | |
| 93 | for _, tc := range testCases { |
| 94 | t.Run(strings.TrimPrefix(tc.prefix, "/"), func(t *testing.T) { |
| 95 | fg := promapi.NewFailoverGroup("test", srv.URL+tc.prefix, []*promapi.Prometheus{ |
| 96 | promapi.NewPrometheus("test", srv.URL+tc.prefix, "", nil, tc.timeout, 1, 100, nil), |
| 97 | }, true, "up", nil, nil, nil) |
| 98 | |
| 99 | reg := prometheus.NewRegistry() |
| 100 | fg.StartWorkers(reg) |
| 101 | defer fg.Close(reg) |
| 102 | |
| 103 | flags, err := fg.Flags(t.Context()) |
| 104 | if tc.err != "" { |
| 105 | require.EqualError(t, err, tc.err, tc) |
| 106 | } else { |
| 107 | require.NoError(t, err) |
| 108 | require.Equal(t, *flags, tc.flags) |
| 109 | } |
| 110 | }) |
| 111 | } |
| 112 | } |
| 113 | |