labels.Matcher: Add more test cases

Signed-off-by: beorn7 <beorn@grafana.com>
This commit is contained in:
beorn7 2020-12-23 23:59:25 +01:00
parent 3ce1d888d7
commit f7742393ae
2 changed files with 114 additions and 2 deletions

View File

@ -79,6 +79,41 @@ func TestMatcher(t *testing.T) {
value: "foo-bar", value: "foo-bar",
match: false, match: false,
}, },
{
matcher: mustNewMatcher(t, MatchRegexp, `foo.bar`),
value: "foo-bar",
match: true,
},
{
matcher: mustNewMatcher(t, MatchRegexp, `foo\.bar`),
value: "foo-bar",
match: false,
},
{
matcher: mustNewMatcher(t, MatchRegexp, `foo\.bar`),
value: "foo.bar",
match: true,
},
{
matcher: mustNewMatcher(t, MatchEqual, "foo\nbar"),
value: "foo\nbar",
match: true,
},
{
matcher: mustNewMatcher(t, MatchRegexp, "foo.bar"),
value: "foo\nbar",
match: false,
},
{
matcher: mustNewMatcher(t, MatchRegexp, "(?s)foo.bar"),
value: "foo\nbar",
match: true,
},
{
matcher: mustNewMatcher(t, MatchEqual, "~!=\""),
value: "~!=\"",
match: true,
},
} }
for _, test := range tests { for _, test := range tests {

View File

@ -22,7 +22,7 @@ func TestMatchers(t *testing.T) {
testCases := []struct { testCases := []struct {
input string input string
want []*Matcher want []*Matcher
err error err string
}{ }{
{ {
input: `{foo="bar"}`, input: `{foo="bar"}`,
@ -130,11 +130,88 @@ func TestMatchers(t *testing.T) {
return append(ms, m, m2) return append(ms, m, m2)
}(), }(),
}, },
{
input: `{foo = "bar", dings != "bums", }`,
want: func() []*Matcher {
ms := []*Matcher{}
m, _ := NewMatcher(MatchEqual, "foo", "bar")
m2, _ := NewMatcher(MatchNotEqual, "dings", "bums")
return append(ms, m, m2)
}(),
},
{
input: `foo=bar,dings!=bums`,
want: func() []*Matcher {
ms := []*Matcher{}
m, _ := NewMatcher(MatchEqual, "foo", "bar")
m2, _ := NewMatcher(MatchNotEqual, "dings", "bums")
return append(ms, m, m2)
}(),
},
{
input: `{quote="She said: \"Hi, ladies! That's gender-neutral…\""}`,
want: func() []*Matcher {
ms := []*Matcher{}
m, _ := NewMatcher(MatchEqual, "quote", `She said: "Hi, ladies! That's gender-neutral…"`)
return append(ms, m)
}(),
},
{
input: `statuscode=~"5.."`,
want: func() []*Matcher {
ms := []*Matcher{}
m, _ := NewMatcher(MatchRegexp, "statuscode", "5..")
return append(ms, m)
}(),
},
{
input: `tricky=~~~`,
want: func() []*Matcher {
ms := []*Matcher{}
m, _ := NewMatcher(MatchRegexp, "tricky", "~~")
return append(ms, m)
}(),
},
{
input: `trickier==\\=\=\""`,
want: func() []*Matcher {
ms := []*Matcher{}
m, _ := NewMatcher(MatchEqual, "trickier", `=\=\="`)
return append(ms, m)
}(),
},
{
input: `contains_quote != "\"" , contains_comma !~ "foo,bar" , `,
want: func() []*Matcher {
ms := []*Matcher{}
m, _ := NewMatcher(MatchNotEqual, "contains_quote", `"`)
m2, _ := NewMatcher(MatchNotRegexp, "contains_comma", "foo,bar")
return append(ms, m, m2)
}(),
},
{
input: `contains_unescaped_quote = foo"bar`,
err: `matcher value contains unescaped double quote: foo"bar`,
},
{
input: `{invalid-name = "valid label"}`,
err: `bad matcher format: invalid-name = "valid label"`,
},
{
input: `{foo=~"invalid[regexp"}`,
err: "error parsing regexp: missing closing ]: `[regexp)$`",
},
} }
for i, tc := range testCases { for i, tc := range testCases {
got, err := ParseMatchers(tc.input) got, err := ParseMatchers(tc.input)
if tc.err != err { if err != nil && tc.err == "" {
t.Fatalf("got error where none expected (i=%d): %v", i, err)
}
if err == nil && tc.err != "" {
t.Fatalf("expected error but got none (i=%d): %v", i, tc.err)
}
if err != nil && err.Error() != tc.err {
t.Fatalf("error not equal (i=%d):\ngot %v\nwant %v", i, err, tc.err) t.Fatalf("error not equal (i=%d):\ngot %v\nwant %v", i, err, tc.err)
} }
if !reflect.DeepEqual(got, tc.want) { if !reflect.DeepEqual(got, tc.want) {