Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tests): Run test cases based on features #980

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
227 changes: 115 additions & 112 deletions tests/appender.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ import (
"go.beyondstorage.io/v5/types"
)

func TestAppender(t *testing.T, store types.Storager) {
Convey("Given a basic Storager", t, func() {

func testAppender(t *testing.T, store types.Storager, f types.StorageFeatures) {
if f.CreateAppend && f.Delete {
Convey("When CreateAppend", func() {
path := uuid.NewString()
o, err := store.CreateAppend(path)
Expand All @@ -39,50 +38,6 @@ func TestAppender(t *testing.T, store types.Storager) {
})
})

Convey("When CreateAppend with an existing object", func() {
path := uuid.NewString()
o, err := store.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 = store.WriteAppend(o, r, size)
if err != nil {
t.Fatal(err)
}

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

o, err = store.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("When Delete", func() {
path := uuid.NewString()
_, err := store.CreateAppend(path)
Expand All @@ -101,76 +56,124 @@ func TestAppender(t *testing.T, store types.Storager) {
})
})

Convey("When WriteAppend", func() {
path := uuid.NewString()
o, err := store.CreateAppend(path)
if err != nil {
t.Error(err)
}

defer func() {
err := store.Delete(path)
if f.WriteAppend {
Convey("When WriteAppend", func() {
path := uuid.NewString()
o, err := store.CreateAppend(path)
if err != nil {
t.Error(err)
}
}()

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

n, err := store.WriteAppend(o, r, size)

Convey("WriteAppend error should be nil", func() {
So(err, ShouldBeNil)
})
Convey("WriteAppend size should be equal to n", func() {
So(n, ShouldEqual, size)
defer func() {
err := store.Delete(path)
if err != nil {
t.Error(err)
}
}()

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

n, err := store.WriteAppend(o, r, size)

Convey("WriteAppend error should be nil", func() {
So(err, ShouldBeNil)
})
Convey("WriteAppend size should be equal to n", func() {
So(n, ShouldEqual, size)
})
})
})

Convey("When CommitAppend", func() {
path := uuid.NewString()
o, err := store.CreateAppend(path)
if err != nil {
t.Error(err)
if f.CommitAppend {
Convey("When CommitAppend", func() {
path := uuid.NewString()
o, err := store.CreateAppend(path)
if err != nil {
t.Error(err)
}

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

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

_, err = store.WriteAppend(o, bytes.NewReader(content), size)
if err != nil {
t.Error(err)
}

_, err = store.WriteAppend(o, bytes.NewReader(content), size)
if err != nil {
t.Error(err)
}

err = store.CommitAppend(o)

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

var buf bytes.Buffer
_, err = store.Read(path, &buf, pairs.WithSize(size*2))

Convey("Read error should be nil", func() {
So(err, ShouldBeNil)
})
Convey("The content should be match", func() {
So(sha256.Sum256(buf.Bytes()), ShouldResemble, sha256.Sum256(bytes.Repeat(content, 2)))
})
})

Convey("When CreateAppend with an existing object", func() {
path := uuid.NewString()
o, err := store.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 = store.WriteAppend(o, r, size)
if err != nil {
t.Fatal(err)
}

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

o, err = store.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)
})
})
}

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

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

_, err = store.WriteAppend(o, bytes.NewReader(content), size)
if err != nil {
t.Error(err)
}

_, err = store.WriteAppend(o, bytes.NewReader(content), size)
if err != nil {
t.Error(err)
}

err = store.CommitAppend(o)

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

var buf bytes.Buffer
_, err = store.Read(path, &buf, pairs.WithSize(size*2))

Convey("Read error should be nil", func() {
So(err, ShouldBeNil)
})
Convey("The content should be match", func() {
So(sha256.Sum256(buf.Bytes()), ShouldResemble, sha256.Sum256(bytes.Repeat(content, 2)))
})
})
})
}
}
}
Loading