Skip to content

Commit

Permalink
Merge pull request #2603 from sinkingpoint/master
Browse files Browse the repository at this point in the history
Default the isEqual flag to true in alertmanager
  • Loading branch information
roidelapluie authored May 27, 2021
2 parents 5ad7a10 + 6a072db commit 53491b4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
5 changes: 4 additions & 1 deletion pkg/labels/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ func (m Matcher) MarshalJSON() ([]byte, error) {
}

func (m *Matcher) UnmarshalJSON(data []byte) error {
var v1m apiV1Matcher
v1m := apiV1Matcher{
IsEqual: true,
}

if err := json.Unmarshal(data, &v1m); err != nil {
return err
}
Expand Down
62 changes: 61 additions & 1 deletion pkg/labels/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ line`,
}
}

func TestMatcherJSON(t *testing.T) {
func TestMatcherJSONMarshal(t *testing.T) {
tests := []struct {
name string
op MatchType
Expand Down Expand Up @@ -255,3 +255,63 @@ func TestMatcherJSON(t *testing.T) {
}
}
}

func TestMatcherJSONUnmarshal(t *testing.T) {
tests := []struct {
name string
op MatchType
value string
want string
}{
{
name: "foo",
op: MatchEqual,
value: "bar",
want: `{"name":"foo","value":"bar","isRegex":false}`,
},
{
name: `foo`,
op: MatchEqual,
value: `bar`,
want: `{"name":"foo","value":"bar","isRegex":false,"isEqual":true}`,
},
{
name: `foo`,
op: MatchNotEqual,
value: `bar`,
want: `{"name":"foo","value":"bar","isRegex":false,"isEqual":false}`,
},
{
name: `foo`,
op: MatchRegexp,
value: `bar`,
want: `{"name":"foo","value":"bar","isRegex":true,"isEqual":true}`,
},
{
name: `foo`,
op: MatchNotRegexp,
value: `bar`,
want: `{"name":"foo","value":"bar","isRegex":true,"isEqual":false}`,
},
}

cmp := func(m1, m2 Matcher) bool {
return m1.Name == m2.Name && m1.Value == m2.Value && m1.Type == m2.Type
}

for _, test := range tests {
var m Matcher
if err := json.Unmarshal([]byte(test.want), &m); err != nil {
t.Fatal(err)
}

m2, err := NewMatcher(test.op, test.name, test.value)
if err != nil {
t.Fatal(err)
}

if !cmp(m, *m2) {
t.Errorf("Unmarshaling seems to be producing unexpected matchers; got %#v, expected %#v", m, m2)
}
}
}

0 comments on commit 53491b4

Please sign in to comment.