dashboard_importer_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package plugins
  2. import (
  3. "io/ioutil"
  4. "testing"
  5. "github.com/grafana/grafana/pkg/components/simplejson"
  6. m "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/services/dashboards"
  8. "github.com/grafana/grafana/pkg/setting"
  9. . "github.com/smartystreets/goconvey/convey"
  10. "gopkg.in/ini.v1"
  11. )
  12. func TestDashboardImport(t *testing.T) {
  13. pluginScenario("When importing a plugin dashboard", t, func() {
  14. origNewDashboardService := dashboards.NewService
  15. mock := &dashboards.FakeDashboardService{}
  16. dashboards.MockDashboardService(mock)
  17. cmd := ImportDashboardCommand{
  18. PluginId: "test-app",
  19. Path: "dashboards/connections.json",
  20. OrgId: 1,
  21. User: &m.SignedInUser{UserId: 1, OrgRole: m.ROLE_ADMIN},
  22. Inputs: []ImportDashboardInput{
  23. {Name: "*", Type: "datasource", Value: "graphite"},
  24. },
  25. }
  26. err := ImportDashboard(&cmd)
  27. So(err, ShouldBeNil)
  28. Convey("should install dashboard", func() {
  29. So(cmd.Result, ShouldNotBeNil)
  30. resultStr, _ := mock.SavedDashboards[0].Dashboard.Data.EncodePretty()
  31. expectedBytes, _ := ioutil.ReadFile("testdata/test-app/dashboards/connections_result.json")
  32. expectedJson, _ := simplejson.NewJson(expectedBytes)
  33. expectedStr, _ := expectedJson.EncodePretty()
  34. So(string(resultStr), ShouldEqual, string(expectedStr))
  35. panel := mock.SavedDashboards[0].Dashboard.Data.Get("rows").GetIndex(0).Get("panels").GetIndex(0)
  36. So(panel.Get("datasource").MustString(), ShouldEqual, "graphite")
  37. })
  38. Reset(func() {
  39. dashboards.NewService = origNewDashboardService
  40. })
  41. })
  42. Convey("When evaling dashboard template", t, func() {
  43. template, _ := simplejson.NewJson([]byte(`{
  44. "__inputs": [
  45. {
  46. "name": "DS_NAME",
  47. "type": "datasource"
  48. }
  49. ],
  50. "test": {
  51. "prop": "${DS_NAME}"
  52. }
  53. }`))
  54. evaluator := &DashTemplateEvaluator{
  55. template: template,
  56. inputs: []ImportDashboardInput{
  57. {Name: "*", Type: "datasource", Value: "my-server"},
  58. },
  59. }
  60. res, err := evaluator.Eval()
  61. So(err, ShouldBeNil)
  62. Convey("should render template", func() {
  63. So(res.GetPath("test", "prop").MustString(), ShouldEqual, "my-server")
  64. })
  65. Convey("should not include inputs in output", func() {
  66. inputs := res.Get("__inputs")
  67. So(inputs.Interface(), ShouldBeNil)
  68. })
  69. })
  70. }
  71. func pluginScenario(desc string, t *testing.T, fn func()) {
  72. Convey("Given a plugin", t, func() {
  73. setting.Raw = ini.Empty()
  74. sec, _ := setting.Raw.NewSection("plugin.test-app")
  75. sec.NewKey("path", "testdata/test-app")
  76. pm := &PluginManager{}
  77. err := pm.Init()
  78. So(err, ShouldBeNil)
  79. Convey(desc, fn)
  80. })
  81. }