-
Notifications
You must be signed in to change notification settings - Fork 0
/
minio.go
118 lines (104 loc) · 3.04 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
package minio
import (
"context"
"fmt"
"log"
"net/http"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/ory/dockertest/v3"
"github.com/rudderlabs/rudder-go-kit/httputil"
"github.com/rudderlabs/rudder-go-kit/testhelper/docker/resource"
)
type Resource struct {
BucketName string
AccessKeyID string
AccessKeySecret string
Endpoint string
Region string
Client *minio.Client
}
func (mr *Resource) ToFileManagerConfig(prefix string) map[string]any {
return map[string]any{
"bucketName": mr.BucketName,
"accessKeyID": mr.AccessKeyID,
"secretAccessKey": mr.AccessKeySecret,
"accessKey": mr.AccessKeySecret,
"enableSSE": false,
"prefix": prefix,
"endPoint": mr.Endpoint,
"s3ForcePathStyle": true,
"disableSSL": true,
"useSSL": false,
"region": mr.Region,
}
}
func Setup(pool *dockertest.Pool, d resource.Cleaner, opts ...func(*Config)) (*Resource, error) {
const (
bucket = "rudder-saas"
region = "us-east-1"
accessKeyId = "MYACCESSKEY"
secretAccessKey = "MYSECRETKEY"
)
c := &Config{
Tag: "latest",
Options: []string{},
}
for _, opt := range opts {
opt(c)
}
minioContainer, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: "minio/minio",
Tag: c.Tag,
Cmd: []string{"server", "/data"},
Env: append([]string{
fmt.Sprintf("MINIO_ACCESS_KEY=%s", accessKeyId),
fmt.Sprintf("MINIO_SECRET_KEY=%s", secretAccessKey),
fmt.Sprintf("MINIO_SITE_REGION=%s", region),
"MINIO_API_SELECT_PARQUET=on",
}, c.Options...),
})
if err != nil {
return nil, fmt.Errorf("could not start resource: %s", err)
}
d.Cleanup(func() {
if err := pool.Purge(minioContainer); err != nil {
log.Printf("Could not purge minio resource: %s \n", err)
}
})
endpoint := fmt.Sprintf("localhost:%s", minioContainer.GetPort("9000/tcp"))
// check if minio server is up & running.
if err := pool.Retry(func() error {
url := fmt.Sprintf("http://%s/minio/health/live", endpoint)
resp, err := http.Get(url)
if err != nil {
return err
}
defer func() { httputil.CloseResponse(resp) }()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("status code not OK")
}
return nil
}); err != nil {
log.Fatalf("Could not connect to docker: %s", err)
}
client, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyId, secretAccessKey, ""),
Secure: false,
})
if err != nil {
return nil, fmt.Errorf("could not create minio client: %w", err)
}
// creating bucket inside minio where testing will happen.
if err := client.MakeBucket(context.Background(), bucket, minio.MakeBucketOptions{Region: region}); err != nil {
return nil, fmt.Errorf("could not create bucket %q: %w", bucket, err)
}
return &Resource{
BucketName: bucket,
AccessKeyID: accessKeyId,
AccessKeySecret: secretAccessKey,
Endpoint: endpoint,
Region: region,
Client: client,
}, nil
}