@@ -14,6 +14,7 @@ import (
14
14
"testing"
15
15
16
16
"github.com/Azure/azure-pipeline-go/pipeline"
17
+ "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
17
18
"github.com/Azure/go-autorest/autorest"
18
19
"github.com/Azure/go-autorest/autorest/mocks"
19
20
"github.com/Azure/go-autorest/autorest/to"
@@ -35,6 +36,32 @@ import (
35
36
"github.com/openshift/cluster-image-registry-operator/pkg/envvar"
36
37
)
37
38
39
+ const mockTenantID = "00000000-0000-0000-0000-000000000000"
40
+
41
+ type testDoer struct {
42
+ response * http.Response
43
+ body string
44
+ statusCode int
45
+ header http.Header
46
+ }
47
+
48
+ // Do implements the Doer interface for mocking.
49
+ // Do accepts the passed policy request and body, then appends the response and emits it.
50
+ func (td * testDoer ) Do (r * policy.Request ) (resp * http.Response , err error ) {
51
+ // Helps in emitting sequential Responses for the same client
52
+ if td .response != nil {
53
+ return r .Next ()
54
+ }
55
+ resp = & http.Response {
56
+ StatusCode : td .statusCode ,
57
+ Request : r .Raw (),
58
+ Body : io .NopCloser (bytes .NewBufferString (td .body )),
59
+ Header : td .header ,
60
+ }
61
+ td .response = resp
62
+ return resp , nil
63
+ }
64
+
38
65
func TestGetConfig (t * testing.T ) {
39
66
for _ , tt := range []struct {
40
67
name string
@@ -261,7 +288,7 @@ func findEnvVar(envvars envvar.List, name string) *envvar.EnvVar {
261
288
return nil
262
289
}
263
290
264
- func TestConfigEnv (t * testing.T ) {
291
+ func TestConfigEnvNonAzureStackHub (t * testing.T ) {
265
292
ctx := context .Background ()
266
293
267
294
cr := & imageregistryv1.Config {}
@@ -290,6 +317,7 @@ func TestConfigEnv(t *testing.T) {
290
317
Data : map [string ][]byte {
291
318
"azure_subscription_id" : []byte ("subscription_id" ),
292
319
"azure_client_id" : []byte ("client_id" ),
320
+ "azure_tenant_id" : []byte (mockTenantID ),
293
321
"azure_client_secret" : []byte ("client_secret" ),
294
322
"azure_resourcegroup" : []byte ("resourcegroup" ),
295
323
},
@@ -305,16 +333,12 @@ func TestConfigEnv(t *testing.T) {
305
333
sender .AppendResponse (mocks .NewResponseWithContent (`{"keys":[{"value":"firstKey"}]}` ))
306
334
sender .AppendResponse (mocks .NewResponseWithContent (`{"keys":[{"value":"firstKey"}]}` ))
307
335
308
- httpSender := pipeline .FactoryFunc (func (next pipeline.Policy , po * pipeline.PolicyOptions ) pipeline.PolicyFunc {
309
- return func (ctx context.Context , request pipeline.Request ) (pipeline.Response , error ) {
310
- return pipeline .NewHTTPResponse (mocks .NewResponseWithContent (`{}` )), nil
311
- }
312
- })
313
-
314
336
d := NewDriver (ctx , config , & listers .StorageListers )
315
337
d .authorizer = authorizer
316
338
d .sender = sender
317
- d .httpSender = httpSender
339
+ d .policies = []policy.Policy {
340
+ & testDoer {statusCode : http .StatusCreated },
341
+ }
318
342
err := d .CreateStorage (cr )
319
343
if err != nil {
320
344
t .Fatal (err )
@@ -340,7 +364,7 @@ func TestConfigEnv(t *testing.T) {
340
364
}
341
365
}
342
366
343
- func TestConfigEnvWorkloadIdentity (t * testing.T ) {
367
+ func TestConfigEnvWorkloadIdentityNonAzureStackHub (t * testing.T ) {
344
368
ctx := context .Background ()
345
369
346
370
config := & imageregistryv1.ImageRegistryConfigStorageAzure {}
@@ -383,16 +407,15 @@ func TestConfigEnvWorkloadIdentity(t *testing.T) {
383
407
sender .AppendResponse (mocks .NewResponseWithContent (`{"name":"account"}` ))
384
408
sender .AppendResponse (mocks .NewResponseWithContent (`{"keys":[{"value":"firstKey"}]}` ))
385
409
sender .AppendResponse (mocks .NewResponseWithContent (`{"keys":[{"value":"firstKey"}]}` ))
386
- httpSender := pipeline .FactoryFunc (func (next pipeline.Policy , po * pipeline.PolicyOptions ) pipeline.PolicyFunc {
387
- return func (ctx context.Context , request pipeline.Request ) (pipeline.Response , error ) {
388
- return pipeline .NewHTTPResponse (mocks .NewResponseWithContent (`{}` )), nil
389
- }
390
- })
391
410
392
411
d := NewDriver (ctx , config , & listers .StorageListers )
393
412
d .authorizer = authorizer
394
413
d .sender = sender
395
- d .httpSender = httpSender
414
+ d .policies = []policy.Policy {
415
+ & testDoer {
416
+ statusCode : http .StatusAccepted ,
417
+ },
418
+ }
396
419
397
420
envvars , err := d .ConfigEnv ()
398
421
if err != nil {
@@ -1157,7 +1180,7 @@ func Test_containerExists(t *testing.T) {
1157
1180
}
1158
1181
}
1159
1182
1160
- func Test_storageManagementState (t * testing.T ) {
1183
+ func Test_storageManagementStateNonAzureStackHub (t * testing.T ) {
1161
1184
builder := cirofake .NewFixturesBuilder ()
1162
1185
builder .AddInfraConfig (& configv1.Infrastructure {
1163
1186
ObjectMeta : metav1.ObjectMeta {
@@ -1181,17 +1204,20 @@ func Test_storageManagementState(t *testing.T) {
1181
1204
Data : map [string ][]byte {
1182
1205
"azure_subscription_id" : []byte ("subscription_id" ),
1183
1206
"azure_client_id" : []byte ("client_id" ),
1207
+ "azure_tenant_id" : []byte (mockTenantID ),
1184
1208
"azure_client_secret" : []byte ("client_secret" ),
1185
1209
"azure_resourcegroup" : []byte ("resourcegroup" ),
1186
1210
},
1187
1211
})
1188
1212
listers := builder .BuildListers ()
1213
+ containerNotFoundHeader := http.Header {}
1214
+ containerNotFoundHeader .Add ("x-ms-error-code" , "ContainerNotFound" )
1189
1215
1190
1216
for _ , tt := range []struct {
1191
1217
name string
1192
1218
registryConfig * imageregistryv1.Config
1193
1219
mockResponses []* http.Response
1194
- httpSender func ( int ) func ( _ context. Context , _ pipeline. Request ) (pipeline. Response , error )
1220
+ policies []policy. Policy
1195
1221
err string
1196
1222
checkFn func (* imageregistryv1.Config )
1197
1223
}{
@@ -1209,6 +1235,11 @@ func Test_storageManagementState(t *testing.T) {
1209
1235
t .Error ("unexpected empty container" )
1210
1236
}
1211
1237
},
1238
+ policies : []policy.Policy {
1239
+ & testDoer {
1240
+ statusCode : http .StatusCreated ,
1241
+ },
1242
+ },
1212
1243
},
1213
1244
{
1214
1245
name : "user providing container and account name (both already exist)" ,
@@ -1237,6 +1268,11 @@ func Test_storageManagementState(t *testing.T) {
1237
1268
t .Errorf ("container has changed to %s" , cr .Spec .Storage .Azure .Container )
1238
1269
}
1239
1270
},
1271
+ policies : []policy.Policy {
1272
+ & testDoer {
1273
+ statusCode : http .StatusOK ,
1274
+ },
1275
+ },
1240
1276
},
1241
1277
{
1242
1278
name : "user providing container and account name (both don't exist)" ,
@@ -1250,19 +1286,6 @@ func Test_storageManagementState(t *testing.T) {
1250
1286
},
1251
1287
},
1252
1288
},
1253
- httpSender : func (req int ) func (_ context.Context , _ pipeline.Request ) (pipeline.Response , error ) {
1254
- if req == 0 {
1255
- return func (_ context.Context , _ pipeline.Request ) (pipeline.Response , error ) {
1256
- r := mocks .NewResponseWithStatus ("" , http .StatusNotFound )
1257
- r .Header = map [string ][]string {}
1258
- r .Header .Add ("x-ms-error-code" , "ContainerNotFound" )
1259
- return pipeline .NewHTTPResponse (r ), nil
1260
- }
1261
- }
1262
- return func (_ context.Context , _ pipeline.Request ) (pipeline.Response , error ) {
1263
- return pipeline .NewHTTPResponse (mocks .NewResponseWithContent (`{}` )), nil
1264
- }
1265
- },
1266
1289
checkFn : func (cr * imageregistryv1.Config ) {
1267
1290
if cr .Spec .Storage .ManagementState != imageregistryv1 .StorageManagementStateManaged {
1268
1291
t .Errorf ("expected to be managed, %q instead" , cr .Spec .Storage .ManagementState )
@@ -1274,6 +1297,15 @@ func Test_storageManagementState(t *testing.T) {
1274
1297
t .Errorf ("container has changed to %s" , cr .Spec .Storage .Azure .Container )
1275
1298
}
1276
1299
},
1300
+ policies : []policy.Policy {
1301
+ & testDoer {
1302
+ statusCode : http .StatusNotFound ,
1303
+ header : containerNotFoundHeader ,
1304
+ },
1305
+ & testDoer {
1306
+ statusCode : http .StatusCreated ,
1307
+ },
1308
+ },
1277
1309
},
1278
1310
{
1279
1311
name : "user providing container and account name (only account name exists)" ,
@@ -1298,23 +1330,19 @@ func Test_storageManagementState(t *testing.T) {
1298
1330
t .Errorf ("container has changed to %s" , cr .Spec .Storage .Azure .Container )
1299
1331
}
1300
1332
},
1301
- httpSender : func (req int ) func (_ context.Context , _ pipeline.Request ) (pipeline.Response , error ) {
1302
- if req == 0 {
1303
- return func (_ context.Context , _ pipeline.Request ) (pipeline.Response , error ) {
1304
- r := mocks .NewResponseWithStatus ("" , http .StatusNotFound )
1305
- r .Header = map [string ][]string {}
1306
- r .Header .Add ("x-ms-error-code" , "ContainerNotFound" )
1307
- return pipeline .NewHTTPResponse (r ), nil
1308
- }
1309
- }
1310
- return func (_ context.Context , _ pipeline.Request ) (pipeline.Response , error ) {
1311
- return pipeline .NewHTTPResponse (mocks .NewResponseWithContent (`{}` )), nil
1312
- }
1313
- },
1314
1333
mockResponses : []* http.Response {
1315
1334
mocks .NewResponseWithContent (`{"nameAvailable":false}` ),
1316
1335
mocks .NewResponseWithContent (`{"keys":[{"value":"firstKey"}]}` ),
1317
1336
},
1337
+ policies : []policy.Policy {
1338
+ & testDoer {
1339
+ statusCode : http .StatusNotFound ,
1340
+ header : containerNotFoundHeader ,
1341
+ },
1342
+ & testDoer {
1343
+ statusCode : http .StatusCreated ,
1344
+ },
1345
+ },
1318
1346
},
1319
1347
{
1320
1348
name : "do not overwrite management state already set by user" ,
@@ -1336,6 +1364,9 @@ func Test_storageManagementState(t *testing.T) {
1336
1364
t .Error ("unexpected empty container" )
1337
1365
}
1338
1366
},
1367
+ policies : []policy.Policy {
1368
+ & testDoer {statusCode : http .StatusCreated },
1369
+ },
1339
1370
},
1340
1371
} {
1341
1372
t .Run (tt .name , func (t * testing.T ) {
@@ -1363,23 +1394,9 @@ func Test_storageManagementState(t *testing.T) {
1363
1394
)
1364
1395
drv .authorizer = autorest.NullAuthorizer {}
1365
1396
drv .sender = sender
1366
-
1367
- var requestCounter int
1368
- drv .httpSender = pipeline .FactoryFunc (
1369
- func (_ pipeline.Policy , _ * pipeline.PolicyOptions ) pipeline.PolicyFunc {
1370
- defer func () {
1371
- requestCounter ++
1372
- }()
1373
-
1374
- if tt .httpSender != nil {
1375
- return tt .httpSender (requestCounter )
1376
- }
1377
-
1378
- return func (_ context.Context , _ pipeline.Request ) (pipeline.Response , error ) {
1379
- return pipeline .NewHTTPResponse (mocks .NewResponseWithContent (`{}` )), nil
1380
- }
1381
- },
1382
- )
1397
+ if tt .policies != nil {
1398
+ drv .policies = tt .policies
1399
+ }
1383
1400
1384
1401
if err := drv .CreateStorage (tt .registryConfig ); err != nil {
1385
1402
if len (tt .err ) == 0 {
0 commit comments