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

feat: Set up integration tests #21

Merged
merged 23 commits into from
Aug 31, 2021
Merged
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ require (
github.com/jcmturner/goidentity v6.0.1+incompatible // indirect
github.com/jcmturner/gokrb5 v8.4.2+incompatible // indirect
github.com/jcmturner/rpc v1.1.1 // indirect
github.com/stretchr/testify v1.7.0 // indirect
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykE
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
95 changes: 95 additions & 0 deletions readdir_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package hdfs

import (
"fmt"
"math/rand"
"path"
"testing"

"github.com/beyondstorage/go-endpoint"
ps "github.com/beyondstorage/go-storage/v4/pairs"
"github.com/beyondstorage/go-storage/v4/types"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)

func TestHdfsDirerWithFiles(t *testing.T) {
for i := 0; i < 100; i++ {
numbers := 1000 + rand.Intn(1000)

t.Run(fmt.Sprintf("list %d files", numbers), func(t *testing.T) {
testHdfsReaddir(t, numbers)
})
}
}

func testHdfsReaddir(t *testing.T, numbers int) {
tmpDir := t.TempDir()
host := "127.0.0.1"
port := 9000

s, err := newStorager(
ps.WithEndpoint(endpoint.NewTCP(host, port).String()),
ps.WithWorkDir(tmpDir),
)
if err != nil {
t.Errorf("new storager: %v", err)
}

err = s.hdfs.MkdirAll(tmpDir, 0666)
if err != nil {
t.Error(err)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto


for i := 0; i < numbers; i++ {

filename := uuid.New().String()

f, err := s.hdfs.Create(path.Join(s.workDir, filename))
if err != nil {
t.Error(err)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto (or assert.NoError)

Copy link
Contributor

Choose a reason for hiding this comment

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

Similarly for code below


defer func() {
closeErr := f.Close()
if err == nil {
err = closeErr
}
}()

if err != nil {
t.Error(err)
}
}

expected := make(map[string]string)
fi, err := s.hdfs.ReadDir(tmpDir)
if err != nil {
t.Error(err)
}
for _, v := range fi {
expected[v.Name()] = tmpDir
}

actual := make(map[string]string)
it, err := s.List(s.workDir)
if err != nil {
t.Error(err)
}

for {
o, err := it.Next()
if err == types.IterateDone {
break
}
_, exist := actual[o.Path]
if exist {
t.Errorf("file %s exists already", o.Path)
return
}

actual[o.Path] = o.ID
}
assert.Equal(t, expected, actual)

}
2 changes: 1 addition & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (s *Storage) getAbsPath(path string) string {
if filepath.IsAbs(path) {
return path
}
return s.workDir + path
return filepath.Join(s.workDir, path)
}

func (s *Storage) formatError(op string, err error, path ...string) error {
Expand Down
74 changes: 74 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package hdfs

import (
"errors"
"os"
"testing"

"github.com/beyondstorage/go-endpoint"
"github.com/beyondstorage/go-storage/v4/pairs"
"github.com/beyondstorage/go-storage/v4/services"
"github.com/stretchr/testify/assert"
)

func TestNewClient(t *testing.T) {
host := "127.0.0.1"
port := 9000
c, err := NewStorager(
pairs.WithEndpoint(endpoint.NewTCP(host, port).String()),
)
assert.NotNil(t, c)
assert.NoError(t, err)
}

func TestFormatOsError(t *testing.T) {
testErr := errors.New("test error")
tests := []struct {
name string
input error
expected error
}{
{
"not found",
os.ErrNotExist,
services.ErrObjectNotExist,
},
{
"not supported error",
testErr,
services.ErrUnexpected,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := formatError(tt.input)
assert.True(t, errors.Is(err, tt.expected))
})
}
}

func TestGetAbsPath(t *testing.T) {
cases := []struct {
name string
base string
path string
expectedPath string
}{
{"direct path", "", "abc", "abc"},
{"under direct path", "", "root/abc", "root/abc"},
{"under direct path", "", "root/abc/", "root/abc"},
{"under root", "/", "abc", "/abc"},
{"under exist dir", "/root", "abc", "/root/abc"},
{"under new dir", "/root", "abc/", "/root/abc"},
}

for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
client := Storage{workDir: tt.base}

getPath := client.getAbsPath(tt.path)
assert.Equal(t, tt.expectedPath, getPath)
})
}
}