2016-03-11 10:31:57 -06:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
2016-03-12 03:13:49 -06:00
|
|
|
"io/ioutil"
|
2016-03-11 10:31:57 -06:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2016-03-11 17:13:06 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2016-03-11 10:31:57 -06:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
"gopkg.in/ini.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDashboardImport(t *testing.T) {
|
|
|
|
|
|
|
|
Convey("When importing plugin dashboard", t, func() {
|
|
|
|
setting.Cfg = ini.Empty()
|
|
|
|
sec, _ := setting.Cfg.NewSection("plugin.test-app")
|
|
|
|
sec.NewKey("path", "../../tests/test-app")
|
|
|
|
err := Init()
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
var importedDash *m.Dashboard
|
|
|
|
bus.AddHandler("test", func(cmd *m.SaveDashboardCommand) error {
|
|
|
|
importedDash = cmd.GetDashboardModel()
|
|
|
|
cmd.Result = importedDash
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
cmd := ImportDashboardCommand{
|
|
|
|
PluginId: "test-app",
|
|
|
|
Path: "dashboards/connections.json",
|
|
|
|
OrgId: 1,
|
|
|
|
UserId: 1,
|
2016-03-11 16:28:33 -06:00
|
|
|
Inputs: []ImportDashboardInput{
|
2016-03-12 03:13:49 -06:00
|
|
|
{Name: "*", Type: "datasource", Value: "graphite"},
|
2016-03-11 16:28:33 -06:00
|
|
|
},
|
2016-03-11 10:31:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
err = ImportDashboard(&cmd)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
Convey("should install dashboard", func() {
|
|
|
|
So(importedDash, ShouldNotBeNil)
|
2016-03-11 16:28:33 -06:00
|
|
|
|
2016-03-12 03:13:49 -06:00
|
|
|
resultStr, _ := importedDash.Data.EncodePretty()
|
|
|
|
expectedBytes, _ := ioutil.ReadFile("../../tests/test-app/dashboards/connections_result.json")
|
|
|
|
expectedJson, _ := simplejson.NewJson(expectedBytes)
|
|
|
|
expectedStr, _ := expectedJson.EncodePretty()
|
2016-03-11 16:28:33 -06:00
|
|
|
|
2016-03-12 03:13:49 -06:00
|
|
|
So(string(resultStr), ShouldEqual, string(expectedStr))
|
|
|
|
|
|
|
|
panel := importedDash.Data.Get("rows").GetIndex(0).Get("panels").GetIndex(0)
|
|
|
|
So(panel.Get("datasource").MustString(), ShouldEqual, "graphite")
|
2016-03-11 10:31:57 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When evaling dashboard template", t, func() {
|
2016-03-11 17:13:06 -06:00
|
|
|
template, _ := simplejson.NewJson([]byte(`{
|
2016-03-12 03:40:55 -06:00
|
|
|
"__inputs": [
|
|
|
|
{
|
|
|
|
"name": "DS_NAME",
|
2016-03-11 10:31:57 -06:00
|
|
|
"type": "datasource"
|
|
|
|
}
|
2016-03-12 03:40:55 -06:00
|
|
|
],
|
2016-03-11 10:31:57 -06:00
|
|
|
"test": {
|
2016-03-12 03:40:55 -06:00
|
|
|
"prop": "${DS_NAME}"
|
2016-03-11 10:31:57 -06:00
|
|
|
}
|
|
|
|
}`))
|
|
|
|
|
|
|
|
evaluator := &DashTemplateEvaluator{
|
|
|
|
template: template,
|
|
|
|
inputs: []ImportDashboardInput{
|
|
|
|
{Name: "*", Type: "datasource", Value: "my-server"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := evaluator.Eval()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
Convey("should render template", func() {
|
2016-03-11 17:13:06 -06:00
|
|
|
So(res.GetPath("test", "prop").MustString(), ShouldEqual, "my-server")
|
2016-03-11 10:31:57 -06:00
|
|
|
})
|
2016-03-11 16:28:33 -06:00
|
|
|
|
|
|
|
Convey("should not include inputs in output", func() {
|
2016-03-11 17:13:06 -06:00
|
|
|
inputs := res.Get("__inputs")
|
|
|
|
So(inputs.Interface(), ShouldBeNil)
|
2016-03-11 16:28:33 -06:00
|
|
|
})
|
|
|
|
|
2016-03-11 10:31:57 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|