Skip to content

Commit

Permalink
chore: add a docker resource for minio
Browse files Browse the repository at this point in the history
  • Loading branch information
atzoum committed Oct 24, 2023
1 parent 3e0196f commit ee33a9f
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 0 deletions.
119 changes: 119 additions & 0 deletions testhelper/docker/resource/minio.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package resource

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"
minioconfig "github.com/rudderlabs/rudder-go-kit/testhelper/docker/resource/minio"
)

type MinioResource struct {
BucketName string
AccessKeyID string
AccessKeySecret string
Endpoint string
Region string
Client *minio.Client
}

func (mr *MinioResource) 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 SetupMinio(pool *dockertest.Pool, d cleaner, opts ...func(*minioconfig.Config)) (*MinioResource, error) {
const (
bucket = "rudder-saas"
region = "us-east-1"
accessKeyId = "MYACCESSKEY"
secretAccessKey = "MYSECRETKEY"
prefix = "some-prefix"
)

c := &minioconfig.Config{
Tag: "latest",
Options: []string{},
}
for _, opt := range opts {
opt(c)
}

Check warning on line 57 in testhelper/docker/resource/minio.go

View check run for this annotation

Codecov / codecov/patch

testhelper/docker/resource/minio.go#L56-L57

Added lines #L56 - L57 were not covered by tests

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)
}

Check warning on line 72 in testhelper/docker/resource/minio.go

View check run for this annotation

Codecov / codecov/patch

testhelper/docker/resource/minio.go#L71-L72

Added lines #L71 - L72 were not covered by tests
d.Cleanup(func() {
if err := pool.Purge(minioContainer); err != nil {
log.Printf("Could not purge minio resource: %s \n", err)
}

Check warning on line 76 in testhelper/docker/resource/minio.go

View check run for this annotation

Codecov / codecov/patch

testhelper/docker/resource/minio.go#L75-L76

Added lines #L75 - L76 were not covered by tests
})

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")
}

Check warning on line 92 in testhelper/docker/resource/minio.go

View check run for this annotation

Codecov / codecov/patch

testhelper/docker/resource/minio.go#L91-L92

Added lines #L91 - L92 were not covered by tests
return nil
}); err != nil {
log.Fatalf("Could not connect to docker: %s", err)
}

Check warning on line 96 in testhelper/docker/resource/minio.go

View check run for this annotation

Codecov / codecov/patch

testhelper/docker/resource/minio.go#L94-L96

Added lines #L94 - L96 were not covered by tests

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)
}

Check warning on line 104 in testhelper/docker/resource/minio.go

View check run for this annotation

Codecov / codecov/patch

testhelper/docker/resource/minio.go#L103-L104

Added lines #L103 - L104 were not covered by tests

// 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)
}

Check warning on line 109 in testhelper/docker/resource/minio.go

View check run for this annotation

Codecov / codecov/patch

testhelper/docker/resource/minio.go#L108-L109

Added lines #L108 - L109 were not covered by tests

return &MinioResource{
BucketName: bucket,
AccessKeyID: accessKeyId,
AccessKeySecret: secretAccessKey,
Endpoint: endpoint,
Region: region,
Client: client,
}, nil
}
20 changes: 20 additions & 0 deletions testhelper/docker/resource/minio/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package minio

type Opt func(*Config)

func WithTag(tag string) Opt {
return func(c *Config) {
c.Tag = tag
}

Check warning on line 8 in testhelper/docker/resource/minio/config.go

View check run for this annotation

Codecov / codecov/patch

testhelper/docker/resource/minio/config.go#L5-L8

Added lines #L5 - L8 were not covered by tests
}

func WithOptions(options ...string) Opt {
return func(c *Config) {
c.Options = options
}

Check warning on line 14 in testhelper/docker/resource/minio/config.go

View check run for this annotation

Codecov / codecov/patch

testhelper/docker/resource/minio/config.go#L11-L14

Added lines #L11 - L14 were not covered by tests
}

type Config struct {
Tag string
Options []string
}
51 changes: 51 additions & 0 deletions testhelper/docker/resource/minio_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package resource_test

import (
"context"
"testing"

"github.com/minio/minio-go/v7"
"github.com/ory/dockertest/v3"
"github.com/stretchr/testify/require"

"github.com/rudderlabs/rudder-go-kit/filemanager"
"github.com/rudderlabs/rudder-go-kit/testhelper/docker/resource"
)

func TestMinioResource(t *testing.T) {
const prefix = "some-prefix"
const objectName = "minio.object"
pool, err := dockertest.NewPool("")
require.NoError(t, err)
minioResource, err := resource.SetupMinio(pool, t)
require.NoError(t, err)
_, err = minioResource.Client.FPutObject(context.Background(), minioResource.BucketName, prefix+"/"+objectName, "testdata/minio.object", minio.PutObjectOptions{})
require.NoError(t, err)
c := minioResource.ToFileManagerConfig("some-prefix")

t.Run("can use a minio filemanager", func(t *testing.T) {
fm, err := filemanager.New(&filemanager.Settings{
Provider: "MINIO",
Config: c,
})
require.NoError(t, err)

it := fm.ListFilesWithPrefix(context.Background(), "", "some-prefix", 1)
items, err := it.Next()
require.NoError(t, err)
require.Len(t, items, 1)
})

t.Run("can use a s3 filemanager", func(t *testing.T) {
fm, err := filemanager.New(&filemanager.Settings{
Provider: "S3",
Config: c,
})
require.NoError(t, err)

it := fm.ListFilesWithPrefix(context.Background(), "", "some-prefix", 1)
items, err := it.Next()
require.NoError(t, err)
require.Len(t, items, 1)
})
}
1 change: 1 addition & 0 deletions testhelper/docker/resource/testdata/minio.object
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
minio object

0 comments on commit ee33a9f

Please sign in to comment.