db_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. LastRegister: time.Now(),
  33. }
  34. w, err = boltDB.CreateWorker(w)
  35. So(err, ShouldBeNil)
  36. }
  37. Convey("get existent worker", func() {
  38. _, err := boltDB.GetWorker(testWorkerIDs[0])
  39. So(err, ShouldBeNil)
  40. })
  41. Convey("list existent workers", func() {
  42. ws, err := boltDB.ListWorkers()
  43. So(err, ShouldBeNil)
  44. So(len(ws), ShouldEqual, 2)
  45. })
  46. Convey("get non-existent worker", func() {
  47. _, err := boltDB.GetWorker("invalid workerID")
  48. So(err, ShouldNotBeNil)
  49. })
  50. Convey("delete existent worker", func() {
  51. err := boltDB.DeleteWorker(testWorkerIDs[0])
  52. So(err, ShouldBeNil)
  53. _, err = boltDB.GetWorker(testWorkerIDs[0])
  54. So(err, ShouldNotBeNil)
  55. ws, err := boltDB.ListWorkers()
  56. So(err, ShouldBeNil)
  57. So(len(ws), ShouldEqual, 1)
  58. })
  59. Convey("delete non-existent worker", func() {
  60. err := boltDB.DeleteWorker("invalid workerID")
  61. So(err, ShouldNotBeNil)
  62. ws, err := boltDB.ListWorkers()
  63. So(err, ShouldBeNil)
  64. So(len(ws), ShouldEqual, 2)
  65. })
  66. })
  67. Convey("update mirror status", func() {
  68. status := []MirrorStatus{
  69. MirrorStatus{
  70. Name: "arch-sync1",
  71. Worker: testWorkerIDs[0],
  72. IsMaster: true,
  73. Status: Success,
  74. LastUpdate: time.Now(),
  75. LastStarted: time.Now().Add(-time.Minute),
  76. LastEnded: time.Now(),
  77. Upstream: "mirrors.tuna.tsinghua.edu.cn",
  78. Size: "3GB",
  79. },
  80. MirrorStatus{
  81. Name: "arch-sync2",
  82. Worker: testWorkerIDs[1],
  83. IsMaster: true,
  84. Status: Disabled,
  85. LastUpdate: time.Now().Add(-time.Hour),
  86. LastStarted: time.Now().Add(-time.Minute),
  87. LastEnded: time.Now(),
  88. Upstream: "mirrors.tuna.tsinghua.edu.cn",
  89. Size: "4GB",
  90. },
  91. MirrorStatus{
  92. Name: "arch-sync3",
  93. Worker: testWorkerIDs[1],
  94. IsMaster: true,
  95. Status: Success,
  96. LastUpdate: time.Now().Add(-time.Minute),
  97. LastStarted: time.Now().Add(-time.Second),
  98. LastEnded: time.Now(),
  99. Upstream: "mirrors.tuna.tsinghua.edu.cn",
  100. Size: "4GB",
  101. },
  102. }
  103. for _, s := range status {
  104. _, err := boltDB.UpdateMirrorStatus(s.Worker, s.Name, s)
  105. So(err, ShouldBeNil)
  106. }
  107. Convey("get mirror status", func() {
  108. m, err := boltDB.GetMirrorStatus(testWorkerIDs[0], status[0].Name)
  109. So(err, ShouldBeNil)
  110. expectedJSON, err := json.Marshal(status[0])
  111. So(err, ShouldBeNil)
  112. actualJSON, err := json.Marshal(m)
  113. So(err, ShouldBeNil)
  114. So(string(actualJSON), ShouldEqual, string(expectedJSON))
  115. })
  116. Convey("list mirror status", func() {
  117. ms, err := boltDB.ListMirrorStatus(testWorkerIDs[0])
  118. So(err, ShouldBeNil)
  119. expectedJSON, err := json.Marshal([]MirrorStatus{status[0]})
  120. So(err, ShouldBeNil)
  121. actualJSON, err := json.Marshal(ms)
  122. So(err, ShouldBeNil)
  123. So(string(actualJSON), ShouldEqual, string(expectedJSON))
  124. })
  125. Convey("list all mirror status", func() {
  126. ms, err := boltDB.ListAllMirrorStatus()
  127. So(err, ShouldBeNil)
  128. expectedJSON, err := json.Marshal(status)
  129. So(err, ShouldBeNil)
  130. actualJSON, err := json.Marshal(ms)
  131. So(err, ShouldBeNil)
  132. So(string(actualJSON), ShouldEqual, string(expectedJSON))
  133. })
  134. Convey("flush disabled jobs", func() {
  135. ms, err := boltDB.ListAllMirrorStatus()
  136. So(err, ShouldBeNil)
  137. So(len(ms), ShouldEqual, 3)
  138. err = boltDB.FlushDisabledJobs()
  139. So(err, ShouldBeNil)
  140. ms, err = boltDB.ListAllMirrorStatus()
  141. So(err, ShouldBeNil)
  142. So(len(ms), ShouldEqual, 2)
  143. })
  144. })
  145. })
  146. }