cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/promapi/metadata_test.go
133lines · 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 "mixed": |
| 34 | w.WriteHeader(200) |
| 35 | w.Header().Set("Content-Type", "application/json") |
| 36 | _, _ = w.Write([]byte(`{"status":"success","data":{"mixed":[{"type":"gauge","help":"Text1","unit":"abc"},{"type":"counter","help":"Text2","unit":""}]}}`)) |
| 37 | case "notfound": |
| 38 | w.WriteHeader(200) |
| 39 | w.Header().Set("Content-Type", "application/json") |
| 40 | _, _ = w.Write([]byte(`{"status":"success","data":{}}`)) |
| 41 | case "once": |
| 42 | w.WriteHeader(200) |
| 43 | w.Header().Set("Content-Type", "application/json") |
| 44 | _, _ = w.Write([]byte(`{"status":"success","data":{"once":[{"type":"gauge","help":"Text","unit":""}]}}`)) |
| 45 | case "slow": |
| 46 | w.WriteHeader(200) |
| 47 | w.Header().Set("Content-Type", "application/json") |
| 48 | time.Sleep(time.Second) |
| 49 | _, _ = w.Write([]byte(`{"status":"success","data":{"once":[{"type":"gauge","help":"Text","unit":""}]}}`)) |
| 50 | case "error": |
| 51 | w.WriteHeader(500) |
| 52 | _, _ = w.Write([]byte("fake error\n")) |
| 53 | default: |
| 54 | w.WriteHeader(400) |
| 55 | w.Header().Set("Content-Type", "application/json") |
| 56 | _, _ = w.Write([]byte(`{"status":"error","errorType":"bad_data","error":"unhandled metric"}`)) |
| 57 | } |
| 58 | })) |
| 59 | defer srv.Close() |
| 60 | |
| 61 | type testCaseT struct { |
| 62 | metric string |
| 63 | timeout time.Duration |
| 64 | metadata promapi.MetadataResult |
| 65 | err string |
| 66 | } |
| 67 | |
| 68 | testCases := []testCaseT{ |
| 69 | { |
| 70 | metric: "gauge", |
| 71 | timeout: time.Second, |
| 72 | metadata: promapi.MetadataResult{ |
| 73 | URI: srv.URL, |
| 74 | Metadata: []v1.Metadata{{Type: "gauge", Help: "Text", Unit: ""}}, |
| 75 | }, |
| 76 | }, |
| 77 | { |
| 78 | metric: "counter", |
| 79 | timeout: time.Second, |
| 80 | metadata: promapi.MetadataResult{ |
| 81 | URI: srv.URL, |
| 82 | Metadata: []v1.Metadata{{Type: "counter", Help: "Text", Unit: ""}}, |
| 83 | }, |
| 84 | }, |
| 85 | { |
| 86 | metric: "mixed", |
| 87 | timeout: time.Second, |
| 88 | metadata: promapi.MetadataResult{ |
| 89 | URI: srv.URL, |
| 90 | Metadata: []v1.Metadata{ |
| 91 | {Type: "gauge", Help: "Text1", Unit: "abc"}, |
| 92 | {Type: "counter", Help: "Text2", Unit: ""}, |
| 93 | }, |
| 94 | }, |
| 95 | }, |
| 96 | { |
| 97 | metric: "slow", |
| 98 | timeout: time.Millisecond * 10, |
| 99 | err: "connection timeout", |
| 100 | }, |
| 101 | { |
| 102 | metric: "error", |
| 103 | timeout: time.Second, |
| 104 | err: "server_error: server error: 500", |
| 105 | }, |
| 106 | { |
| 107 | metric: "once", |
| 108 | timeout: time.Second, |
| 109 | metadata: promapi.MetadataResult{ |
| 110 | URI: srv.URL, |
| 111 | Metadata: []v1.Metadata{{Type: "gauge", Help: "Text", Unit: ""}}, |
| 112 | }, |
| 113 | }, |
| 114 | } |
| 115 | |
| 116 | for _, tc := range testCases { |
| 117 | t.Run(tc.metric, func(t *testing.T) { |
| 118 | prom := promapi.NewPrometheus("test", srv.URL, tc.timeout, 1, 100, 100) |
| 119 | prom.StartWorkers() |
| 120 | defer prom.Close() |
| 121 | |
| 122 | metadata, err := prom.Metadata(context.Background(), tc.metric) |
| 123 | if tc.err != "" { |
| 124 | require.EqualError(t, err, tc.err, tc) |
| 125 | } else { |
| 126 | require.NoError(t, err) |
| 127 | } |
| 128 | if metadata != nil { |
| 129 | require.Equal(t, *metadata, tc.metadata) |
| 130 | } |
| 131 | }) |
| 132 | } |
| 133 | } |
| 134 | |