edge/pkg/storage/testsuite/blob_store_ops.go

130 lines
2.6 KiB
Go

package testsuite
import (
"bytes"
"context"
"io"
"testing"
"forge.cadoles.com/arcad/edge/pkg/storage"
"github.com/pkg/errors"
)
type blobStoreTestCase struct {
Name string
Run func(ctx context.Context, store storage.BlobStore) error
}
var blobStoreTestCases = []blobStoreTestCase{
{
Name: "Open new bucket",
Run: func(ctx context.Context, store storage.BlobStore) error {
bucket, err := store.OpenBucket(ctx, "open-new-bucket")
if err != nil {
return errors.WithStack(err)
}
if bucket == nil {
return errors.New("bucket should not be nil")
}
size, err := bucket.Size(ctx)
if err != nil {
return errors.WithStack(err)
}
if e, g := int64(0), size; e != g {
return errors.Errorf("bucket size: expected '%v', got '%v'", e, g)
}
blobs, err := bucket.List(ctx)
if err != nil {
return errors.WithStack(err)
}
if e, g := 0, len(blobs); e != g {
return errors.Errorf("len(blobs): expected '%v', got '%v'", e, g)
}
if err := bucket.Close(); err != nil {
return errors.WithStack(err)
}
return nil
},
},
{
Name: "Create blob",
Run: func(ctx context.Context, store storage.BlobStore) error {
bucket, err := store.OpenBucket(ctx, "create-blob")
if err != nil {
return errors.WithStack(err)
}
blobID := storage.NewBlobID()
writer, err := bucket.NewWriter(ctx, blobID)
if err != nil {
return errors.WithStack(err)
}
data := []byte("foo")
written, err := writer.Write(data)
if err != nil {
return errors.WithStack(err)
}
if e, g := len(data), written; e != g {
return errors.Errorf("length of written data: expected '%v', got '%v'", e, g)
}
if err := writer.Close(); err != nil {
panic(errors.WithStack(err))
}
reader, err := bucket.NewReader(ctx, blobID)
if err != nil {
return errors.WithStack(err)
}
var buf bytes.Buffer
written64, err := io.Copy(&buf, reader)
if err != nil {
return errors.WithStack(err)
}
if e, g := int64(len(data)), written64; e != g {
return errors.Errorf("length of written data: expected '%v', got '%v'", e, g)
}
if err := reader.Close(); err != nil {
panic(errors.WithStack(err))
}
if err := bucket.Close(); err != nil {
return errors.WithStack(err)
}
return nil
},
},
}
func testBlobStoreOps(t *testing.T, store storage.BlobStore) {
for _, tc := range blobStoreTestCases {
func(tc blobStoreTestCase) {
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
if err := tc.Run(ctx, store); err != nil {
t.Errorf("%+v", errors.WithStack(err))
}
})
}(tc)
}
}