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 <george.robinson@grafana.com>

---------

Signed-off-by: George Robinson <george.robinson@grafana.com>
This commit is contained in:
George Robinson 2023-05-12 10:32:45 +01:00 committed by GitHub
parent 9de8ef3675
commit f67d03fe28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 105 additions and 0 deletions

View File

@ -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)