dashboard_snapshot_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package sqlstore
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/go-xorm/xorm"
  6. . "github.com/smartystreets/goconvey/convey"
  7. "github.com/grafana/grafana/pkg/components/simplejson"
  8. m "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/setting"
  10. )
  11. func TestDashboardSnapshotDBAccess(t *testing.T) {
  12. Convey("Testing DashboardSnapshot data access", t, func() {
  13. InitTestDB(t)
  14. Convey("Given saved snapshot", func() {
  15. cmd := m.CreateDashboardSnapshotCommand{
  16. Key: "hej",
  17. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  18. "hello": "mupp",
  19. }),
  20. UserId: 1000,
  21. OrgId: 1,
  22. }
  23. err := CreateDashboardSnapshot(&cmd)
  24. So(err, ShouldBeNil)
  25. Convey("Should be able to get snapshot by key", func() {
  26. query := m.GetDashboardSnapshotQuery{Key: "hej"}
  27. err = GetDashboardSnapshot(&query)
  28. So(err, ShouldBeNil)
  29. So(query.Result, ShouldNotBeNil)
  30. So(query.Result.Dashboard.Get("hello").MustString(), ShouldEqual, "mupp")
  31. })
  32. Convey("And the user has the admin role", func() {
  33. Convey("Should return all the snapshots", func() {
  34. query := m.GetDashboardSnapshotsQuery{
  35. OrgId: 1,
  36. SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_ADMIN},
  37. }
  38. err := SearchDashboardSnapshots(&query)
  39. So(err, ShouldBeNil)
  40. So(query.Result, ShouldNotBeNil)
  41. So(len(query.Result), ShouldEqual, 1)
  42. })
  43. })
  44. Convey("And the user has the editor role and has created a snapshot", func() {
  45. Convey("Should return all the snapshots", func() {
  46. query := m.GetDashboardSnapshotsQuery{
  47. OrgId: 1,
  48. SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_EDITOR, UserId: 1000},
  49. }
  50. err := SearchDashboardSnapshots(&query)
  51. So(err, ShouldBeNil)
  52. So(query.Result, ShouldNotBeNil)
  53. So(len(query.Result), ShouldEqual, 1)
  54. })
  55. })
  56. Convey("And the user has the editor role and has not created any snapshot", func() {
  57. Convey("Should not return any snapshots", func() {
  58. query := m.GetDashboardSnapshotsQuery{
  59. OrgId: 1,
  60. SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_EDITOR, UserId: 2},
  61. }
  62. err := SearchDashboardSnapshots(&query)
  63. So(err, ShouldBeNil)
  64. So(query.Result, ShouldNotBeNil)
  65. So(len(query.Result), ShouldEqual, 0)
  66. })
  67. })
  68. Convey("And the user is anonymous", func() {
  69. cmd := m.CreateDashboardSnapshotCommand{
  70. Key: "strangesnapshotwithuserid0",
  71. DeleteKey: "adeletekey",
  72. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  73. "hello": "mupp",
  74. }),
  75. UserId: 0,
  76. OrgId: 1,
  77. }
  78. err := CreateDashboardSnapshot(&cmd)
  79. So(err, ShouldBeNil)
  80. Convey("Should not return any snapshots", func() {
  81. query := m.GetDashboardSnapshotsQuery{
  82. OrgId: 1,
  83. SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_EDITOR, IsAnonymous: true, UserId: 0},
  84. }
  85. err := SearchDashboardSnapshots(&query)
  86. So(err, ShouldBeNil)
  87. So(query.Result, ShouldNotBeNil)
  88. So(len(query.Result), ShouldEqual, 0)
  89. })
  90. })
  91. })
  92. })
  93. }
  94. func TestDeleteExpiredSnapshots(t *testing.T) {
  95. Convey("Testing dashboard snapshots clean up", t, func() {
  96. x := InitTestDB(t)
  97. setting.SnapShotRemoveExpired = true
  98. notExpiredsnapshot := createTestSnapshot(x, "key1", 1000)
  99. createTestSnapshot(x, "key2", -1000)
  100. createTestSnapshot(x, "key3", -1000)
  101. Convey("Clean up old dashboard snapshots", func() {
  102. err := DeleteExpiredSnapshots(&m.DeleteExpiredSnapshotsCommand{})
  103. So(err, ShouldBeNil)
  104. query := m.GetDashboardSnapshotsQuery{
  105. OrgId: 1,
  106. SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_ADMIN},
  107. }
  108. err = SearchDashboardSnapshots(&query)
  109. So(err, ShouldBeNil)
  110. So(len(query.Result), ShouldEqual, 1)
  111. So(query.Result[0].Key, ShouldEqual, notExpiredsnapshot.Key)
  112. })
  113. Convey("Don't delete anything if there are no expired snapshots", func() {
  114. err := DeleteExpiredSnapshots(&m.DeleteExpiredSnapshotsCommand{})
  115. So(err, ShouldBeNil)
  116. query := m.GetDashboardSnapshotsQuery{
  117. OrgId: 1,
  118. SignedInUser: &m.SignedInUser{OrgRole: m.ROLE_ADMIN},
  119. }
  120. SearchDashboardSnapshots(&query)
  121. So(len(query.Result), ShouldEqual, 1)
  122. })
  123. })
  124. }
  125. func createTestSnapshot(x *xorm.Engine, key string, expires int64) *m.DashboardSnapshot {
  126. cmd := m.CreateDashboardSnapshotCommand{
  127. Key: key,
  128. DeleteKey: "delete" + key,
  129. Dashboard: simplejson.NewFromAny(map[string]interface{}{
  130. "hello": "mupp",
  131. }),
  132. UserId: 1000,
  133. OrgId: 1,
  134. Expires: expires,
  135. }
  136. err := CreateDashboardSnapshot(&cmd)
  137. So(err, ShouldBeNil)
  138. // Set expiry date manually - to be able to create expired snapshots
  139. expireDate := time.Now().Add(time.Second * time.Duration(expires))
  140. _, err = x.Exec("update dashboard_snapshot set expires = ? where "+dialect.Quote("key")+" = ?", expireDate, key)
  141. So(err, ShouldBeNil)
  142. return cmd.Result
  143. }