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