2
0

db_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. Upstream: "mirrors.tuna.tsinghua.edu.cn",
  59. Size: "3GB",
  60. },
  61. MirrorStatus{
  62. Name: "arch-sync2",
  63. Worker: testWorkerIDs[1],
  64. IsMaster: true,
  65. Status: Disabled,
  66. LastUpdate: time.Now(),
  67. Upstream: "mirrors.tuna.tsinghua.edu.cn",
  68. Size: "4GB",
  69. },
  70. MirrorStatus{
  71. Name: "arch-sync3",
  72. Worker: testWorkerIDs[1],
  73. IsMaster: true,
  74. Status: Success,
  75. LastUpdate: time.Now(),
  76. Upstream: "mirrors.tuna.tsinghua.edu.cn",
  77. Size: "4GB",
  78. },
  79. }
  80. for _, s := range status {
  81. _, err := boltDB.UpdateMirrorStatus(s.Worker, s.Name, s)
  82. So(err, ShouldBeNil)
  83. }
  84. Convey("get mirror status", func() {
  85. m, err := boltDB.GetMirrorStatus(testWorkerIDs[0], status[0].Name)
  86. So(err, ShouldBeNil)
  87. expectedJSON, err := json.Marshal(status[0])
  88. So(err, ShouldBeNil)
  89. actualJSON, err := json.Marshal(m)
  90. So(err, ShouldBeNil)
  91. So(string(actualJSON), ShouldEqual, string(expectedJSON))
  92. })
  93. Convey("list mirror status", func() {
  94. ms, err := boltDB.ListMirrorStatus(testWorkerIDs[0])
  95. So(err, ShouldBeNil)
  96. expectedJSON, err := json.Marshal([]MirrorStatus{status[0]})
  97. So(err, ShouldBeNil)
  98. actualJSON, err := json.Marshal(ms)
  99. So(err, ShouldBeNil)
  100. So(string(actualJSON), ShouldEqual, string(expectedJSON))
  101. })
  102. Convey("list all mirror status", func() {
  103. ms, err := boltDB.ListAllMirrorStatus()
  104. So(err, ShouldBeNil)
  105. expectedJSON, err := json.Marshal(status)
  106. So(err, ShouldBeNil)
  107. actualJSON, err := json.Marshal(ms)
  108. So(err, ShouldBeNil)
  109. So(string(actualJSON), ShouldEqual, string(expectedJSON))
  110. })
  111. Convey("flush disabled jobs", func() {
  112. ms, err := boltDB.ListAllMirrorStatus()
  113. So(err, ShouldBeNil)
  114. So(len(ms), ShouldEqual, 3)
  115. err = boltDB.FlushDisabledJobs()
  116. So(err, ShouldBeNil)
  117. ms, err = boltDB.ListAllMirrorStatus()
  118. So(err, ShouldBeNil)
  119. So(len(ms), ShouldEqual, 2)
  120. })
  121. })
  122. })
  123. }