Add tests

parent bf95dd05
Pipeline #17530217 passed with stages
in 30 minutes and 18 seconds
......@@ -207,6 +207,7 @@ mocks: $(MOCKERY)
GOPATH=$(ORIGINAL_GOPATH) mockery $(MOCKERY_FLAGS) -dir=./vendor/github.com/ayufan/golang-kardianos-service -output=./helpers/service/mocks -name='(Interface|Logger)'
GOPATH=$(ORIGINAL_GOPATH) mockery $(MOCKERY_FLAGS) -dir=./helpers/docker -all -inpkg
GOPATH=$(ORIGINAL_GOPATH) mockery $(MOCKERY_FLAGS) -dir=./common -all -inpkg
GOPATH=$(ORIGINAL_GOPATH) mockery $(MOCKERY_FLAGS) -dir=./shells -name fakeIAMCredentialsProvider -inpkg
test-docker:
make test-docker-image IMAGE=centos:6 TYPE=rpm
......
......@@ -52,11 +52,19 @@ func getCacheObjectName(build *common.Build, cache *common.CacheConfig, key stri
return path.Join(cache.Path, runnerSegment, "project", strconv.Itoa(build.JobInfo.ProjectID), key)
}
type fakeIAMCredentialsProvider interface {
credentials.Provider
}
var iamFactory = func() *credentials.Credentials {
return credentials.NewIAM("")
}
func getCacheStorageClient(cache *common.CacheConfig) (scl *minio.Client, err error) {
// If the server address or credentials aren't specified then use IAM
// instance profile credentials and talk to "real" S3.
//If the server address or credentials aren't specified then use IAM
//instance profile credentials and talk to "real" S3.
if cache.ServerAddress == "" || cache.AccessKey == "" || cache.SecretKey == "" {
iam := credentials.NewIAM("")
iam := iamFactory()
scl, err = minio.NewWithCredentials("s3.amazonaws.com", iam, true, "")
} else {
scl, err = minio.New(cache.ServerAddress, cache.AccessKey, cache.SecretKey, !cache.Insecure)
......
......@@ -3,6 +3,7 @@ package shells
import (
"testing"
"github.com/minio/minio-go/pkg/credentials"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
......@@ -12,14 +13,28 @@ import (
func defaultS3CacheFactory() *common.CacheConfig {
return &common.CacheConfig{
Type: "s3",
BucketName: "test",
BucketLocation: "location",
ServerAddress: "server.com",
AccessKey: "access",
SecretKey: "key",
BucketName: "test",
BucketLocation: "location",
}
}
func iamS3CacheFactory(t *testing.T) (*common.CacheConfig, *mockFakeIAMCredentialsProvider) {
cacheConfig := defaultS3CacheFactory()
cacheConfig.ServerAddress = ""
cacheConfig.AccessKey = ""
cacheConfig.SecretKey = ""
iamProvider := &mockFakeIAMCredentialsProvider{}
iamFactory = func() *credentials.Credentials {
return credentials.New(iamProvider)
}
return cacheConfig, iamProvider
}
func defaults3CacheBuild(cacheConfig *common.CacheConfig) *common.Build {
return &common.Build{
JobResponse: common.JobResponse{
......@@ -49,6 +64,27 @@ func TestS3CacheUploadURL(t *testing.T) {
require.NotNil(t, url)
assert.Equal(t, s3Cache.ServerAddress, url.Host)
assert.Regexp(t, "^https://", url)
assert.Contains(t, url.String(), "X-Amz-Credential=access%2F")
}
func TestS3CacheUploadURLForIamCredentials(t *testing.T) {
s3Cache, iamProvider := iamS3CacheFactory(t)
fakeValue := credentials.Value{
AccessKeyID: "access-from-iam",
SecretAccessKey: "secret-from-iam",
}
iamProvider.On("Retrieve").Return(fakeValue, nil).Once()
iamProvider.On("IsExpired").Return(false)
defer iamProvider.AssertExpectations(t)
s3Cache.Insecure = false
s3CacheBuild := defaults3CacheBuild(s3Cache)
url := getCacheUploadURL(s3CacheBuild, "key")
require.NotNil(t, url)
assert.Equal(t, "test.s3.amazonaws.com", url.Host)
assert.Regexp(t, "^https://", url)
assert.Contains(t, url.String(), "X-Amz-Credential=access-from-iam%2F")
}
func TestS3CacheUploadInsecureURL(t *testing.T) {
......
package shells
import credentials "github.com/minio/minio-go/pkg/credentials"
import mock "github.com/stretchr/testify/mock"
// mockFakeIAMCredentialsProvider is an autogenerated mock type for the fakeIAMCredentialsProvider type
type mockFakeIAMCredentialsProvider struct {
mock.Mock
}
// IsExpired provides a mock function with given fields:
func (_m *mockFakeIAMCredentialsProvider) IsExpired() bool {
ret := _m.Called()
var r0 bool
if rf, ok := ret.Get(0).(func() bool); ok {
r0 = rf()
} else {
r0 = ret.Get(0).(bool)
}
return r0
}
// Retrieve provides a mock function with given fields:
func (_m *mockFakeIAMCredentialsProvider) Retrieve() (credentials.Value, error) {
ret := _m.Called()
var r0 credentials.Value
if rf, ok := ret.Get(0).(func() credentials.Value); ok {
r0 = rf()
} else {
r0 = ret.Get(0).(credentials.Value)
}
var r1 error
if rf, ok := ret.Get(1).(func() error); ok {
r1 = rf()
} else {
r1 = ret.Error(1)
}
return r0, r1
}
var _ fakeIAMCredentialsProvider = (*mockFakeIAMCredentialsProvider)(nil)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment