cloudflare/pint

Public

mirrored from https://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.58.1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

internal/promapi/config_test.go

194lines · modecode

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