Skip to content

Commit

Permalink
Test different queries on flog stream (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
trueleo authored Dec 15, 2023
1 parent ae7a2fc commit 21e02f5
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
20 changes: 20 additions & 0 deletions quest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,26 @@ func TestSmokeQueryTwoStreams(t *testing.T) {
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 TestSmokeSetAlert(t *testing.T) {
req, _ := NewGlob.Client.NewRequest("PUT", "logstream/"+NewGlob.Stream+"/alert", strings.NewReader(AlertBody))
response, err := NewGlob.Client.Do(req)
Expand Down
42 changes: 42 additions & 0 deletions test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ 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)
Expand Down Expand Up @@ -123,6 +140,31 @@ func QueryTwoLogStreamCount(t *testing.T, client HTTPClient, stream1 string, str
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 10 minutes of data only
endTime := time.Now().Add(time.Second).Format(time.RFC3339Nano)
startTime := time.Now().Add(-10 * 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)
Expand Down

0 comments on commit 21e02f5

Please sign in to comment.