-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_utils.go
493 lines (432 loc) · 23 KB
/
test_utils.go
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// Copyright (c) 2023 Cloudnatively Services Pvt Ltd
//
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os/exec"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
)
const (
sleepDuration = 2 * time.Second
)
func flogStreamFields() []string {
return []string{
"p_timestamp",
"p_tags",
"p_metadata",
"host",
"'user-identifier'",
"datetime",
"method",
"request",
"protocol",
"status",
"bytes",
"referer",
}
}
func readAsString(body io.Reader) string {
r, _ := io.ReadAll(body)
return string(r)
}
func readJsonBody[T any](body io.Reader) (res T, err error) {
r, _ := io.ReadAll(body)
err = json.Unmarshal(r, &res)
return
}
func Sleep() {
time.Sleep(sleepDuration)
}
func CreateStream(t *testing.T, client HTTPClient, stream string) {
req, _ := client.NewRequest("PUT", "logstream/"+stream, nil)
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s", response.Status)
}
func CreateStreamWithHeader(t *testing.T, client HTTPClient, stream string, header map[string]string) {
req, _ := client.NewRequest("PUT", "logstream/"+stream, nil)
for k, v := range header {
req.Header.Add(k, v)
}
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s", response.Status)
}
func CreateStreamWithSchemaBody(t *testing.T, client HTTPClient, stream string, header map[string]string, schema_payload string) {
req, _ := client.NewRequest("PUT", "logstream/"+stream, bytes.NewBufferString(schema_payload))
for k, v := range header {
req.Header.Add(k, v)
}
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s", response.Status)
}
func DetectSchema(t *testing.T, client HTTPClient, sampleJson string, schemaBody string) {
req, _ := client.NewRequest("POST", "logstream/schema/detect", bytes.NewBufferString(sampleJson))
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
body := readAsString(response.Body)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s", response.Status)
require.JSONEq(t, schemaBody, body, "Schema detection failed")
}
func DeleteStream(t *testing.T, client HTTPClient, stream string) {
req, _ := client.NewRequest("DELETE", "logstream/"+stream, nil)
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s", response.Status)
}
func RunFlog(t *testing.T, client HTTPClient, stream string) {
cmd := exec.Command("flog", "-f", "json", "-n", "50")
var out strings.Builder
cmd.Stdout = &out
err := cmd.Run()
require.NoErrorf(t, err, "Failed to run flog: %s", err)
for _, obj := range strings.SplitN(out.String(), "\n", 50) {
var payload strings.Builder
payload.WriteRune('[')
payload.WriteString(obj)
payload.WriteRune(']')
req, _ := client.NewRequest("POST", "ingest", bytes.NewBufferString(payload.String()))
req.Header.Add("X-P-Stream", stream)
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s resp %s", response.Status, readAsString(response.Body))
}
}
func IngestOneEventWithTimePartition_TimeStampMismatch(t *testing.T, client HTTPClient, stream string) {
var test_payload string = `{"source_time":"2024-03-26T18:08:00.434Z","level":"info","message":"Application is failing","version":"1.2.0","user_id":13912,"device_id":4138,"session_id":"abc","os":"Windows","host":"112.168.1.110","location":"ngeuprqhynuvpxgp","request_body":"rnkmffyawtdcindtrdqruyxbndbjpfsptzpwtujbmkwcqastmxwbvjwphmyvpnhordwljnodxhtvpjesjldtifswqbpyuhlcytmm","status_code":300,"app_meta":"ckgpibhmlusqqfunnpxbfxbc", "new_field_added_by":"ingestor 8020"}`
req, _ := client.NewRequest("POST", "ingest", bytes.NewBufferString(test_payload))
req.Header.Add("X-P-Stream", stream)
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 400, response.StatusCode, "Server returned http code: %s resp %s", response.Status, readAsString(response.Body))
}
func IngestOneEventWithTimePartition_NoTimePartitionInLog(t *testing.T, client HTTPClient, stream string) {
var test_payload string = `{"level":"info","message":"Application is failing","version":"1.2.0","user_id":13912,"device_id":4138,"session_id":"abc","os":"Windows","host":"112.168.1.110","location":"ngeuprqhynuvpxgp","request_body":"rnkmffyawtdcindtrdqruyxbndbjpfsptzpwtujbmkwcqastmxwbvjwphmyvpnhordwljnodxhtvpjesjldtifswqbpyuhlcytmm","status_code":300,"app_meta":"ckgpibhmlusqqfunnpxbfxbc", "new_field_added_by":"ingestor 8020"}`
req, _ := client.NewRequest("POST", "ingest", bytes.NewBufferString(test_payload))
req.Header.Add("X-P-Stream", stream)
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 400, response.StatusCode, "Server returned http code: %s resp %s", response.Status, readAsString(response.Body))
}
func IngestOneEventWithTimePartition_IncorrectDateTimeFormatTimePartitionInLog(t *testing.T, client HTTPClient, stream string) {
var test_payload string = `{"source_time":"2024-03-26", "level":"info","message":"Application is failing","version":"1.2.0","user_id":13912,"device_id":4138,"session_id":"abc","os":"Windows","host":"112.168.1.110","location":"ngeuprqhynuvpxgp","request_body":"rnkmffyawtdcindtrdqruyxbndbjpfsptzpwtujbmkwcqastmxwbvjwphmyvpnhordwljnodxhtvpjesjldtifswqbpyuhlcytmm","status_code":300,"app_meta":"ckgpibhmlusqqfunnpxbfxbc", "new_field_added_by":"ingestor 8020"}`
req, _ := client.NewRequest("POST", "ingest", bytes.NewBufferString(test_payload))
req.Header.Add("X-P-Stream", stream)
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 400, response.StatusCode, "Server returned http code: %s resp %s", response.Status, readAsString(response.Body))
}
func IngestOneEventForStaticSchemaStream_NewFieldInLog(t *testing.T, client HTTPClient, stream string) {
var test_payload string = `{"source_time":"2024-03-26", "level":"info","message":"Application is failing","version":"1.2.0","user_id":13912,"device_id":4138,"session_id":"abc","os":"Windows","host":"112.168.1.110","location":"ngeuprqhynuvpxgp","request_body":"rnkmffyawtdcindtrdqruyxbndbjpfsptzpwtujbmkwcqastmxwbvjwphmyvpnhordwljnodxhtvpjesjldtifswqbpyuhlcytmm","status_code":300,"app_meta":"ckgpibhmlusqqfunnpxbfxbc", "new_field_added_by":"ingestor 8020"}`
req, _ := client.NewRequest("POST", "ingest", bytes.NewBufferString(test_payload))
req.Header.Add("X-P-Stream", stream)
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 400, response.StatusCode, "Server returned http code: %s resp %s", response.Status, readAsString(response.Body))
}
func IngestOneEventForStaticSchemaStream_SameFieldsInLog(t *testing.T, client HTTPClient, stream string) {
var test_payload string = `{"source_time":"2024-03-26", "level":"info","message":"Application is failing","version":"1.2.0","user_id":13912,"device_id":4138,"session_id":"abc","os":"Windows","host":"112.168.1.110","location":"ngeuprqhynuvpxgp","request_body":"rnkmffyawtdcindtrdqruyxbndbjpfsptzpwtujbmkwcqastmxwbvjwphmyvpnhordwljnodxhtvpjesjldtifswqbpyuhlcytmm","status_code":300,"app_meta":"ckgpibhmlusqqfunnpxbfxbc"}`
req, _ := client.NewRequest("POST", "ingest", bytes.NewBufferString(test_payload))
req.Header.Add("X-P-Stream", stream)
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s resp %s", response.Status, readAsString(response.Body))
}
func QueryLogStreamCount(t *testing.T, client HTTPClient, stream string, count uint64) {
// Query last 30 minutes of data only
endTime := time.Now().Add(time.Second).Format(time.RFC3339Nano)
startTime := time.Now().Add(-30 * time.Minute).Format(time.RFC3339Nano)
query := map[string]interface{}{
"query": "select count(*) as count from " + stream,
"startTime": startTime,
"endTime": endTime,
}
queryJSON, _ := json.Marshal(query)
req, _ := client.NewRequest("POST", "query", bytes.NewBuffer(queryJSON))
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
body := readAsString(response.Body)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, body)
expected := fmt.Sprintf(`[{"count":%d}]`, count)
require.Equalf(t, expected, body, "Query count incorrect; Expected %s, Actual %s", expected, body)
}
func QueryLogStreamCount_Historical(t *testing.T, client HTTPClient, stream string, count uint64) {
// Query last 30 minutes of data only
now := time.Now()
startTime := now.AddDate(0, 0, -33).Format(time.RFC3339Nano)
endTime := now.AddDate(0, 0, -27).Format(time.RFC3339Nano)
query := map[string]interface{}{
"query": "select count(*) as count from " + stream,
"startTime": startTime,
"endTime": endTime,
}
queryJSON, _ := json.Marshal(query)
req, _ := client.NewRequest("POST", "query", bytes.NewBuffer(queryJSON))
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
body := readAsString(response.Body)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, body)
expected := fmt.Sprintf(`[{"count":%d}]`, count)
require.Equalf(t, expected, body, "Query count incorrect; Expected %s, Actual %s", expected, body)
}
func QueryTwoLogStreamCount(t *testing.T, client HTTPClient, stream1 string, stream2 string, count uint64) {
// Query last 30 minutes of data only
endTime := time.Now().Add(time.Second).Format(time.RFC3339Nano)
startTime := time.Now().Add(-30 * time.Minute).Format(time.RFC3339Nano)
query := map[string]interface{}{
"query": fmt.Sprintf("select sum(c) as count from (select count(*) as c from %s union all select count(*) as c from %s)", stream1, stream2),
"startTime": startTime,
"endTime": endTime,
}
queryJSON, _ := json.Marshal(query)
req, _ := client.NewRequest("POST", "query", bytes.NewBuffer(queryJSON))
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
body := readAsString(response.Body)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, body)
expected := fmt.Sprintf(`[{"count":%d}]`, count)
require.Equalf(t, expected, body, "Query count incorrect; Expected %s, Actual %s", expected, body)
}
func AssertQueryOK(t *testing.T, client HTTPClient, query string, args ...any) {
// Query last 30 minutes of data only
endTime := time.Now().Add(time.Second).Format(time.RFC3339Nano)
startTime := time.Now().Add(-30 * time.Minute).Format(time.RFC3339Nano)
var finalQuery string
if len(args) == 0 {
finalQuery = query
} else {
finalQuery = fmt.Sprintf(query, args...)
}
queryJSON, _ := json.Marshal(map[string]interface{}{
"query": finalQuery,
"startTime": startTime,
"endTime": endTime,
})
req, _ := client.NewRequest("POST", "query", bytes.NewBuffer(queryJSON))
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
body := readAsString(response.Body)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, body)
}
func AssertStreamSchema(t *testing.T, client HTTPClient, stream string, schema string) {
req, _ := client.NewRequest("GET", "logstream/"+stream+"/schema", nil)
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
body := readAsString(response.Body)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, body)
require.JSONEq(t, schema, body, "Get schema response doesn't match with expected schema")
}
func CreateRole(t *testing.T, client HTTPClient, name string, role string) {
req, _ := client.NewRequest("PUT", "role/"+name, strings.NewReader(role))
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body))
}
func AssertRole(t *testing.T, client HTTPClient, name string, role string) {
req, _ := client.NewRequest("GET", "role/"+name, nil)
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
body := readAsString(response.Body)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, body)
require.JSONEq(t, role, body, "Get role response doesn't match with retention config returned")
}
func CreateUser(t *testing.T, client HTTPClient, user string) string {
req, _ := client.NewRequest("POST", "user/"+user, nil)
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
body := readAsString(response.Body)
require.Equalf(t, 400, response.StatusCode, "Server returned http code: %s resp %s", response.Status, readAsString(response.Body))
return body
}
func CreateUserWithRole(t *testing.T, client HTTPClient, user string, roles []string) string {
payload, _ := json.Marshal(roles)
req, _ := client.NewRequest("POST", "user/"+user, bytes.NewBuffer(payload))
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
body := readAsString(response.Body)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, body)
return body
}
func AssignRolesToUser(t *testing.T, client HTTPClient, user string, roles []string) {
payload, _ := json.Marshal(roles)
req, _ := client.NewRequest("PUT", "user/"+user+"/role", bytes.NewBuffer(payload))
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body))
}
func AssertUserRole(t *testing.T, client HTTPClient, user string, roleName, roleBody string) {
req, _ := client.NewRequest("GET", "user/"+user+"/role", nil)
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
userRoleBody := readAsString(response.Body)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, userRoleBody)
expectedRoleBody := fmt.Sprintf(`{"%s":%s}`, roleName, roleBody)
require.JSONEq(t, userRoleBody, expectedRoleBody, "Get user role response doesn't match with expected role")
}
func RegenPassword(t *testing.T, client HTTPClient, user string) string {
req, _ := client.NewRequest("POST", "user/"+user+"/generate-new-password", nil)
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
body := readAsString(response.Body)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, body)
return body
}
func SetUserRole(t *testing.T, client HTTPClient, user string, roles []string) {
payload, _ := json.Marshal(roles)
req, _ := client.NewRequest("PUT", "user/"+user+"/role", bytes.NewBuffer(payload))
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body))
}
func DeleteUser(t *testing.T, client HTTPClient, user string) {
req, _ := client.NewRequest("DELETE", "user/"+user, nil)
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body))
}
func DeleteRole(t *testing.T, client HTTPClient, roleName string) {
req, _ := client.NewRequest("DELETE", "role/"+roleName, nil)
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body))
}
func SetDefaultRole(t *testing.T, client HTTPClient, roleName string) {
payload, _ := json.Marshal(roleName)
req, _ := client.NewRequest("PUT", "role/default", bytes.NewBuffer(payload))
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body))
}
func AssertDefaultRole(t *testing.T, client HTTPClient, roleName string) {
req, _ := client.NewRequest("GET", "role/default", nil)
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
body := readAsString(response.Body)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, body)
require.Equalf(t, roleName, body, "Get default role response doesn't match with expected role")
}
func PutSingleEventExpectErr(t *testing.T, client HTTPClient, stream string) {
payload := `{
"id": "id;objectId",
"maxRunDistance": "float;1;20;1",
"cpf": "cpf",
"cnpj": "cnpj",
"pretendSalary": "money",
"age": "int;20;80",
"gender": "gender",
"firstName": "firstName",
"lastName": "lastName",
"phone": "maskInt;+55 (83) 9####-####",
"address": "address",
"hairColor": "color"
}`
req, _ := client.NewRequest("POST", "logstream/"+stream, bytes.NewBufferString(payload))
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed when expected to pass: %s", err)
require.Equalf(t, 403, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body))
}
func PutSingleEvent(t *testing.T, client HTTPClient, stream string) {
payload := `{
"id": "id;objectId",
"maxRunDistance": "float;1;20;1",
"cpf": "cpf",
"cnpj": "cnpj",
"pretendSalary": "money",
"age": "int;20;80",
"gender": "gender",
"firstName": "firstName",
"lastName": "lastName",
"phone": "maskInt;+55 (83) 9####-####",
"address": "address",
"hairColor": "color"
}`
req, _ := client.NewRequest("POST", "logstream/"+stream, bytes.NewBufferString(payload))
response, err := client.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body))
}
func checkAPIAccess(t *testing.T, queryClient HTTPClient, ingestClient HTTPClient, stream string, role string) {
switch role {
case "editor":
// Check access to non-protected API
req, _ := queryClient.NewRequest("GET", "liveness", nil)
response, err := queryClient.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body))
// Check access to protected API with access
req, _ = queryClient.NewRequest("GET", "logstream", nil)
response, err = queryClient.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body))
// Attempt to call protected API without access
req, _ = queryClient.NewRequest("DELETE", "logstream/"+stream, nil)
response, err = queryClient.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body))
case "writer":
// Check access to non-protected API
req, _ := queryClient.NewRequest("GET", "liveness", nil)
response, err := queryClient.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body))
// Check access to protected API with access
req, _ = queryClient.NewRequest("GET", "logstream", nil)
response, err = queryClient.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body))
// Attempt to call protected API without access
req, _ = queryClient.NewRequest("DELETE", "logstream/"+stream, nil)
response, err = queryClient.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 403, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body))
case "reader":
// Check access to non-protected API
req, _ := queryClient.NewRequest("GET", "liveness", nil)
response, err := queryClient.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body))
// Check access to protected API with access
req, _ = queryClient.NewRequest("GET", "logstream", nil)
response, err = queryClient.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body))
// Attempt to call protected API without access
req, _ = queryClient.NewRequest("DELETE", "logstream/"+stream, nil)
response, err = queryClient.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 403, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body))
case "ingestor":
// Check access to non-protected API
req, _ := queryClient.NewRequest("GET", "liveness", nil)
response, err := queryClient.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body))
// Check access to protected API with access
PutSingleEvent(t, ingestClient, stream)
// Attempt to call protected API without access
req, _ = queryClient.NewRequest("DELETE", "logstream/"+stream, nil)
response, err = queryClient.Do(req)
require.NoErrorf(t, err, "Request failed: %s", err)
require.Equalf(t, 403, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, readAsString(response.Body))
}
}