dashboard_importer_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package plugins
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/components/dynmap"
  6. m "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/setting"
  8. . "github.com/smartystreets/goconvey/convey"
  9. "gopkg.in/ini.v1"
  10. )
  11. func TestDashboardImport(t *testing.T) {
  12. Convey("When importing plugin dashboard", t, func() {
  13. setting.Cfg = ini.Empty()
  14. sec, _ := setting.Cfg.NewSection("plugin.test-app")
  15. sec.NewKey("path", "../../tests/test-app")
  16. err := Init()
  17. So(err, ShouldBeNil)
  18. var importedDash *m.Dashboard
  19. bus.AddHandler("test", func(cmd *m.SaveDashboardCommand) error {
  20. importedDash = cmd.GetDashboardModel()
  21. cmd.Result = importedDash
  22. return nil
  23. })
  24. cmd := ImportDashboardCommand{
  25. PluginId: "test-app",
  26. Path: "dashboards/connections.json",
  27. OrgId: 1,
  28. UserId: 1,
  29. Inputs: []ImportDashboardInput{
  30. {Name: "*", Type: "datasource"},
  31. },
  32. }
  33. err = ImportDashboard(&cmd)
  34. So(err, ShouldBeNil)
  35. Convey("should install dashboard", func() {
  36. So(importedDash, ShouldNotBeNil)
  37. dashData := dynmap.NewFromMap(importedDash.Data)
  38. So(dashData.String(), ShouldEqual, "")
  39. rows := importedDash.Data["rows"].([]interface{})
  40. row1 := rows[0].(map[string]interface{})
  41. panels := row1["panels"].([]interface{})
  42. panel := panels[0].(map[string]interface{})
  43. So(panel["datasource"], ShouldEqual, "graphite")
  44. So(importedDash.Data["__inputs"], ShouldBeNil)
  45. })
  46. })
  47. Convey("When evaling dashboard template", t, func() {
  48. template, _ := dynmap.NewObjectFromBytes([]byte(`{
  49. "__inputs": {
  50. "graphite": {
  51. "type": "datasource"
  52. }
  53. },
  54. "test": {
  55. "prop": "$__graphite"
  56. }
  57. }`))
  58. evaluator := &DashTemplateEvaluator{
  59. template: template,
  60. inputs: []ImportDashboardInput{
  61. {Name: "*", Type: "datasource", Value: "my-server"},
  62. },
  63. }
  64. res, err := evaluator.Eval()
  65. So(err, ShouldBeNil)
  66. Convey("should render template", func() {
  67. So(res.MustGetString("test.prop", ""), ShouldEqual, "my-server")
  68. })
  69. Convey("should not include inputs in output", func() {
  70. _, err := res.GetObject("__inputs")
  71. So(err, ShouldNotBeNil)
  72. })
  73. })
  74. }