github.com/nilium/gitlab-runner@v12.5.0+incompatible/cache/s3/adapter_test.go (about)

     1  package s3
     2  
     3  import (
     4  	"errors"
     5  	"net/url"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/mock"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"gitlab.com/gitlab-org/gitlab-runner/cache"
    14  	"gitlab.com/gitlab-org/gitlab-runner/common"
    15  )
    16  
    17  var defaultTimeout = 1 * time.Hour
    18  
    19  func defaultCacheFactory() *common.CacheConfig {
    20  	return &common.CacheConfig{
    21  		Type: "s3",
    22  		S3: &common.CacheS3Config{
    23  			ServerAddress:  "server.com",
    24  			AccessKey:      "access",
    25  			SecretKey:      "key",
    26  			BucketName:     "test",
    27  			BucketLocation: "location"},
    28  	}
    29  }
    30  
    31  type cacheOperationTest struct {
    32  	errorOnMinioClientInitialization bool
    33  	errorOnURLPresigning             bool
    34  
    35  	presignedURL *url.URL
    36  	expectedURL  *url.URL
    37  }
    38  
    39  func onFakeMinioURLGenerator(tc cacheOperationTest) func() {
    40  	client := new(mockMinioClient)
    41  
    42  	var err error
    43  	if tc.errorOnURLPresigning {
    44  		err = errors.New("test error")
    45  	}
    46  
    47  	client.On("PresignedGetObject", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tc.presignedURL, err)
    48  	client.On("PresignedPutObject", mock.Anything, mock.Anything, mock.Anything).Return(tc.presignedURL, err)
    49  
    50  	oldNewMinioURLGenerator := newMinioClient
    51  	newMinioClient = func(s3 *common.CacheS3Config) (minioClient, error) {
    52  		if tc.errorOnMinioClientInitialization {
    53  			return nil, errors.New("test error")
    54  		}
    55  		return client, nil
    56  	}
    57  
    58  	return func() {
    59  		newMinioClient = oldNewMinioURLGenerator
    60  	}
    61  }
    62  
    63  func testCacheOperation(t *testing.T, operationName string, operation func(adapter cache.Adapter) *url.URL, tc cacheOperationTest) {
    64  	t.Run(operationName, func(t *testing.T) {
    65  		cleanupMinioURLGeneratorMock := onFakeMinioURLGenerator(tc)
    66  		defer cleanupMinioURLGeneratorMock()
    67  
    68  		cacheConfig := defaultCacheFactory()
    69  
    70  		adapter, err := New(cacheConfig, defaultTimeout, "key")
    71  
    72  		if tc.errorOnMinioClientInitialization {
    73  			assert.EqualError(t, err, "error while creating S3 cache storage client: test error")
    74  
    75  			return
    76  		}
    77  		require.NoError(t, err)
    78  
    79  		URL := operation(adapter)
    80  		assert.Equal(t, tc.expectedURL, URL)
    81  	})
    82  }
    83  
    84  func TestCacheOperation(t *testing.T) {
    85  	URL, err := url.Parse("https://s3.example.com")
    86  	require.NoError(t, err)
    87  
    88  	tests := map[string]cacheOperationTest{
    89  		"error-on-minio-client-initialization": {
    90  			errorOnMinioClientInitialization: true,
    91  		},
    92  		"error-on-presigning-url": {
    93  			errorOnURLPresigning: true,
    94  			presignedURL:         URL,
    95  			expectedURL:          nil,
    96  		},
    97  		"presigned-url": {
    98  			presignedURL: URL,
    99  			expectedURL:  URL,
   100  		},
   101  	}
   102  
   103  	for testName, test := range tests {
   104  		t.Run(testName, func(t *testing.T) {
   105  			testCacheOperation(t, "GetDownloadURL", func(adapter cache.Adapter) *url.URL { return adapter.GetDownloadURL() }, test)
   106  			testCacheOperation(t, "GetUploadURL", func(adapter cache.Adapter) *url.URL { return adapter.GetUploadURL() }, test)
   107  		})
   108  	}
   109  }
   110  
   111  func TestNoConfiguration(t *testing.T) {
   112  	s3Cache := defaultCacheFactory()
   113  	s3Cache.S3 = nil
   114  
   115  	adapter, err := New(s3Cache, defaultTimeout, "key")
   116  	assert.Nil(t, adapter)
   117  
   118  	assert.EqualError(t, err, "missing S3 configuration")
   119  }