Skip to content

Commit

Permalink
Merge pull request #31 from Clarilab/add-test-utils-and-alternative-h…
Browse files Browse the repository at this point in the history
…ealth-check-methods

feat: add test utils and alternative health check methods
  • Loading branch information
nicoandrewss authored Oct 8, 2024
2 parents 7447351 + f26d6bf commit 151adcd
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 4 deletions.
15 changes: 11 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/minio/minio-go/v7/pkg/credentials"
)

const name = "s3"

type client struct {
minioClient *minio.Client
bucketName string
Expand Down Expand Up @@ -50,10 +52,7 @@ func NewClient(details *ClientDetails, options ...ClientOption) (Client, error)
}
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

exists, err := client.minioClient.BucketExists(ctx, details.BucketName)
exists, err := client.minioClient.BucketExists(context.Background(), details.BucketName)
if err != nil {
return nil, fmt.Errorf(errMessage, err)
}
Expand All @@ -76,3 +75,11 @@ func (c *client) Close() {
func (c *client) IsOnline() bool {
return c.minioClient.IsOnline()
}

func (c *client) IsHealthy() bool {
return c.IsOnline()
}

func (c *client) GetName() string {
return name
}
6 changes: 6 additions & 0 deletions s3client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@ type Client interface {

// IsOnline reports true if the client is online. If the health-check has not been enabled this will always return true.
IsOnline() bool

// IsHealthy reports true if the client is online. If the health-check has not been enabled this will always return true.
IsHealthy() bool

// GetName returns the name of the client.
GetName() string
}
80 changes: 80 additions & 0 deletions testutils/s3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package testutils

import (
"context"
"fmt"
"net"
"strconv"

"github.com/Clarilab/s3-client/v4"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/orlangure/gnomock"
)

const (
user = "admin"
passwd = "password"
imageTag = "quay.io/minio/minio:latest"
minioPort = 9000

userEnv = "MINIO_ROOT_USER=" + user
userPasswd = "MINIO_ROOT_PASSWORD=" + passwd
command = "server"
arg = "/data"
)

// StopFunc is a function to stop the created container.
type StopFunc func() error

// NewClient starts a container with a running MinIO instance
// and returns a new s3.Client, a function to stop the container on purpose and an error.
//
// Notes:
// Only meant to be used for testing purposes.
// Host MUST have a docker engine running.
func NewClient(bucketName string, options ...s3.ClientOption) (s3.Client, StopFunc, error) {
const errMessage = "failed to create new client: %w"

container, err := gnomock.StartCustom(
imageTag,
gnomock.DefaultTCP(minioPort),
gnomock.WithUseLocalImagesFirst(),
gnomock.WithEnv(userEnv),
gnomock.WithEnv(userPasswd),
gnomock.WithCommand(command, arg),
)
if err != nil {
return nil, nil, fmt.Errorf(errMessage, err)
}

url := net.JoinHostPort(container.Host, strconv.Itoa(container.DefaultPort()))

minioClient, err := minio.New(url, &minio.Options{
Secure: false,
Creds: credentials.NewStaticV4(user, passwd, ""),
})
if err != nil {
return nil, nil, fmt.Errorf(errMessage, err)
}

if err = minioClient.MakeBucket(context.Background(), bucketName, minio.MakeBucketOptions{}); err != nil {
return nil, nil, fmt.Errorf(errMessage, err)
}

conn, err := s3.NewClient(
&s3.ClientDetails{
Host: url,
AccessKey: user,
AccessSecret: passwd,
BucketName: bucketName,
Secure: false,
},
options...,
)
if err != nil {
return nil, nil, fmt.Errorf(errMessage, err)
}

return conn, func() error { return gnomock.Stop(container) }, nil
}

0 comments on commit 151adcd

Please sign in to comment.