github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/pkg/block/s3/main_test.go (about)

     1  package s3_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/minio/minio-go/v7"
    11  	"github.com/minio/minio-go/v7/pkg/credentials"
    12  	"github.com/ory/dockertest/v3"
    13  )
    14  
    15  const (
    16  	minioContainerTimeoutSeconds = 10 * 60 // 10 min
    17  	bucketName                   = "bucket1"
    18  	minioTestEndpoint            = "127.0.0.1"
    19  	minioTestAccessKeyID         = "Q3AM3UQ867SPQQA43P2F"
    20  	minioTestSecretAccessKey     = "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"
    21  )
    22  
    23  var (
    24  	blockURL string
    25  	pool     *dockertest.Pool
    26  )
    27  
    28  func newClient(port string) (*minio.Client, error) {
    29  	creds := credentials.NewStaticV4(minioTestAccessKeyID, minioTestSecretAccessKey, "")
    30  	return minio.New(fmt.Sprintf("%s:%s", minioTestEndpoint, port), &minio.Options{Creds: creds})
    31  }
    32  
    33  func TestMain(m *testing.M) {
    34  	var err error
    35  	pool, err = dockertest.NewPool("")
    36  	if err != nil {
    37  		log.Fatalf("Could not connect to Docker: %s", err)
    38  	}
    39  	resource, err := pool.RunWithOptions(&dockertest.RunOptions{
    40  		Repository: "minio/minio",
    41  		Tag:        "RELEASE.2023-06-09T07-32-12Z",
    42  		Env: []string{
    43  			fmt.Sprintf("MINIO_ROOT_USER=%s", minioTestAccessKeyID),
    44  			fmt.Sprintf("MINIO_ROOT_PASSWORD=%s", minioTestSecretAccessKey),
    45  		},
    46  		Cmd: []string{
    47  			"server",
    48  			"start",
    49  		},
    50  	})
    51  	if err != nil {
    52  		panic(err)
    53  	}
    54  
    55  	// set cleanup
    56  	closer := func() {
    57  		err := pool.Purge(resource)
    58  		if err != nil {
    59  			panic("could not purge minio container: " + err.Error())
    60  		}
    61  	}
    62  
    63  	// expire, just to make sure
    64  	err = resource.Expire(minioContainerTimeoutSeconds)
    65  	if err != nil {
    66  		panic("could not expire minio container: " + err.Error())
    67  	}
    68  
    69  	// Create a test client and bucket
    70  	client, err := newClient(resource.GetPort("9000/tcp"))
    71  	if err != nil {
    72  		log.Fatalf("create client: %s", err)
    73  	}
    74  	blockURL = client.EndpointURL().String()
    75  
    76  	err = client.MakeBucket(context.Background(), bucketName, minio.MakeBucketOptions{
    77  		Region: "us-east-1",
    78  	})
    79  	if err != nil {
    80  		log.Fatalf("create bucket: %s", err)
    81  	}
    82  
    83  	code := m.Run()
    84  	closer()
    85  	os.Exit(code)
    86  }