cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.49.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/promapi/flags_test.go

115lines · 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 prefix string
55 timeout time.Duration
56 flags promapi.FlagsResult
57 err string
58 }
59
60 testCases := []testCaseT{
61 {
62 prefix: "/default",
63 timeout: time.Second,
64 flags: promapi.FlagsResult{
65 URI: srv.URL + "/default",
66 PublicURI: srv.URL + "/default",
67 Flags: v1.FlagsResult{},
68 },
69 },
70 {
71 prefix: "/foo",
72 timeout: time.Second,
73 flags: promapi.FlagsResult{
74 URI: srv.URL + "/foo",
75 PublicURI: srv.URL + "/foo",
76 Flags: v1.FlagsResult{"foo": "bar"},
77 },
78 },
79 {
80 prefix: "/slow",
81 timeout: time.Millisecond * 10,
82 err: "connection timeout",
83 },
84 {
85 prefix: "/error",
86 timeout: time.Second,
87 err: "server_error: server error: 500",
88 },
89 {
90 prefix: "/badYaml",
91 timeout: time.Second,
92 err: `bad_response: JSON parse error: expected colon after object key`,
93 },
94 }
95
96 for _, tc := range testCases {
97 t.Run(strings.TrimPrefix(tc.prefix, "/"), func(t *testing.T) {
98 fg := promapi.NewFailoverGroup("test", srv.URL+tc.prefix, []*promapi.Prometheus{
99 promapi.NewPrometheus("test", srv.URL+tc.prefix, "", nil, tc.timeout, 1, 100, nil),
100 }, true, "up", nil, nil, nil)
101
102 reg := prometheus.NewRegistry()
103 fg.StartWorkers(reg)
104 defer fg.Close(reg)
105
106 flags, err := fg.Flags(context.Background())
107 if tc.err != "" {
108 require.EqualError(t, err, tc.err, tc)
109 } else {
110 require.NoError(t, err)
111 require.Equal(t, *flags, tc.flags)
112 }
113 })
114 }
115}
116