-
Notifications
You must be signed in to change notification settings - Fork 568
/
minio.go
129 lines (119 loc) · 3.66 KB
/
minio.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package dockertestenv
import (
"context"
"crypto/rand"
"fmt"
"strings"
"sync"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
docker "github.com/docker/docker/client"
minio "github.com/minio/minio-go/v7"
minioCreds "github.com/minio/minio-go/v7/pkg/credentials"
"github.com/pachyderm/pachyderm/v2/src/internal/backoff"
"github.com/pachyderm/pachyderm/v2/src/internal/obj"
"github.com/pachyderm/pachyderm/v2/src/internal/require"
"gocloud.dev/blob"
"gocloud.dev/blob/s3blob"
)
const (
minioPort = 9000
)
func NewTestObjClient(ctx context.Context, t testing.TB) obj.Client {
t.Helper()
dclient := newDockerClient()
defer dclient.Close()
err := backoff.Retry(func() error {
return ensureMinio(ctx, dclient)
}, backoff.NewConstantBackOff(time.Second*3))
require.NoError(t, err)
endpoint := getMinioEndpoint()
id := "minioadmin"
secret := "minioadmin"
client, err := minio.New(endpoint, &minio.Options{
Creds: minioCreds.NewStaticV4(id, secret, ""),
Secure: false,
})
require.NoError(t, err)
bucketName := newTestMinioBucket(ctx, t, client)
oc, err := obj.NewMinioClient(endpoint, bucketName, id, secret, false, false)
require.NoError(t, err)
return obj.WrapWithTestURL(oc)
}
// newTestMinioBucket creates a new bucket, which will be cleaned up when the test finishes
func newTestMinioBucket(ctx context.Context, t testing.TB, client *minio.Client) string {
bucketName := testBucketName(t)
t.Log("bucket:", bucketName)
err := client.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{})
require.NoError(t, err)
t.Cleanup(func() {
err := client.RemoveBucketWithOptions(ctx, bucketName, minio.RemoveBucketOptions{ForceDelete: true})
require.NoError(t, err)
})
return bucketName
}
func getMinioEndpoint() string {
host := getDockerHost()
return fmt.Sprintf("%s:%d", host, minioPort)
}
func testBucketName(t testing.TB) string {
buf := make([]byte, 4)
_, err := rand.Read(buf[:])
require.NoError(t, err)
tname := t.Name()
tname = strings.ToLower(tname)
tname = strings.ReplaceAll(tname, "/", "-")
return fmt.Sprintf("%s-%x", tname, buf[:])
}
var minioLock sync.Mutex
func ensureMinio(ctx context.Context, dclient docker.APIClient) error {
const (
containerName = "pach_test_minio"
imageName = "minio/minio"
)
minioLock.Lock()
defer minioLock.Unlock()
return ensureContainer(ctx, dclient, containerName, containerSpec{
Image: imageName,
Cmd: []string{"server", "/data", `--console-address=:9001`},
PortMap: map[uint16]uint16{
minioPort: minioPort,
minioPort + 1: minioPort + 1,
},
})
}
func NewTestBucket(ctx context.Context, t testing.TB) (*blob.Bucket, string) {
t.Helper()
dclient := newDockerClient()
defer dclient.Close()
err := backoff.Retry(func() error {
return ensureMinio(ctx, dclient)
}, backoff.NewConstantBackOff(time.Second*3))
require.NoError(t, err)
endpoint := getMinioEndpoint()
id := "minioadmin"
secret := "minioadmin"
client, err := minio.New(endpoint, &minio.Options{
Creds: minioCreds.NewStaticV4(id, secret, ""),
Secure: false,
})
require.NoError(t, err)
bucketName := newTestMinioBucket(ctx, t, client)
sess, err := session.NewSession(&aws.Config{
Region: aws.String("dummy-region"),
Credentials: credentials.NewStaticCredentials(id, secret, ""),
Endpoint: aws.String(endpoint),
DisableSSL: aws.Bool(true),
S3ForcePathStyle: aws.Bool(true),
})
require.NoError(t, err)
bucket, err := s3blob.OpenBucket(ctx, sess, bucketName, nil)
require.NoError(t, err)
return bucket, obj.ObjectStoreURL{
Scheme: "minio",
Bucket: fmt.Sprintf("%s/%s", endpoint, bucketName),
}.String()
}