cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.65.3

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/promapi/flags_test.go

113lines · modecode

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