dashboard_version_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package sqlstore
  2. import (
  3. "reflect"
  4. "testing"
  5. . "github.com/smartystreets/goconvey/convey"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. m "github.com/grafana/grafana/pkg/models"
  8. "github.com/grafana/grafana/pkg/setting"
  9. )
  10. func updateTestDashboard(dashboard *m.Dashboard, data map[string]interface{}) {
  11. data["title"] = dashboard.Title
  12. saveCmd := m.SaveDashboardCommand{
  13. OrgId: dashboard.OrgId,
  14. Overwrite: true,
  15. Dashboard: simplejson.NewFromAny(data),
  16. }
  17. err := SaveDashboard(&saveCmd)
  18. So(err, ShouldBeNil)
  19. }
  20. func TestGetDashboardVersion(t *testing.T) {
  21. Convey("Testing dashboard version retrieval", t, func() {
  22. InitTestDB(t)
  23. Convey("Get a Dashboard ID and version ID", func() {
  24. savedDash := insertTestDashboard("test dash 26", 1, 0, false, "diff")
  25. query := m.GetDashboardVersionQuery{
  26. DashboardId: savedDash.Id,
  27. Version: savedDash.Version,
  28. OrgId: 1,
  29. }
  30. err := GetDashboardVersion(&query)
  31. So(err, ShouldBeNil)
  32. So(savedDash.Id, ShouldEqual, query.DashboardId)
  33. So(savedDash.Version, ShouldEqual, query.Version)
  34. dashCmd := m.GetDashboardQuery{
  35. OrgId: savedDash.OrgId,
  36. Slug: savedDash.Slug,
  37. }
  38. err = GetDashboard(&dashCmd)
  39. So(err, ShouldBeNil)
  40. eq := reflect.DeepEqual(dashCmd.Result.Data, query.Result.Data)
  41. So(eq, ShouldEqual, true)
  42. })
  43. Convey("Attempt to get a version that doesn't exist", func() {
  44. query := m.GetDashboardVersionQuery{
  45. DashboardId: int64(999),
  46. Version: 123,
  47. OrgId: 1,
  48. }
  49. err := GetDashboardVersion(&query)
  50. So(err, ShouldNotBeNil)
  51. So(err, ShouldEqual, m.ErrDashboardVersionNotFound)
  52. })
  53. })
  54. }
  55. func TestGetDashboardVersions(t *testing.T) {
  56. Convey("Testing dashboard versions retrieval", t, func() {
  57. InitTestDB(t)
  58. savedDash := insertTestDashboard("test dash 43", 1, 0, false, "diff-all")
  59. Convey("Get all versions for a given Dashboard ID", func() {
  60. query := m.GetDashboardVersionsQuery{DashboardId: savedDash.Id, OrgId: 1}
  61. err := GetDashboardVersions(&query)
  62. So(err, ShouldBeNil)
  63. So(len(query.Result), ShouldEqual, 1)
  64. })
  65. Convey("Attempt to get the versions for a non-existent Dashboard ID", func() {
  66. query := m.GetDashboardVersionsQuery{DashboardId: int64(999), OrgId: 1}
  67. err := GetDashboardVersions(&query)
  68. So(err, ShouldNotBeNil)
  69. So(err, ShouldEqual, m.ErrNoVersionsForDashboardId)
  70. So(len(query.Result), ShouldEqual, 0)
  71. })
  72. Convey("Get all versions for an updated dashboard", func() {
  73. updateTestDashboard(savedDash, map[string]interface{}{
  74. "tags": "different-tag",
  75. })
  76. query := m.GetDashboardVersionsQuery{DashboardId: savedDash.Id, OrgId: 1}
  77. err := GetDashboardVersions(&query)
  78. So(err, ShouldBeNil)
  79. So(len(query.Result), ShouldEqual, 2)
  80. })
  81. })
  82. }
  83. func TestDeleteExpiredVersions(t *testing.T) {
  84. Convey("Testing dashboard versions clean up", t, func() {
  85. InitTestDB(t)
  86. versionsToKeep := 5
  87. versionsToWrite := 10
  88. setting.DashboardVersionsToKeep = versionsToKeep
  89. savedDash := insertTestDashboard("test dash 53", 1, "diff-all")
  90. for i := 0; i < versionsToWrite-1; i++ {
  91. updateTestDashboard(savedDash, map[string]interface{}{
  92. "tags": "different-tag",
  93. })
  94. }
  95. Convey("Clean up old dashboard versions", func() {
  96. err := DeleteExpiredVersions(&m.DeleteExpiredVersionsCommand{})
  97. So(err, ShouldBeNil)
  98. query := m.GetDashboardVersionsQuery{DashboardId: savedDash.Id, OrgId: 1}
  99. GetDashboardVersions(&query)
  100. So(len(query.Result), ShouldEqual, versionsToKeep)
  101. // Ensure latest versions were kept
  102. So(query.Result[versionsToKeep-1].Version, ShouldEqual, versionsToWrite-versionsToKeep+1)
  103. So(query.Result[0].Version, ShouldEqual, versionsToWrite)
  104. })
  105. Convey("Don't delete anything if there're no expired versions", func() {
  106. setting.DashboardVersionsToKeep = versionsToWrite
  107. err := DeleteExpiredVersions(&m.DeleteExpiredVersionsCommand{})
  108. So(err, ShouldBeNil)
  109. query := m.GetDashboardVersionsQuery{DashboardId: savedDash.Id, OrgId: 1}
  110. GetDashboardVersions(&query)
  111. So(len(query.Result), ShouldEqual, versionsToWrite)
  112. })
  113. })
  114. }