dashboard_importer_test.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. {
  51. "name": "DS_NAME",
  52. "type": "datasource"
  53. }
  54. ],
  55. "test": {
  56. "prop": "${DS_NAME}"
  57. }
  58. }`))
  59. evaluator := &DashTemplateEvaluator{
  60. template: template,
  61. inputs: []ImportDashboardInput{
  62. {Name: "*", Type: "datasource", Value: "my-server"},
  63. },
  64. }
  65. res, err := evaluator.Eval()
  66. So(err, ShouldBeNil)
  67. Convey("should render template", func() {
  68. So(res.GetPath("test", "prop").MustString(), ShouldEqual, "my-server")
  69. })
  70. Convey("should not include inputs in output", func() {
  71. inputs := res.Get("__inputs")
  72. So(inputs.Interface(), ShouldBeNil)
  73. })
  74. })
  75. }