Skip to content

Commit

Permalink
Add tests to TestMatchers (#3357)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
grobinson-grafana authored May 12, 2023
1 parent 9de8ef3 commit f67d03f
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions pkg/labels/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f67d03f

Please sign in to comment.