db_test.go 4.4 KB

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