From f67d03fe2854191bb36dbcb305ec507237583aa2 Mon Sep 17 00:00:00 2001 From: George Robinson Date: Fri, 12 May 2023 10:32:45 +0100 Subject: [PATCH] Add tests to TestMatchers (#3357) * Add tests to TestMatchers This commit adds a number of tests to TestMatchers that asserts some of the more nuanced behavior when parsing label matchers. Signed-off-by: George Robinson --------- Signed-off-by: George Robinson --- pkg/labels/parse_test.go | 105 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/pkg/labels/parse_test.go b/pkg/labels/parse_test.go index e1203f4a..e30d13c4 100644 --- a/pkg/labels/parse_test.go +++ b/pkg/labels/parse_test.go @@ -24,6 +24,42 @@ func TestMatchers(t *testing.T) { want []*Matcher err string }{ + { + input: `{}`, + want: make([]*Matcher, 0), + }, + { + input: `{foo='}`, + want: func() []*Matcher { + ms := []*Matcher{} + m, _ := NewMatcher(MatchEqual, "foo", "'") + return append(ms, m) + }(), + }, + { + input: "{foo=`}", + want: func() []*Matcher { + ms := []*Matcher{} + m, _ := NewMatcher(MatchEqual, "foo", "`") + return append(ms, m) + }(), + }, + { + input: "{foo=\\\"}", + want: func() []*Matcher { + ms := []*Matcher{} + m, _ := NewMatcher(MatchEqual, "foo", "\"") + return append(ms, m) + }(), + }, + { + input: `{foo=bar}`, + want: func() []*Matcher { + ms := []*Matcher{} + m, _ := NewMatcher(MatchEqual, "foo", "bar") + return append(ms, m) + }(), + }, { input: `{foo="bar"}`, want: func() []*Matcher { @@ -32,6 +68,14 @@ func TestMatchers(t *testing.T) { return append(ms, m) }(), }, + { + input: `{foo=~bar.*}`, + want: func() []*Matcher { + ms := []*Matcher{} + m, _ := NewMatcher(MatchRegexp, "foo", "bar.*") + return append(ms, m) + }(), + }, { input: `{foo=~"bar.*"}`, want: func() []*Matcher { @@ -40,6 +84,14 @@ func TestMatchers(t *testing.T) { return append(ms, m) }(), }, + { + input: `{foo!=bar}`, + want: func() []*Matcher { + ms := []*Matcher{} + m, _ := NewMatcher(MatchNotEqual, "foo", "bar") + return append(ms, m) + }(), + }, { input: `{foo!="bar"}`, want: func() []*Matcher { @@ -48,6 +100,14 @@ func TestMatchers(t *testing.T) { return append(ms, m) }(), }, + { + input: `{foo!~bar.*}`, + want: func() []*Matcher { + ms := []*Matcher{} + m, _ := NewMatcher(MatchNotRegexp, "foo", "bar.*") + return append(ms, m) + }(), + }, { input: `{foo!~"bar.*"}`, want: func() []*Matcher { @@ -189,6 +249,31 @@ func TestMatchers(t *testing.T) { return append(ms, m, m2) }(), }, + { + input: `{foo=bar}}`, + want: func() []*Matcher { + ms := []*Matcher{} + m, _ := NewMatcher(MatchEqual, "foo", "bar}") + return append(ms, m) + }(), + }, + { + input: `{foo=bar}},}`, + want: func() []*Matcher { + ms := []*Matcher{} + m, _ := NewMatcher(MatchEqual, "foo", "bar}}") + return append(ms, m) + }(), + }, + { + input: `{foo=,bar=}}`, + want: func() []*Matcher { + ms := []*Matcher{} + m1, _ := NewMatcher(MatchEqual, "foo", "") + m2, _ := NewMatcher(MatchEqual, "bar", "}") + return append(ms, m1, m2) + }(), + }, { input: `job=`, want: func() []*Matcher { @@ -245,6 +330,26 @@ func TestMatchers(t *testing.T) { input: `"foo="bar""`, err: `bad matcher format: "foo="bar""`, }, + { + input: `{{foo=`, + err: `bad matcher format: {foo=`, + }, + { + input: `{foo=`, + want: func() []*Matcher { + ms := []*Matcher{} + m, _ := NewMatcher(MatchEqual, "foo", "") + return append(ms, m) + }(), + }, + { + input: `{foo=}b`, + want: func() []*Matcher { + ms := []*Matcher{} + m, _ := NewMatcher(MatchEqual, "foo", "}b") + return append(ms, m) + }(), + }, } { t.Run(tc.input, func(t *testing.T) { got, err := ParseMatchers(tc.input)