cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/promapi/config_test.go
192lines · modecode
| 1 | package promapi_test |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/prometheus/client_golang/prometheus" |
| 14 | "github.com/stretchr/testify/assert" |
| 15 | "github.com/stretchr/testify/require" |
| 16 | |
| 17 | "github.com/cloudflare/pint/internal/promapi" |
| 18 | ) |
| 19 | |
| 20 | func TestConfig(t *testing.T) { |
| 21 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 22 | switch r.URL.Path { |
| 23 | case "/30s/api/v1/status/config": |
| 24 | w.WriteHeader(200) |
| 25 | w.Header().Set("Content-Type", "application/json") |
| 26 | _, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"global:\n scrape_interval: 30s\n"}}`)) |
| 27 | case "/1m/api/v1/status/config": |
| 28 | w.WriteHeader(200) |
| 29 | w.Header().Set("Content-Type", "application/json") |
| 30 | _, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"global:\n scrape_interval: 1m\n"}}`)) |
| 31 | case "/default/api/v1/status/config": |
| 32 | w.WriteHeader(200) |
| 33 | w.Header().Set("Content-Type", "application/json") |
| 34 | _, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"global:\n {}\n"}}`)) |
| 35 | case "/once/api/v1/status/config": |
| 36 | w.WriteHeader(200) |
| 37 | w.Header().Set("Content-Type", "application/json") |
| 38 | _, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"global:\n {}\n"}}`)) |
| 39 | case "/slow/api/v1/status/config": |
| 40 | w.WriteHeader(200) |
| 41 | w.Header().Set("Content-Type", "application/json") |
| 42 | time.Sleep(time.Second * 2) |
| 43 | _, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"global:\n {}\n"}}`)) |
| 44 | case "/error/api/v1/status/config": |
| 45 | w.WriteHeader(500) |
| 46 | _, _ = w.Write([]byte("fake error\n")) |
| 47 | case "/badYaml/api/v1/status/config": |
| 48 | w.WriteHeader(200) |
| 49 | w.Header().Set("Content-Type", "application/json") |
| 50 | _, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"invalid yaml"}}`)) |
| 51 | default: |
| 52 | w.WriteHeader(400) |
| 53 | w.Header().Set("Content-Type", "application/json") |
| 54 | _, _ = w.Write([]byte(`{"status":"error","errorType":"bad_data","error":"unhandled path"}`)) |
| 55 | } |
| 56 | })) |
| 57 | defer srv.Close() |
| 58 | |
| 59 | type testCaseT struct { |
| 60 | prefix string |
| 61 | err string |
| 62 | cfg promapi.ConfigResult |
| 63 | timeout time.Duration |
| 64 | } |
| 65 | |
| 66 | defaults := promapi.PrometheusConfig{ |
| 67 | Global: promapi.ConfigSectionGlobal{ |
| 68 | ScrapeInterval: time.Minute, |
| 69 | ScrapeTimeout: time.Second * 10, |
| 70 | EvaluationInterval: time.Minute, |
| 71 | ExternalLabels: nil, |
| 72 | }, |
| 73 | } |
| 74 | |
| 75 | testCases := []testCaseT{ |
| 76 | { |
| 77 | prefix: "/default", |
| 78 | timeout: time.Second, |
| 79 | cfg: promapi.ConfigResult{ |
| 80 | URI: srv.URL + "/default", |
| 81 | Config: defaults, |
| 82 | }, |
| 83 | }, |
| 84 | { |
| 85 | prefix: "/1m", |
| 86 | timeout: time.Second, |
| 87 | cfg: promapi.ConfigResult{ |
| 88 | URI: srv.URL + "/1m", |
| 89 | Config: defaults, |
| 90 | }, |
| 91 | }, |
| 92 | { |
| 93 | prefix: "/30s", |
| 94 | timeout: time.Second, |
| 95 | cfg: promapi.ConfigResult{ |
| 96 | URI: srv.URL + "/30s", |
| 97 | Config: promapi.PrometheusConfig{ |
| 98 | Global: promapi.ConfigSectionGlobal{ |
| 99 | ScrapeInterval: time.Second * 30, |
| 100 | ScrapeTimeout: time.Second * 10, |
| 101 | EvaluationInterval: time.Minute, |
| 102 | ExternalLabels: nil, |
| 103 | }, |
| 104 | }, |
| 105 | }, |
| 106 | }, |
| 107 | { |
| 108 | prefix: "/slow", |
| 109 | timeout: time.Millisecond * 10, |
| 110 | err: "connection timeout", |
| 111 | }, |
| 112 | { |
| 113 | prefix: "/error", |
| 114 | timeout: time.Second, |
| 115 | err: "server_error: server error: 500", |
| 116 | }, |
| 117 | { |
| 118 | prefix: "/badYaml", |
| 119 | timeout: time.Second, |
| 120 | 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), |
| 121 | }, |
| 122 | } |
| 123 | |
| 124 | for _, tc := range testCases { |
| 125 | t.Run(strings.TrimPrefix(tc.prefix, "/"), func(t *testing.T) { |
| 126 | prom := promapi.NewPrometheus("test", srv.URL+tc.prefix, "", nil, tc.timeout, 1, 100, nil) |
| 127 | prom.StartWorkers() |
| 128 | defer prom.Close() |
| 129 | |
| 130 | cfg, err := prom.Config(context.Background(), time.Minute) |
| 131 | if tc.err != "" { |
| 132 | require.EqualError(t, err, tc.err, tc) |
| 133 | } else { |
| 134 | require.NoError(t, err) |
| 135 | require.Equal(t, *cfg, tc.cfg) |
| 136 | } |
| 137 | }) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | func TestConfigHeaders(t *testing.T) { |
| 142 | type testCaseT struct { |
| 143 | config map[string]string |
| 144 | request map[string]string |
| 145 | shouldFail bool |
| 146 | } |
| 147 | |
| 148 | testCases := []testCaseT{ |
| 149 | { |
| 150 | config: nil, |
| 151 | request: nil, |
| 152 | }, |
| 153 | { |
| 154 | config: nil, |
| 155 | request: map[string]string{"X-Foo": "bar"}, |
| 156 | shouldFail: true, |
| 157 | }, |
| 158 | { |
| 159 | config: map[string]string{"X-Foo": "bar", "X-Bar": "foo"}, |
| 160 | request: map[string]string{"X-Foo": "bar", "X-Bar": "foo"}, |
| 161 | }, |
| 162 | } |
| 163 | |
| 164 | for i, tc := range testCases { |
| 165 | t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 166 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 167 | for k, v := range tc.request { |
| 168 | if tc.shouldFail { |
| 169 | assert.NotEqual(t, r.Header.Get(k), v) |
| 170 | } else { |
| 171 | assert.Equal(t, r.Header.Get(k), v) |
| 172 | } |
| 173 | } |
| 174 | w.WriteHeader(200) |
| 175 | w.Header().Set("Content-Type", "application/json") |
| 176 | _, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"global:\n scrape_interval: 30s\n"}}`)) |
| 177 | })) |
| 178 | defer srv.Close() |
| 179 | |
| 180 | fg := promapi.NewFailoverGroup("test", srv.URL, []*promapi.Prometheus{ |
| 181 | promapi.NewPrometheus("test", srv.URL, "", tc.config, time.Second, 1, 100, nil), |
| 182 | }, true, "up", nil, nil, nil) |
| 183 | |
| 184 | reg := prometheus.NewRegistry() |
| 185 | fg.StartWorkers(reg) |
| 186 | defer fg.Close(reg) |
| 187 | |
| 188 | _, err := fg.Config(context.Background(), 0) |
| 189 | require.NoError(t, err) |
| 190 | }) |
| 191 | } |
| 192 | } |
| 193 | |