cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.28.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/promapi/flags_test.go

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