diff --git a/pkg/labels/matcher_test.go b/pkg/labels/matcher_test.go index dfedf8d8..8469f075 100644 --- a/pkg/labels/matcher_test.go +++ b/pkg/labels/matcher_test.go @@ -79,6 +79,41 @@ func TestMatcher(t *testing.T) { value: "foo-bar", 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 { diff --git a/pkg/labels/parse_test.go b/pkg/labels/parse_test.go index b4b6b02c..7adca325 100644 --- a/pkg/labels/parse_test.go +++ b/pkg/labels/parse_test.go @@ -22,7 +22,7 @@ func TestMatchers(t *testing.T) { testCases := []struct { input string want []*Matcher - err error + err string }{ { input: `{foo="bar"}`, @@ -130,11 +130,88 @@ func TestMatchers(t *testing.T) { 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 { 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) } if !reflect.DeepEqual(got, tc.want) {