forked from parseablehq/quest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquest_test.go
280 lines (245 loc) · 9.31 KB
/
quest_test.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
// 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"
"fmt"
"os/exec"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
const (
vus = "10"
duration = "5m"
schema_count = "20"
events_count = "10"
)
func TestSmokeListLogStream(t *testing.T) {
req, err := NewGlob.Client.NewRequest("GET", "logstream", nil)
require.NoErrorf(t, err, "Request failed: %s", err)
response, err := NewGlob.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)
res, err := readJsonBody[[]string](bytes.NewBufferString(body))
if err != nil {
for _, stream := range res {
if stream == NewGlob.Stream {
DeleteStream(t, NewGlob.Client, NewGlob.Stream)
}
}
}
}
func TestSmokeCreateStream(t *testing.T) {
CreateStream(t, NewGlob.Client, NewGlob.Stream)
}
func TestSmokeIngestEventsToStream(t *testing.T) {
RunFlog(t, NewGlob.Stream)
QueryLogStreamCount(t, NewGlob.Client, NewGlob.Stream, 50)
AssertStreamSchema(t, NewGlob.Client, NewGlob.Stream, FlogJsonSchema)
DeleteStream(t, NewGlob.Client, NewGlob.Stream)
}
func TestSmokeQueryTwoStreams(t *testing.T) {
stream1 := NewGlob.Stream + "1"
stream2 := NewGlob.Stream + "2"
RunFlog(t, stream1)
RunFlog(t, stream2)
QueryTwoLogStreamCount(t, NewGlob.Client, stream1, stream2, 100)
DeleteStream(t, NewGlob.Client, stream1)
DeleteStream(t, NewGlob.Client, stream2)
}
func TestSmokeRunQueries(t *testing.T) {
RunFlog(t, NewGlob.Stream)
// test count
QueryLogStreamCount(t, NewGlob.Client, NewGlob.Stream, 50)
// test yeild all values
AssertQueryOK(t, NewGlob.Client, "SELECT * FROM %s", NewGlob.Stream)
AssertQueryOK(t, NewGlob.Client, "SELECT * FROM %s OFFSET 25 LIMIT 25", NewGlob.Stream)
// test fetch single column
for _, item := range flogStreamFields() {
AssertQueryOK(t, NewGlob.Client, "SELECT %s FROM %s", item, NewGlob.Stream)
}
// test basic filter
AssertQueryOK(t, NewGlob.Client, "SELECT * FROM %s WHERE method = 'POST'", NewGlob.Stream)
// test group by
AssertQueryOK(t, NewGlob.Client, "SELECT method, COUNT(*) FROM %s GROUP BY method", NewGlob.Stream)
AssertQueryOK(t, NewGlob.Client, `SELECT DATE_TRUNC('minute', p_timestamp) as minute, COUNT(*) FROM %s GROUP BY minute`, NewGlob.Stream)
DeleteStream(t, NewGlob.Client, NewGlob.Stream)
}
func TestSmokeLoadWithK6Stream(t *testing.T) {
CreateStream(t, NewGlob.Client, NewGlob.Stream)
cmd := exec.Command("k6",
"run",
"-e", fmt.Sprintf("P_URL=%s", NewGlob.Url.String()),
"-e", fmt.Sprintf("P_USERNAME=%s", NewGlob.Username),
"-e", fmt.Sprintf("P_PASSWORD=%s", NewGlob.Password),
"-e", fmt.Sprintf("P_STREAM=%s", NewGlob.Stream),
"./scripts/smoke.js")
cmd.Run()
cmd.Output()
QueryLogStreamCount(t, NewGlob.Client, NewGlob.Stream, 60000)
AssertStreamSchema(t, NewGlob.Client, NewGlob.Stream, SchemaBody)
}
func TestSmokeSetAlert(t *testing.T) {
req, _ := NewGlob.Client.NewRequest("PUT", "logstream/"+NewGlob.Stream+"/alert", strings.NewReader(AlertBody))
response, err := NewGlob.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 TestSmokeGetAlert(t *testing.T) {
req, _ := NewGlob.Client.NewRequest("GET", "logstream/"+NewGlob.Stream+"/alert", nil)
response, err := NewGlob.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, AlertBody, body, "Get alert response doesn't match with Alert config returned")
}
func TestSmokeSetRetention(t *testing.T) {
req, _ := NewGlob.Client.NewRequest("PUT", "logstream/"+NewGlob.Stream+"/retention", strings.NewReader(RetentionBody))
response, err := NewGlob.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 TestSmokeGetRetention(t *testing.T) {
req, _ := NewGlob.Client.NewRequest("GET", "logstream/"+NewGlob.Stream+"/retention", nil)
response, err := NewGlob.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, RetentionBody, body, "Get retention response doesn't match with retention config returned")
}
// This test calls all the User API endpoints
// in a sequence to check if they work as expected.
func TestSmoke_AllUsersAPI(t *testing.T) {
CreateRole(t, NewGlob.Client, "dummyrole", dummyRole)
AssertRole(t, NewGlob.Client, "dummyrole", dummyRole)
CreateUser(t, NewGlob.Client, "dummyuser")
AssignRolesToUser(t, NewGlob.Client, "dummyuser", []string{"dummyrole"})
AssertUserRole(t, NewGlob.Client, "dummyuser", "dummyrole", dummyRole)
RegenPassword(t, NewGlob.Client, "dummyuser")
DeleteUser(t, NewGlob.Client, "dummyuser")
CreateUserWithRole(t, NewGlob.Client, "dummyuser", []string{"dummyrole"})
AssertUserRole(t, NewGlob.Client, "dummyuser", "dummyrole", dummyRole)
RegenPassword(t, NewGlob.Client, "dummyuser")
DeleteUser(t, NewGlob.Client, "dummyuser")
DeleteRole(t, NewGlob.Client, "dummyrole")
}
// This test checks that a new user doesn't get any role by default
// even if a default role is set.
func TestSmoke_NewUserNoRole(t *testing.T) {
CreateRole(t, NewGlob.Client, "dummyrole", dummyRole)
SetDefaultRole(t, NewGlob.Client, "dummyrole")
AssertDefaultRole(t, NewGlob.Client, "\"dummyrole\"")
password := CreateUser(t, NewGlob.Client, "dummyuser")
userClient := NewGlob.Client
userClient.Username = "dummyuser"
userClient.Password = password
PutSingleEventExpectErr(t, userClient, NewGlob.Stream)
DeleteUser(t, NewGlob.Client, "dummyuser")
}
func TestSmokeRbacBasic(t *testing.T) {
CreateRole(t, NewGlob.Client, "dummy", dummyRole)
AssertRole(t, NewGlob.Client, "dummy", dummyRole)
CreateUserWithRole(t, NewGlob.Client, "dummy", []string{"dummy"})
userClient := NewGlob.Client
userClient.Username = "dummy"
userClient.Password = RegenPassword(t, NewGlob.Client, "dummy")
checkAPIAccess(t, userClient, NewGlob.Stream, "editor")
DeleteUser(t, NewGlob.Client, "dummy")
DeleteRole(t, NewGlob.Client, "dummy")
}
func TestSmokeRoles(t *testing.T) {
cases := []struct {
roleName string
body string
}{
{
roleName: "editor",
body: RoleEditor,
},
{
roleName: "reader",
body: RoleReader(NewGlob.Stream),
},
{
roleName: "writer",
body: RoleWriter(NewGlob.Stream),
},
{
roleName: "ingester",
body: RoleIngester(NewGlob.Stream),
},
}
for _, tc := range cases {
t.Run(tc.roleName, func(t *testing.T) {
CreateRole(t, NewGlob.Client, tc.roleName, tc.body)
AssertRole(t, NewGlob.Client, tc.roleName, tc.body)
username := tc.roleName + "_user"
password := CreateUserWithRole(t, NewGlob.Client, username, []string{tc.roleName})
userClient := NewGlob.Client
userClient.Username = username
userClient.Password = password
checkAPIAccess(t, userClient, NewGlob.Stream, tc.roleName)
DeleteUser(t, NewGlob.Client, username)
DeleteRole(t, NewGlob.Client, tc.roleName)
})
}
}
func TestLoadStreamBatchWithK6(t *testing.T) {
if NewGlob.Mode == "load" {
cmd := exec.Command("k6",
"run",
"-e", fmt.Sprintf("P_URL=%s", NewGlob.Url.String()),
"-e", fmt.Sprintf("P_USERNAME=%s", NewGlob.Username),
"-e", fmt.Sprintf("P_PASSWORD=%s", NewGlob.Password),
"-e", fmt.Sprintf("P_STREAM=%s", NewGlob.Stream),
"-e", fmt.Sprintf("P_SCHEMA_COUNT=%s", schema_count),
"-e", fmt.Sprintf("P_EVENTS_COUNT=%s", schema_count),
"./scripts/load_batch_events.js",
"--vus=", vus,
"--duration=", duration)
cmd.Run()
op, err := cmd.Output()
if err != nil {
t.Log(err)
}
t.Log(string(op))
}
}
func TestLoadStreamNoBatchWithK6(t *testing.T) {
if NewGlob.Mode == "load" {
cmd := exec.Command("k6",
"run",
"-e", fmt.Sprintf("P_URL=%s", NewGlob.Url.String()),
"-e", fmt.Sprintf("P_USERNAME=%s", NewGlob.Username),
"-e", fmt.Sprintf("P_PASSWORD=%s", NewGlob.Password),
"-e", fmt.Sprintf("P_STREAM=%s", NewGlob.Stream),
"-e", fmt.Sprintf("P_SCHEMA_COUNT=%s", schema_count),
"./scripts/load_single_events.js",
"--vus=", vus,
"--duration=", duration)
cmd.Run()
op, err := cmd.Output()
if err != nil {
t.Log(err)
}
t.Log(string(op))
}
}
func TestDeleteStream(t *testing.T) {
DeleteStream(t, NewGlob.Client, NewGlob.Stream)
}