-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathprobes_test.go
More file actions
339 lines (270 loc) · 11 KB
/
probes_test.go
File metadata and controls
339 lines (270 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//go:build integration
package e2e
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
"github.com/efficientgo/core/testutil"
"github.com/efficientgo/e2e"
)
func TestProbes_CreateAndGetProbe(t *testing.T) {
e, err := e2e.New(e2e.WithName(uniqueE2ENetworkName(t)))
testutil.Ok(t, err)
t.Cleanup(e.Close)
prepareConfigsAndCerts(t, e)
_, token, rateLimiterAddr := startBaseServices(t, e)
probesEndpoint := startServicesForProbes(t, e)
api, err := newObservatoriumAPIService(
e,
withProbesEndpoint("http://"+probesEndpoint),
withRateLimiter(rateLimiterAddr),
withMetricsEndpoints("http://dummy-metrics:9090", "http://dummy-metrics:9090"), // Add minimal metrics endpoints since probes are mounted within metrics route group
)
testutil.Ok(t, err)
testutil.Ok(t, e2e.StartAndWaitReady(api))
t.Run("create-and-get-probe", func(t *testing.T) {
// Test payload for creating a probe
probePayload := map[string]interface{}{
"static_url": "http://example.com/test",
"status": "pending",
"labels": map[string]string{
"env": "test",
},
}
// Create probe
payloadBytes, err := json.Marshal(probePayload)
testutil.Ok(t, err)
createURL := fmt.Sprintf("https://%s/api/metrics/v1/%s/probes", api.Endpoint("https"), defaultTenantName)
req, err := http.NewRequest("POST", createURL, bytes.NewBuffer(payloadBytes))
testutil.Ok(t, err)
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: getTLSClientConfig(t, e),
},
}
resp, err := client.Do(req)
testutil.Ok(t, err)
defer resp.Body.Close()
testutil.Equals(t, http.StatusCreated, resp.StatusCode)
// Parse response to get probe ID
body, err := io.ReadAll(resp.Body)
testutil.Ok(t, err)
var createdProbe map[string]interface{}
testutil.Ok(t, json.Unmarshal(body, &createdProbe))
probeID, ok := createdProbe["id"].(string)
testutil.Assert(t, ok, "probe ID should be a string")
testutil.Assert(t, probeID != "", "probe ID should not be empty")
// Get probe by ID
getURL := fmt.Sprintf("https://%s/api/metrics/v1/%s/probes/%s", api.Endpoint("https"), defaultTenantName, probeID)
getReq, err := http.NewRequest("GET", getURL, nil)
testutil.Ok(t, err)
getReq.Header.Set("Authorization", "Bearer "+token)
getResp, err := client.Do(getReq)
testutil.Ok(t, err)
defer getResp.Body.Close()
testutil.Equals(t, http.StatusOK, getResp.StatusCode)
getBody, err := io.ReadAll(getResp.Body)
testutil.Ok(t, err)
var retrievedProbe map[string]interface{}
testutil.Ok(t, json.Unmarshal(getBody, &retrievedProbe))
// Verify the retrieved probe matches what we created
testutil.Equals(t, probeID, retrievedProbe["id"].(string))
testutil.Equals(t, "http://example.com/test", retrievedProbe["static_url"].(string))
testutil.Equals(t, "pending", retrievedProbe["status"].(string))
// Verify labels (including system labels)
labels, ok := retrievedProbe["labels"].(map[string]interface{})
testutil.Assert(t, ok, "labels should be a map")
testutil.Equals(t, "test", labels["env"].(string))
// System labels should be present
testutil.Assert(t, labels["app"] != nil, "system app label should be present")
testutil.Assert(t, labels["rhobs-synthetics/static-url-hash"] != nil, "system url-hash label should be present")
testutil.Assert(t, labels["rhobs-synthetics/status"] != nil, "system status label should be present")
})
}
func TestProbes_ListProbes(t *testing.T) {
e, err := e2e.New(e2e.WithName(uniqueE2ENetworkName(t)))
testutil.Ok(t, err)
t.Cleanup(e.Close)
prepareConfigsAndCerts(t, e)
_, token, rateLimiterAddr := startBaseServices(t, e)
probesEndpoint := startServicesForProbes(t, e)
api, err := newObservatoriumAPIService(
e,
withProbesEndpoint("http://"+probesEndpoint),
withRateLimiter(rateLimiterAddr),
withMetricsEndpoints("http://dummy-metrics:9090", "http://dummy-metrics:9090"), // Add minimal metrics endpoints since probes are mounted within metrics route group
)
testutil.Ok(t, err)
testutil.Ok(t, e2e.StartAndWaitReady(api))
t.Run("list-probes", func(t *testing.T) {
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: getTLSClientConfig(t, e),
},
}
// Create a couple of test probes
for i, env := range []string{"prod", "test"} {
probePayload := map[string]interface{}{
"static_url": fmt.Sprintf("http://example.com/probe-%d", i),
"status": "pending",
"labels": map[string]string{
"env": env,
},
}
payloadBytes, err := json.Marshal(probePayload)
testutil.Ok(t, err)
createURL := fmt.Sprintf("https://%s/api/metrics/v1/%s/probes", api.Endpoint("https"), defaultTenantName)
req, err := http.NewRequest("POST", createURL, bytes.NewBuffer(payloadBytes))
testutil.Ok(t, err)
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
testutil.Ok(t, err)
resp.Body.Close()
testutil.Equals(t, http.StatusCreated, resp.StatusCode)
}
// List all probes
listURL := fmt.Sprintf("https://%s/api/metrics/v1/%s/probes", api.Endpoint("https"), defaultTenantName)
listReq, err := http.NewRequest("GET", listURL, nil)
testutil.Ok(t, err)
listReq.Header.Set("Authorization", "Bearer "+token)
listResp, err := client.Do(listReq)
testutil.Ok(t, err)
defer listResp.Body.Close()
testutil.Equals(t, http.StatusOK, listResp.StatusCode)
listBody, err := io.ReadAll(listResp.Body)
testutil.Ok(t, err)
var response map[string]interface{}
testutil.Ok(t, json.Unmarshal(listBody, &response))
probes, ok := response["probes"].([]interface{})
testutil.Assert(t, ok, "probes should be an array")
// Should have at least 2 probes
testutil.Assert(t, len(probes) >= 2, "should have at least 2 probes")
// Test label selector filtering
filterURL := fmt.Sprintf("https://%s/api/metrics/v1/%s/probes?label_selector=env=prod", api.Endpoint("https"), defaultTenantName)
filterReq, err := http.NewRequest("GET", filterURL, nil)
testutil.Ok(t, err)
filterReq.Header.Set("Authorization", "Bearer "+token)
filterResp, err := client.Do(filterReq)
testutil.Ok(t, err)
defer filterResp.Body.Close()
testutil.Equals(t, http.StatusOK, filterResp.StatusCode)
filterBody, err := io.ReadAll(filterResp.Body)
testutil.Ok(t, err)
var filteredResponse map[string]interface{}
testutil.Ok(t, json.Unmarshal(filterBody, &filteredResponse))
filteredProbes, ok := filteredResponse["probes"].([]interface{})
testutil.Assert(t, ok, "filtered probes should be an array")
// Should have exactly 1 probe with env=prod
testutil.Equals(t, 1, len(filteredProbes))
probe, ok := filteredProbes[0].(map[string]interface{})
testutil.Assert(t, ok, "probe should be a map")
labels, ok := probe["labels"].(map[string]interface{})
testutil.Assert(t, ok, "labels should be a map")
testutil.Equals(t, "prod", labels["env"].(string))
})
}
func TestProbes_CreateProbeConflict(t *testing.T) {
e, err := e2e.New(e2e.WithName(uniqueE2ENetworkName(t)))
testutil.Ok(t, err)
t.Cleanup(e.Close)
prepareConfigsAndCerts(t, e)
_, token, rateLimiterAddr := startBaseServices(t, e)
probesEndpoint := startServicesForProbes(t, e)
api, err := newObservatoriumAPIService(
e,
withProbesEndpoint("http://"+probesEndpoint),
withRateLimiter(rateLimiterAddr),
withMetricsEndpoints("http://dummy-metrics:9090", "http://dummy-metrics:9090"), // Add minimal metrics endpoints since probes are mounted within metrics route group
)
testutil.Ok(t, err)
testutil.Ok(t, e2e.StartAndWaitReady(api))
t.Run("create-probe-conflict", func(t *testing.T) {
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: getTLSClientConfig(t, e),
},
}
probePayload := map[string]interface{}{
"static_url": "http://example.com/duplicate",
"status": "pending",
"labels": map[string]string{
"env": "test",
},
}
payloadBytes, err := json.Marshal(probePayload)
testutil.Ok(t, err)
createURL := fmt.Sprintf("https://%s/api/metrics/v1/%s/probes", api.Endpoint("https"), defaultTenantName)
// Create first probe
req1, err := http.NewRequest("POST", createURL, bytes.NewBuffer(payloadBytes))
testutil.Ok(t, err)
req1.Header.Set("Authorization", "Bearer "+token)
req1.Header.Set("Content-Type", "application/json")
resp1, err := client.Do(req1)
testutil.Ok(t, err)
resp1.Body.Close()
testutil.Equals(t, http.StatusCreated, resp1.StatusCode)
// Attempt to create second probe with same URL
req2, err := http.NewRequest("POST", createURL, bytes.NewBuffer(payloadBytes))
testutil.Ok(t, err)
req2.Header.Set("Authorization", "Bearer "+token)
req2.Header.Set("Content-Type", "application/json")
resp2, err := client.Do(req2)
testutil.Ok(t, err)
defer resp2.Body.Close()
// Should get conflict response
testutil.Equals(t, http.StatusConflict, resp2.StatusCode)
})
}
func TestProbes_UnauthorizedAccess(t *testing.T) {
e, err := e2e.New(e2e.WithName(uniqueE2ENetworkName(t)))
testutil.Ok(t, err)
t.Cleanup(e.Close)
prepareConfigsAndCerts(t, e)
_, _, rateLimiterAddr := startBaseServices(t, e)
probesEndpoint := startServicesForProbes(t, e)
api, err := newObservatoriumAPIService(
e,
withProbesEndpoint("http://"+probesEndpoint),
withRateLimiter(rateLimiterAddr),
withMetricsEndpoints("http://dummy-metrics:9090", "http://dummy-metrics:9090"), // Add minimal metrics endpoints since probes are mounted within metrics route group
)
testutil.Ok(t, err)
testutil.Ok(t, e2e.StartAndWaitReady(api))
t.Run("unauthorized-access", func(t *testing.T) {
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: getTLSClientConfig(t, e),
},
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse // Don't follow redirects
},
}
// Test without authorization header
listURL := fmt.Sprintf("https://%s/api/metrics/v1/%s/probes", api.Endpoint("https"), defaultTenantName)
req, err := http.NewRequest("GET", listURL, nil)
testutil.Ok(t, err)
// Deliberately not setting Authorization header
resp, err := client.Do(req)
testutil.Ok(t, err)
defer resp.Body.Close()
// Should get unauthorized response (401) or redirect to login (302)
testutil.Assert(t, resp.StatusCode == http.StatusUnauthorized || resp.StatusCode == http.StatusFound,
fmt.Sprintf("expected 401 or 302, got %d", resp.StatusCode))
// Test with invalid token (properly formatted JWT but invalid signature)
reqInvalid, err := http.NewRequest("GET", listURL, nil)
testutil.Ok(t, err)
reqInvalid.Header.Set("Authorization", "Bearer invalid-token")
respInvalid, err := client.Do(reqInvalid)
testutil.Ok(t, err)
defer respInvalid.Body.Close()
// Should get unauthorized, forbidden, or server error response
testutil.Assert(t, respInvalid.StatusCode == http.StatusUnauthorized || respInvalid.StatusCode == http.StatusForbidden || respInvalid.StatusCode == http.StatusInternalServerError,
fmt.Sprintf("expected 401, 403, or 500, got %d", respInvalid.StatusCode))
})
}