dashboard_importer_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package plugins
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/components/simplejson"
  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. dashStr, _ := importedDash.Data.EncodePretty()
  38. So(string(dashStr), ShouldEqual, "")
  39. // So(panel["datasource"], ShouldEqual, "graphite")
  40. // So(importedDash.Data["__inputs"], ShouldBeNil)
  41. })
  42. })
  43. Convey("When evaling dashboard template", t, func() {
  44. template, _ := simplejson.NewJson([]byte(`{
  45. "__inputs": {
  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.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. }