dashboard_importer_test.go 2.6 KB

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