db_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package manager
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. . "github.com/smartystreets/goconvey/convey"
  10. . "github.com/tuna/tunasync/internal"
  11. )
  12. func TestBoltAdapter(t *testing.T) {
  13. Convey("boltAdapter should work", t, func() {
  14. tmpDir, err := ioutil.TempDir("", "tunasync")
  15. defer os.RemoveAll(tmpDir)
  16. So(err, ShouldBeNil)
  17. dbType, dbFile := "bolt", filepath.Join(tmpDir, "bolt.db")
  18. boltDB, err := makeDBAdapter(dbType, dbFile)
  19. So(err, ShouldBeNil)
  20. defer func() {
  21. // close boltDB
  22. err := boltDB.Close()
  23. So(err, ShouldBeNil)
  24. }()
  25. testWorkerIDs := []string{"test_worker1", "test_worker2"}
  26. Convey("create worker", func() {
  27. for _, id := range testWorkerIDs {
  28. w := WorkerStatus{
  29. ID: id,
  30. Token: "token_" + id,
  31. LastOnline: time.Now(),
  32. }
  33. w, err = boltDB.CreateWorker(w)
  34. So(err, ShouldBeNil)
  35. }
  36. Convey("get existent worker", func() {
  37. _, err := boltDB.GetWorker(testWorkerIDs[0])
  38. So(err, ShouldBeNil)
  39. })
  40. Convey("list existent workers", func() {
  41. ws, err := boltDB.ListWorkers()
  42. So(err, ShouldBeNil)
  43. So(len(ws), ShouldEqual, 2)
  44. })
  45. Convey("get non-existent worker", func() {
  46. _, err := boltDB.GetWorker("invalid workerID")
  47. So(err, ShouldNotBeNil)
  48. })
  49. Convey("delete existent worker", func() {
  50. err := boltDB.DeleteWorker(testWorkerIDs[0])
  51. So(err, ShouldBeNil)
  52. _, err = boltDB.GetWorker(testWorkerIDs[0])
  53. So(err, ShouldNotBeNil)
  54. ws, err := boltDB.ListWorkers()
  55. So(err, ShouldBeNil)
  56. So(len(ws), ShouldEqual, 1)
  57. })
  58. Convey("delete non-existent worker", func() {
  59. err := boltDB.DeleteWorker("invalid workerID")
  60. So(err, ShouldNotBeNil)
  61. ws, err := boltDB.ListWorkers()
  62. So(err, ShouldBeNil)
  63. So(len(ws), ShouldEqual, 2)
  64. })
  65. })
  66. Convey("update mirror status", func() {
  67. status := []MirrorStatus{
  68. MirrorStatus{
  69. Name: "arch-sync1",
  70. Worker: testWorkerIDs[0],
  71. IsMaster: true,
  72. Status: Success,
  73. LastUpdate: time.Now(),
  74. LastStarted: time.Now().Add(-time.Minute),
  75. LastEnded: time.Now(),
  76. Upstream: "mirrors.tuna.tsinghua.edu.cn",
  77. Size: "3GB",
  78. },
  79. MirrorStatus{
  80. Name: "arch-sync2",
  81. Worker: testWorkerIDs[1],
  82. IsMaster: true,
  83. Status: Disabled,
  84. LastUpdate: time.Now().Add(-time.Hour),
  85. LastStarted: time.Now().Add(-time.Minute),
  86. LastEnded: time.Now(),
  87. Upstream: "mirrors.tuna.tsinghua.edu.cn",
  88. Size: "4GB",
  89. },
  90. MirrorStatus{
  91. Name: "arch-sync3",
  92. Worker: testWorkerIDs[1],
  93. IsMaster: true,
  94. Status: Success,
  95. LastUpdate: time.Now().Add(-time.Minute),
  96. LastStarted: time.Now().Add(-time.Second),
  97. LastEnded: time.Now(),
  98. Upstream: "mirrors.tuna.tsinghua.edu.cn",
  99. Size: "4GB",
  100. },
  101. }
  102. for _, s := range status {
  103. _, err := boltDB.UpdateMirrorStatus(s.Worker, s.Name, s)
  104. So(err, ShouldBeNil)
  105. }
  106. Convey("get mirror status", func() {
  107. m, err := boltDB.GetMirrorStatus(testWorkerIDs[0], status[0].Name)
  108. So(err, ShouldBeNil)
  109. expectedJSON, err := json.Marshal(status[0])
  110. So(err, ShouldBeNil)
  111. actualJSON, err := json.Marshal(m)
  112. So(err, ShouldBeNil)
  113. So(string(actualJSON), ShouldEqual, string(expectedJSON))
  114. })
  115. Convey("list mirror status", func() {
  116. ms, err := boltDB.ListMirrorStatus(testWorkerIDs[0])
  117. So(err, ShouldBeNil)
  118. expectedJSON, err := json.Marshal([]MirrorStatus{status[0]})
  119. So(err, ShouldBeNil)
  120. actualJSON, err := json.Marshal(ms)
  121. So(err, ShouldBeNil)
  122. So(string(actualJSON), ShouldEqual, string(expectedJSON))
  123. })
  124. Convey("list all mirror status", func() {
  125. ms, err := boltDB.ListAllMirrorStatus()
  126. So(err, ShouldBeNil)
  127. expectedJSON, err := json.Marshal(status)
  128. So(err, ShouldBeNil)
  129. actualJSON, err := json.Marshal(ms)
  130. So(err, ShouldBeNil)
  131. So(string(actualJSON), ShouldEqual, string(expectedJSON))
  132. })
  133. Convey("flush disabled jobs", func() {
  134. ms, err := boltDB.ListAllMirrorStatus()
  135. So(err, ShouldBeNil)
  136. So(len(ms), ShouldEqual, 3)
  137. err = boltDB.FlushDisabledJobs()
  138. So(err, ShouldBeNil)
  139. ms, err = boltDB.ListAllMirrorStatus()
  140. So(err, ShouldBeNil)
  141. So(len(ms), ShouldEqual, 2)
  142. })
  143. })
  144. })
  145. }