dashboard_importer_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package plugins
  2. import (
  3. "context"
  4. "io/ioutil"
  5. "testing"
  6. "github.com/grafana/grafana/pkg/bus"
  7. "github.com/grafana/grafana/pkg/components/simplejson"
  8. m "github.com/grafana/grafana/pkg/models"
  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. var importedDash *m.Dashboard
  16. bus.AddHandler("test", func(cmd *m.SaveDashboardCommand) error {
  17. importedDash = cmd.GetDashboardModel()
  18. cmd.Result = importedDash
  19. return nil
  20. })
  21. cmd := ImportDashboardCommand{
  22. PluginId: "test-app",
  23. Path: "dashboards/connections.json",
  24. OrgId: 1,
  25. UserId: 1,
  26. Inputs: []ImportDashboardInput{
  27. {Name: "*", Type: "datasource", Value: "graphite"},
  28. },
  29. }
  30. err := ImportDashboard(&cmd)
  31. So(err, ShouldBeNil)
  32. Convey("should install dashboard", func() {
  33. So(importedDash, ShouldNotBeNil)
  34. resultStr, _ := importedDash.Data.EncodePretty()
  35. expectedBytes, _ := ioutil.ReadFile("../../tests/test-app/dashboards/connections_result.json")
  36. expectedJson, _ := simplejson.NewJson(expectedBytes)
  37. expectedStr, _ := expectedJson.EncodePretty()
  38. So(string(resultStr), ShouldEqual, string(expectedStr))
  39. panel := importedDash.Data.Get("rows").GetIndex(0).Get("panels").GetIndex(0)
  40. So(panel.Get("datasource").MustString(), ShouldEqual, "graphite")
  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.TODO())
  78. So(err, ShouldBeNil)
  79. Convey(desc, fn)
  80. })
  81. }