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"
|
|
|
|
|
2016-03-11 17:13:06 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2020-02-28 05:51:21 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2018-02-19 04:12:56 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
2016-03-11 10:31:57 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
"gopkg.in/ini.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDashboardImport(t *testing.T) {
|
2017-11-28 05:49:10 -06:00
|
|
|
pluginScenario("When importing a plugin dashboard", t, func() {
|
2018-02-19 04:12:56 -06:00
|
|
|
origNewDashboardService := dashboards.NewService
|
|
|
|
mock := &dashboards.FakeDashboardService{}
|
|
|
|
dashboards.MockDashboardService(mock)
|
2017-11-27 11:23:57 -06:00
|
|
|
|
|
|
|
cmd := ImportDashboardCommand{
|
|
|
|
PluginId: "test-app",
|
|
|
|
Path: "dashboards/connections.json",
|
|
|
|
OrgId: 1,
|
2020-02-28 05:51:21 -06:00
|
|
|
User: &models.SignedInUser{UserId: 1, OrgRole: models.ROLE_ADMIN},
|
2017-11-27 11:23:57 -06:00
|
|
|
Inputs: []ImportDashboardInput{
|
|
|
|
{Name: "*", Type: "datasource", Value: "graphite"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-11-28 05:49:10 -06:00
|
|
|
err := ImportDashboard(&cmd)
|
2017-11-27 11:23:57 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
Convey("should install dashboard", func() {
|
2018-02-19 04:12:56 -06:00
|
|
|
So(cmd.Result, ShouldNotBeNil)
|
2017-11-27 11:23:57 -06:00
|
|
|
|
2018-02-19 04:12:56 -06:00
|
|
|
resultStr, _ := mock.SavedDashboards[0].Dashboard.Data.EncodePretty()
|
2018-09-17 10:29:11 -05:00
|
|
|
expectedBytes, _ := ioutil.ReadFile("testdata/test-app/dashboards/connections_result.json")
|
2017-11-27 11:23:57 -06:00
|
|
|
expectedJson, _ := simplejson.NewJson(expectedBytes)
|
|
|
|
expectedStr, _ := expectedJson.EncodePretty()
|
|
|
|
|
|
|
|
So(string(resultStr), ShouldEqual, string(expectedStr))
|
|
|
|
|
2018-02-19 04:12:56 -06:00
|
|
|
panel := mock.SavedDashboards[0].Dashboard.Data.Get("rows").GetIndex(0).Get("panels").GetIndex(0)
|
2017-11-27 11:23:57 -06:00
|
|
|
So(panel.Get("datasource").MustString(), ShouldEqual, "graphite")
|
|
|
|
})
|
2018-02-19 04:12:56 -06:00
|
|
|
|
|
|
|
Reset(func() {
|
|
|
|
dashboards.NewService = origNewDashboardService
|
|
|
|
})
|
2017-11-27 11:23:57 -06:00
|
|
|
})
|
|
|
|
|
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(`{
|
2017-11-27 10:08:39 -06:00
|
|
|
"__inputs": [
|
|
|
|
{
|
|
|
|
"name": "DS_NAME",
|
|
|
|
"type": "datasource"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"test": {
|
|
|
|
"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
|
|
|
})
|
|
|
|
}
|
2017-11-28 05:49:10 -06:00
|
|
|
|
|
|
|
func pluginScenario(desc string, t *testing.T, fn func()) {
|
|
|
|
Convey("Given a plugin", t, func() {
|
2018-04-30 09:21:04 -05:00
|
|
|
setting.Raw = ini.Empty()
|
|
|
|
sec, _ := setting.Raw.NewSection("plugin.test-app")
|
2019-10-11 14:02:15 -05:00
|
|
|
_, err := sec.NewKey("path", "testdata/test-app")
|
|
|
|
So(err, ShouldBeNil)
|
2017-11-28 05:49:10 -06:00
|
|
|
|
2018-04-27 08:11:55 -05:00
|
|
|
pm := &PluginManager{}
|
2019-10-11 14:02:15 -05:00
|
|
|
err = pm.Init()
|
2018-04-27 08:11:55 -05:00
|
|
|
So(err, ShouldBeNil)
|
2019-10-11 14:02:15 -05:00
|
|
|
|
2017-11-28 05:49:10 -06:00
|
|
|
Convey(desc, fn)
|
|
|
|
})
|
|
|
|
}
|