cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/promapi/metadata_test.go
118lines · modecode
| 1 | package promapi_test |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | v1 "github.com/prometheus/client_golang/api/prometheus/v1" |
| 11 | "github.com/stretchr/testify/require" |
| 12 | |
| 13 | "github.com/cloudflare/pint/internal/promapi" |
| 14 | ) |
| 15 | |
| 16 | func TestMetadata(t *testing.T) { |
| 17 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 18 | err := r.ParseForm() |
| 19 | if err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | metric := r.Form.Get("metric") |
| 23 | |
| 24 | switch metric { |
| 25 | case "gauge": |
| 26 | w.WriteHeader(200) |
| 27 | w.Header().Set("Content-Type", "application/json") |
| 28 | _, _ = w.Write([]byte(`{"status":"success","data":{"gauge":[{"type":"gauge","help":"Text","unit":""}]}}`)) |
| 29 | case "counter": |
| 30 | w.WriteHeader(200) |
| 31 | w.Header().Set("Content-Type", "application/json") |
| 32 | _, _ = w.Write([]byte(`{"status":"success","data":{"counter":[{"type":"counter","help":"Text","unit":""}]}}`)) |
| 33 | case "notfound": |
| 34 | w.WriteHeader(200) |
| 35 | w.Header().Set("Content-Type", "application/json") |
| 36 | _, _ = w.Write([]byte(`{"status":"success","data":{}}`)) |
| 37 | case "once": |
| 38 | w.WriteHeader(200) |
| 39 | w.Header().Set("Content-Type", "application/json") |
| 40 | _, _ = w.Write([]byte(`{"status":"success","data":{"once":[{"type":"gauge","help":"Text","unit":""}]}}`)) |
| 41 | case "slow": |
| 42 | w.WriteHeader(200) |
| 43 | w.Header().Set("Content-Type", "application/json") |
| 44 | time.Sleep(time.Second) |
| 45 | _, _ = w.Write([]byte(`{"status":"success","data":{"once":[{"type":"gauge","help":"Text","unit":""}]}}`)) |
| 46 | case "error": |
| 47 | w.WriteHeader(500) |
| 48 | _, _ = w.Write([]byte("fake error\n")) |
| 49 | default: |
| 50 | w.WriteHeader(400) |
| 51 | w.Header().Set("Content-Type", "application/json") |
| 52 | _, _ = w.Write([]byte(`{"status":"error","errorType":"bad_data","error":"unhandled metric"}`)) |
| 53 | } |
| 54 | })) |
| 55 | defer srv.Close() |
| 56 | |
| 57 | type testCaseT struct { |
| 58 | metric string |
| 59 | timeout time.Duration |
| 60 | metadata promapi.MetadataResult |
| 61 | err string |
| 62 | } |
| 63 | |
| 64 | testCases := []testCaseT{ |
| 65 | { |
| 66 | metric: "gauge", |
| 67 | timeout: time.Second, |
| 68 | metadata: promapi.MetadataResult{ |
| 69 | URI: srv.URL, |
| 70 | Metadata: []v1.Metadata{{Type: "gauge", Help: "Text", Unit: ""}}, |
| 71 | }, |
| 72 | }, |
| 73 | { |
| 74 | metric: "counter", |
| 75 | timeout: time.Second, |
| 76 | metadata: promapi.MetadataResult{ |
| 77 | URI: srv.URL, |
| 78 | Metadata: []v1.Metadata{{Type: "counter", Help: "Text", Unit: ""}}, |
| 79 | }, |
| 80 | }, |
| 81 | { |
| 82 | metric: "slow", |
| 83 | timeout: time.Millisecond * 10, |
| 84 | err: "connection timeout", |
| 85 | }, |
| 86 | { |
| 87 | metric: "error", |
| 88 | timeout: time.Second, |
| 89 | err: "server_error: server error: 500", |
| 90 | }, |
| 91 | { |
| 92 | metric: "once", |
| 93 | timeout: time.Second, |
| 94 | metadata: promapi.MetadataResult{ |
| 95 | URI: srv.URL, |
| 96 | Metadata: []v1.Metadata{{Type: "gauge", Help: "Text", Unit: ""}}, |
| 97 | }, |
| 98 | }, |
| 99 | } |
| 100 | |
| 101 | for _, tc := range testCases { |
| 102 | t.Run(tc.metric, func(t *testing.T) { |
| 103 | prom := promapi.NewPrometheus("test", srv.URL, tc.timeout, 1, 100, 100) |
| 104 | prom.StartWorkers() |
| 105 | defer prom.Close() |
| 106 | |
| 107 | metadata, err := prom.Metadata(context.Background(), tc.metric) |
| 108 | if tc.err != "" { |
| 109 | require.EqualError(t, err, tc.err, tc) |
| 110 | } else { |
| 111 | require.NoError(t, err) |
| 112 | } |
| 113 | if metadata != nil { |
| 114 | require.Equal(t, *metadata, tc.metadata) |
| 115 | } |
| 116 | }) |
| 117 | } |
| 118 | } |
| 119 | |