Skip to content
This repository has been archived by the owner on Oct 14, 2021. It is now read-only.

Commit

Permalink
Implement GSP-134 Write Behavior Consistency (#32)
Browse files Browse the repository at this point in the history
* Implement GSP-134 Write Behavior Consistency

* Update test cases

* Update test cases
  • Loading branch information
JinnyYi authored Jul 12, 2021
1 parent e08a973 commit 876caec
Show file tree
Hide file tree
Showing 6 changed files with 291 additions and 22 deletions.
53 changes: 50 additions & 3 deletions appender.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,54 @@ func TestAppender(t *testing.T, store types.Storager) {
})
})

Convey("When CreateAppend with an existing object", func() {
path := uuid.NewString()
o, err := ap.CreateAppend(path)

defer func() {
err := store.Delete(path)
if err != nil {
t.Error(err)
}
}()

Convey("The first returned error should be nil", func() {
So(err, ShouldBeNil)
})

size := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB
r := io.LimitReader(randbytes.NewRand(), size)

_, err = ap.WriteAppend(o, r, size)
if err != nil {
t.Fatal(err)
}

err = ap.CommitAppend(o)
if err != nil {
t.Fatal(err)
}

o, err = ap.CreateAppend(path)

Convey("The second returned error also should be nil", func() {
So(err, ShouldBeNil)
})

Convey("The Object Mode should be appendable", func() {
// Append object's mode must be appendable.
So(o.Mode.IsAppend(), ShouldBeTrue)
})

Convey("The object append offset should be 0", func() {
So(o.MustGetAppendOffset(), ShouldBeZeroValue)
})

Convey("The object size should be 0", func() {
So(o.MustGetContentLength(), ShouldBeZeroValue)
})
})

Convey("When Delete", func() {
path := uuid.NewString()
_, err := ap.CreateAppend(path)
Expand All @@ -50,12 +98,12 @@ func TestAppender(t *testing.T, store types.Storager) {
}

err = store.Delete(path)
Convey("The first returning error should be nil", func() {
Convey("The first returned error should be nil", func() {
So(err, ShouldBeNil)
})

err = store.Delete(path)
Convey("The second returning error also should be nil", func() {
Convey("The second returned error also should be nil", func() {
So(err, ShouldBeNil)
})
})
Expand Down Expand Up @@ -128,5 +176,4 @@ func TestAppender(t *testing.T, store types.Storager) {
})
})
})

}
95 changes: 95 additions & 0 deletions copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tests
import (
"bytes"
"crypto/md5"
"errors"
"io"
"io/ioutil"
"math/rand"
Expand All @@ -11,7 +12,9 @@ import (
"github.com/google/uuid"
. "github.com/smartystreets/goconvey/convey"

"github.com/beyondstorage/go-storage/v4/pairs"
"github.com/beyondstorage/go-storage/v4/pkg/randbytes"
"github.com/beyondstorage/go-storage/v4/services"
"github.com/beyondstorage/go-storage/v4/types"
)

Expand Down Expand Up @@ -66,5 +69,97 @@ func TestCopier(t *testing.T, store types.Storager) {
})
})
})

Convey("When Copy to an existing file", func() {
srcSize := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB
content, _ := ioutil.ReadAll(io.LimitReader(randbytes.NewRand(), srcSize))
src := uuid.New().String()

_, err := store.Write(src, bytes.NewReader(content), srcSize)
if err != nil {
t.Fatal(err)
}

defer func() {
err = store.Delete(src)
if err != nil {
t.Error(err)
}
}()

dstSize := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB
r := io.LimitReader(randbytes.NewRand(), dstSize)
dst := uuid.New().String()

_, err = store.Write(dst, r, dstSize)
if err != nil {
t.Fatal(err)
}

defer func() {
err = store.Delete(dst)
if err != nil {
t.Error(err)
}
}()

err = c.Copy(src, dst)
Convey("The error should be nil", func() {
So(err, ShouldBeNil)
})

Convey("Read should get dst object data without error", func() {
var buf bytes.Buffer
n, err := store.Read(dst, &buf)

Convey("The error should be nil", func() {
So(err, ShouldBeNil)
})

Convey("The content should be match", func() {
So(buf, ShouldNotBeNil)
So(n, ShouldEqual, srcSize)
So(md5.Sum(buf.Bytes()), ShouldResemble, md5.Sum(content))
})
})
})

if d, ok := store.(types.Direr); ok {
Convey("When Copy to an existing dir", func() {
srcSize := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB
r := io.LimitReader(randbytes.NewRand(), srcSize)
src := uuid.New().String()

_, err := store.Write(src, r, srcSize)
if err != nil {
t.Fatal(err)
}

defer func() {
err = store.Delete(src)
if err != nil {
t.Error(err)
}
}()

dst := uuid.New().String()
_, err = d.CreateDir(dst)
if err != nil {
t.Fatal(err)
}

defer func() {
err = store.Delete(dst, pairs.WithObjectMode(types.ModeDir))
if err != nil {
t.Error(err)
}
}()

err = c.Copy(src, dst)
Convey("The error should be ErrObjectModeInvalid", func() {
So(errors.Is(err, services.ErrObjectModeInvalid), ShouldBeTrue)
})
})
}
})
}
8 changes: 4 additions & 4 deletions direr.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ func TestDirer(t *testing.T, store types.Storager) {
}
}()

