2023-02-09 12:16:36 +01:00
|
|
|
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{
|
|
|
|
{
|
2023-09-13 06:03:25 +02:00
|
|
|
Name: "Open then delete bucket",
|
2023-02-09 12:16:36 +01:00
|
|
|
Run: func(ctx context.Context, store storage.BlobStore) error {
|
2023-09-13 06:03:25 +02:00
|
|
|
bucketName := "open-new-bucket"
|
|
|
|
|
|
|
|
bucket, err := store.OpenBucket(ctx, bucketName)
|
2023-02-09 12:16:36 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
if err := store.DeleteBucket(ctx, bucketName); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buckets, err := store.ListBuckets(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, b := range buckets {
|
|
|
|
if b != bucketName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.Errorf("bucket '%s' should be deleted", bucketName)
|
|
|
|
}
|
|
|
|
|
2023-02-09 12:16:36 +01:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2023-10-24 22:52:33 +02:00
|
|
|
reader, err = bucket.NewReader(ctx, blobID)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2023-02-09 12:16:36 +01:00
|
|
|
if err := bucket.Close(); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
func testBlobStoreOps(ctx context.Context, t *testing.T, store storage.BlobStore) {
|
2023-02-09 12:16:36 +01:00
|
|
|
for _, tc := range blobStoreTestCases {
|
|
|
|
func(tc blobStoreTestCase) {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
if err := tc.Run(ctx, store); err != nil {
|
|
|
|
t.Errorf("%+v", errors.WithStack(err))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}(tc)
|
|
|
|
}
|
|
|
|
}
|