From a5f980d2c69709bfc6f3b9a7255c74cbc7f52ab1 Mon Sep 17 00:00:00 2001 From: Ofek Atar Date: Tue, 9 May 2023 00:17:18 +0300 Subject: [PATCH 1/2] refactor: create minioClient interface --- pkg/storage/minio/minio.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/storage/minio/minio.go b/pkg/storage/minio/minio.go index b105aea18..c43ff3f49 100644 --- a/pkg/storage/minio/minio.go +++ b/pkg/storage/minio/minio.go @@ -1,3 +1,5 @@ +//go:generate mockery --inpackage=false --exported --name=minioCore + package minio import ( @@ -10,9 +12,13 @@ import ( minio "github.com/minio/minio-go/v6" ) +type minioCore interface { + ListObjectsV2(bucketName, objectPrefix, continuationToken string, fetchOwner bool, delimiter string, maxkeys int, startAfter string) (minio.ListBucketV2Result, error) +} + type storageImpl struct { minioClient *minio.Client - minioCore *minio.Core + minioCore minioCore bucketName string } From e4517de2ad5b28b6ccc66d4826d2c85ede11ad71 Mon Sep 17 00:00:00 2001 From: Ofek Atar Date: Tue, 9 May 2023 00:55:46 +0300 Subject: [PATCH 2/2] test: storage catalog --- go.mod | 1 + go.sum | 1 + pkg/storage/minio/cataloger_test.go | 40 +++++++++++++++++++++ pkg/storage/minio/mocks/minioCore.go | 52 ++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 pkg/storage/minio/cataloger_test.go create mode 100644 pkg/storage/minio/mocks/minioCore.go diff --git a/go.mod b/go.mod index 1d02f2bee..14479a23a 100644 --- a/go.mod +++ b/go.mod @@ -94,6 +94,7 @@ require ( github.com/prometheus/common v0.26.0 // indirect github.com/prometheus/procfs v0.6.0 // indirect github.com/rogpeppe/go-internal v1.3.0 // indirect + github.com/stretchr/objx v0.1.1 // indirect github.com/tinylib/msgp v1.0.2 // indirect github.com/uber/jaeger-client-go v2.25.0+incompatible // indirect github.com/xdg-go/pbkdf2 v1.0.0 // indirect diff --git a/go.sum b/go.sum index ecd49958c..57d4867de 100644 --- a/go.sum +++ b/go.sum @@ -477,6 +477,7 @@ github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3 github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= diff --git a/pkg/storage/minio/cataloger_test.go b/pkg/storage/minio/cataloger_test.go new file mode 100644 index 000000000..1da5a6b87 --- /dev/null +++ b/pkg/storage/minio/cataloger_test.go @@ -0,0 +1,40 @@ +package minio + +import ( + "context" + "testing" + + "github.com/gomods/athens/pkg/paths" + "github.com/gomods/athens/pkg/storage/minio/mocks" + "github.com/minio/minio-go/v6" + "github.com/stretchr/testify/require" +) + +func TestCatalog(t *testing.T) { + token := "test-token" + bucketName := "test-bucket" + pageSize := 10 + + mockMinioCore := mocks.NewMinioCore(t) + mockMinioCore.On("ListObjectsV2", bucketName, "", token, false, "", 0, "").Return(minio.ListBucketV2Result{ + Contents: []minio.ObjectInfo{ + { + Key: "test-version/test.info", + }, + }, + }, nil) + + st := &storageImpl{ + minioCore: mockMinioCore, + bucketName: bucketName, + } + + gotAllPathsParams, gotToken, gotErr := st.Catalog(context.Background(), token, pageSize) + + expectedToken := "" + expectedAllPathsParams := []paths.AllPathParams{{Module: "/test.info", Version: "test-version"}} + + require.NoError(t, gotErr) + require.Equal(t, expectedToken, gotToken) + require.Equal(t, expectedAllPathsParams, gotAllPathsParams) +} diff --git a/pkg/storage/minio/mocks/minioCore.go b/pkg/storage/minio/mocks/minioCore.go new file mode 100644 index 000000000..20a1ee516 --- /dev/null +++ b/pkg/storage/minio/mocks/minioCore.go @@ -0,0 +1,52 @@ +// Code generated by mockery v2.20.0. DO NOT EDIT. + +package mocks + +import ( + minio "github.com/minio/minio-go/v6" + mock "github.com/stretchr/testify/mock" +) + +// MinioCore is an autogenerated mock type for the minioCore type +type MinioCore struct { + mock.Mock +} + +// ListObjectsV2 provides a mock function with given fields: bucketName, objectPrefix, continuationToken, fetchOwner, delimiter, maxkeys, startAfter +func (_m *MinioCore) ListObjectsV2(bucketName string, objectPrefix string, continuationToken string, fetchOwner bool, delimiter string, maxkeys int, startAfter string) (minio.ListBucketV2Result, error) { + ret := _m.Called(bucketName, objectPrefix, continuationToken, fetchOwner, delimiter, maxkeys, startAfter) // nolint: typecheck // linter does not recognize struct embedding + + var r0 minio.ListBucketV2Result + var r1 error + if rf, ok := ret.Get(0).(func(string, string, string, bool, string, int, string) (minio.ListBucketV2Result, error)); ok { + return rf(bucketName, objectPrefix, continuationToken, fetchOwner, delimiter, maxkeys, startAfter) + } + if rf, ok := ret.Get(0).(func(string, string, string, bool, string, int, string) minio.ListBucketV2Result); ok { + r0 = rf(bucketName, objectPrefix, continuationToken, fetchOwner, delimiter, maxkeys, startAfter) + } else { + r0 = ret.Get(0).(minio.ListBucketV2Result) + } + + if rf, ok := ret.Get(1).(func(string, string, string, bool, string, int, string) error); ok { + r1 = rf(bucketName, objectPrefix, continuationToken, fetchOwner, delimiter, maxkeys, startAfter) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +type mockConstructorTestingTNewMinioCore interface { + mock.TestingT + Cleanup(func()) +} + +// NewMinioCore creates a new instance of MinioCore. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +func NewMinioCore(t mockConstructorTestingTNewMinioCore) *MinioCore { + mock := &MinioCore{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) // nolint: typecheck // linter does not recognize struct embedding + + return mock +}