2016-06-09 15:21:28 -05:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAlertingModelTest(t *testing.T) {
|
|
|
|
Convey("Testing Alerting model", t, func() {
|
2020-06-01 10:11:25 -05:00
|
|
|
json1, err := simplejson.NewJson([]byte(`{ "field": "value" }`))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
json2, err := simplejson.NewJson([]byte(`{ "field": "value" }`))
|
|
|
|
So(err, ShouldBeNil)
|
2016-06-09 15:21:28 -05:00
|
|
|
|
2016-06-11 03:26:48 -05:00
|
|
|
rule1 := &Alert{
|
2016-08-12 03:12:04 -05:00
|
|
|
Settings: json1,
|
|
|
|
Name: "Namn",
|
|
|
|
Message: "Message",
|
2016-06-09 15:21:28 -05:00
|
|
|
}
|
|
|
|
|
2016-06-11 03:26:48 -05:00
|
|
|
rule2 := &Alert{
|
2016-08-12 03:12:04 -05:00
|
|
|
Settings: json2,
|
|
|
|
Name: "Namn",
|
|
|
|
Message: "Message",
|
2016-06-09 15:21:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Convey("Testing AlertRule equals", func() {
|
|
|
|
So(rule1.ContainsUpdates(rule2), ShouldBeFalse)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Changing the expression should contain update", func() {
|
2020-06-01 10:11:25 -05:00
|
|
|
json2, err := simplejson.NewJson([]byte(`{ "field": "newValue" }`))
|
|
|
|
So(err, ShouldBeNil)
|
2016-06-13 08:18:19 -05:00
|
|
|
rule1.Settings = json2
|
2016-06-09 15:21:28 -05:00
|
|
|
So(rule1.ContainsUpdates(rule2), ShouldBeTrue)
|
|
|
|
})
|
2019-06-06 06:29:30 -05:00
|
|
|
|
|
|
|
Convey("Should parse alertRule tags correctly", func() {
|
2020-06-01 10:11:25 -05:00
|
|
|
json2, err := simplejson.NewJson([]byte(`{
|
2019-06-06 06:29:30 -05:00
|
|
|
"field": "value",
|
|
|
|
"alertRuleTags": {
|
|
|
|
"foo": "bar",
|
|
|
|
"waldo": "fred",
|
|
|
|
"tagMap": { "mapValue": "value" }
|
|
|
|
}
|
|
|
|
}`))
|
2020-06-01 10:11:25 -05:00
|
|
|
So(err, ShouldBeNil)
|
2019-06-06 06:29:30 -05:00
|
|
|
rule1.Settings = json2
|
|
|
|
expectedTags := []*Tag{
|
|
|
|
{Id: 0, Key: "foo", Value: "bar"},
|
|
|
|
{Id: 0, Key: "waldo", Value: "fred"},
|
|
|
|
{Id: 0, Key: "tagMap", Value: ""},
|
|
|
|
}
|
|
|
|
actualTags := rule1.GetTagsFromSettings()
|
|
|
|
|
|
|
|
So(len(actualTags), ShouldEqual, len(expectedTags))
|
|
|
|
for _, tag := range expectedTags {
|
|
|
|
So(ContainsTag(actualTags, tag), ShouldBeTrue)
|
|
|
|
}
|
|
|
|
})
|
2016-06-09 15:21:28 -05:00
|
|
|
})
|
|
|
|
}
|