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 then delete bucket", Run: func(ctx context.Context, store storage.BlobStore) error { bucketName := "open-new-bucket" bucket, err := store.OpenBucket(ctx, bucketName) 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) } 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) } 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)) } 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)) } if err := bucket.Close(); err != nil { return errors.WithStack(err) } return nil }, }, } func testBlobStoreOps(ctx context.Context, t *testing.T, store storage.BlobStore) { 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) } }