From a5d6fd8a80a884a57acb471a226cf0350e690727 Mon Sep 17 00:00:00 2001 From: abyss-w <75366604+abyss-w@users.noreply.github.com> Date: Tue, 27 Jul 2021 14:17:56 +0800 Subject: [PATCH] test: Implement GSP-86 Add linker integration tests (#40) * test: Implement GSP-86 Add linker integration tests * test:Add some cases to linker integration tests * test: Modify some cases in linker integration test * fix: Resolve conflicts * test: Add some cases in linker integration test * revert: Removing the parts we haven't agreed on yet * fix: Delete Read should get path object data without error * test: Modified a few minor areas --- .gitignore | 6 +- linker.go | 204 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 linker.go diff --git a/.gitignore b/.gitignore index 2a61605..06d7002 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ -*.yaml \ No newline at end of file +*.yaml + +# Jetbrain IDE +.idea +*.iml \ No newline at end of file diff --git a/linker.go b/linker.go new file mode 100644 index 0000000..3f3fc52 --- /dev/null +++ b/linker.go @@ -0,0 +1,204 @@ +package tests + +import ( + "io" + "math/rand" + "testing" + + "github.com/google/uuid" + . "github.com/smartystreets/goconvey/convey" + + "github.com/beyondstorage/go-storage/v4/pkg/randbytes" + "github.com/beyondstorage/go-storage/v4/types" +) + +func TestLinker(t *testing.T, store types.Storager) { + Convey("Given a basic Storager", t, func() { + l, ok := store.(types.Linker) + So(ok, ShouldBeTrue) + + Convey("When create a link object", func() { + size := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB + r := io.LimitReader(randbytes.NewRand(), size) + target := uuid.New().String() + + _, err := store.Write(target, r, size) + if err != nil { + t.Fatal(err) + } + + defer func() { + err = store.Delete(target) + if err != nil { + t.Error(err) + } + }() + + path := uuid.New().String() + o, err := l.CreateLink(path, target) + + defer func() { + err = store.Delete(path) + if err != nil { + t.Error(err) + } + }() + + Convey("The error should be nil", func() { + So(err, ShouldBeNil) + }) + + Convey("The object mode should be link", func() { + // Link object's mode must be link. + So(o.Mode.IsLink(), ShouldBeTrue) + }) + + Convey("The linkTarget of the object must be the same as the target", func() { + // The linkTarget must be the same as the target. + linkTarget, ok := o.GetLinkTarget() + + So(ok, ShouldBeTrue) + So(linkTarget, ShouldEqual, target) + }) + + Convey("Stat should get path object without error", func() { + obj, err := store.Stat(path) + + Convey("The error should be nil", func() { + So(err, ShouldBeNil) + }) + + Convey("The object mode should be link", func() { + // Link object's mode must be link. + So(obj.Mode.IsLink(), ShouldBeTrue) + }) + + Convey("The linkTarget of the object must be the same as the target", func() { + // The linkTarget must be the same as the target. + linkTarget, ok := obj.GetLinkTarget() + + So(ok, ShouldBeTrue) + So(linkTarget, ShouldEqual, target) + }) + }) + }) + + Convey("When create a link object from a not existing target", func() { + target := uuid.New().String() + + path := uuid.New().String() + o, err := l.CreateLink(path, target) + + defer func() { + err = store.Delete(path) + if err != nil { + t.Error(err) + } + }() + + Convey("The error should be nil", func() { + So(err, ShouldBeNil) + }) + + Convey("The object mode should be link", func() { + // Link object's mode must be link. + So(o.Mode.IsLink(), ShouldBeTrue) + }) + + Convey("The linkTarget of the object must be the same as the target", func() { + linkTarget, ok := o.GetLinkTarget() + + So(ok, ShouldBeTrue) + So(linkTarget, ShouldEqual, target) + }) + + Convey("Stat should get path object without error", func() { + obj, err := store.Stat(path) + + Convey("The error should be nil", func() { + So(err, ShouldBeNil) + }) + + Convey("The object mode should be link", func() { + // Link object's mode must be link. + So(obj.Mode.IsLink(), ShouldBeTrue) + }) + + Convey("The linkTarget of the object must be the same as the target", func() { + // The linkTarget must be the same as the target. + linkTarget, ok := obj.GetLinkTarget() + + So(ok, ShouldBeTrue) + So(linkTarget, ShouldEqual, target) + }) + }) + }) + + Convey("When CreateLink to an existing path", func() { + firstSize := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB + firstR := io.LimitReader(randbytes.NewRand(), firstSize) + firstTarget := uuid.New().String() + + _, err := store.Write(firstTarget, firstR, firstSize) + if err != nil { + t.Fatal(err) + } + + defer func() { + err = store.Delete(firstTarget) + if err != nil { + t.Error(err) + } + }() + + path := uuid.New().String() + o, err := l.CreateLink(path, firstTarget) + + defer func() { + err = store.Delete(path) + if err != nil { + t.Error(err) + } + }() + + Convey("The first returned error should be nil", func() { + So(err, ShouldBeNil) + }) + + secondSize := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB + secondR := io.LimitReader(randbytes.NewRand(), secondSize) + secondTarget := uuid.New().String() + + _, err = store.Write(secondTarget, secondR, secondSize) + if err != nil { + t.Fatal(err) + } + + defer func() { + err = store.Delete(secondTarget) + if err != nil { + t.Error(err) + } + }() + + o, err = l.CreateLink(path, secondTarget) + + Convey("The second returned error should also be nil", func() { + So(err, ShouldBeNil) + }) + + Convey("The object mode should be link", func() { + // Link object's mode must be link. + So(o.Mode.IsLink(), ShouldBeTrue) + }) + + Convey("The linkTarget of the object must be the same as the secondTarget", func() { + // The linkTarget must be the same as the secondTarget. + linkTarget, ok := o.GetLinkTarget() + + So(ok, ShouldBeTrue) + So(linkTarget, ShouldEqual, secondTarget) + }) + }) + }) +}