forked from tuna/tunasync
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(manager): implement db.go and tests
- Loading branch information
1 parent
00eddc3
commit a11fbe2
Showing
3 changed files
with
237 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package manager | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
. "github.com/tuna/tunasync/internal" | ||
) | ||
|
||
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) | ||
}() | ||
|
||
testWorkerIDs := []string{"test_worker1", "test_worker2"} | ||
Convey("create worker", func() { | ||
for _, id := range testWorkerIDs { | ||
w := worker{ | ||
ID: id, | ||
Token: "token_" + id, | ||
LastOnline: time.Now(), | ||
} | ||
w, err = boltDB.CreateWorker(w) | ||
So(err, ShouldBeNil) | ||
} | ||
|
||
Convey("get exists worker", func() { | ||
_, err := boltDB.GetWorker(testWorkerIDs[0]) | ||
So(err, ShouldBeNil) | ||
}) | ||
|
||
Convey("list exist worker", func() { | ||
ws, err := boltDB.ListWorkers() | ||
So(err, ShouldBeNil) | ||
So(len(ws), ShouldEqual, 2) | ||
}) | ||
|
||
Convey("get inexist worker", func() { | ||
_, err := boltDB.GetWorker("invalid workerID") | ||
So(err, ShouldNotBeNil) | ||
}) | ||
}) | ||
|
||
Convey("update mirror status", func() { | ||
status1 := mirrorStatus{ | ||
Name: "arch-sync1", | ||
Worker: testWorkerIDs[0], | ||
IsMaster: true, | ||
Status: Success, | ||
LastUpdate: time.Now(), | ||
Upstream: "mirrors.tuna.tsinghua.edu.cn", | ||
Size: "3GB", | ||
} | ||
status2 := mirrorStatus{ | ||
Name: "arch-sync2", | ||
Worker: testWorkerIDs[1], | ||
IsMaster: true, | ||
Status: Success, | ||
LastUpdate: time.Now(), | ||
Upstream: "mirrors.tuna.tsinghua.edu.cn", | ||
Size: "4GB", | ||
} | ||
|
||
_, err := boltDB.UpdateMirrorStatus(status1.Worker, status1.Name, status1) | ||
_, err = boltDB.UpdateMirrorStatus(status2.Worker, status2.Name, status2) | ||
So(err, ShouldBeNil) | ||
|
||
Convey("get mirror status", func() { | ||
m, err := boltDB.GetMirrorStatus(testWorkerIDs[0], status1.Name) | ||
So(err, ShouldBeNil) | ||
expectedJSON, err := json.Marshal(status1) | ||
So(err, ShouldBeNil) | ||
actualJSON, err := json.Marshal(m) | ||
So(err, ShouldBeNil) | ||
So(string(actualJSON), ShouldEqual, string(expectedJSON)) | ||
}) | ||
|
||
Convey("list mirror status", func() { | ||
ms, err := boltDB.ListMirrorStatus(testWorkerIDs[0]) | ||
So(err, ShouldBeNil) | ||
expectedJSON, err := json.Marshal([]mirrorStatus{status1}) | ||
So(err, ShouldBeNil) | ||
actualJSON, err := json.Marshal(ms) | ||
So(err, ShouldBeNil) | ||
So(string(actualJSON), ShouldEqual, string(expectedJSON)) | ||
}) | ||
|
||
Convey("list all mirror status", func() { | ||
ms, err := boltDB.ListAllMirrorStatus() | ||
So(err, ShouldBeNil) | ||
expectedJSON, err := json.Marshal([]mirrorStatus{status1, status2}) | ||
So(err, ShouldBeNil) | ||
actualJSON, err := json.Marshal(ms) | ||
So(err, ShouldBeNil) | ||
So(string(actualJSON), ShouldEqual, string(expectedJSON)) | ||
}) | ||
|
||
}) | ||
|
||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters