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 1 commit
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
241 changes: 122 additions & 119 deletions tests/appender.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,159 +18,162 @@ import (
func TestAppender(t *testing.T, store types.Storager) {
Convey("Given a basic Storager", t, func() {

Convey("When CreateAppend", func() {
path := uuid.NewString()
o, err := store.CreateAppend(path)
f := store.Features()
if f.CreateAppend && f.WriteAppend && f.CommitAppend && f.Write && f.Read && f.Delete && f.Stat {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should only check CreateAppend here and check the needed features when we use them so we can check as many cases as possible.

Convey("When CreateAppend", func() {
path := uuid.NewString()
o, err := store.CreateAppend(path)

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 appendable", func() {
// Append object's mode must be appendable.
So(o.Mode.IsAppend(), ShouldBeTrue)
})
})

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

Convey("The error should be nil", func() {
So(err, ShouldBeNil)
})
defer func() {
err := store.Delete(path)
if err != nil {
t.Error(err)
}
}()

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

Convey("When CreateAppend with an existing object", func() {
path := uuid.NewString()
o, err := store.CreateAppend(path)
size := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB
r := io.LimitReader(randbytes.NewRand(), size)

defer func() {
err := store.Delete(path)
_, err = store.WriteAppend(o, r, size)
if err != nil {
t.Error(err)
t.Fatal(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.CommitAppend(o)
if err != nil {
t.Fatal(err)
}

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

err = store.CommitAppend(o)
if err != nil {
t.Fatal(err)
}
Convey("The second returned error also should be nil", func() {
So(err, ShouldBeNil)
})

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

Convey("The second returned error also should be nil", func() {
So(err, ShouldBeNil)
Convey("The object append offset should be 0", func() {
So(o.MustGetAppendOffset(), ShouldBeZeroValue)
})
})

Convey("The Object Mode should be appendable", func() {
// Append object's mode must be appendable.
So(o.Mode.IsAppend(), ShouldBeTrue)
})
Convey("When Delete", func() {
path := uuid.NewString()
_, err := store.CreateAppend(path)
if err != nil {
t.Error(err)
}

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

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

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

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

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

defer func() {
err := store.Delete(path)
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)
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("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)
}

defer func() {
err := store.Delete(path)
Convey("When CommitAppend", 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))
defer func() {
err := store.Delete(path)
if err != nil {
t.Error(err)
}
}()

_, err = store.WriteAppend(o, bytes.NewReader(content), size)
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)
_, err = store.WriteAppend(o, bytes.NewReader(content), size)
if err != nil {
t.Error(err)
}

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

var buf bytes.Buffer
_, err = store.Read(path, &buf, pairs.WithSize(size*2))
Convey("CommitAppend error should be nil", func() {
So(err, ShouldBeNil)
})

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)))
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