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

Commit

Permalink
test: Add GSP-749 test cases (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
JinnyYi authored Sep 24, 2021
1 parent 5dd8afe commit 226fca4
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ format:
vet:
go vet ./...

build: tidy check
build: tidy format check
go build ./...

tidy:
Expand Down
98 changes: 98 additions & 0 deletions storager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"io/ioutil"
"math/rand"
"path/filepath"
"testing"

"github.com/google/uuid"
Expand Down Expand Up @@ -38,6 +39,8 @@ func TestStorager(t *testing.T, store types.Storager) {
})
})

workDir := store.Metadata().WorkDir

Convey("When Read a file", func() {
size := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB
content, err := ioutil.ReadAll(io.LimitReader(randbytes.NewRand(), size))
Expand Down Expand Up @@ -433,5 +436,100 @@ func TestStorager(t *testing.T, store types.Storager) {
So(osize, ShouldEqual, size)
})
})

Convey("When testing GSP-749 unify path behavior", func() {
Convey("When using absolute path", func() {
size := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB
content, err := ioutil.ReadAll(io.LimitReader(randbytes.NewRand(), size))
if err != nil {
t.Error(err)
}

path := uuid.New().String()
absPath := filepath.Join(workDir, path)
_, err = store.Write(absPath, bytes.NewReader(content), size)
if err != nil {
t.Error(err)
}
defer func() {
err := store.Delete(absPath)
if err != nil {
t.Error(err)
}
}()

Convey("Stat should get Object without error", func() {
o, err := store.Stat(absPath)

Convey("The error should be nil", func() {
So(err, ShouldBeNil)
So(o, ShouldNotBeNil)
So(o.Path, ShouldEqual, filepath.ToSlash(absPath))
})
})

Convey("Read should get Object content without error", func() {
var buf bytes.Buffer
n, err := store.Read(absPath, &buf)

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

Convey("The content should be match", func() {
So(buf, ShouldNotBeNil)

So(n, ShouldEqual, size)
So(sha256.Sum256(buf.Bytes()), ShouldResemble, sha256.Sum256(content))
})
})
})

Convey("When using backslash in path", func() {
size := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB
content, err := ioutil.ReadAll(io.LimitReader(randbytes.NewRand(), size))
if err != nil {
t.Error(err)
}

path := uuid.New().String() + "\\" + uuid.New().String()
_, err = store.Write(path, bytes.NewReader(content), size)
if err != nil {
t.Error(err)
}
defer func() {
err := store.Delete(path)
if err != nil {
t.Error(err)
}
}()

Convey("Stat should get Object without error", func() {
o, err := store.Stat(path)

Convey("The error should be nil", func() {
So(err, ShouldBeNil)
So(o, ShouldNotBeNil)
So(o.Path, ShouldEqual, filepath.ToSlash(path))
})
})

Convey("Read should get Object content without error", func() {
var buf bytes.Buffer
n, err := store.Read(path, &buf)

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

Convey("The content should be match", func() {
So(buf, ShouldNotBeNil)

So(n, ShouldEqual, size)
So(sha256.Sum256(buf.Bytes()), ShouldResemble, sha256.Sum256(content))
})
})
})
})
})
}

0 comments on commit 226fca4

Please sign in to comment.