2
0

db_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package manager
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "sort"
  9. "testing"
  10. "time"
  11. "github.com/alicebob/miniredis"
  12. . "github.com/smartystreets/goconvey/convey"
  13. . "github.com/tuna/tunasync/internal"
  14. )
  15. func SortMirrorStatus(status []MirrorStatus) {
  16. sort.Slice(status, func(l, r int) bool {
  17. return status[l].Name < status[r].Name
  18. })
  19. }
  20. func DBAdapterTest(db dbAdapter) {
  21. var err error
  22. testWorkerIDs := []string{"test_worker1", "test_worker2"}
  23. Convey("create worker", func() {
  24. for _, id := range testWorkerIDs {
  25. w := WorkerStatus{
  26. ID: id,
  27. Token: "token_" + id,
  28. LastOnline: time.Now(),
  29. LastRegister: time.Now(),
  30. }
  31. w, err = db.CreateWorker(w)
  32. So(err, ShouldBeNil)
  33. }
  34. Convey("get existent worker", func() {
  35. _, err := db.GetWorker(testWorkerIDs[0])
  36. So(err, ShouldBeNil)
  37. })
  38. Convey("list existent workers", func() {
  39. ws, err := db.ListWorkers()
  40. So(err, ShouldBeNil)
  41. So(len(ws), ShouldEqual, 2)
  42. })
  43. Convey("get non-existent worker", func() {
  44. _, err := db.GetWorker("invalid workerID")
  45. So(err, ShouldNotBeNil)
  46. })
  47. Convey("delete existent worker", func() {
  48. err := db.DeleteWorker(testWorkerIDs[0])
  49. So(err, ShouldBeNil)
  50. _, err = db.GetWorker(testWorkerIDs[0])
  51. So(err, ShouldNotBeNil)
  52. ws, err := db.ListWorkers()
  53. So(err, ShouldBeNil)
  54. So(len(ws), ShouldEqual, 1)
  55. })
  56. Convey("delete non-existent worker", func() {
  57. err := db.DeleteWorker("invalid workerID")
  58. So(err, ShouldNotBeNil)
  59. ws, err := db.ListWorkers()
  60. So(err, ShouldBeNil)
  61. So(len(ws), ShouldEqual, 2)
  62. })
  63. })
  64. Convey("update mirror status", func() {
  65. status := []MirrorStatus{
  66. MirrorStatus{
  67. Name: "arch-sync1",
  68. Worker: testWorkerIDs[0],
  69. IsMaster: true,
  70. Status: Success,
  71. LastUpdate: time.Now(),
  72. LastStarted: time.Now().Add(-time.Minute),
  73. LastEnded: time.Now(),
  74. Upstream: "mirrors.tuna.tsinghua.edu.cn",
  75. Size: "3GB",
  76. },
  77. MirrorStatus{
  78. Name: "arch-sync2",
  79. Worker: testWorkerIDs[1],
  80. IsMaster: true,
  81. Status: Disabled,
  82. LastUpdate: time.Now().Add(-time.Hour),
  83. LastStarted: time.Now().Add(-time.Minute),
  84. LastEnded: time.Now(),
  85. Upstream: "mirrors.tuna.tsinghua.edu.cn",
  86. Size: "4GB",
  87. },
  88. MirrorStatus{
  89. Name: "arch-sync3",
  90. Worker: testWorkerIDs[1],
  91. IsMaster: true,
  92. Status: Success,
  93. LastUpdate: time.Now().Add(-time.Minute),
  94. LastStarted: time.Now().Add(-time.Second),
  95. LastEnded: time.Now(),
  96. Upstream: "mirrors.tuna.tsinghua.edu.cn",
  97. Size: "4GB",
  98. },
  99. }
  100. SortMirrorStatus(status)
  101. for _, s := range status {
  102. _, err := db.UpdateMirrorStatus(s.Worker, s.Name, s)
  103. So(err, ShouldBeNil)
  104. }
  105. Convey("get mirror status", func() {
  106. m, err := db.GetMirrorStatus(testWorkerIDs[0], status[0].Name)
  107. So(err, ShouldBeNil)
  108. expectedJSON, err := json.Marshal(status[0])
  109. So(err, ShouldBeNil)
  110. actualJSON, err := json.Marshal(m)
  111. So(err, ShouldBeNil)
  112. So(string(actualJSON), ShouldEqual, string(expectedJSON))
  113. })
  114. Convey("list mirror status", func() {
  115. ms, err := db.ListMirrorStatus(testWorkerIDs[0])
  116. So(err, ShouldBeNil)
  117. expectedJSON, err := json.Marshal([]MirrorStatus{status[0]})
  118. So(err, ShouldBeNil)
  119. actualJSON, err := json.Marshal(ms)
  120. So(err, ShouldBeNil)
  121. So(string(actualJSON), ShouldEqual, string(expectedJSON))
  122. })
  123. Convey("list all mirror status", func() {
  124. ms, err := db.ListAllMirrorStatus()
  125. So(err, ShouldBeNil)
  126. SortMirrorStatus(ms)
  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 := db.ListAllMirrorStatus()
  135. So(err, ShouldBeNil)
  136. So(len(ms), ShouldEqual, 3)
  137. err = db.FlushDisabledJobs()
  138. So(err, ShouldBeNil)
  139. ms, err = db.ListAllMirrorStatus()
  140. So(err, ShouldBeNil)
  141. So(len(ms), ShouldEqual, 2)
  142. })
  143. })
  144. return
  145. }
  146. func TestBoltAdapter(t *testing.T) {
  147. Convey("boltAdapter should work", t, func() {
  148. tmpDir, err := ioutil.TempDir("", "tunasync")
  149. defer os.RemoveAll(tmpDir)
  150. So(err, ShouldBeNil)
  151. dbType, dbFile := "bolt", filepath.Join(tmpDir, "bolt.db")
  152. boltDB, err := makeDBAdapter(dbType, dbFile)
  153. So(err, ShouldBeNil)
  154. defer func() {
  155. // close boltDB
  156. err := boltDB.Close()
  157. So(err, ShouldBeNil)
  158. }()
  159. DBAdapterTest(boltDB)
  160. })
  161. Convey("redisAdapter should work", t, func() {
  162. mr, err := miniredis.Run()
  163. So(err, ShouldBeNil)
  164. addr := fmt.Sprintf("redis://%s", mr.Addr())
  165. redisDB, err := makeDBAdapter("redis", addr)
  166. So(err, ShouldBeNil)
  167. defer func() {
  168. // close redisDB
  169. err := redisDB.Close()
  170. So(err, ShouldBeNil)
  171. mr.Close()
  172. }()
  173. DBAdapterTest(redisDB)
  174. })
  175. }