dashboard_importer_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. folderId := int64(1000)
  20. var importedDash *m.Dashboard
  21. var createdFolder *m.Dashboard
  22. bus.AddHandler("test", func(cmd *m.SaveDashboardCommand) error {
  23. if cmd.IsFolder {
  24. createdFolder = cmd.GetDashboardModel()
  25. createdFolder.Id = folderId
  26. cmd.Result = createdFolder
  27. } else {
  28. importedDash = cmd.GetDashboardModel()
  29. cmd.Result = importedDash
  30. }
  31. return nil
  32. })
  33. bus.AddHandler("test", func(cmd *m.GetDashboardQuery) error {
  34. return nil
  35. })
  36. cmd := ImportDashboardCommand{
  37. PluginId: "test-app",
  38. Path: "dashboards/connections.json",
  39. OrgId: 1,
  40. UserId: 1,
  41. Inputs: []ImportDashboardInput{
  42. {Name: "*", Type: "datasource", Value: "graphite"},
  43. },
  44. }
  45. err = ImportDashboard(&cmd)
  46. So(err, ShouldBeNil)
  47. Convey("should install dashboard", func() {
  48. So(importedDash, ShouldNotBeNil)
  49. resultStr, _ := importedDash.Data.EncodePretty()
  50. expectedBytes, _ := ioutil.ReadFile("../../tests/test-app/dashboards/connections_result.json")
  51. expectedJson, _ := simplejson.NewJson(expectedBytes)
  52. expectedStr, _ := expectedJson.EncodePretty()
  53. So(string(resultStr), ShouldEqual, string(expectedStr))
  54. panel := importedDash.Data.Get("rows").GetIndex(0).Get("panels").GetIndex(0)
  55. So(panel.Get("datasource").MustString(), ShouldEqual, "graphite")
  56. So(importedDash.FolderId, ShouldEqual, folderId)
  57. })
  58. Convey("should create app folder", func() {
  59. So(createdFolder.Title, ShouldEqual, "App: Test App")
  60. So(createdFolder.Id, ShouldEqual, folderId)
  61. })
  62. })
  63. Convey("When re-importing plugin dashboard", t, func() {
  64. setting.Cfg = ini.Empty()
  65. sec, _ := setting.Cfg.NewSection("plugin.test-app")
  66. sec.NewKey("path", "../../tests/test-app")
  67. err := Init()
  68. So(err, ShouldBeNil)
  69. folderId := int64(1000)
  70. var importedDash *m.Dashboard
  71. var createdFolder *m.Dashboard
  72. bus.AddHandler("test", func(cmd *m.SaveDashboardCommand) error {
  73. if cmd.IsFolder {
  74. cmd.Result = cmd.GetDashboardModel()
  75. } else {
  76. importedDash = cmd.GetDashboardModel()
  77. cmd.Result = importedDash
  78. }
  79. return nil
  80. })
  81. bus.AddHandler("test", func(cmd *m.GetDashboardQuery) error {
  82. cmd.Result = &m.Dashboard{
  83. Id: 1000,
  84. Title: "Something",
  85. }
  86. return nil
  87. })
  88. cmd := ImportDashboardCommand{
  89. PluginId: "test-app",
  90. Path: "dashboards/connections.json",
  91. OrgId: 1,
  92. UserId: 1,
  93. Inputs: []ImportDashboardInput{
  94. {Name: "*", Type: "datasource", Value: "graphite"},
  95. },
  96. }
  97. err = ImportDashboard(&cmd)
  98. So(err, ShouldBeNil)
  99. Convey("should install dashboard", func() {
  100. So(importedDash, ShouldNotBeNil)
  101. resultStr, _ := importedDash.Data.EncodePretty()
  102. expectedBytes, _ := ioutil.ReadFile("../../tests/test-app/dashboards/connections_result.json")
  103. expectedJson, _ := simplejson.NewJson(expectedBytes)
  104. expectedStr, _ := expectedJson.EncodePretty()
  105. So(string(resultStr), ShouldEqual, string(expectedStr))
  106. panel := importedDash.Data.Get("rows").GetIndex(0).Get("panels").GetIndex(0)
  107. So(panel.Get("datasource").MustString(), ShouldEqual, "graphite")
  108. So(importedDash.FolderId, ShouldEqual, folderId)
  109. })
  110. Convey("should not create app folder", func() {
  111. So(createdFolder, ShouldBeNil)
  112. })
  113. })
  114. Convey("When evaling dashboard template", t, func() {
  115. template, _ := simplejson.NewJson([]byte(`{
  116. "__inputs": [
  117. {
  118. "name": "DS_NAME",
  119. "type": "datasource"
  120. }
  121. ],
  122. "test": {
  123. "prop": "${DS_NAME}"
  124. }
  125. }`))
  126. evaluator := &DashTemplateEvaluator{
  127. template: template,
  128. inputs: []ImportDashboardInput{
  129. {Name: "*", Type: "datasource", Value: "my-server"},
  130. },
  131. }
  132. res, err := evaluator.Eval()
  133. So(err, ShouldBeNil)
  134. Convey("should render template", func() {
  135. So(res.GetPath("test", "prop").MustString(), ShouldEqual, "my-server")
  136. })
  137. Convey("should not include inputs in output", func() {
  138. inputs := res.Get("__inputs")
  139. So(inputs.Interface(), ShouldBeNil)
  140. })
  141. })
  142. }