2016-05-25 04:14:59 -05:00
|
|
|
package sqlstore
|
2016-04-18 07:15:03 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2016-05-25 04:14:59 -05:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/alerting"
|
2016-04-18 07:15:03 -05:00
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
)
|
|
|
|
|
2016-06-10 03:00:00 -05:00
|
|
|
func TestAlertModelParsing(t *testing.T) {
|
2016-04-18 07:15:03 -05:00
|
|
|
|
2016-06-10 03:00:00 -05:00
|
|
|
Convey("Parsing alert info from json", t, func() {
|
|
|
|
Convey("Parsing and validating alerts from dashboards", func() {
|
|
|
|
json := `{
|
2016-04-27 02:06:19 -05:00
|
|
|
"id": 57,
|
2016-04-18 07:15:03 -05:00
|
|
|
"title": "Graphite 4",
|
|
|
|
"originalTitle": "Graphite 4",
|
|
|
|
"tags": [
|
|
|
|
"graphite"
|
|
|
|
],
|
|
|
|
"rows": [
|
|
|
|
{
|
2016-06-10 03:00:00 -05:00
|
|
|
|
2016-04-18 07:15:03 -05:00
|
|
|
"panels": [
|
|
|
|
{
|
2016-04-27 02:06:19 -05:00
|
|
|
"title": "Active desktop users",
|
|
|
|
"editable": true,
|
|
|
|
"type": "graph",
|
|
|
|
"id": 3,
|
|
|
|
"targets": [
|
|
|
|
{
|
|
|
|
"refId": "A",
|
|
|
|
"target": "aliasByNode(statsd.fakesite.counters.session_start.desktop.count, 4)"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"datasource": null,
|
2016-04-19 08:34:23 -05:00
|
|
|
"alerting": {
|
2016-06-10 03:00:00 -05:00
|
|
|
"name": "alert name",
|
|
|
|
"description": "description",
|
2016-05-30 11:52:38 -05:00
|
|
|
"frequency": 10,
|
2016-06-09 15:21:28 -05:00
|
|
|
"warning": {
|
|
|
|
"op": ">",
|
|
|
|
"level": 10
|
|
|
|
},
|
|
|
|
"critical": {
|
|
|
|
"op": ">",
|
|
|
|
"level": 20
|
|
|
|
},
|
|
|
|
"function": "static",
|
|
|
|
"valueQuery": {
|
|
|
|
"queryRefId": "A",
|
|
|
|
"from": "5m",
|
|
|
|
"to": "now",
|
|
|
|
"agg": "avg",
|
|
|
|
"params": [
|
|
|
|
"#A",
|
|
|
|
"5m",
|
|
|
|
"now",
|
|
|
|
"avg"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"evalQuery": {
|
|
|
|
"queryRefId": "A",
|
|
|
|
"from": "5m",
|
|
|
|
"to": "now",
|
|
|
|
"agg": "avg",
|
|
|
|
"params": [
|
|
|
|
"#A",
|
|
|
|
"5m",
|
|
|
|
"now",
|
|
|
|
"avg"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"evalStringParam1": "",
|
|
|
|
"name": "Alerting Panel Title alert"
|
2016-06-10 03:00:00 -05:00
|
|
|
}
|
2016-04-27 02:06:19 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"title": "Active mobile users",
|
|
|
|
"id": 4,
|
2016-04-18 07:15:03 -05:00
|
|
|
"targets": [
|
|
|
|
{
|
|
|
|
"refId": "A",
|
2016-04-27 02:06:19 -05:00
|
|
|
"target": "aliasByNode(statsd.fakesite.counters.session_start.mobile.count, 4)"
|
2016-04-18 07:15:03 -05:00
|
|
|
}
|
|
|
|
],
|
2016-04-27 02:06:19 -05:00
|
|
|
"datasource": "graphite2",
|
|
|
|
"alerting": {
|
2016-06-10 03:00:00 -05:00
|
|
|
"name": "alert name",
|
|
|
|
"description": "description",
|
2016-05-30 11:52:38 -05:00
|
|
|
"frequency": 10,
|
2016-06-09 15:21:28 -05:00
|
|
|
"warning": {
|
|
|
|
"op": ">",
|
|
|
|
"level": 10
|
|
|
|
},
|
|
|
|
"critical": {
|
|
|
|
"op": ">",
|
|
|
|
"level": 20
|
|
|
|
},
|
|
|
|
"function": "static",
|
|
|
|
"valueQuery": {
|
|
|
|
"queryRefId": "A",
|
|
|
|
"from": "5m",
|
|
|
|
"to": "now",
|
|
|
|
"agg": "avg",
|
|
|
|
"params": [
|
|
|
|
"#A",
|
|
|
|
"5m",
|
|
|
|
"now",
|
|
|
|
"avg"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"evalQuery": {
|
|
|
|
"queryRefId": "A",
|
|
|
|
"from": "5m",
|
|
|
|
"to": "now",
|
|
|
|
"agg": "avg",
|
|
|
|
"params": [
|
|
|
|
"#A",
|
|
|
|
"5m",
|
|
|
|
"now",
|
|
|
|
"avg"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"evalStringParam1": "",
|
|
|
|
"name": "Alerting Panel Title alert"
|
2016-06-10 03:00:00 -05:00
|
|
|
}
|
2016-04-18 07:15:03 -05:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"title": "Row"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"collapse": false,
|
|
|
|
"editable": true,
|
|
|
|
"height": "250px",
|
|
|
|
"panels": [
|
|
|
|
{
|
|
|
|
"datasource": "InfluxDB",
|
|
|
|
"id": 2,
|
|
|
|
"targets": [
|
|
|
|
{
|
|
|
|
"dsType": "influxdb",
|
|
|
|
"groupBy": [
|
|
|
|
{
|
|
|
|
"params": [
|
|
|
|
"$interval"
|
|
|
|
],
|
|
|
|
"type": "time"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"params": [
|
|
|
|
"null"
|
|
|
|
],
|
|
|
|
"type": "fill"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"measurement": "cpu",
|
|
|
|
"policy": "default",
|
|
|
|
"query": "SELECT mean(\"value\") FROM \"cpu\" WHERE $timeFilter GROUP BY time($interval) fill(null)",
|
|
|
|
"refId": "A",
|
|
|
|
"resultFormat": "table",
|
|
|
|
"select": [
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"params": [
|
|
|
|
"value"
|
|
|
|
],
|
|
|
|
"type": "field"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"params": [],
|
|
|
|
"type": "mean"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
],
|
|
|
|
"tags": [],
|
|
|
|
"target": ""
|
|
|
|
}
|
|
|
|
],
|
2016-04-27 02:06:19 -05:00
|
|
|
"title": "Broken influxdb panel",
|
2016-04-18 07:15:03 -05:00
|
|
|
"transform": "table",
|
2016-06-10 03:00:00 -05:00
|
|
|
"type": "table"
|
2016-04-18 07:15:03 -05:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"title": "New row"
|
|
|
|
}
|
2016-06-10 03:00:00 -05:00
|
|
|
]
|
|
|
|
|
2016-04-18 07:15:03 -05:00
|
|
|
}`
|
2016-06-10 03:00:00 -05:00
|
|
|
dashboardJSON, _ := simplejson.NewJson([]byte(json))
|
|
|
|
cmd := &m.SaveDashboardCommand{
|
|
|
|
Dashboard: dashboardJSON,
|
|
|
|
UserId: 1,
|
|
|
|
OrgId: 1,
|
|
|
|
Overwrite: true,
|
|
|
|
Result: &m.Dashboard{
|
|
|
|
Id: 1,
|
|
|
|
},
|
|
|
|
}
|
2016-04-18 07:15:03 -05:00
|
|
|
|
2016-06-10 03:00:00 -05:00
|
|
|
InitTestDB(t)
|
2016-05-25 04:14:59 -05:00
|
|
|
|
2016-06-10 03:00:00 -05:00
|
|
|
AddDataSource(&m.AddDataSourceCommand{
|
|
|
|
Name: "graphite2",
|
|
|
|
OrgId: 1,
|
|
|
|
Type: m.DS_INFLUXDB,
|
|
|
|
Access: m.DS_ACCESS_DIRECT,
|
|
|
|
Url: "http://test",
|
|
|
|
IsDefault: false,
|
|
|
|
Database: "site",
|
|
|
|
})
|
2016-05-25 04:14:59 -05:00
|
|
|
|
2016-06-10 03:00:00 -05:00
|
|
|
AddDataSource(&m.AddDataSourceCommand{
|
|
|
|
Name: "InfluxDB",
|
|
|
|
OrgId: 1,
|
|
|
|
Type: m.DS_GRAPHITE,
|
|
|
|
Access: m.DS_ACCESS_DIRECT,
|
|
|
|
Url: "http://test",
|
|
|
|
IsDefault: true,
|
|
|
|
})
|
2016-04-18 07:15:03 -05:00
|
|
|
|
2016-06-10 03:00:00 -05:00
|
|
|
alerts := alerting.ParseAlertsFromDashboard(cmd)
|
2016-04-18 07:15:03 -05:00
|
|
|
|
2016-06-10 03:00:00 -05:00
|
|
|
Convey("all properties have been set", func() {
|
|
|
|
So(alerts, ShouldNotBeEmpty)
|
|
|
|
So(len(alerts), ShouldEqual, 2)
|
2016-04-18 07:15:03 -05:00
|
|
|
|
2016-06-10 03:00:00 -05:00
|
|
|
for _, v := range alerts {
|
|
|
|
So(v.DashboardId, ShouldEqual, 1)
|
|
|
|
So(v.PanelId, ShouldNotEqual, 0)
|
2016-05-30 11:52:38 -05:00
|
|
|
|
2016-06-10 03:00:00 -05:00
|
|
|
So(v.Name, ShouldNotBeEmpty)
|
|
|
|
So(v.Description, ShouldNotBeEmpty)
|
|
|
|
}
|
|
|
|
})
|
2016-04-18 07:15:03 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|