dashboards_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package plugins
  2. import (
  3. "context"
  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 TestPluginDashboards(t *testing.T) {
  13. Convey("When asking plugin dashboard info", t, func() {
  14. setting.Cfg = ini.Empty()
  15. sec, _ := setting.Cfg.NewSection("plugin.test-app")
  16. sec.NewKey("path", "../../tests/test-app")
  17. err := initPlugins(context.Background())
  18. So(err, ShouldBeNil)
  19. bus.AddHandler("test", func(query *m.GetDashboardQuery) error {
  20. if query.Slug == "nginx-connections" {
  21. dash := m.NewDashboard("Nginx Connections")
  22. dash.Data.Set("revision", "1.1")
  23. query.Result = dash
  24. return nil
  25. }
  26. return m.ErrDashboardNotFound
  27. })
  28. bus.AddHandler("test", func(query *m.GetDashboardsByPluginIdQuery) error {
  29. var data = simplejson.New()
  30. data.Set("title", "Nginx Connections")
  31. data.Set("revision", 22)
  32. query.Result = []*m.Dashboard{
  33. {Slug: "nginx-connections", Data: data},
  34. }
  35. return nil
  36. })
  37. dashboards, err := GetPluginDashboards(1, "test-app")
  38. So(err, ShouldBeNil)
  39. Convey("should return 2 dashboarrd", func() {
  40. So(len(dashboards), ShouldEqual, 2)
  41. })
  42. Convey("should include installed version info", func() {
  43. So(dashboards[0].Title, ShouldEqual, "Nginx Connections")
  44. So(dashboards[0].Revision, ShouldEqual, 25)
  45. So(dashboards[0].ImportedRevision, ShouldEqual, 22)
  46. So(dashboards[0].ImportedUri, ShouldEqual, "db/nginx-connections")
  47. So(dashboards[1].Revision, ShouldEqual, 2)
  48. So(dashboards[1].ImportedRevision, ShouldEqual, 0)
  49. })
  50. })
  51. }