Skip to content

Commit 21e02f5

Browse files
authored
Test different queries on flog stream (#48)
1 parent ae7a2fc commit 21e02f5

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

quest_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,26 @@ func TestSmokeQueryTwoStreams(t *testing.T) {
8888
DeleteStream(t, NewGlob.Client, stream2)
8989
}
9090

91+
func TestSmokeRunQueries(t *testing.T) {
92+
RunFlog(t, NewGlob.Stream)
93+
// test count
94+
QueryLogStreamCount(t, NewGlob.Client, NewGlob.Stream, 50)
95+
// test yeild all values
96+
AssertQueryOK(t, NewGlob.Client, "SELECT * FROM %s", NewGlob.Stream)
97+
AssertQueryOK(t, NewGlob.Client, "SELECT * FROM %s OFFSET 25 LIMIT 25", NewGlob.Stream)
98+
// test fetch single column
99+
for _, item := range flogStreamFields() {
100+
AssertQueryOK(t, NewGlob.Client, "SELECT %s FROM %s", item, NewGlob.Stream)
101+
}
102+
// test basic filter
103+
AssertQueryOK(t, NewGlob.Client, "SELECT * FROM %s WHERE method = 'POST'", NewGlob.Stream)
104+
// test group by
105+
AssertQueryOK(t, NewGlob.Client, "SELECT method, COUNT(*) FROM %s GROUP BY method", NewGlob.Stream)
106+
AssertQueryOK(t, NewGlob.Client, `SELECT DATE_TRUNC('minute', p_timestamp) as minute, COUNT(*) FROM %s GROUP BY minute`, NewGlob.Stream)
107+
108+
DeleteStream(t, NewGlob.Client, NewGlob.Stream)
109+
}
110+
91111
func TestSmokeSetAlert(t *testing.T) {
92112
req, _ := NewGlob.Client.NewRequest("PUT", "logstream/"+NewGlob.Stream+"/alert", strings.NewReader(AlertBody))
93113
response, err := NewGlob.Client.Do(req)

test_utils.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ const (
3333
sleepDuration = 2 * time.Second
3434
)
3535

36+
func flogStreamFields() []string {
37+
return []string{
38+
"p_timestamp",
39+
"p_tags",
40+
"p_metadata",
41+
"host",
42+
"'user-identifier'",
43+
"datetime",
44+
"method",
45+
"request",
46+
"protocol",
47+
"status",
48+
"bytes",
49+
"referer",
50+
}
51+
}
52+
3653
func readAsString(body io.Reader) string {
3754
r, _ := io.ReadAll(body)
3855
return string(r)
@@ -123,6 +140,31 @@ func QueryTwoLogStreamCount(t *testing.T, client HTTPClient, stream1 string, str
123140
require.Equalf(t, expected, body, "Query count incorrect; Expected %s, Actual %s", expected, body)
124141
}
125142

143+
func AssertQueryOK(t *testing.T, client HTTPClient, query string, args ...any) {
144+
// Query last 10 minutes of data only
145+
endTime := time.Now().Add(time.Second).Format(time.RFC3339Nano)
146+
startTime := time.Now().Add(-10 * time.Minute).Format(time.RFC3339Nano)
147+
148+
var finalQuery string
149+
if len(args) == 0 {
150+
finalQuery = query
151+
} else {
152+
finalQuery = fmt.Sprintf(query, args...)
153+
}
154+
155+
queryJSON, _ := json.Marshal(map[string]interface{}{
156+
"query": finalQuery,
157+
"startTime": startTime,
158+
"endTime": endTime,
159+
})
160+
161+
req, _ := client.NewRequest("POST", "query", bytes.NewBuffer(queryJSON))
162+
response, err := client.Do(req)
163+
require.NoErrorf(t, err, "Request failed: %s", err)
164+
body := readAsString(response.Body)
165+
require.Equalf(t, 200, response.StatusCode, "Server returned http code: %s and response: %s", response.Status, body)
166+
}
167+
126168
func AssertStreamSchema(t *testing.T, client HTTPClient, stream string, schema string) {
127169
req, _ := client.NewRequest("GET", "logstream/"+stream+"/schema", nil)
128170
response, err := client.Do(req)

0 commit comments

Comments
 (0)