s3-share/internal/s3/mock.go

81 lines
1.4 KiB
Go
Raw Normal View History

2022-06-01 09:55:33 +00:00
package s3
import (
"bytes"
"context"
"crypto/md5"
"fmt"
"git.kapelle.org/niklas/s3share/internal/types"
)
type mockS3 struct {
objects map[string]mockObject
}
type mockObject struct {
content []byte
contentType string
}
type mockObjectReader struct {
*bytes.Reader
}
func (r mockObjectReader) Close() error {
// NOOP
return nil
}
func NewMockS3() S3 {
return &mockS3{
objects: map[string]mockObject{
"test.txt": {
content: []byte("test.txt"),
contentType: "text/plain",
},
"test.png": {
content: []byte("test.png"),
contentType: "image/png",
},
"dir/test": {
content: []byte("test"),
contentType: "application/octet-stream",
},
},
}
}
func (m *mockS3) GetObject(ctx context.Context, key string) (ObjectReader, error) {
mockObj, exist := m.objects[key]
if !exist {
return nil, fmt.Errorf("Object not found")
}
reader := bytes.NewReader(mockObj.content)
return mockObjectReader{reader}, nil
}
func (m *mockS3) GetObjectMetadata(ctx context.Context, key string) (*types.Metadata, error) {
mockObj, exist := m.objects[key]
if !exist {
return nil, nil
}
return &types.Metadata{
Size: int64(len(mockObj.content)),
ETag: fmt.Sprintf("%x", md5.Sum(mockObj.content)),
ContentType: mockObj.contentType,
}, nil
}
func (m *mockS3) KeyExists(ctx context.Context, key string) (bool, error) {
_, exist := m.objects[key]
return exist, nil
}