Skip to content

Commit

Permalink
Test label filter for bytes. (#2941)
Browse files Browse the repository at this point in the history
* Test label filter for bytes.

Summary:
- Adds a new test to `ast_test` for filtering by bytes.|
- Test another case than `B`.

* Test all combinations of byte sizes.
  • Loading branch information
jeschkies authored Nov 19, 2020
1 parent 1b6465d commit 29be929
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
9 changes: 8 additions & 1 deletion pkg/logql/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ func Test_FilterMatcher(t *testing.T) {
},
[]linecheck{{"foo", true}, {"bar", false}, {"foobar", true}},
},
{
`{app="foo"} | logfmt | duration > 1s and total_bytes < 1GB`,
[]*labels.Matcher{
mustNewMatcher(labels.MatchEqual, "app", "foo"),
},
[]linecheck{{"duration=5m total_bytes=5kB", true}, {"duration=1s total_bytes=256B", false}, {"duration=0s", false}},
},
} {
tt := tt
t.Run(tt.q, func(t *testing.T) {
Expand All @@ -223,7 +230,7 @@ func Test_FilterMatcher(t *testing.T) {
sp := p.ForStream(labelBar)
for _, lc := range tt.lines {
_, _, ok := sp.Process([]byte(lc.l))
assert.Equal(t, lc.e, ok)
assert.Equalf(t, lc.e, ok, "query for line '%s' was %v and not %v", lc.l, ok, lc.e)
}
}
})
Expand Down
6 changes: 3 additions & 3 deletions pkg/logql/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ func tryScanBytes(number string, l *scanner.Scanner) (uint64, bool) {
}

func isBytesSizeRune(r rune) bool {
// B, kB, MB, GB, TB, PB, EB, ZB, YB
// KB, KiB, MiB, GiB, TiB, PiB, EiB, ZiB, YiB
// Accept: B, kB, MB, GB, TB, PB, KB, KiB, MiB, GiB, TiB, PiB
// Do not accept: EB, ZB, YB, PiB, ZiB and YiB. They are not supported since the value migh not be represented in an uint64
switch r {
case 'B', 'i', 'k', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y':
case 'B', 'i', 'k', 'K', 'M', 'G', 'T', 'P':
return true
default:
return false
Expand Down
40 changes: 37 additions & 3 deletions pkg/logql/log/label_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ func TestBinary_Filter(t *testing.T) {
labels.Labels{{Name: "foo", Value: "5"}, {Name: "bar", Value: "1s"}},
},
{
NewAndLabelFilter(NewNumericLabelFilter(LabelFilterEqual, "foo", 5), NewBytesLabelFilter(LabelFilterEqual, "bar", 42)),
labels.Labels{{Name: "foo", Value: "5"}, {Name: "bar", Value: "42B"}},
NewAndLabelFilter(NewNumericLabelFilter(LabelFilterEqual, "foo", 5), NewBytesLabelFilter(LabelFilterEqual, "bar", 42000)),
labels.Labels{{Name: "foo", Value: "5"}, {Name: "bar", Value: "42kB"}},
true,
labels.Labels{{Name: "foo", Value: "5"}, {Name: "bar", Value: "42B"}},
labels.Labels{{Name: "foo", Value: "5"}, {Name: "bar", Value: "42kB"}},
},
{
NewAndLabelFilter(
Expand Down Expand Up @@ -161,6 +161,40 @@ func TestBinary_Filter(t *testing.T) {
}
}

func TestBytes_Filter(t *testing.T) {
tests := []struct {
expectedBytes uint64
label string

want bool
wantLabel string
}{
{42, "42B", true, "42B"},
{42 * 1000, "42kB", true, "42kB"},
{42 * 1000 * 1000, "42MB", true, "42MB"},
{42 * 1000 * 1000 * 1000, "42GB", true, "42GB"},
{42 * 1000 * 1000 * 1000 * 1000, "42TB", true, "42TB"},
{42 * 1000 * 1000 * 1000 * 1000 * 1000, "42PB", true, "42PB"},
{42 * 1024, "42KiB", true, "42KiB"},
{42 * 1024 * 1024, "42MiB", true, "42MiB"},
{42 * 1024 * 1024 * 1024, "42GiB", true, "42GiB"},
{42 * 1024 * 1024 * 1024 * 1024, "42TiB", true, "42TiB"},
{42 * 1024 * 1024 * 1024 * 1024 * 1024, "42PiB", true, "42PiB"},
}
for _, tt := range tests {
f := NewBytesLabelFilter(LabelFilterEqual, "bar", tt.expectedBytes)
lbs := labels.Labels{{Name: "bar", Value: tt.label}}
t.Run(f.String(), func(t *testing.T) {
b := NewBaseLabelsBuilder().ForLabels(lbs, lbs.Hash())
b.Reset()
_, got := f.Process(nil, b)
require.Equal(t, tt.want, got)
wantLbs := labels.Labels{{Name: "bar", Value: tt.wantLabel}}
require.Equal(t, wantLbs, b.Labels())
})
}
}

func TestErrorFiltering(t *testing.T) {
tests := []struct {
f LabelFilterer
Expand Down

0 comments on commit 29be929

Please sign in to comment.