github.com/rudderlabs/rudder-go-kit@v0.30.0/testhelper/docker/resource/minio/minio.go (about)

     1  package minio
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  	"net/http"
     8  
     9  	"github.com/minio/minio-go/v7"
    10  	"github.com/minio/minio-go/v7/pkg/credentials"
    11  	"github.com/ory/dockertest/v3"
    12  
    13  	"github.com/rudderlabs/rudder-go-kit/httputil"
    14  	"github.com/rudderlabs/rudder-go-kit/testhelper/docker/resource"
    15  )
    16  
    17  type Resource struct {
    18  	BucketName      string
    19  	AccessKeyID     string
    20  	AccessKeySecret string
    21  	Endpoint        string
    22  	Region          string
    23  	Client          *minio.Client
    24  }
    25  
    26  func (mr *Resource) ToFileManagerConfig(prefix string) map[string]any {
    27  	return map[string]any{
    28  		"bucketName":       mr.BucketName,
    29  		"accessKeyID":      mr.AccessKeyID,
    30  		"secretAccessKey":  mr.AccessKeySecret,
    31  		"accessKey":        mr.AccessKeySecret,
    32  		"enableSSE":        false,
    33  		"prefix":           prefix,
    34  		"endPoint":         mr.Endpoint,
    35  		"s3ForcePathStyle": true,
    36  		"disableSSL":       true,
    37  		"useSSL":           false,
    38  		"region":           mr.Region,
    39  	}
    40  }
    41  
    42  func Setup(pool *dockertest.Pool, d resource.Cleaner, opts ...func(*Config)) (*Resource, error) {
    43  	const (
    44  		bucket          = "rudder-saas"
    45  		region          = "us-east-1"
    46  		accessKeyId     = "MYACCESSKEY"
    47  		secretAccessKey = "MYSECRETKEY"
    48  	)
    49  
    50  	c := &Config{
    51  		Tag:     "latest",
    52  		Options: []string{},
    53  	}
    54  	for _, opt := range opts {
    55  		opt(c)
    56  	}
    57  
    58  	minioContainer, err := pool.RunWithOptions(&dockertest.RunOptions{
    59  		Repository: "minio/minio",
    60  		Tag:        c.Tag,
    61  		Cmd:        []string{"server", "/data"},
    62  		Env: append([]string{
    63  			fmt.Sprintf("MINIO_ACCESS_KEY=%s", accessKeyId),
    64  			fmt.Sprintf("MINIO_SECRET_KEY=%s", secretAccessKey),
    65  			fmt.Sprintf("MINIO_SITE_REGION=%s", region),
    66  			"MINIO_API_SELECT_PARQUET=on",
    67  		}, c.Options...),
    68  	})
    69  	if err != nil {
    70  		return nil, fmt.Errorf("could not start resource: %s", err)
    71  	}
    72  	d.Cleanup(func() {
    73  		if err := pool.Purge(minioContainer); err != nil {
    74  			log.Printf("Could not purge minio resource: %s \n", err)
    75  		}
    76  	})
    77  
    78  	endpoint := fmt.Sprintf("localhost:%s", minioContainer.GetPort("9000/tcp"))
    79  
    80  	// check if minio server is up & running.
    81  	if err := pool.Retry(func() error {
    82  		url := fmt.Sprintf("http://%s/minio/health/live", endpoint)
    83  		resp, err := http.Get(url)
    84  		if err != nil {
    85  			return err
    86  		}
    87  		defer func() { httputil.CloseResponse(resp) }()
    88  
    89  		if resp.StatusCode != http.StatusOK {
    90  			return fmt.Errorf("status code not OK")
    91  		}
    92  		return nil
    93  	}); err != nil {
    94  		log.Fatalf("Could not connect to docker: %s", err)
    95  	}
    96  
    97  	client, err := minio.New(endpoint, &minio.Options{
    98  		Creds:  credentials.NewStaticV4(accessKeyId, secretAccessKey, ""),
    99  		Secure: false,
   100  	})
   101  	if err != nil {
   102  		return nil, fmt.Errorf("could not create minio client: %w", err)
   103  	}
   104  
   105  	// creating bucket inside minio where testing will happen.
   106  	if err := client.MakeBucket(context.Background(), bucket, minio.MakeBucketOptions{Region: region}); err != nil {
   107  		return nil, fmt.Errorf("could not create bucket %q: %w", bucket, err)
   108  	}
   109  
   110  	return &Resource{
   111  		BucketName:      bucket,
   112  		AccessKeyID:     accessKeyId,
   113  		AccessKeySecret: secretAccessKey,
   114  		Endpoint:        endpoint,
   115  		Region:          region,
   116  		Client:          client,
   117  	}, nil
   118  }