cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.30.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/promapi/flags_test.go

108lines · 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/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/api/v1/status/flags":
21 w.WriteHeader(200)
22 w.Header().Set("Content-Type", "application/json")
23 _, _ = w.Write([]byte(`{"status":"success","data":{}}`))
24 case "/foo/api/v1/status/flags":
25 w.WriteHeader(200)
26 w.Header().Set("Content-Type", "application/json")
27 _, _ = w.Write([]byte(`{"status":"success","data":{"foo":"bar"}}`))
28 case "/once/api/v1/status/flags":
29 w.WriteHeader(200)
30 w.Header().Set("Content-Type", "application/json")
31 _, _ = w.Write([]byte(`{"status":"success","data":{}}`))
32 case "/slow/api/v1/status/flags":
33 w.WriteHeader(200)
34 w.Header().Set("Content-Type", "application/json")
35 time.Sleep(time.Second)
36 _, _ = w.Write([]byte(`{"status":"success","data":{}}`))
37 case "/error/api/v1/status/flags":
38 w.WriteHeader(500)
39 _, _ = w.Write([]byte("fake error\n"))
40 case "/badYaml/api/v1/status/flags":
41 w.WriteHeader(200)
42 w.Header().Set("Content-Type", "application/json")
43 _, _ = w.Write([]byte(`{"status":"success","data":{"xxx"}}`))
44 default:
45 w.WriteHeader(400)
46 w.Header().Set("Content-Type", "application/json")
47 _, _ = w.Write([]byte(`{"status":"error","errorType":"bad_data","error":"unhandled path"}`))
48 }
49 }))
50 defer srv.Close()
51
52 type testCaseT struct {
53 prefix string
54 timeout time.Duration
55 flags promapi.FlagsResult
56 err string
57 }
58
59 testCases := []testCaseT{
60 {
61 prefix: "/default",
62 timeout: time.Second,
63 flags: promapi.FlagsResult{
64 URI: srv.URL + "/default",
65 Flags: v1.FlagsResult{},
66 },
67 },
68 {
69 prefix: "/foo",
70 timeout: time.Second,
71 flags: promapi.FlagsResult{
72 URI: srv.URL + "/foo",
73 Flags: v1.FlagsResult{"foo": "bar"},
74 },
75 },
76 {
77 prefix: "/slow",
78 timeout: time.Millisecond * 10,
79 err: "connection timeout",
80 },
81 {
82 prefix: "/error",
83 timeout: time.Second,
84 err: "server_error: server error: 500",
85 },
86 {
87 prefix: "/badYaml",
88 timeout: time.Second,
89 err: `bad_response: JSON parse error: expected colon after object key`,
90 },
91 }
92
93 for _, tc := range testCases {
94 t.Run(strings.TrimPrefix(tc.prefix, "/"), func(t *testing.T) {
95 prom := promapi.NewPrometheus("test", srv.URL+tc.prefix, tc.timeout, 1, 1000, 100)
96 prom.StartWorkers()
97 defer prom.Close()
98
99 flags, err := prom.Flags(context.Background())
100 if tc.err != "" {
101 require.EqualError(t, err, tc.err, tc)
102 } else {
103 require.NoError(t, err)
104 require.Equal(t, *flags, tc.flags)
105 }
106 })
107 }
108}
109