From 2ca778276fd39b9bffdeee6c34ab9f06970d29cb Mon Sep 17 00:00:00 2001 From: Sydonian <794346190@qq.com> Date: Tue, 9 May 2023 11:48:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=B0=83=E8=AF=95=E5=8F=91?= =?UTF-8?q?=E7=8E=B0=E7=9A=84Bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/cmdline/bucket.go | 2 +- internal/cmdline/commandline.go | 61 +++++++++++++++++++------------- internal/cmdline/object.go | 2 +- internal/services/object_test.go | 54 ++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 27 deletions(-) create mode 100644 internal/services/object_test.go diff --git a/internal/cmdline/bucket.go b/internal/cmdline/bucket.go index 94f80e9..68397b4 100644 --- a/internal/cmdline/bucket.go +++ b/internal/cmdline/bucket.go @@ -7,7 +7,7 @@ import ( "gitlink.org.cn/cloudream/client/internal/services" ) -func (c *Commandline) GetUserBuckets() error { +func (c *Commandline) ListUserBuckets() error { userID := 0 buckets, err := services.BucketSvc(c.svc).GetUserBuckets(userID) diff --git a/internal/cmdline/commandline.go b/internal/cmdline/commandline.go index e73703e..96cb178 100644 --- a/internal/cmdline/commandline.go +++ b/internal/cmdline/commandline.go @@ -60,41 +60,32 @@ func (c *Commandline) DispatchCommand(cmd string, args []string) { os.Exit(1) } - case "move": - objectID, err := strconv.Atoi(args[0]) - if err != nil { - fmt.Printf("invalid object id %s, err: %s", args[0], err.Error()) - os.Exit(1) - } - stgID, err := strconv.Atoi(args[1]) - if err != nil { - fmt.Printf("invalid storage id %s, err: %s", args[1], err.Error()) - os.Exit(1) - } - - if err := c.Move(objectID, stgID); err != nil { - fmt.Printf("move failed, err: %s", err.Error()) + case "storage": + if len(args) == 0 { + fmt.Printf("need more arg") os.Exit(1) } + cmd := args[0] - case "ls": - if len(args) == 0 { - if err := c.GetUserBuckets(); err != nil { - fmt.Printf("get user buckets failed, err: %s", err.Error()) + switch cmd { + case "move": + objectID, err := strconv.Atoi(args[1]) + if err != nil { + fmt.Printf("invalid object id %s, err: %s", args[1], err.Error()) os.Exit(1) } - } else { - bucketID, err := strconv.Atoi(args[0]) + stgID, err := strconv.Atoi(args[2]) if err != nil { - fmt.Printf("invalid bucket id %s, err: %s", args[1], err.Error()) + fmt.Printf("invalid storage id %s, err: %s", args[2], err.Error()) os.Exit(1) } - if err := c.GetBucketObjects(bucketID); err != nil { - fmt.Printf("get bucket objects failed, err: %s", err.Error()) + if err := c.Move(objectID, stgID); err != nil { + fmt.Printf("move failed, err: %s", err.Error()) os.Exit(1) } } + case "bucket": if len(args) == 0 { fmt.Printf("need more arg") @@ -102,12 +93,20 @@ func (c *Commandline) DispatchCommand(cmd string, args []string) { } cmd := args[0] - if cmd == "new" { + switch cmd { + case "ls": + if err := c.ListUserBuckets(); err != nil { + fmt.Printf("get user buckets failed, err: %s", err.Error()) + os.Exit(1) + } + + case "new": if err := c.CreateBucket(args[1]); err != nil { fmt.Printf("create bucket failed, err: %s", err.Error()) os.Exit(1) } - } else if cmd == "delete" { + + case "delete": bucketID, err := strconv.Atoi(args[1]) if err != nil { fmt.Printf("invalid bucket id %s, err: %s", args[1], err.Error()) @@ -127,6 +126,18 @@ func (c *Commandline) DispatchCommand(cmd string, args []string) { } switch args[0] { + case "ls": + bucketID, err := strconv.Atoi(args[1]) + if err != nil { + fmt.Printf("invalid bucket id %s, err: %s", args[1], err.Error()) + os.Exit(1) + } + + if err := c.ListBucketObjects(bucketID); err != nil { + fmt.Printf("get bucket objects failed, err: %s", err.Error()) + os.Exit(1) + } + case "update": objectID, err := strconv.Atoi(args[1]) if err != nil { diff --git a/internal/cmdline/object.go b/internal/cmdline/object.go index 133eb59..af151e0 100644 --- a/internal/cmdline/object.go +++ b/internal/cmdline/object.go @@ -10,7 +10,7 @@ import ( "gitlink.org.cn/cloudream/client/internal/services" ) -func (c *Commandline) GetBucketObjects(bucketID int) error { +func (c *Commandline) ListBucketObjects(bucketID int) error { userID := 0 objects, err := services.BucketSvc(c.svc).GetBucketObjects(userID, bucketID) diff --git a/internal/services/object_test.go b/internal/services/object_test.go new file mode 100644 index 0000000..2182721 --- /dev/null +++ b/internal/services/object_test.go @@ -0,0 +1,54 @@ +package services + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" + coormsg "gitlink.org.cn/cloudream/rabbitmq/message/coordinator" +) + +func Test_chooseUpdateRepObjectNode(t *testing.T) { + testcases := []struct { + title string + nodes []coormsg.PreUpdateRepObjectRespNode + wantNodeID int + }{ + { + title: "选择同地域,包含旧数据的节点", + nodes: []coormsg.PreUpdateRepObjectRespNode{ + coormsg.NewPreUpdateRepObjectRespNode(0, "", "", true, false), + coormsg.NewPreUpdateRepObjectRespNode(1, "", "", false, false), + coormsg.NewPreUpdateRepObjectRespNode(2, "", "", false, true), + coormsg.NewPreUpdateRepObjectRespNode(3, "", "", true, true), + }, + wantNodeID: 3, + }, + + { + title: "选择包含旧数据的节点", + nodes: []coormsg.PreUpdateRepObjectRespNode{ + coormsg.NewPreUpdateRepObjectRespNode(0, "", "", true, false), + coormsg.NewPreUpdateRepObjectRespNode(1, "", "", false, false), + coormsg.NewPreUpdateRepObjectRespNode(2, "", "", false, true), + }, + wantNodeID: 2, + }, + + { + title: "选择包含同地域的节点", + nodes: []coormsg.PreUpdateRepObjectRespNode{ + coormsg.NewPreUpdateRepObjectRespNode(0, "", "", true, false), + coormsg.NewPreUpdateRepObjectRespNode(1, "", "", false, false), + }, + wantNodeID: 0, + }, + } + + var svc ObjectService + for _, test := range testcases { + Convey(test.title, t, func() { + chooseNode := svc.chooseUpdateRepObjectNode(test.nodes) + So(chooseNode.ID, ShouldEqual, test.wantNodeID) + }) + } +}