dashboard_importer_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. }
  31. err = ImportDashboard(&cmd)
  32. So(err, ShouldBeNil)
  33. Convey("should install dashboard", func() {
  34. So(importedDash, ShouldNotBeNil)
  35. rows := importedDash.Data["rows"].([]interface{})
  36. row1 := rows[0].(map[string]interface{})
  37. panels := row1["panels"].([]interface{})
  38. panel := panels[0].(map[string]interface{})
  39. So(panel["datasource"], ShouldEqual, "graphite")
  40. So(importedDash.Data["__inputs"], ShouldBeNil)
  41. })
  42. })
  43. Convey("When evaling dashboard template", t, func() {
  44. template, _ := dynmap.NewObjectFromBytes([]byte(`{
  45. "__input": {
  46. "graphite": {
  47. "type": "datasource"
  48. }
  49. },
  50. "test": {
  51. "prop": "$__graphite"
  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.MustGetString("test.prop", ""), ShouldEqual, "my-server")
  64. })
  65. })
  66. }