163 lines
4.1 KiB
Go
163 lines
4.1 KiB
Go
|
package s3_test
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"io/ioutil"
|
||
|
"strings"
|
||
|
"testing"
|
||
|
"time"
|
||
|
|
||
|
"git.kapelle.org/niklas/s3browser/internal/s3"
|
||
|
"git.kapelle.org/niklas/s3browser/internal/types"
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
func setup(t *testing.T) (s3.S3Service, context.Context, *assert.Assertions) {
|
||
|
service, _ := s3.NewMockS3([]string{"bucket1", "bucket2"})
|
||
|
ctx := context.Background()
|
||
|
assert := assert.New(t)
|
||
|
|
||
|
return service, ctx, assert
|
||
|
}
|
||
|
|
||
|
func TestBuckets(t *testing.T) {
|
||
|
s3, ctx, assert := setup(t)
|
||
|
|
||
|
buckets, err := s3.ListBuckets(ctx)
|
||
|
assert.NoError(err)
|
||
|
|
||
|
assert.Len(buckets, 2)
|
||
|
|
||
|
assert.Contains(buckets, "bucket1")
|
||
|
assert.Contains(buckets, "bucket2")
|
||
|
}
|
||
|
|
||
|
func TestPut(t *testing.T) {
|
||
|
s3, ctx, assert := setup(t)
|
||
|
content := "FileContent"
|
||
|
|
||
|
err := s3.PutObject(ctx, *types.ParseID("bucket1:/file1"), strings.NewReader(content), int64(len(content)))
|
||
|
assert.NoError(err)
|
||
|
}
|
||
|
|
||
|
func TestPutAndGet(t *testing.T) {
|
||
|
s3, ctx, assert := setup(t)
|
||
|
|
||
|
content := "FileContent"
|
||
|
id := *types.ParseID("bucket1:/file1")
|
||
|
|
||
|
err := s3.PutObject(ctx, *types.ParseID("bucket1:/file1"), strings.NewReader(content), int64(len(content)))
|
||
|
assert.NoError(err)
|
||
|
|
||
|
reader, err := s3.GetObject(ctx, id)
|
||
|
assert.NoError(err)
|
||
|
|
||
|
readerContent, err := ioutil.ReadAll(reader)
|
||
|
assert.NoError(err)
|
||
|
|
||
|
assert.Equal(content, string(readerContent))
|
||
|
}
|
||
|
|
||
|
func TestStat(t *testing.T) {
|
||
|
s3, ctx, assert := setup(t)
|
||
|
|
||
|
content := "FileContent"
|
||
|
id := *types.ParseID("bucket1:/file1")
|
||
|
|
||
|
now := time.Now()
|
||
|
|
||
|
err := s3.PutObject(ctx, id, strings.NewReader(content), int64(len(content)))
|
||
|
assert.NoError(err)
|
||
|
|
||
|
obj, err := s3.StatObject(ctx, id)
|
||
|
assert.NoError(err)
|
||
|
|
||
|
assert.Equal(id.String(), obj.ID.String())
|
||
|
assert.Equal(int64(len(content)), obj.Size)
|
||
|
assert.NotEmpty(obj.ETag)
|
||
|
assert.WithinDuration(now, obj.LastModified, time.Second*1)
|
||
|
}
|
||
|
|
||
|
func TestRemove(t *testing.T) {
|
||
|
s3, ctx, assert := setup(t)
|
||
|
|
||
|
content := "FileContent"
|
||
|
id := *types.ParseID("bucket1:/file1")
|
||
|
|
||
|
err := s3.PutObject(ctx, id, strings.NewReader(content), int64(len(content)))
|
||
|
assert.NoError(err)
|
||
|
|
||
|
err = s3.RemoveObject(ctx, id)
|
||
|
assert.NoError(err)
|
||
|
|
||
|
_, err = s3.StatObject(ctx, id)
|
||
|
assert.Error(err)
|
||
|
}
|
||
|
|
||
|
func TestList(t *testing.T) {
|
||
|
s3, ctx, assert := setup(t)
|
||
|
|
||
|
content1 := "FileContent1"
|
||
|
id1 := *types.ParseID("bucket1:/file1")
|
||
|
|
||
|
err := s3.PutObject(ctx, id1, strings.NewReader(content1), int64(len(content1)))
|
||
|
assert.NoError(err)
|
||
|
|
||
|
content2 := "FileContent2"
|
||
|
id2 := *types.ParseID("bucket1:/file2")
|
||
|
|
||
|
err = s3.PutObject(ctx, id2, strings.NewReader(content2), int64(len(content2)))
|
||
|
assert.NoError(err)
|
||
|
|
||
|
listID := types.ParseID("bucket1:/")
|
||
|
|
||
|
objects, err := s3.ListObjects(ctx, *listID)
|
||
|
assert.NoError(err)
|
||
|
|
||
|
assert.Len(objects, 2)
|
||
|
}
|
||
|
|
||
|
func TestListRecursive(t *testing.T) {
|
||
|
s3, ctx, assert := setup(t)
|
||
|
|
||
|
s3.PutObject(ctx, *types.ParseID("bucket1:/file1"), strings.NewReader("content"), int64(len("content")))
|
||
|
s3.PutObject(ctx, *types.ParseID("bucket1:/path1/file1"), strings.NewReader("content"), int64(len("content")))
|
||
|
s3.PutObject(ctx, *types.ParseID("bucket1:/path1/file2"), strings.NewReader("content"), int64(len("content")))
|
||
|
s3.PutObject(ctx, *types.ParseID("bucket1:/path1/path2/file1"), strings.NewReader("content"), int64(len("content")))
|
||
|
s3.PutObject(ctx, *types.ParseID("bucket1:/path3/path4/file1"), strings.NewReader("content"), int64(len("content")))
|
||
|
|
||
|
objects, err := s3.ListObjectsRecursive(ctx, *types.ParseID("bucket1:/path1/"))
|
||
|
assert.NoError(err)
|
||
|
assert.Len(objects, 3)
|
||
|
}
|
||
|
|
||
|
func TestCopy(t *testing.T) {
|
||
|
s3, ctx, assert := setup(t)
|
||
|
|
||
|
id1 := *types.ParseID("bucket1:/file1")
|
||
|
id2 := *types.ParseID("bucket1:/file2")
|
||
|
|
||
|
s3.PutObject(ctx, id1, strings.NewReader("content"), int64(len("content")))
|
||
|
|
||
|
err := s3.CopyObject(ctx, id1, id2)
|
||
|
assert.NoError(err)
|
||
|
|
||
|
obj1, err := s3.StatObject(ctx, id1)
|
||
|
assert.NoError(err)
|
||
|
assert.NotNil(obj1)
|
||
|
|
||
|
obj2, err := s3.StatObject(ctx, id1)
|
||
|
assert.NoError(err)
|
||
|
assert.NotNil(obj2)
|
||
|
|
||
|
assert.Equal(obj1.ETag, obj2.ETag)
|
||
|
assert.Equal(obj1.Size, obj2.Size)
|
||
|
|
||
|
obj2Reader, err := s3.GetObject(ctx, id2)
|
||
|
assert.NoError(err)
|
||
|
|
||
|
obj2Content, err := ioutil.ReadAll(obj2Reader)
|
||
|
assert.NoError(err)
|
||
|
assert.Equal([]byte("content"), obj2Content)
|
||
|
}
|