2021-01-26 02:11:14 +03:00
|
|
|
package s3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2021-01-26 10:41:20 +03:00
|
|
|
|
2021-02-04 09:43:40 +03:00
|
|
|
"github.com/minio/minio-go/v7"
|
2021-01-26 10:41:20 +03:00
|
|
|
"github.com/unistack-org/micro/v3"
|
2021-09-06 18:09:07 +03:00
|
|
|
"github.com/unistack-org/micro/v3/util/id"
|
2021-01-26 02:11:14 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStore(t *testing.T) {
|
|
|
|
if len(os.Getenv("INTEGRATION_TESTS")) > 0 {
|
|
|
|
t.Skip()
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
s := NewStore(
|
|
|
|
AccessKey("Q3AM3UQ867SPQQA43P2F"),
|
|
|
|
SecretKey("zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"),
|
|
|
|
Endpoint("https://play.minio.io"),
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := s.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-01-26 10:41:20 +03:00
|
|
|
if err := s.Init(); err != nil {
|
|
|
|
t.Fatalf("double init test failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-01-26 02:11:14 +03:00
|
|
|
if err := s.Connect(ctx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-01-26 10:41:20 +03:00
|
|
|
if err := s.Connect(ctx); err != nil {
|
|
|
|
t.Fatalf("double connect test failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-01-26 02:11:14 +03:00
|
|
|
defer func() {
|
|
|
|
if err := s.Disconnect(ctx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-01-29 14:45:37 +03:00
|
|
|
svc := micro.NewService(micro.Stores(s))
|
2021-01-26 10:41:20 +03:00
|
|
|
if err := svc.Init(); err != nil {
|
|
|
|
t.Fatalf("service init failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-01-26 02:11:14 +03:00
|
|
|
val := []byte("test")
|
|
|
|
key := "key"
|
|
|
|
|
2021-09-06 18:12:04 +03:00
|
|
|
uid, err := id.New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
bucket := "micro-store-s3-" + uid
|
2021-02-04 09:43:40 +03:00
|
|
|
if err := s.Write(ctx, key, val, WriteBucket(bucket), ContentType("text/plain")); err != nil {
|
2021-01-26 02:11:14 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
val = nil
|
|
|
|
|
2021-02-04 09:43:40 +03:00
|
|
|
if err := s.Exists(ctx, key, ExistsBucket(bucket)); err != nil {
|
2021-01-26 02:11:14 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-02-04 09:43:40 +03:00
|
|
|
if err := s.Read(ctx, key, &val, ReadBucket(bucket)); err != nil {
|
2021-01-26 02:11:14 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
} else if !bytes.Equal(val, []byte("test")) {
|
|
|
|
t.Fatalf("read bytes are not equal %s != %s", val, "test")
|
|
|
|
}
|
|
|
|
|
2021-02-04 09:43:40 +03:00
|
|
|
names, err := s.List(ctx, ListBucket(bucket))
|
2021-01-26 02:11:14 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var found bool
|
|
|
|
for _, n := range names {
|
|
|
|
if n == "key" {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
t.Fatalf("key not found in %v", names)
|
|
|
|
}
|
|
|
|
|
2021-02-04 09:43:40 +03:00
|
|
|
objectsCh := make(chan minio.ObjectInfo)
|
|
|
|
minioClient := s.(*s3Store).client
|
|
|
|
// Send object names that are needed to be removed to objectsCh
|
|
|
|
go func() {
|
|
|
|
defer close(objectsCh)
|
|
|
|
// List all objects from a bucket-name with a matching prefix.
|
|
|
|
for object := range minioClient.ListObjects(context.Background(), bucket, minio.ListObjectsOptions{Recursive: true}) {
|
|
|
|
if object.Err != nil {
|
|
|
|
t.Fatal(object.Err)
|
|
|
|
}
|
|
|
|
objectsCh <- object
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
opts := minio.RemoveObjectsOptions{
|
|
|
|
GovernanceBypass: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
for rErr := range minioClient.RemoveObjects(context.Background(), bucket, objectsCh, opts) {
|
|
|
|
t.Fatalf("Error detected during deletion: %v", rErr)
|
|
|
|
}
|
2021-01-26 02:11:14 +03:00
|
|
|
}
|