Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(test): switch to minimock and add tests #687

Merged
merged 3 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions pkg/handler/mock_service_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 14 additions & 13 deletions pkg/minio/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ import (
"time"

"github.com/gofrs/uuid"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/minio/minio-go/v7/pkg/lifecycle"
"go.uber.org/zap"

miniogo "github.com/minio/minio-go/v7"

"github.com/instill-ai/model-backend/config"

log "github.com/instill-ai/model-backend/pkg/logger"
)

type MinioI interface {
UploadFile(ctx context.Context, filePath string, fileContent any, fileMimeType string) (url string, objectInfo *minio.ObjectInfo, err error)
UploadFileBytes(ctx context.Context, filePath string, fileBytes []byte, fileMimeType string) (url string, objectInfo *minio.ObjectInfo, err error)
UploadFile(ctx context.Context, filePath string, fileContent any, fileMimeType string) (url string, objectInfo *miniogo.ObjectInfo, err error)
UploadFileBytes(ctx context.Context, filePath string, fileBytes []byte, fileMimeType string) (url string, objectInfo *miniogo.ObjectInfo, err error)
DeleteFile(ctx context.Context, filePath string) (err error)
GetFile(ctx context.Context, filePath string) ([]byte, error)
GetFilesByPaths(ctx context.Context, filePaths []string) ([]FileContent, error)
Expand All @@ -32,7 +33,7 @@ type MinioI interface {
const Location = "us-east-1"

type Minio struct {
client *minio.Client
client *miniogo.Client
bucket string
}

Expand All @@ -44,7 +45,7 @@ func NewMinioClientAndInitBucket(ctx context.Context, cfg *config.MinioConfig) (
logger.Info("Initializing Minio client and bucket...")

endpoint := net.JoinHostPort(cfg.Host, cfg.Port)
client, err := minio.New(endpoint, &minio.Options{
client, err := miniogo.New(endpoint, &miniogo.Options{
Creds: credentials.NewStaticV4(cfg.RootUser, cfg.RootPwd, ""),
Secure: cfg.Secure,
})
Expand All @@ -66,7 +67,7 @@ func NewMinioClientAndInitBucket(ctx context.Context, cfg *config.MinioConfig) (
return &Minio{client: client, bucket: cfg.BucketName}, nil
}

if err = client.MakeBucket(ctx, cfg.BucketName, minio.MakeBucketOptions{
if err = client.MakeBucket(ctx, cfg.BucketName, miniogo.MakeBucketOptions{
Region: Location,
}); err != nil {
logger.Error("creating Bucket failed", zap.Error(err))
Expand All @@ -93,12 +94,12 @@ func NewMinioClientAndInitBucket(ctx context.Context, cfg *config.MinioConfig) (
return &Minio{client: client, bucket: cfg.BucketName}, nil
}

func (m *Minio) UploadFile(ctx context.Context, filePath string, fileContent any, fileMimeType string) (url string, objectInfo *minio.ObjectInfo, err error) {
func (m *Minio) UploadFile(ctx context.Context, filePath string, fileContent any, fileMimeType string) (url string, objectInfo *miniogo.ObjectInfo, err error) {
jsonData, _ := json.Marshal(fileContent)
return m.UploadFileBytes(ctx, filePath, jsonData, fileMimeType)
}

func (m *Minio) UploadFileBytes(ctx context.Context, filePath string, fileBytes []byte, fileMimeType string) (url string, objectInfo *minio.ObjectInfo, err error) {
func (m *Minio) UploadFileBytes(ctx context.Context, filePath string, fileBytes []byte, fileMimeType string) (url string, objectInfo *miniogo.ObjectInfo, err error) {
logger, err := log.GetZapLogger(ctx)
if err != nil {
return "", nil, err
Expand All @@ -107,14 +108,14 @@ func (m *Minio) UploadFileBytes(ctx context.Context, filePath string, fileBytes
reader := bytes.NewReader(fileBytes)

// Create the file path with folder structure
_, err = m.client.PutObject(ctx, m.bucket, filePath, reader, int64(len(fileBytes)), minio.PutObjectOptions{ContentType: fileMimeType})
_, err = m.client.PutObject(ctx, m.bucket, filePath, reader, int64(len(fileBytes)), miniogo.PutObjectOptions{ContentType: fileMimeType})
if err != nil {
logger.Error("Failed to upload file to MinIO", zap.Error(err))
return "", nil, err
}

// Get the object stat (metadata)
stat, err := m.client.StatObject(ctx, m.bucket, filePath, minio.StatObjectOptions{})
stat, err := m.client.StatObject(ctx, m.bucket, filePath, miniogo.StatObjectOptions{})
if err != nil {
return "", nil, err
}
Expand All @@ -135,7 +136,7 @@ func (m *Minio) DeleteFile(ctx context.Context, filePathName string) (err error)
return err
}
// Delete the file from MinIO
err = m.client.RemoveObject(ctx, m.bucket, filePathName, minio.RemoveObjectOptions{})
err = m.client.RemoveObject(ctx, m.bucket, filePathName, miniogo.RemoveObjectOptions{})
if err != nil {
logger.Error("Failed to delete file from MinIO", zap.Error(err))
return err
Expand All @@ -150,7 +151,7 @@ func (m *Minio) GetFile(ctx context.Context, filePathName string) ([]byte, error
}

// Get the object using the client
object, err := m.client.GetObject(ctx, m.bucket, filePathName, minio.GetObjectOptions{})
object, err := m.client.GetObject(ctx, m.bucket, filePathName, miniogo.GetObjectOptions{})
if err != nil {
logger.Error("Failed to get file from MinIO", zap.Error(err))
return nil, err
Expand Down Expand Up @@ -192,7 +193,7 @@ func (m *Minio) GetFilesByPaths(ctx context.Context, filePaths []string) ([]File
go func(filePath string) {
defer wg.Done()

obj, err := m.client.GetObject(ctx, m.bucket, filePath, minio.GetObjectOptions{})
obj, err := m.client.GetObject(ctx, m.bucket, filePath, miniogo.GetObjectOptions{})
if err != nil {
logger.Error("Failed to get object from MinIO", zap.String("path", filePath), zap.Error(err))
errCh <- err
Expand Down
5 changes: 2 additions & 3 deletions pkg/mock/generator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package mock

//go:generate minimock -g -i github.com/instill-ai/model-backend/pkg/minio.MinioI -o ./ -s "_mock.gen.go"

// todo: port the `mockgen` generated files to `minimock`
//go:generate mockgen -destination mock_repository.go -package $GOPACKAGE github.com/instill-ai/model-backend/pkg/repository Repository
//go:generate minimock -g -i github.com/instill-ai/model-backend/pkg/repository.Repository -o ./ -s "_mock.gen.go"
//go:generate minimock -g -i github.com/instill-ai/model-backend/pkg/ray.Ray -o ./ -s "_mock.gen.go"
26 changes: 13 additions & 13 deletions pkg/mock/minio_i_mock.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading