db_test.go 3.6 KB

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