silence: Add tests for Not(Equal|Regexp) matchers

... and fix a bug in validating silences with such matchers, caught
while writing them.

Signed-off-by: Kiril Vladimirov <kiril@vladimiroff.org>
This commit is contained in:
Kiril Vladimirov 2021-01-18 22:58:20 +02:00
parent 91083d6cd9
commit f5382af591
2 changed files with 81 additions and 2 deletions

View File

@ -418,11 +418,11 @@ func validateMatcher(m *pb.Matcher) error {
return fmt.Errorf("invalid label name %q", m.Name)
}
switch m.Type {
case pb.Matcher_EQUAL:
case pb.Matcher_EQUAL, pb.Matcher_NOT_EQUAL:
if !model.LabelValue(m.Pattern).IsValid() {
return fmt.Errorf("invalid label value %q", m.Pattern)
}
case pb.Matcher_REGEXP:
case pb.Matcher_REGEXP, pb.Matcher_NOT_REGEXP:
if _, err := regexp.Compile(m.Pattern); err != nil {
return fmt.Errorf("invalid regular expression %q: %s", m.Pattern, err)
}

View File

@ -121,6 +121,19 @@ func TestSilencesSnapshot(t *testing.T) {
},
ExpiresAt: now,
},
{
Silence: &pb.Silence{
Id: "3dfb2528-59ce-41eb-b465-f875a4e744a4",
Matchers: []*pb.Matcher{
{Name: "label1", Pattern: "val1", Type: pb.Matcher_NOT_EQUAL},
{Name: "label2", Pattern: "val.+", Type: pb.Matcher_NOT_REGEXP},
},
StartsAt: now,
EndsAt: now,
UpdatedAt: now,
},
ExpiresAt: now,
},
{
Silence: &pb.Silence{
Id: "4b1e760d-182c-4980-b873-c1a6827c9817",
@ -461,6 +474,14 @@ func TestQMatches(t *testing.T) {
},
drop: true,
},
{
sil: &pb.Silence{
Matchers: []*pb.Matcher{
{Name: "job", Pattern: "test", Type: pb.Matcher_NOT_EQUAL},
},
},
drop: false,
},
{
sil: &pb.Silence{
Matchers: []*pb.Matcher{
@ -470,6 +491,15 @@ func TestQMatches(t *testing.T) {
},
drop: false,
},
{
sil: &pb.Silence{
Matchers: []*pb.Matcher{
{Name: "job", Pattern: "test", Type: pb.Matcher_EQUAL},
{Name: "method", Pattern: "POST", Type: pb.Matcher_NOT_EQUAL},
},
},
drop: true,
},
{
sil: &pb.Silence{
Matchers: []*pb.Matcher{
@ -478,6 +508,14 @@ func TestQMatches(t *testing.T) {
},
drop: true,
},
{
sil: &pb.Silence{
Matchers: []*pb.Matcher{
{Name: "path", Pattern: "/user/.+", Type: pb.Matcher_NOT_REGEXP},
},
},
drop: false,
},
{
sil: &pb.Silence{
Matchers: []*pb.Matcher{
@ -878,6 +916,27 @@ func TestValidateMatcher(t *testing.T) {
Type: pb.Matcher_EQUAL,
},
err: "",
}, {
m: &pb.Matcher{
Name: "a",
Pattern: "b",
Type: pb.Matcher_NOT_EQUAL,
},
err: "",
}, {
m: &pb.Matcher{
Name: "a",
Pattern: "b",
Type: pb.Matcher_REGEXP,
},
err: "",
}, {
m: &pb.Matcher{
Name: "a",
Pattern: "b",
Type: pb.Matcher_NOT_REGEXP,
},
err: "",
}, {
m: &pb.Matcher{
Name: "00",
@ -892,6 +951,13 @@ func TestValidateMatcher(t *testing.T) {
Type: pb.Matcher_REGEXP,
},
err: "invalid regular expression",
}, {
m: &pb.Matcher{
Name: "a",
Pattern: "))",
Type: pb.Matcher_NOT_REGEXP,
},
err: "invalid regular expression",
}, {
m: &pb.Matcher{
Name: "a",
@ -1120,6 +1186,19 @@ func TestStateCoding(t *testing.T) {
},
ExpiresAt: now.Add(24 * time.Hour),
},
{
Silence: &pb.Silence{
Id: "3dfb2528-59ce-41eb-b465-f875a4e744a4",
Matchers: []*pb.Matcher{
{Name: "label1", Pattern: "val1", Type: pb.Matcher_NOT_EQUAL},
{Name: "label2", Pattern: "val.+", Type: pb.Matcher_NOT_REGEXP},
},
StartsAt: now,
EndsAt: now,
UpdatedAt: now,
},
ExpiresAt: now,
},
},
},
}