Skip to content

Commit

Permalink
chore(fix): handle TestListLogEntriesRequest flakiness
Browse files Browse the repository at this point in the history
remove timestamp comparison which fails due to (milli-)seconds mismatch when
executing time.Now() to get filter time delayed after time.Now() in the test.
separately test default (-24h) look back period
  • Loading branch information
minherz committed Feb 9, 2022
1 parent c6158ca commit 6c8a02b
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions logging/logadmin/logadmin_test.go
Expand Up @@ -23,6 +23,7 @@ import (
"net/http"
"net/url"
"os"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -261,40 +262,59 @@ func TestFromLogEntry(t *testing.T) {
}
}

func TestListLogEntriesRequestDefaults(t *testing.T) {
const timeFilterPrefix = "timestamp >= "

got := listLogEntriesRequest("projects/PROJECT_ID", nil)

// parse time from filter
if len(got.Filter) < len(timeFilterPrefix) {
t.Errorf("expect default filter to start with %v; got %v", timeFilterPrefix, got.Filter)
}
filterTime, e := time.Parse(time.RFC3339, strings.Trim(got.Filter[len(timeFilterPrefix):], "\""))
if e != nil {
t.Errorf("expect default filter to have time in RFC3339; got %v", got.Filter)
}
timeDiff := time.Now().UTC().Sub(filterTime)

// Default is client's project ID, 24 hour lookback, and no orderBy.
if !testutil.Equal(got.ResourceNames, []string{"projects/PROJECT_ID"}) || got.OrderBy != "" || timeDiff.Hours() < 24 {
t.Errorf("expect {\"projects/PROJECT_ID\"} resource names, 24 hour look back filter and native order\ngot %v", got)
}
}

func TestListLogEntriesRequest(t *testing.T) {
dayAgo := time.Now().Add(-24 * time.Hour).UTC().Format(time.RFC3339)
for _, test := range []struct {
opts []EntriesOption
resourceNames []string
filter string
filterPrefix string
orderBy string
}{
// Default is client's project ID, 24 hour lookback, and orderBy.
{nil, []string{"projects/PROJECT_ID"}, `timestamp >= "` + dayAgo + `"`, ""},
// Timestamp default does not override user's filter
{[]EntriesOption{NewestFirst(), Filter(`timestamp > "2020-10-30T15:39:09Z"`)},
[]string{"projects/PROJECT_ID"}, `timestamp > "2020-10-30T15:39:09Z"`, "timestamp desc"},
[]string{"projects/PROJECT_ID"}, `timestamp > "2020-10-30T15:39:09Z"`, "", "timestamp desc"},
{[]EntriesOption{NewestFirst(), Filter("f")},
[]string{"projects/PROJECT_ID"}, `f AND timestamp >= "` + dayAgo + `"`, "timestamp desc"},
[]string{"projects/PROJECT_ID"}, "", "f AND timestamp >= \"", "timestamp desc"},
{[]EntriesOption{ProjectIDs([]string{"foo"})},
[]string{"projects/foo"}, `timestamp >= "` + dayAgo + `"`, ""},
[]string{"projects/foo"}, "", "timestamp >= \"", ""},
{[]EntriesOption{ResourceNames([]string{"folders/F", "organizations/O"})},
[]string{"folders/F", "organizations/O"}, `timestamp >= "` + dayAgo + `"`, ""},
[]string{"folders/F", "organizations/O"}, "", "timestamp >= \"", ""},
{[]EntriesOption{NewestFirst(), Filter("f"), ProjectIDs([]string{"foo"})},
[]string{"projects/foo"}, `f AND timestamp >= "` + dayAgo + `"`, "timestamp desc"},
[]string{"projects/foo"}, "", "f AND timestamp >= \"", "timestamp desc"},
{[]EntriesOption{NewestFirst(), Filter("f"), ProjectIDs([]string{"foo"})},
[]string{"projects/foo"}, `f AND timestamp >= "` + dayAgo + `"`, "timestamp desc"},
[]string{"projects/foo"}, "", "f AND timestamp >= \"", "timestamp desc"},
// If there are repeats, last one wins.
{[]EntriesOption{NewestFirst(), Filter("no"), ProjectIDs([]string{"foo"}), Filter("f")},
[]string{"projects/foo"}, `f AND timestamp >= "` + dayAgo + `"`, "timestamp desc"},
[]string{"projects/foo"}, "", "f AND timestamp >= \"", "timestamp desc"},
} {
got := listLogEntriesRequest("projects/PROJECT_ID", test.opts)
want := &logpb.ListLogEntriesRequest{
ResourceNames: test.resourceNames,
Filter: test.filter,
OrderBy: test.orderBy,
}
if !proto.Equal(got, want) {
if (test.filterPrefix == "" && !proto.Equal(got, want)) || !strings.HasPrefix(got.Filter, test.filterPrefix) || got.OrderBy != test.orderBy || !testutil.Equal(got.ResourceNames, test.resourceNames) {
t.Errorf("%v:\ngot %v\nwant %v", test.opts, got, want)
}
}
Expand Down

0 comments on commit 6c8a02b

Please sign in to comment.