cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/promapi/config_test.go
136lines · modecode
| 1 | package promapi_test |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/stretchr/testify/require" |
| 13 | |
| 14 | "github.com/cloudflare/pint/internal/promapi" |
| 15 | ) |
| 16 | |
| 17 | func TestConfig(t *testing.T) { |
| 18 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 19 | switch r.URL.Path { |
| 20 | case "/30s/api/v1/status/config": |
| 21 | w.WriteHeader(200) |
| 22 | w.Header().Set("Content-Type", "application/json") |
| 23 | _, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"global:\n scrape_interval: 30s\n"}}`)) |
| 24 | case "/1m/api/v1/status/config": |
| 25 | w.WriteHeader(200) |
| 26 | w.Header().Set("Content-Type", "application/json") |
| 27 | _, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"global:\n scrape_interval: 1m\n"}}`)) |
| 28 | case "/default/api/v1/status/config": |
| 29 | w.WriteHeader(200) |
| 30 | w.Header().Set("Content-Type", "application/json") |
| 31 | _, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"global:\n {}\n"}}`)) |
| 32 | case "/once/api/v1/status/config": |
| 33 | w.WriteHeader(200) |
| 34 | w.Header().Set("Content-Type", "application/json") |
| 35 | _, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"global:\n {}\n"}}`)) |
| 36 | case "/slow/api/v1/status/config": |
| 37 | w.WriteHeader(200) |
| 38 | w.Header().Set("Content-Type", "application/json") |
| 39 | time.Sleep(time.Second) |
| 40 | _, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"global:\n {}\n"}}`)) |
| 41 | case "/error/api/v1/status/config": |
| 42 | w.WriteHeader(500) |
| 43 | _, _ = w.Write([]byte("fake error\n")) |
| 44 | case "/badYaml/api/v1/status/config": |
| 45 | w.WriteHeader(200) |
| 46 | w.Header().Set("Content-Type", "application/json") |
| 47 | _, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"invalid yaml"}}`)) |
| 48 | default: |
| 49 | w.WriteHeader(400) |
| 50 | w.Header().Set("Content-Type", "application/json") |
| 51 | _, _ = w.Write([]byte(`{"status":"error","errorType":"bad_data","error":"unhandled path"}`)) |
| 52 | } |
| 53 | })) |
| 54 | defer srv.Close() |
| 55 | |
| 56 | type testCaseT struct { |
| 57 | prefix string |
| 58 | timeout time.Duration |
| 59 | cfg promapi.ConfigResult |
| 60 | err string |
| 61 | } |
| 62 | |
| 63 | defaults := promapi.PrometheusConfig{ |
| 64 | Global: promapi.ConfigSectionGlobal{ |
| 65 | ScrapeInterval: time.Minute, |
| 66 | ScrapeTimeout: time.Second * 10, |
| 67 | EvaluationInterval: time.Minute, |
| 68 | ExternalLabels: nil, |
| 69 | }, |
| 70 | } |
| 71 | |
| 72 | testCases := []testCaseT{ |
| 73 | { |
| 74 | prefix: "/default", |
| 75 | timeout: time.Second, |
| 76 | cfg: promapi.ConfigResult{ |
| 77 | URI: srv.URL + "/default", |
| 78 | Config: defaults, |
| 79 | }, |
| 80 | }, |
| 81 | { |
| 82 | prefix: "/1m", |
| 83 | timeout: time.Second, |
| 84 | cfg: promapi.ConfigResult{ |
| 85 | URI: srv.URL + "/1m", |
| 86 | Config: defaults, |
| 87 | }, |
| 88 | }, |
| 89 | { |
| 90 | prefix: "/30s", |
| 91 | timeout: time.Second, |
| 92 | cfg: promapi.ConfigResult{ |
| 93 | URI: srv.URL + "/30s", |
| 94 | Config: promapi.PrometheusConfig{ |
| 95 | Global: promapi.ConfigSectionGlobal{ |
| 96 | ScrapeInterval: time.Second * 30, |
| 97 | ScrapeTimeout: time.Second * 10, |
| 98 | EvaluationInterval: time.Minute, |
| 99 | ExternalLabels: nil, |
| 100 | }, |
| 101 | }, |
| 102 | }, |
| 103 | }, |
| 104 | { |
| 105 | prefix: "/slow", |
| 106 | timeout: time.Millisecond * 10, |
| 107 | err: "connection timeout", |
| 108 | }, |
| 109 | { |
| 110 | prefix: "/error", |
| 111 | timeout: time.Second, |
| 112 | err: "server_error: server error: 500", |
| 113 | }, |
| 114 | { |
| 115 | prefix: "/badYaml", |
| 116 | timeout: time.Second, |
| 117 | err: fmt.Sprintf("failed to decode config data in %s/badYaml response: yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `invalid...` into promapi.PrometheusConfig", srv.URL), |
| 118 | }, |
| 119 | } |
| 120 | |
| 121 | for _, tc := range testCases { |
| 122 | t.Run(strings.TrimPrefix(tc.prefix, "/"), func(t *testing.T) { |
| 123 | prom := promapi.NewPrometheus("test", srv.URL+tc.prefix, tc.timeout, 1, 1000, 100) |
| 124 | prom.StartWorkers() |
| 125 | defer prom.Close() |
| 126 | |
| 127 | cfg, err := prom.Config(context.Background()) |
| 128 | if tc.err != "" { |
| 129 | require.EqualError(t, err, tc.err, tc) |
| 130 | } else { |
| 131 | require.NoError(t, err) |
| 132 | require.Equal(t, *cfg, tc.cfg) |
| 133 | } |
| 134 | }) |
| 135 | } |
| 136 | } |
| 137 | |