mirror of
https://github.com/tuna/tunasync.git
synced 2025-04-21 04:42:46 +00:00
Rearrange and fix db tests
This commit is contained in:
parent
90b4e5debb
commit
d341c0c99d
@ -5,6 +5,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -12,22 +13,14 @@ import (
|
|||||||
. "github.com/tuna/tunasync/internal"
|
. "github.com/tuna/tunasync/internal"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestBoltAdapter(t *testing.T) {
|
func SortMirrorStatus(status []MirrorStatus) {
|
||||||
Convey("boltAdapter should work", t, func() {
|
sort.Slice(status, func(l, r int) bool {
|
||||||
tmpDir, err := ioutil.TempDir("", "tunasync")
|
return status[l].Name < status[r].Name
|
||||||
defer os.RemoveAll(tmpDir)
|
})
|
||||||
So(err, ShouldBeNil)
|
}
|
||||||
|
|
||||||
dbType, dbFile := "bolt", filepath.Join(tmpDir, "bolt.db")
|
|
||||||
boltDB, err := makeDBAdapter(dbType, dbFile)
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
// close boltDB
|
|
||||||
err := boltDB.Close()
|
|
||||||
So(err, ShouldBeNil)
|
|
||||||
}()
|
|
||||||
|
|
||||||
|
func DBAdapterTest(db dbAdapter) {
|
||||||
|
var err error
|
||||||
testWorkerIDs := []string{"test_worker1", "test_worker2"}
|
testWorkerIDs := []string{"test_worker1", "test_worker2"}
|
||||||
Convey("create worker", func() {
|
Convey("create worker", func() {
|
||||||
for _, id := range testWorkerIDs {
|
for _, id := range testWorkerIDs {
|
||||||
@ -37,40 +30,40 @@ func TestBoltAdapter(t *testing.T) {
|
|||||||
LastOnline: time.Now(),
|
LastOnline: time.Now(),
|
||||||
LastRegister: time.Now(),
|
LastRegister: time.Now(),
|
||||||
}
|
}
|
||||||
w, err = boltDB.CreateWorker(w)
|
w, err = db.CreateWorker(w)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
}
|
}
|
||||||
|
|
||||||
Convey("get existent worker", func() {
|
Convey("get existent worker", func() {
|
||||||
_, err := boltDB.GetWorker(testWorkerIDs[0])
|
_, err := db.GetWorker(testWorkerIDs[0])
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("list existent workers", func() {
|
Convey("list existent workers", func() {
|
||||||
ws, err := boltDB.ListWorkers()
|
ws, err := db.ListWorkers()
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(len(ws), ShouldEqual, 2)
|
So(len(ws), ShouldEqual, 2)
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("get non-existent worker", func() {
|
Convey("get non-existent worker", func() {
|
||||||
_, err := boltDB.GetWorker("invalid workerID")
|
_, err := db.GetWorker("invalid workerID")
|
||||||
So(err, ShouldNotBeNil)
|
So(err, ShouldNotBeNil)
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("delete existent worker", func() {
|
Convey("delete existent worker", func() {
|
||||||
err := boltDB.DeleteWorker(testWorkerIDs[0])
|
err := db.DeleteWorker(testWorkerIDs[0])
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
_, err = boltDB.GetWorker(testWorkerIDs[0])
|
_, err = db.GetWorker(testWorkerIDs[0])
|
||||||
So(err, ShouldNotBeNil)
|
So(err, ShouldNotBeNil)
|
||||||
ws, err := boltDB.ListWorkers()
|
ws, err := db.ListWorkers()
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(len(ws), ShouldEqual, 1)
|
So(len(ws), ShouldEqual, 1)
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("delete non-existent worker", func() {
|
Convey("delete non-existent worker", func() {
|
||||||
err := boltDB.DeleteWorker("invalid workerID")
|
err := db.DeleteWorker("invalid workerID")
|
||||||
So(err, ShouldNotBeNil)
|
So(err, ShouldNotBeNil)
|
||||||
ws, err := boltDB.ListWorkers()
|
ws, err := db.ListWorkers()
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(len(ws), ShouldEqual, 2)
|
So(len(ws), ShouldEqual, 2)
|
||||||
})
|
})
|
||||||
@ -112,15 +105,16 @@ func TestBoltAdapter(t *testing.T) {
|
|||||||
Size: "4GB",
|
Size: "4GB",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
SortMirrorStatus(status)
|
||||||
|
|
||||||
for _, s := range status {
|
for _, s := range status {
|
||||||
_, err := boltDB.UpdateMirrorStatus(s.Worker, s.Name, s)
|
_, err := db.UpdateMirrorStatus(s.Worker, s.Name, s)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Convey("get mirror status", func() {
|
Convey("get mirror status", func() {
|
||||||
m, err := boltDB.GetMirrorStatus(testWorkerIDs[0], status[0].Name)
|
m, err := db.GetMirrorStatus(testWorkerIDs[0], status[0].Name)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
expectedJSON, err := json.Marshal(status[0])
|
expectedJSON, err := json.Marshal(status[0])
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
@ -130,7 +124,7 @@ func TestBoltAdapter(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
Convey("list mirror status", func() {
|
Convey("list mirror status", func() {
|
||||||
ms, err := boltDB.ListMirrorStatus(testWorkerIDs[0])
|
ms, err := db.ListMirrorStatus(testWorkerIDs[0])
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
expectedJSON, err := json.Marshal([]MirrorStatus{status[0]})
|
expectedJSON, err := json.Marshal([]MirrorStatus{status[0]})
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
@ -140,8 +134,10 @@ func TestBoltAdapter(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
Convey("list all mirror status", func() {
|
Convey("list all mirror status", func() {
|
||||||
ms, err := boltDB.ListAllMirrorStatus()
|
ms, err := db.ListAllMirrorStatus()
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
|
SortMirrorStatus(ms)
|
||||||
|
|
||||||
expectedJSON, err := json.Marshal(status)
|
expectedJSON, err := json.Marshal(status)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
actualJSON, err := json.Marshal(ms)
|
actualJSON, err := json.Marshal(ms)
|
||||||
@ -150,17 +146,36 @@ func TestBoltAdapter(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
Convey("flush disabled jobs", func() {
|
Convey("flush disabled jobs", func() {
|
||||||
ms, err := boltDB.ListAllMirrorStatus()
|
ms, err := db.ListAllMirrorStatus()
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(len(ms), ShouldEqual, 3)
|
So(len(ms), ShouldEqual, 3)
|
||||||
err = boltDB.FlushDisabledJobs()
|
err = db.FlushDisabledJobs()
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
ms, err = boltDB.ListAllMirrorStatus()
|
ms, err = db.ListAllMirrorStatus()
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(len(ms), ShouldEqual, 2)
|
So(len(ms), ShouldEqual, 2)
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBoltAdapter(t *testing.T) {
|
||||||
|
Convey("boltAdapter should work", t, func() {
|
||||||
|
tmpDir, err := ioutil.TempDir("", "tunasync")
|
||||||
|
defer os.RemoveAll(tmpDir)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
|
dbType, dbFile := "bolt", filepath.Join(tmpDir, "bolt.db")
|
||||||
|
boltDB, err := makeDBAdapter(dbType, dbFile)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
// close boltDB
|
||||||
|
err := boltDB.Close()
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
}()
|
||||||
|
|
||||||
|
DBAdapterTest(boltDB)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user