cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.13.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/promapi/config_test.go

165lines · modecode

1package promapi_test
2
3import (
4 "context"
5 "fmt"
6 "net/http"
7 "net/http/httptest"
8 "strings"
9 "sync"
10 "testing"
11 "time"
12
13 "github.com/stretchr/testify/assert"
14
15 "github.com/cloudflare/pint/internal/promapi"
16)
17
18func TestConfig(t *testing.T) {
19 done := sync.Map{}
20 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
21 switch r.URL.Path {
22 case "/30s/api/v1/status/config":
23 w.WriteHeader(200)
24 w.Header().Set("Content-Type", "application/json")
25 _, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"global:\n scrape_interval: 30s\n"}}`))
26 case "/1m/api/v1/status/config":
27 w.WriteHeader(200)
28 w.Header().Set("Content-Type", "application/json")
29 _, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"global:\n scrape_interval: 1m\n"}}`))
30 case "/default/api/v1/status/config":
31 w.WriteHeader(200)
32 w.Header().Set("Content-Type", "application/json")
33 _, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"global:\n {}\n"}}`))
34 case "/once/api/v1/status/config":
35 if _, wasDone := done.Load(r.URL.Path); wasDone {
36 w.WriteHeader(500)
37 _, _ = w.Write([]byte("path already requested\n"))
38 return
39 }
40 w.WriteHeader(200)
41 w.Header().Set("Content-Type", "application/json")
42 _, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"global:\n {}\n"}}`))
43 done.Store(r.URL.Path, true)
44 case "/slow/api/v1/status/config":
45 w.WriteHeader(200)
46 w.Header().Set("Content-Type", "application/json")
47 time.Sleep(time.Second)
48 _, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"global:\n {}\n"}}`))
49 case "/error/api/v1/status/config":
50 w.WriteHeader(500)
51 _, _ = w.Write([]byte("fake error\n"))
52 case "/badYaml/api/v1/status/config":
53 w.WriteHeader(200)
54 w.Header().Set("Content-Type", "application/json")
55 _, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"invalid yaml"}}`))
56 default:
57 w.WriteHeader(400)
58 w.Header().Set("Content-Type", "application/json")
59 _, _ = w.Write([]byte(`{"status":"error","errorType":"bad_data","error":"unhandled path"}`))
60 }
61 }))
62 defer srv.Close()
63
64 type testCaseT struct {
65 prefix string
66 timeout time.Duration
67 cfg promapi.PrometheusConfig
68 err string
69 runs int
70 }
71
72 defaults := promapi.PrometheusConfig{
73 Global: promapi.ConfigSectionGlobal{
74 ScrapeInterval: time.Minute,
75 ScrapeTimeout: time.Second * 10,
76 EvaluationInterval: time.Minute,
77 ExternalLabels: nil,
78 },
79 }
80
81 testCases := []testCaseT{
82 {
83 prefix: "/default",
84 timeout: time.Second,
85 cfg: defaults,
86 runs: 5,
87 },
88 {
89 prefix: "/1m",
90 timeout: time.Second,
91 cfg: defaults,
92 runs: 5,
93 },
94 {
95 prefix: "/30s",
96 timeout: time.Second,
97 cfg: promapi.PrometheusConfig{
98 Global: promapi.ConfigSectionGlobal{
99 ScrapeInterval: time.Second * 30,
100 ScrapeTimeout: time.Second * 10,
101 EvaluationInterval: time.Minute,
102 ExternalLabels: nil,
103 },
104 },
105 runs: 1,
106 },
107 {
108 prefix: "/slow",
109 timeout: time.Millisecond * 10,
110 err: fmt.Sprintf(`failed to query Prometheus config: Get "%s/slow/api/v1/status/config": context deadline exceeded`, srv.URL),
111 runs: 5,
112 },
113 {
114 prefix: "/error",
115 timeout: time.Second,
116 err: "failed to query Prometheus config: server_error: server error: 500",
117 runs: 5,
118 },
119 {
120 prefix: "/badYaml",
121 timeout: time.Second,
122 err: fmt.Sprintf("failed to decode config data in %s/badYaml response: yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `invalid...` into promapi.PrometheusConfig", srv.URL),
123 runs: 5,
124 },
125 {
126 prefix: "/once",
127 timeout: time.Second,
128 cfg: defaults,
129 runs: 10,
130 },
131 // make sure /once fails on second query
132 {
133 prefix: "/once",
134 timeout: time.Second,
135 runs: 2,
136 err: "failed to query Prometheus config: server_error: server error: 500",
137 },
138 }
139
140 for _, tc := range testCases {
141 t.Run(strings.TrimPrefix(tc.prefix, "/"), func(t *testing.T) {
142 assert := assert.New(t)
143
144 prom := promapi.NewPrometheus("test", srv.URL+tc.prefix, tc.timeout)
145
146 wg := sync.WaitGroup{}
147 wg.Add(tc.runs)
148 for i := 1; i <= tc.runs; i++ {
149 go func() {
150 cfg, err := prom.Config(context.Background())
151 if tc.err != "" {
152 assert.EqualError(err, tc.err, tc)
153 } else {
154 assert.NoError(err)
155 }
156 if cfg != nil {
157 assert.Equal(*cfg, tc.cfg)
158 }
159 wg.Done()
160 }()
161 }
162 wg.Wait()
163 })
164 }
165}
166