dashboard_importer_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package plugins
  2. import (
  3. "io/ioutil"
  4. "testing"
  5. "github.com/grafana/grafana/pkg/bus"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. m "github.com/grafana/grafana/pkg/models"
  8. "github.com/grafana/grafana/pkg/setting"
  9. . "github.com/smartystreets/goconvey/convey"
  10. "gopkg.in/ini.v1"
  11. )
  12. func TestDashboardImport(t *testing.T) {
  13. Convey("When importing plugin dashboard", t, func() {
  14. setting.Cfg = ini.Empty()
  15. sec, _ := setting.Cfg.NewSection("plugin.test-app")
  16. sec.NewKey("path", "../../tests/test-app")
  17. err := Init()
  18. So(err, ShouldBeNil)
  19. var importedDash *m.Dashboard
  20. bus.AddHandler("test", func(cmd *m.SaveDashboardCommand) error {
  21. importedDash = cmd.GetDashboardModel()
  22. cmd.Result = importedDash
  23. return nil
  24. })
  25. cmd := ImportDashboardCommand{
  26. PluginId: "test-app",
  27. Path: "dashboards/connections.json",
  28. OrgId: 1,
  29. UserId: 1,
  30. Inputs: []ImportDashboardInput{
  31. {Name: "*", Type: "datasource", Value: "graphite"},
  32. },
  33. }
  34. err = ImportDashboard(&cmd)
  35. So(err, ShouldBeNil)
  36. Convey("should install dashboard", func() {
  37. So(importedDash, ShouldNotBeNil)
  38. resultStr, _ := importedDash.Data.EncodePretty()
  39. expectedBytes, _ := ioutil.ReadFile("../../tests/test-app/dashboards/connections_result.json")
  40. expectedJson, _ := simplejson.NewJson(expectedBytes)
  41. expectedStr, _ := expectedJson.EncodePretty()
  42. So(string(resultStr), ShouldEqual, string(expectedStr))
  43. panel := importedDash.Data.Get("rows").GetIndex(0).Get("panels").GetIndex(0)
  44. So(panel.Get("datasource").MustString(), ShouldEqual, "graphite")
  45. })
  46. })
  47. Convey("When evaling dashboard template", t, func() {
  48. template, _ := simplejson.NewJson([]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.GetPath("test", "prop").MustString(), ShouldEqual, "my-server")
  68. })
  69. Convey("should not include inputs in output", func() {
  70. inputs := res.Get("__inputs")
  71. So(inputs.Interface(), ShouldBeNil)
  72. })
  73. })
  74. }