Convey("The first returning error should be nil", func() {
Convey("The first returned error should be nil", func() {
So(err, ShouldBeNil)
})

o, err = d.CreateDir(path)
Convey("The second returning error also should be nil", func() {
Convey("The second returned error also should be nil", func() {
So(err, ShouldBeNil)
})

Expand Down Expand Up @@ -104,12 +104,12 @@ func TestDirer(t *testing.T, store types.Storager) {
}

err = store.Delete(path, pairs.WithObjectMode(types.ModeDir))
Convey("The first returning error should be nil", func() {
Convey("The first returned error should be nil", func() {
So(err, ShouldBeNil)
})

err = store.Delete(path, pairs.WithObjectMode(types.ModeDir))
Convey("The second returning error also should be nil", func() {
Convey("The second returned error also should be nil", func() {
So(err, ShouldBeNil)
})
})
Expand Down
102 changes: 102 additions & 0 deletions mover.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/google/uuid"
. "github.com/smartystreets/goconvey/convey"

"github.com/beyondstorage/go-storage/v4/pairs"
"github.com/beyondstorage/go-storage/v4/pkg/randbytes"
"github.com/beyondstorage/go-storage/v4/services"
"github.com/beyondstorage/go-storage/v4/types"
Expand Down Expand Up @@ -76,5 +77,106 @@ func TestMover(t *testing.T, store types.Storager) {
})
})
})

Convey("When Move to an existing file", func() {
srcSize := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB
content, _ := ioutil.ReadAll(io.LimitReader(randbytes.NewRand(), srcSize))
src := uuid.New().String()

_, err := store.Write(src, bytes.NewReader(content), srcSize)
if err != nil {
t.Fatal(err)
}

defer func() {
err = store.Delete(src)
if err != nil {
t.Error(err)
}
}()

dstSize := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB
r := io.LimitReader(randbytes.NewRand(), dstSize)
dst := uuid.New().String()

_, err = store.Write(dst, r, dstSize)
if err != nil {
t.Fatal(err)
}

defer func() {
err = store.Delete(dst)
if err != nil {
t.Error(err)
}
}()

err = m.Move(src, dst)
Convey("The error should be nil", func() {
So(err, ShouldBeNil)
})

Convey("Stat should get src object not exist", func() {
_, err := store.Stat(src)

Convey("The error should be ErrObjectNotExist", func() {
So(errors.Is(err, services.ErrObjectNotExist), ShouldBeTrue)
})
})

Convey("Read should get dst object data without error", func() {
var buf bytes.Buffer
n, err := store.Read(dst, &buf)

Convey("The error should be nil", func() {
So(err, ShouldBeNil)
})

Convey("The content should be match", func() {
So(buf, ShouldNotBeNil)
So(n, ShouldEqual, srcSize)
So(md5.Sum(buf.Bytes()), ShouldResemble, md5.Sum(content))
})
})
})

if d, ok := store.(types.Direr); ok {
Convey("When Move to an existing dir", func() {

srcSize := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB
r := io.LimitReader(randbytes.NewRand(), srcSize)
src := uuid.New().String()

_, err := store.Write(src, r, srcSize)
if err != nil {
t.Fatal(err)
}

defer func() {
err = store.Delete(src)
if err != nil {
t.Error(err)
}
}()

dst := uuid.New().String()
_, err = d.CreateDir(dst)
if err != nil {
t.Fatal(err)
}

defer func() {
err = store.Delete(dst, pairs.WithObjectMode(types.ModeDir))
if err != nil {
t.Error(err)
}
}()

err = m.Move(src, dst)
Convey("The error should be ErrObjectModeInvalid", func() {
So(errors.Is(err, services.ErrObjectModeInvalid), ShouldBeTrue)
})
})
}
})
}
25 changes: 19 additions & 6 deletions multiparter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,30 @@ func TestMultiparter(t *testing.T, store types.Storager) {
path := uuid.New().String()
o, err := m.CreateMultipart(path)

defer func() {
err := store.Delete(path, pairs.WithMultipartID(o.MustGetMultipartID()))
Convey("The first returned error should be nil", func() {
So(err, ShouldBeNil)
})

defer func(multipartID string) {
err := store.Delete(path, pairs.WithMultipartID(multipartID))
if err != nil {
t.Error(err)
}
}()
}(o.MustGetMultipartID())

Convey("The error should be nil", func() {
o, err = m.CreateMultipart(path)

Convey("The second returned error also should be nil", func() {
So(err, ShouldBeNil)
})

defer func() {
err := store.Delete(path, pairs.WithMultipartID(o.MustGetMultipartID()))
if err != nil {
t.Error(err)
}
}()

Convey("The Object Mode should be part", func() {
// Multipart object's mode must be Part.
So(o.Mode.IsPart(), ShouldBeTrue)
Expand All @@ -55,12 +68,12 @@ func TestMultiparter(t *testing.T, store types.Storager) {
}

err = store.Delete(path, pairs.WithMultipartID(o.MustGetMultipartID()))
Convey("The first returning error should be nil", func() {
Convey("The first returned error should be nil", func() {
So(err, ShouldBeNil)
})

err = store.Delete(path, pairs.WithMultipartID(o.MustGetMultipartID()))
Convey("The second returning error also should be nil", func() {
Convey("The second returned error also should be nil", func() {
So(err, ShouldBeNil)
})
})
Expand Down
Loading

0 comments on commit 876caec

Please sign in to comment.