Prechádzať zdrojové kódy

Rearrange and fix db tests

jiegec 5 rokov pred
rodič
commit
d341c0c99d
1 zmenil súbory, kde vykonal 149 pridanie a 134 odobranie
  1. 149 134
      manager/db_test.go

+ 149 - 134
manager/db_test.go

@@ -5,6 +5,7 @@ import (
 	"io/ioutil"
 	"os"
 	"path/filepath"
+	"sort"
 	"testing"
 	"time"
 
@@ -12,6 +13,153 @@ import (
 	. "github.com/tuna/tunasync/internal"
 )
 
+func SortMirrorStatus(status []MirrorStatus) {
+	sort.Slice(status, func(l, r int) bool {
+		return status[l].Name < status[r].Name
+	})
+}
+
+func DBAdapterTest(db dbAdapter) {
+	var err error
+	testWorkerIDs := []string{"test_worker1", "test_worker2"}
+	Convey("create worker", func() {
+		for _, id := range testWorkerIDs {
+			w := WorkerStatus{
+				ID:           id,
+				Token:        "token_" + id,
+				LastOnline:   time.Now(),
+				LastRegister: time.Now(),
+			}
+			w, err = db.CreateWorker(w)
+			So(err, ShouldBeNil)
+		}
+
+		Convey("get existent worker", func() {
+			_, err := db.GetWorker(testWorkerIDs[0])
+			So(err, ShouldBeNil)
+		})
+
+		Convey("list existent workers", func() {
+			ws, err := db.ListWorkers()
+			So(err, ShouldBeNil)
+			So(len(ws), ShouldEqual, 2)
+		})
+
+		Convey("get non-existent worker", func() {
+			_, err := db.GetWorker("invalid workerID")
+			So(err, ShouldNotBeNil)
+		})
+
+		Convey("delete existent worker", func() {
+			err := db.DeleteWorker(testWorkerIDs[0])
+			So(err, ShouldBeNil)
+			_, err = db.GetWorker(testWorkerIDs[0])
+			So(err, ShouldNotBeNil)
+			ws, err := db.ListWorkers()
+			So(err, ShouldBeNil)
+			So(len(ws), ShouldEqual, 1)
+		})
+
+		Convey("delete non-existent worker", func() {
+			err := db.DeleteWorker("invalid workerID")
+			So(err, ShouldNotBeNil)
+			ws, err := db.ListWorkers()
+			So(err, ShouldBeNil)
+			So(len(ws), ShouldEqual, 2)
+		})
+	})
+
+	Convey("update mirror status", func() {
+		status := []MirrorStatus{
+			MirrorStatus{
+				Name:        "arch-sync1",
+				Worker:      testWorkerIDs[0],
+				IsMaster:    true,
+				Status:      Success,
+				LastUpdate:  time.Now(),
+				LastStarted: time.Now().Add(-time.Minute),
+				LastEnded:   time.Now(),
+				Upstream:    "mirrors.tuna.tsinghua.edu.cn",
+				Size:        "3GB",
+			},
+			MirrorStatus{
+				Name:        "arch-sync2",
+				Worker:      testWorkerIDs[1],
+				IsMaster:    true,
+				Status:      Disabled,
+				LastUpdate:  time.Now().Add(-time.Hour),
+				LastStarted: time.Now().Add(-time.Minute),
+				LastEnded:   time.Now(),
+				Upstream:    "mirrors.tuna.tsinghua.edu.cn",
+				Size:        "4GB",
+			},
+			MirrorStatus{
+				Name:        "arch-sync3",
+				Worker:      testWorkerIDs[1],
+				IsMaster:    true,
+				Status:      Success,
+				LastUpdate:  time.Now().Add(-time.Minute),
+				LastStarted: time.Now().Add(-time.Second),
+				LastEnded:   time.Now(),
+				Upstream:    "mirrors.tuna.tsinghua.edu.cn",
+				Size:        "4GB",
+			},
+		}
+		SortMirrorStatus(status)
+
+		for _, s := range status {
+			_, err := db.UpdateMirrorStatus(s.Worker, s.Name, s)
+			So(err, ShouldBeNil)
+
+		}
+
+		Convey("get mirror status", func() {
+			m, err := db.GetMirrorStatus(testWorkerIDs[0], status[0].Name)
+			So(err, ShouldBeNil)
+			expectedJSON, err := json.Marshal(status[0])
+			So(err, ShouldBeNil)
+			actualJSON, err := json.Marshal(m)
+			So(err, ShouldBeNil)
+			So(string(actualJSON), ShouldEqual, string(expectedJSON))
+		})
+
+		Convey("list mirror status", func() {
+			ms, err := db.ListMirrorStatus(testWorkerIDs[0])
+			So(err, ShouldBeNil)
+			expectedJSON, err := json.Marshal([]MirrorStatus{status[0]})
+			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 := db.ListAllMirrorStatus()
+			So(err, ShouldBeNil)
+			SortMirrorStatus(ms)
+
+			expectedJSON, err := json.Marshal(status)
+			So(err, ShouldBeNil)
+			actualJSON, err := json.Marshal(ms)
+			So(err, ShouldBeNil)
+			So(string(actualJSON), ShouldEqual, string(expectedJSON))
+		})
+
+		Convey("flush disabled jobs", func() {
+			ms, err := db.ListAllMirrorStatus()
+			So(err, ShouldBeNil)
+			So(len(ms), ShouldEqual, 3)
+			err = db.FlushDisabledJobs()
+			So(err, ShouldBeNil)
+			ms, err = db.ListAllMirrorStatus()
+			So(err, ShouldBeNil)
+			So(len(ms), ShouldEqual, 2)
+		})
+
+	})
+	return
+}
+
 func TestBoltAdapter(t *testing.T) {
 	Convey("boltAdapter should work", t, func() {
 		tmpDir, err := ioutil.TempDir("", "tunasync")
@@ -28,139 +176,6 @@ func TestBoltAdapter(t *testing.T) {
 			So(err, ShouldBeNil)
 		}()
 
-		testWorkerIDs := []string{"test_worker1", "test_worker2"}
-		Convey("create worker", func() {
-			for _, id := range testWorkerIDs {
-				w := WorkerStatus{
-					ID:         id,
-					Token:      "token_" + id,
-					LastOnline: time.Now(),
-					LastRegister: time.Now(),
-				}
-				w, err = boltDB.CreateWorker(w)
-				So(err, ShouldBeNil)
-			}
-
-			Convey("get existent worker", func() {
-				_, err := boltDB.GetWorker(testWorkerIDs[0])
-				So(err, ShouldBeNil)
-			})
-
-			Convey("list existent workers", func() {
-				ws, err := boltDB.ListWorkers()
-				So(err, ShouldBeNil)
-				So(len(ws), ShouldEqual, 2)
-			})
-
-			Convey("get non-existent worker", func() {
-				_, err := boltDB.GetWorker("invalid workerID")
-				So(err, ShouldNotBeNil)
-			})
-
-			Convey("delete existent worker", func() {
-				err := boltDB.DeleteWorker(testWorkerIDs[0])
-				So(err, ShouldBeNil)
-				_, err = boltDB.GetWorker(testWorkerIDs[0])
-				So(err, ShouldNotBeNil)
-				ws, err := boltDB.ListWorkers()
-				So(err, ShouldBeNil)
-				So(len(ws), ShouldEqual, 1)
-			})
-
-			Convey("delete non-existent worker", func() {
-				err := boltDB.DeleteWorker("invalid workerID")
-				So(err, ShouldNotBeNil)
-				ws, err := boltDB.ListWorkers()
-				So(err, ShouldBeNil)
-				So(len(ws), ShouldEqual, 2)
-			})
-		})
-
-		Convey("update mirror status", func() {
-			status := []MirrorStatus{
-				MirrorStatus{
-					Name:        "arch-sync1",
-					Worker:      testWorkerIDs[0],
-					IsMaster:    true,
-					Status:      Success,
-					LastUpdate:  time.Now(),
-					LastStarted: time.Now().Add(-time.Minute),
-					LastEnded:   time.Now(),
-					Upstream:    "mirrors.tuna.tsinghua.edu.cn",
-					Size:        "3GB",
-				},
-				MirrorStatus{
-					Name:        "arch-sync2",
-					Worker:      testWorkerIDs[1],
-					IsMaster:    true,
-					Status:      Disabled,
-					LastUpdate:  time.Now().Add(-time.Hour),
-					LastStarted: time.Now().Add(-time.Minute),
-					LastEnded:   time.Now(),
-					Upstream:    "mirrors.tuna.tsinghua.edu.cn",
-					Size:        "4GB",
-				},
-				MirrorStatus{
-					Name:        "arch-sync3",
-					Worker:      testWorkerIDs[1],
-					IsMaster:    true,
-					Status:      Success,
-					LastUpdate:  time.Now().Add(-time.Minute),
-					LastStarted: time.Now().Add(-time.Second),
-					LastEnded:   time.Now(),
-					Upstream:    "mirrors.tuna.tsinghua.edu.cn",
-					Size:        "4GB",
-				},
-			}
-
-			for _, s := range status {
-				_, err := boltDB.UpdateMirrorStatus(s.Worker, s.Name, s)
-				So(err, ShouldBeNil)
-
-			}
-
-			Convey("get mirror status", func() {
-				m, err := boltDB.GetMirrorStatus(testWorkerIDs[0], status[0].Name)
-				So(err, ShouldBeNil)
-				expectedJSON, err := json.Marshal(status[0])
-				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{status[0]})
-				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(status)
-				So(err, ShouldBeNil)
-				actualJSON, err := json.Marshal(ms)
-				So(err, ShouldBeNil)
-				So(string(actualJSON), ShouldEqual, string(expectedJSON))
-			})
-
-			Convey("flush disabled jobs", func() {
-				ms, err := boltDB.ListAllMirrorStatus()
-				So(err, ShouldBeNil)
-				So(len(ms), ShouldEqual, 3)
-				err = boltDB.FlushDisabledJobs()
-				So(err, ShouldBeNil)
-				ms, err = boltDB.ListAllMirrorStatus()
-				So(err, ShouldBeNil)
-				So(len(ms), ShouldEqual, 2)
-			})
-
-		})
-
+		DBAdapterTest(boltDB)
 	})
 }