Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test different queries on flog stream #48

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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