1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
//go:build !integration
// +build !integration
package gcs
import (
"errors"
"fmt"
"net/http"
"net/url"
"testing"
"time"
"cloud.google.com/go/storage"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitlab-runner/common"
)
var (
accessID = "test-access-id@X.iam.gserviceaccount.com"
privateKey = `-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEAzIrvApxNX3VxH5eYe4vI2kLTqOA9uFTV4clGy8uzQsGQvMjl
frTWCffayxaSvoKxPlvUYbecYpqqqaByLTE+kSDU/D44yrCiLAyWHWXYGZqfEMEG
uHBg4fJK6KcIXlJ3H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P7qVNu
71JKqBtpwkjArghP8ZcnH7yJAoGBAJnHDxFoEfKGvcRH9V195uAeUpOjM0T1U63S
ssNGszLZco9H7Z3KnLoAx4vWAhmy1jfxc5i8HmxdJRnZ31SvMdE7u3ydkfrxk6Yk
VUtqdTA1lE0Ij4Ryyycdd0QJk4ZPufyWjgjPa15+wH7MoVVy388/5WwF1Pb69Tku
wAqc2gkRAoGAcj8a+peaNKa1d5EPE0CtTBUypupZh/R1ewTC9y7OyBPczYhxN5NQ
vvm6J1WGbnxmuhzzvGNNExeZx9dfGLmcvSAvrweiFbi2yHAc1cBLBkc5/CqfS6QW
336Qe2lgsM61/jrYYYqu7W8l6W2juCz0SPqml6rugsP8r6IMJxfziO8=
-----END RSA PRIVATE KEY-----`
bucketName = "test"
objectName = "key"
defaultTimeout = 1 * time.Hour
)
func defaultGCSCache() *common.CacheConfig {
return &common.CacheConfig{
Type: "gcs",
GCS: &common.CacheGCSConfig{
BucketName: bucketName,
},
}
}
type adapterOperationInvalidConfigTestCase struct {
noGCSConfig bool
errorOnCredentialsResolverInitialization bool
credentialsResolverResolveError bool
accessID string
privateKey string
bucketName string
expectedError string
}
func prepareMockedCredentialsResolverInitializer(tc adapterOperationInvalidConfigTestCase) func() {
oldCredentialsResolverInitializer := credentialsResolverInitializer
credentialsResolverInitializer = func(config *common.CacheGCSConfig) (*defaultCredentialsResolver, error) {
if tc.errorOnCredentialsResolverInitialization {
return nil, errors.New("test error")
}
return newDefaultCredentialsResolver(config)
}
return func() {
credentialsResolverInitializer = oldCredentialsResolverInitializer
}
}
func prepareMockedCredentialsResolverForInvalidConfig(adapter *gcsAdapter, tc adapterOperationInvalidConfigTestCase) {
cr := &mockCredentialsResolver{}
resolveCall := cr.On("Resolve")
if tc.credentialsResolverResolveError {
resolveCall.Return(fmt.Errorf("test error"))
} else {
resolveCall.Return(nil)
}
cr.On("Credentials").Return(&common.CacheGCSCredentials{
AccessID: tc.accessID,
PrivateKey: tc.privateKey,
})
adapter.credentialsResolver = cr
}
func testAdapterOperationWithInvalidConfig(
t *testing.T,
name string,
tc adapterOperationInvalidConfigTestCase,
adapter *gcsAdapter,
operation func() *url.URL,
) {
t.Run(name, func(t *testing.T) {
prepareMockedCredentialsResolverForInvalidConfig(adapter, tc)
hook := test.NewGlobal()
u := operation()
assert.Nil(t, u)
message, err := hook.LastEntry().String()
require.NoError(t, err)
assert.Contains(t, message, tc.expectedError)
})
}
func TestAdapterOperation_InvalidConfig(t *testing.T) {
tests := map[string]adapterOperationInvalidConfigTestCase{
"no-gcs-config": {
noGCSConfig: true,
bucketName: bucketName,
expectedError: "Missing GCS configuration",
},
"error-on-credentials-resolver-initialization": {
errorOnCredentialsResolverInitialization: true,
},
"credentials-resolver-resolve-error": {
credentialsResolverResolveError: true,
bucketName: bucketName,
expectedError: "error while resolving GCS credentials: test error",
},
"no-credentials": {
bucketName: bucketName,
expectedError: "storage: missing required GoogleAccessID",
},
"no-access-id": {
privateKey: privateKey,
bucketName: bucketName,
expectedError: "storage: missing required GoogleAccessID",
},
"no-private-key": {
accessID: accessID,
bucketName: bucketName,
expectedError: "storage: exactly one of PrivateKey or SignedBytes must be set",
},
"bucket-not-specified": {
accessID: "access-id",
privateKey: privateKey,
expectedError: "BucketName can't be empty",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
cleanupCredentialsResolverInitializerMock := prepareMockedCredentialsResolverInitializer(tc)
defer cleanupCredentialsResolverInitializerMock()
config := defaultGCSCache()
if tc.noGCSConfig {
config.GCS = nil
} else {
config.GCS.BucketName = tc.bucketName
}
a, err := New(config, defaultTimeout, objectName)
if tc.noGCSConfig {
assert.Nil(t, a)
assert.EqualError(t, err, "missing GCS configuration")
return
}
if tc.errorOnCredentialsResolverInitialization {
assert.Nil(t, a)
assert.EqualError(t, err, "error while initializing GCS credentials resolver: test error")
return
}
require.NotNil(t, a)
require.NoError(t, err)
adapter, ok := a.(*gcsAdapter)
require.True(t, ok, "Adapter should be properly casted to *adapter type")
testAdapterOperationWithInvalidConfig(t, "GetDownloadURL", tc, adapter, a.GetDownloadURL)
testAdapterOperationWithInvalidConfig(t, "GetUploadURL", tc, adapter, a.GetUploadURL)
})
}
}
type adapterOperationTestCase struct {
returnedURL string
returnedError error
assertErrorMessage func(t *testing.T, message string)
}
func prepareMockedCredentialsResolver(adapter *gcsAdapter) func(t *testing.T) {
cr := &mockCredentialsResolver{}
cr.On("Resolve").Return(nil)
cr.On("Credentials").Return(&common.CacheGCSCredentials{
AccessID: accessID,
PrivateKey: privateKey,
})
adapter.credentialsResolver = cr
return func(t *testing.T) {
cr.AssertExpectations(t)
}
}
func prepareMockedSignedURLGenerator(
t *testing.T,
tc adapterOperationTestCase,
expectedMethod string,
expectedContentType string,
adapter *gcsAdapter,
) {
adapter.generateSignedURL = func(bucket string, name string, opts *storage.SignedURLOptions) (string, error) {
require.Equal(t, accessID, opts.GoogleAccessID)
require.Equal(t, privateKey, string(opts.PrivateKey))
require.Equal(t, expectedMethod, opts.Method)
require.Equal(t, expectedContentType, opts.ContentType)
return tc.returnedURL, tc.returnedError
}
}
func testAdapterOperation(
t *testing.T,
tc adapterOperationTestCase,
name string,
expectedMethod string,
expectedContentType string,
adapter *gcsAdapter,
operation func() *url.URL,
) {
t.Skip("DM-disabled");
t.Run(name, func(t *testing.T) {
cleanupCredentialsResolverMock := prepareMockedCredentialsResolver(adapter)
defer cleanupCredentialsResolverMock(t)
prepareMockedSignedURLGenerator(t, tc, expectedMethod, expectedContentType, adapter)
hook := test.NewGlobal()
u := operation()
if tc.assertErrorMessage != nil {
message, err := hook.LastEntry().String()
require.NoError(t, err)
tc.assertErrorMessage(t, message)
return
}
require.Len(t, hook.AllEntries(), 0)
assert.Equal(t, tc.returnedURL, u.String())
})
}
func TestAdapterOperation(t *testing.T) {
//nolint:lll
tests := map[string]adapterOperationTestCase{
"error-on-URL-signing": {
returnedURL: "",
returnedError: fmt.Errorf("test error"),
assertErrorMessage: func(t *testing.T, message string) {
assert.Contains(t, message, "error while generating GCS pre-signed URL: test error")
},
},
"invalid-URL-returned": {
returnedURL: "://test",
returnedError: nil,
assertErrorMessage: func(t *testing.T, message string) {
assert.Contains(t, message, "error while parsing generated URL: parse")
assert.Contains(t, message, "://test")
assert.Contains(t, message, "missing protocol scheme")
},
},
"valid-configuration": {
returnedURL: "https://storage.googleapis.com/test/key?Expires=123456789&GoogleAccessId=test-access-id%40X.iam.gserviceaccount.com&Signature=XYZ",
returnedError: nil,
assertErrorMessage: nil,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
config := defaultGCSCache()
a, err := New(config, defaultTimeout, objectName)
require.NoError(t, err)
adapter, ok := a.(*gcsAdapter)
require.True(t, ok, "Adapter should be properly casted to *adapter type")
testAdapterOperation(
t,
tc,
"GetDownloadURL",
http.MethodGet,
"",
adapter,
a.GetDownloadURL,
)
testAdapterOperation(
t,
tc,
"GetUploadURL",
http.MethodPut,
"application/octet-stream",
adapter,
a.GetUploadURL,
)
headers := adapter.GetUploadHeaders()
assert.Nil(t, headers)
assert.Nil(t, adapter.GetGoCloudURL())
assert.Empty(t, adapter.GetUploadEnv())
})
}
}
|