cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.79.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/promapi/flags_test.go

121lines · modecode

1package promapi_test
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "strings"
7 "testing"
8 "time"
9
10 v1 "github.com/prometheus/client_golang/api/prometheus/v1"
11 "github.com/prometheus/client_golang/prometheus"
12 "github.com/stretchr/testify/require"
13
14 "github.com/cloudflare/pint/internal/promapi"
15)
16
17func TestFlags(t *testing.T) {
18 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
19 switch r.URL.Path {
20 case "/default" + promapi.APIPathFlags:
21 w.WriteHeader(http.StatusOK)
22 w.Header().Set("Content-Type", "application/json")
23 _, _ = w.Write([]byte(`{"status":"success","data":{}}`))
24 case "/foo" + promapi.APIPathFlags:
25 w.WriteHeader(http.StatusOK)
26 w.Header().Set("Content-Type", "application/json")
27 _, _ = w.Write([]byte(`{"status":"success","data":{"foo":"bar"}}`))
28 case "/once" + promapi.APIPathFlags:
29 w.WriteHeader(http.StatusOK)
30 w.Header().Set("Content-Type", "application/json")
31 _, _ = w.Write([]byte(`{"status":"success","data":{}}`))
32 case "/slow" + promapi.APIPathFlags:
33 w.WriteHeader(http.StatusOK)
34 w.Header().Set("Content-Type", "application/json")
35 time.Sleep(time.Second * 2)
36 _, _ = w.Write([]byte(`{"status":"success","data":{}}`))
37 case "/error" + promapi.APIPathFlags:
38 w.WriteHeader(http.StatusInternalServerError)
39 _, _ = w.Write([]byte("fake error\n"))
40 case "/badYaml" + promapi.APIPathFlags:
41 w.WriteHeader(http.StatusOK)
42 w.Header().Set("Content-Type", "application/json")
43 _, _ = w.Write([]byte(`{"status":"success","data":{"xxx"}}`))
44 case "/apiError" + promapi.APIPathFlags:
45 w.WriteHeader(http.StatusOK)
46 w.Header().Set("Content-Type", "application/json")
47 _, _ = w.Write([]byte(`{"status":"error","errorType":"bad_data","error":"custom error message"}`))
48 default:
49 w.WriteHeader(http.StatusBadRequest)
50 w.Header().Set("Content-Type", "application/json")
51 _, _ = w.Write([]byte(`{"status":"error","errorType":"bad_data","error":"unhandled path"}`))
52 }
53 }))
54 t.Cleanup(srv.Close)
55
56 type testCaseT struct {
57 flags promapi.FlagsResult
58 prefix string
59 err string
60 timeout time.Duration
61 }
62
63 testCases := []testCaseT{
64 {
65 prefix: "/default",
66 timeout: time.Second,
67 flags: promapi.FlagsResult{
68 URI: srv.URL + "/default",
69 Flags: v1.FlagsResult{},
70 },
71 },
72 {
73 prefix: "/foo",
74 timeout: time.Second,
75 flags: promapi.FlagsResult{
76 URI: srv.URL + "/foo",
77 Flags: v1.FlagsResult{"foo": "bar"},
78 },
79 },
80 {
81 prefix: "/slow",
82 timeout: time.Millisecond * 10,
83 err: "connection timeout",
84 },
85 {
86 prefix: "/error",
87 timeout: time.Second,
88 err: "server_error: 500 Internal Server Error",
89 },
90 {
91 prefix: "/badYaml",
92 timeout: time.Second,
93 err: `bad_response: JSON parse error: expected colon after object key`,
94 },
95 {
96 prefix: "/apiError",
97 timeout: time.Second,
98 err: `bad_data: custom error message`,
99 },
100 }
101
102 for _, tc := range testCases {
103 t.Run(strings.TrimPrefix(tc.prefix, "/"), func(t *testing.T) {
104 fg := promapi.NewFailoverGroup("test", srv.URL+tc.prefix, []*promapi.Prometheus{
105 promapi.NewPrometheus("test", srv.URL+tc.prefix, "", nil, tc.timeout, 1, 100, nil),
106 }, true, "up", nil, nil, nil)
107
108 reg := prometheus.NewRegistry()
109 fg.StartWorkers(reg)
110 defer fg.Close(reg)
111
112 flags, err := fg.Flags(t.Context())
113 if tc.err != "" {
114 require.EqualError(t, err, tc.err, tc)
115 } else {
116 require.NoError(t, err)
117 require.Equal(t, *flags, tc.flags)
118 }
119 })
120 }
121}
122