Commit c43bb7fb by Carl Bergquist Committed by Torkel Ödegaard

Extractor should not modify dashboard json (#7256)

* test(dashboard): tests that he extractor does not modify original json

* feat(extractor): validate a cloned version of the dashboard json
parent 2d441bfb
...@@ -60,12 +60,25 @@ func findPanelQueryByRefId(panel *simplejson.Json, refId string) *simplejson.Jso ...@@ -60,12 +60,25 @@ func findPanelQueryByRefId(panel *simplejson.Json, refId string) *simplejson.Jso
return nil return nil
} }
func copyJson(in *simplejson.Json) (*simplejson.Json, error) {
rawJson, err := in.MarshalJSON()
if err != nil {
return nil, err
}
return simplejson.NewJson(rawJson)
}
func (e *DashAlertExtractor) GetAlerts() ([]*m.Alert, error) { func (e *DashAlertExtractor) GetAlerts() ([]*m.Alert, error) {
e.log.Debug("GetAlerts") e.log.Debug("GetAlerts")
alerts := make([]*m.Alert, 0) dashboardJson, err := copyJson(e.Dash.Data)
if err != nil {
return nil, err
}
for _, rowObj := range e.Dash.Data.Get("rows").MustArray() { alerts := make([]*m.Alert, 0)
for _, rowObj := range dashboardJson.Get("rows").MustArray() {
row := simplejson.NewFromAny(rowObj) row := simplejson.NewFromAny(rowObj)
for _, panelObj := range row.Get("panels").MustArray() { for _, panelObj := range row.Get("panels").MustArray() {
......
...@@ -110,6 +110,34 @@ func TestAlertRuleExtraction(t *testing.T) { ...@@ -110,6 +110,34 @@ func TestAlertRuleExtraction(t *testing.T) {
] ]
}` }`
Convey("Extractor should not modify the original json", func() {
dashJson, err := simplejson.NewJson([]byte(json))
So(err, ShouldBeNil)
dash := m.NewDashboardFromJson(dashJson)
getTarget := func(j *simplejson.Json) string {
rowObj := j.Get("rows").MustArray()[0]
row := simplejson.NewFromAny(rowObj)
panelObj := row.Get("panels").MustArray()[0]
panel := simplejson.NewFromAny(panelObj)
conditionObj := panel.Get("alert").Get("conditions").MustArray()[0]
condition := simplejson.NewFromAny(conditionObj)
return condition.Get("query").Get("model").Get("target").MustString()
}
Convey("Dashboard json rows.panels.alert.query.model.target should be empty", func() {
So(getTarget(dashJson), ShouldEqual, "")
})
extractor := NewDashAlertExtractor(dash, 1)
_, _ = extractor.GetAlerts()
Convey("Dashboard json should not be updated after extracting rules", func() {
So(getTarget(dashJson), ShouldEqual, "")
})
})
Convey("Parsing and validating dashboard containing graphite alerts", func() { Convey("Parsing and validating dashboard containing graphite alerts", func() {
dashJson, err := simplejson.NewJson([]byte(json)) dashJson, err := simplejson.NewJson([]byte(json))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment