Skip to content

Commit 73b58d7

Browse files
committed
ARO-9391 Remerge original PR with additional fixes
1 parent 5e20c16 commit 73b58d7

File tree

249 files changed

+56926
-1110
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

249 files changed

+56926
-1110
lines changed

go.sum

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -991,8 +991,8 @@ github.com/phpdave11/gofpdf v1.4.2/go.mod h1:zpO6xFn9yxo3YLyMvW8HcKWVdbNqgIfOOp2
991991
github.com/phpdave11/gofpdi v1.0.12/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI=
992992
github.com/phpdave11/gofpdi v1.0.13/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI=
993993
github.com/pierrec/lz4/v4 v4.1.15/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
994-
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU=
995-
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
994+
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
995+
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
996996
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
997997
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
998998
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -1366,7 +1366,6 @@ golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBc
13661366
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
13671367
golang.org/x/sys v0.0.0-20210603125802-9665404d3644/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
13681368
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1369-
golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
13701369
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
13711370
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
13721371
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

pkg/storage/azure/azure.go

Lines changed: 265 additions & 20 deletions
Large diffs are not rendered by default.

pkg/storage/azure/azure_test.go

Lines changed: 77 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"testing"
1515

1616
"github.com/Azure/azure-pipeline-go/pipeline"
17+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
1718
"github.com/Azure/go-autorest/autorest"
1819
"github.com/Azure/go-autorest/autorest/mocks"
1920
"github.com/google/go-cmp/cmp"
@@ -34,6 +35,32 @@ import (
3435
"github.com/openshift/cluster-image-registry-operator/pkg/envvar"
3536
)
3637

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+
3764
func TestGetConfig(t *testing.T) {
3865
for _, tt := range []struct {
3966
name string
@@ -260,7 +287,7 @@ func findEnvVar(envvars envvar.List, name string) *envvar.EnvVar {
260287
return nil
261288
}
262289

263-
func TestConfigEnv(t *testing.T) {
290+
func TestConfigEnvNonAzureStackHub(t *testing.T) {
264291
ctx := context.Background()
265292

266293
cr := &imageregistryv1.Config{}
@@ -289,6 +316,7 @@ func TestConfigEnv(t *testing.T) {
289316
Data: map[string][]byte{
290317
"azure_subscription_id": []byte("subscription_id"),
291318
"azure_client_id": []byte("client_id"),
319+
"azure_tenant_id": []byte(mockTenantID),
292320
"azure_client_secret": []byte("client_secret"),
293321
"azure_resourcegroup": []byte("resourcegroup"),
294322
},
@@ -304,16 +332,12 @@ func TestConfigEnv(t *testing.T) {
304332
sender.AppendResponse(mocks.NewResponseWithContent(`{"keys":[{"value":"firstKey"}]}`))
305333
sender.AppendResponse(mocks.NewResponseWithContent(`{"keys":[{"value":"firstKey"}]}`))
306334

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-
313335
d := NewDriver(ctx, config, &listers.StorageListers)
314336
d.authorizer = authorizer
315337
d.sender = sender
316-
d.httpSender = httpSender
338+
d.policies = []policy.Policy{
339+
&testDoer{statusCode: http.StatusCreated},
340+
}
317341
err := d.CreateStorage(cr)
318342
if err != nil {
319343
t.Fatal(err)
@@ -339,7 +363,7 @@ func TestConfigEnv(t *testing.T) {
339363
}
340364
}
341365

342-
func TestConfigEnvWorkloadIdentity(t *testing.T) {
366+
func TestConfigEnvWorkloadIdentityNonAzureStackHub(t *testing.T) {
343367
ctx := context.Background()
344368

345369
config := &imageregistryv1.ImageRegistryConfigStorageAzure{}
@@ -382,16 +406,15 @@ func TestConfigEnvWorkloadIdentity(t *testing.T) {
382406
sender.AppendResponse(mocks.NewResponseWithContent(`{"name":"account"}`))
383407
sender.AppendResponse(mocks.NewResponseWithContent(`{"keys":[{"value":"firstKey"}]}`))
384408
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-
})
390409

391410
d := NewDriver(ctx, config, &listers.StorageListers)
392411
d.authorizer = authorizer
393412
d.sender = sender
394-
d.httpSender = httpSender
413+
d.policies = []policy.Policy{
414+
&testDoer{
415+
statusCode: http.StatusAccepted,
416+
},
417+
}
395418

396419
envvars, err := d.ConfigEnv()
397420
if err != nil {
@@ -1154,7 +1177,7 @@ func Test_containerExists(t *testing.T) {
11541177
}
11551178
}
11561179

1157-
func Test_storageManagementState(t *testing.T) {
1180+
func Test_storageManagementStateNonAzureStackHub(t *testing.T) {
11581181
builder := cirofake.NewFixturesBuilder()
11591182
builder.AddInfraConfig(&configv1.Infrastructure{
11601183
ObjectMeta: metav1.ObjectMeta{
@@ -1178,17 +1201,20 @@ func Test_storageManagementState(t *testing.T) {
11781201
Data: map[string][]byte{
11791202
"azure_subscription_id": []byte("subscription_id"),
11801203
"azure_client_id": []byte("client_id"),
1204+
"azure_tenant_id": []byte(mockTenantID),
11811205
"azure_client_secret": []byte("client_secret"),
11821206
"azure_resourcegroup": []byte("resourcegroup"),
11831207
},
11841208
})
11851209
listers := builder.BuildListers()
1210+
containerNotFoundHeader := http.Header{}
1211+
containerNotFoundHeader.Add("x-ms-error-code", "ContainerNotFound")
11861212

11871213
for _, tt := range []struct {
11881214
name string
11891215
registryConfig *imageregistryv1.Config
11901216
mockResponses []*http.Response
1191-
httpSender func(int) func(_ context.Context, _ pipeline.Request) (pipeline.Response, error)
1217+
policies []policy.Policy
11921218
err string
11931219
checkFn func(*imageregistryv1.Config)
11941220
}{
@@ -1206,6 +1232,11 @@ func Test_storageManagementState(t *testing.T) {
12061232
t.Error("unexpected empty container")
12071233
}
12081234
},
1235+
policies: []policy.Policy{
1236+
&testDoer{
1237+
statusCode: http.StatusCreated,
1238+
},
1239+
},
12091240
},
12101241
{
12111242
name: "user providing container and account name (both already exist)",
@@ -1234,6 +1265,11 @@ func Test_storageManagementState(t *testing.T) {
12341265
t.Errorf("container has changed to %s", cr.Spec.Storage.Azure.Container)
12351266
}
12361267
},
1268+
policies: []policy.Policy{
1269+
&testDoer{
1270+
statusCode: http.StatusOK,
1271+
},
1272+
},
12371273
},
12381274
{
12391275
name: "user providing container and account name (both don't exist)",
@@ -1247,19 +1283,6 @@ func Test_storageManagementState(t *testing.T) {
12471283
},
12481284
},
12491285
},
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-
},
12631286
checkFn: func(cr *imageregistryv1.Config) {
12641287
if cr.Spec.Storage.ManagementState != imageregistryv1.StorageManagementStateManaged {
12651288
t.Errorf("expected to be managed, %q instead", cr.Spec.Storage.ManagementState)
@@ -1271,6 +1294,15 @@ func Test_storageManagementState(t *testing.T) {
12711294
t.Errorf("container has changed to %s", cr.Spec.Storage.Azure.Container)
12721295
}
12731296
},
1297+
policies: []policy.Policy{
1298+
&testDoer{
1299+
statusCode: http.StatusNotFound,
1300+
header: containerNotFoundHeader,
1301+
},
1302+
&testDoer{
1303+
statusCode: http.StatusCreated,
1304+
},
1305+
},
12741306
},
12751307
{
12761308
name: "user providing container and account name (only account name exists)",
@@ -1295,23 +1327,19 @@ func Test_storageManagementState(t *testing.T) {
12951327
t.Errorf("container has changed to %s", cr.Spec.Storage.Azure.Container)
12961328
}
12971329
},
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-
},
13111330
mockResponses: []*http.Response{
13121331
mocks.NewResponseWithContent(`{"nameAvailable":false}`),
13131332
mocks.NewResponseWithContent(`{"keys":[{"value":"firstKey"}]}`),
13141333
},
1334+
policies: []policy.Policy{
1335+
&testDoer{
1336+
statusCode: http.StatusNotFound,
1337+
header: containerNotFoundHeader,
1338+
},
1339+
&testDoer{
1340+
statusCode: http.StatusCreated,
1341+
},
1342+
},
13151343
},
13161344
{
13171345
name: "do not overwrite management state already set by user",
@@ -1333,6 +1361,9 @@ func Test_storageManagementState(t *testing.T) {
13331361
t.Error("unexpected empty container")
13341362
}
13351363
},
1364+
policies: []policy.Policy{
1365+
&testDoer{statusCode: http.StatusCreated},
1366+
},
13361367
},
13371368
} {
13381369
t.Run(tt.name, func(t *testing.T) {
@@ -1360,23 +1391,9 @@ func Test_storageManagementState(t *testing.T) {
13601391
)
13611392
drv.authorizer = autorest.NullAuthorizer{}
13621393
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+
}
13801397

13811398
if err := drv.CreateStorage(tt.registryConfig); err != nil {
13821399
if len(tt.err) == 0 {

0 commit comments

Comments
 (0)