This commit is contained in:
Aliaksandr Valialkin 2024-05-17 11:41:29 +02:00
parent c38d3dab4d
commit 3da4b970d7
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB
5 changed files with 76 additions and 70 deletions

View file

@ -50,7 +50,7 @@ func TestStorageSearchStreamIDs(t *testing.T) {
f := func(filterStream string, expectedStreamIDs []streamID) { f := func(filterStream string, expectedStreamIDs []streamID) {
t.Helper() t.Helper()
sf := mustNewStreamFilter(filterStream) sf := mustNewTestStreamFilter(filterStream)
if expectedStreamIDs == nil { if expectedStreamIDs == nil {
expectedStreamIDs = []streamID{} expectedStreamIDs = []streamID{}
} }
@ -68,7 +68,7 @@ func TestStorageSearchStreamIDs(t *testing.T) {
AccountID: 1, AccountID: 1,
ProjectID: 2, ProjectID: 2,
} }
sf := mustNewStreamFilter(`{job="job-0",instance="instance-0"}`) sf := mustNewTestStreamFilter(`{job="job-0",instance="instance-0"}`)
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
streamIDs := idb.searchStreamIDs([]TenantID{tenantID}, sf) streamIDs := idb.searchStreamIDs([]TenantID{tenantID}, sf)
if len(streamIDs) > 0 { if len(streamIDs) > 0 {

View file

@ -1111,15 +1111,6 @@ func parseFilterStream(lex *lexer) (*filterStream, error) {
} }
} }
func newStreamFilter(s string) (*StreamFilter, error) {
lex := newLexer(s)
fs, err := parseFilterStream(lex)
if err != nil {
return nil, err
}
return fs.f, nil
}
func parseAndStreamFilter(lex *lexer) (*andStreamFilter, error) { func parseAndStreamFilter(lex *lexer) (*andStreamFilter, error) {
var filters []*streamTagFilter var filters []*streamTagFilter
for { for {

View file

@ -34,51 +34,6 @@ func TestLexer(t *testing.T) {
[]string{"_stream", ":", "{", "foo", "=", "bar", ",", "a", "=~", "baz", ",", "b", "!=", "cd", ",", "d,}a", "!~", "abc", "}"}) []string{"_stream", ":", "{", "foo", "=", "bar", ",", "a", "=~", "baz", ",", "b", "!=", "cd", ",", "d,}a", "!~", "abc", "}"})
} }
func TestNewStreamFilterSuccess(t *testing.T) {
f := func(s, resultExpected string) {
t.Helper()
sf, err := newStreamFilter(s)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
result := sf.String()
if result != resultExpected {
t.Fatalf("unexpected StreamFilter; got %s; want %s", result, resultExpected)
}
}
f("{}", "{}")
f(`{foo="bar"}`, `{foo="bar"}`)
f(`{ "foo" =~ "bar.+" , baz!="a" or x="y"}`, `{foo=~"bar.+",baz!="a" or x="y"}`)
f(`{"a b"='c}"d' OR de="aaa"}`, `{"a b"="c}\"d" or de="aaa"}`)
f(`{a="b", c="d" or x="y"}`, `{a="b",c="d" or x="y"}`)
}
func TestNewStreamFilterFailure(t *testing.T) {
f := func(s string) {
t.Helper()
sf, err := newStreamFilter(s)
if err == nil {
t.Fatalf("expecting non-nil error")
}
if sf != nil {
t.Fatalf("expecting nil sf; got %v", sf)
}
}
f("")
f("}")
f("{")
f("{foo")
f("{foo}")
f("{'foo")
f("{foo=")
f("{foo or bar}")
f("{foo=bar")
f("{foo=bar baz}")
f("{foo='bar' baz='x'}")
}
func TestParseTimeDuration(t *testing.T) { func TestParseTimeDuration(t *testing.T) {
f := func(s string, durationExpected time.Duration) { f := func(s string, durationExpected time.Duration) {
t.Helper() t.Helper()

View file

@ -501,7 +501,7 @@ func TestStorageSearch(t *testing.T) {
} }
}) })
t.Run("stream-filter-mismatch", func(_ *testing.T) { t.Run("stream-filter-mismatch", func(_ *testing.T) {
sf := mustNewStreamFilter(`{job="foobar",instance=~"host-.+:2345"}`) sf := mustNewTestStreamFilter(`{job="foobar",instance=~"host-.+:2345"}`)
minTimestamp := baseTimestamp minTimestamp := baseTimestamp
maxTimestamp := baseTimestamp + rowsPerBlock*1e9 + blocksPerStream maxTimestamp := baseTimestamp + rowsPerBlock*1e9 + blocksPerStream
f := getBaseFilter(minTimestamp, maxTimestamp, sf) f := getBaseFilter(minTimestamp, maxTimestamp, sf)
@ -517,7 +517,7 @@ func TestStorageSearch(t *testing.T) {
}) })
t.Run("matching-stream-id", func(t *testing.T) { t.Run("matching-stream-id", func(t *testing.T) {
for i := 0; i < streamsPerTenant; i++ { for i := 0; i < streamsPerTenant; i++ {
sf := mustNewStreamFilter(fmt.Sprintf(`{job="foobar",instance="host-%d:234"}`, i)) sf := mustNewTestStreamFilter(fmt.Sprintf(`{job="foobar",instance="host-%d:234"}`, i))
tenantID := TenantID{ tenantID := TenantID{
AccountID: 1, AccountID: 1,
ProjectID: 11, ProjectID: 11,
@ -543,7 +543,7 @@ func TestStorageSearch(t *testing.T) {
} }
}) })
t.Run("matching-multiple-stream-ids", func(t *testing.T) { t.Run("matching-multiple-stream-ids", func(t *testing.T) {
sf := mustNewStreamFilter(`{job="foobar",instance=~"host-[^:]+:234"}`) sf := mustNewTestStreamFilter(`{job="foobar",instance=~"host-[^:]+:234"}`)
tenantID := TenantID{ tenantID := TenantID{
AccountID: 1, AccountID: 1,
ProjectID: 11, ProjectID: 11,
@ -568,7 +568,7 @@ func TestStorageSearch(t *testing.T) {
} }
}) })
t.Run("matching-multiple-stream-ids-with-re-filter", func(t *testing.T) { t.Run("matching-multiple-stream-ids-with-re-filter", func(t *testing.T) {
sf := mustNewStreamFilter(`{job="foobar",instance=~"host-[^:]+:234"}`) sf := mustNewTestStreamFilter(`{job="foobar",instance=~"host-[^:]+:234"}`)
tenantID := TenantID{ tenantID := TenantID{
AccountID: 1, AccountID: 1,
ProjectID: 11, ProjectID: 11,
@ -602,7 +602,7 @@ func TestStorageSearch(t *testing.T) {
} }
}) })
t.Run("matching-stream-id-smaller-time-range", func(t *testing.T) { t.Run("matching-stream-id-smaller-time-range", func(t *testing.T) {
sf := mustNewStreamFilter(`{job="foobar",instance="host-1:234"}`) sf := mustNewTestStreamFilter(`{job="foobar",instance="host-1:234"}`)
tenantID := TenantID{ tenantID := TenantID{
AccountID: 1, AccountID: 1,
ProjectID: 11, ProjectID: 11,
@ -627,7 +627,7 @@ func TestStorageSearch(t *testing.T) {
} }
}) })
t.Run("matching-stream-id-missing-time-range", func(_ *testing.T) { t.Run("matching-stream-id-missing-time-range", func(_ *testing.T) {
sf := mustNewStreamFilter(`{job="foobar",instance="host-1:234"}`) sf := mustNewTestStreamFilter(`{job="foobar",instance="host-1:234"}`)
tenantID := TenantID{ tenantID := TenantID{
AccountID: 1, AccountID: 1,
ProjectID: 11, ProjectID: 11,
@ -649,11 +649,3 @@ func TestStorageSearch(t *testing.T) {
s.MustClose() s.MustClose()
fs.MustRemoveAll(path) fs.MustRemoveAll(path)
} }
func mustNewStreamFilter(s string) *StreamFilter {
sf, err := newStreamFilter(s)
if err != nil {
panic(fmt.Errorf("unexpected error in newStreamFilter(%q): %w", s, err))
}
return sf
}

View file

@ -0,0 +1,68 @@
package logstorage
import (
"fmt"
"testing"
)
func TestNewTestStreamFilterSuccess(t *testing.T) {
f := func(s, resultExpected string) {
t.Helper()
sf, err := newTestStreamFilter(s)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
result := sf.String()
if result != resultExpected {
t.Fatalf("unexpected StreamFilter; got %s; want %s", result, resultExpected)
}
}
f("{}", "{}")
f(`{foo="bar"}`, `{foo="bar"}`)
f(`{ "foo" =~ "bar.+" , baz!="a" or x="y"}`, `{foo=~"bar.+",baz!="a" or x="y"}`)
f(`{"a b"='c}"d' OR de="aaa"}`, `{"a b"="c}\"d" or de="aaa"}`)
f(`{a="b", c="d" or x="y"}`, `{a="b",c="d" or x="y"}`)
}
func TestNewTestStreamFilterFailure(t *testing.T) {
f := func(s string) {
t.Helper()
sf, err := newTestStreamFilter(s)
if err == nil {
t.Fatalf("expecting non-nil error")
}
if sf != nil {
t.Fatalf("expecting nil sf; got %v", sf)
}
}
f("")
f("}")
f("{")
f("{foo")
f("{foo}")
f("{'foo")
f("{foo=")
f("{foo or bar}")
f("{foo=bar")
f("{foo=bar baz}")
f("{foo='bar' baz='x'}")
}
func mustNewTestStreamFilter(s string) *StreamFilter {
sf, err := newTestStreamFilter(s)
if err != nil {
panic(fmt.Errorf("unexpected error in newTestStreamFilter(%q): %w", s, err))
}
return sf
}
func newTestStreamFilter(s string) (*StreamFilter, error) {
lex := newLexer(s)
fs, err := parseFilterStream(lex)
if err != nil {
return nil, err
}
return fs.f, nil
}