Skip to content
This repository was archived by the owner on May 22, 2025. It is now read-only.

Commit 27eaa1b

Browse files
committed
Fix tests
Signed-off-by: Alper Rifat Ulucinar <ulucinar@users.noreply.github.com>
1 parent 081d584 commit 27eaa1b

6 files changed

Lines changed: 30 additions & 163 deletions

File tree

apis/compute/v1alpha3/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ type AKSClusterParameters struct {
8787
Identity Identity `json:"identity"`
8888
}
8989

90+
// Identity represents a system-assigned or user-assigned managed identities
91+
// for the control-plane of the AKS cluster.
9092
type Identity struct {
9193
// Type specifies the type of the managed identity to be used by
9294
// the control-plane. Allowed values are: `SystemAssigned` or

pkg/clients/azure.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ import (
3232
"github.com/Azure/go-autorest/autorest/azure"
3333
"github.com/Azure/go-autorest/autorest/azure/auth"
3434
"github.com/Azure/go-autorest/autorest/to"
35-
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
3635
"github.com/pkg/errors"
3736
corev1 "k8s.io/api/core/v1"
3837
"k8s.io/apimachinery/pkg/types"
3938
"sigs.k8s.io/controller-runtime/pkg/client"
4039

40+
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
41+
4142
"github.com/crossplane/crossplane-runtime/pkg/resource"
4243

4344
"github.com/crossplane/provider-azure/apis/v1alpha3"
@@ -86,16 +87,12 @@ const (
8687

8788
// Credentials Secret content is a json whose keys are below.
8889
const (
89-
CredentialsKeyClientID = "clientId"
90-
CredentialsKeyClientSecret = "clientSecret"
91-
CredentialsKeyTenantID = "tenantId"
92-
CredentialsKeySubscriptionID = "subscriptionId"
93-
CredentialsKeyActiveDirectoryEndpointURL = "activeDirectoryEndpointUrl"
94-
CredentialsKeyResourceManagerEndpointURL = "resourceManagerEndpointUrl"
95-
CredentialsKeyActiveDirectoryGraphResourceID = "activeDirectoryGraphResourceId"
96-
CredentialsKeySQLManagementEndpointURL = "sqlManagementEndpointUrl"
97-
CredentialsKeyGalleryEndpointURL = "galleryEndpointUrl"
98-
CredentialsManagementEndpointURL = "managementEndpointUrl"
90+
CredentialsKeyClientID = "clientId"
91+
CredentialsKeyClientSecret = "clientSecret"
92+
CredentialsKeyTenantID = "tenantId"
93+
CredentialsKeySubscriptionID = "subscriptionId"
94+
CredentialsKeyActiveDirectoryEndpointURL = "activeDirectoryEndpointUrl"
95+
CredentialsKeyResourceManagerEndpointURL = "resourceManagerEndpointUrl"
9996
)
10097

10198
// GetAuthInfo figures out how to connect to Azure API and returns the necessary
@@ -134,7 +131,7 @@ func UseProvider(ctx context.Context, c client.Client, mg resource.Managed) (str
134131

135132
// UseProviderConfig to return the necessary information to construct an Azure
136133
// client.
137-
func UseProviderConfig(ctx context.Context, c client.Client, mg resource.Managed) (string, autorest.Authorizer, error) {
134+
func UseProviderConfig(ctx context.Context, c client.Client, mg resource.Managed) (string, autorest.Authorizer, error) { //nolint:gocyclo
138135
pc := &v1beta1.ProviderConfig{}
139136
t := resource.NewProviderConfigUsageTracker(c, &v1beta1.ProviderConfigUsage{})
140137
if err := t.Track(ctx, mg); err != nil {
@@ -147,10 +144,10 @@ func UseProviderConfig(ctx context.Context, c client.Client, mg resource.Managed
147144
var authorizer autorest.Authorizer
148145
var err error
149146
subscriptionID := ""
150-
switch pc.Spec.Credentials.Source {
147+
switch pc.Spec.Credentials.Source { //nolint:exhaustive
151148
case xpv1.CredentialsSourceSecret:
152-
m, err := getCredentialsMap(ctx, pc, c)
153-
if err != nil {
149+
m, mErr := getCredentialsMap(ctx, pc, c)
150+
if mErr != nil {
154151
return "", nil, err
155152
}
156153
subscriptionID = m[CredentialsKeySubscriptionID]
@@ -373,7 +370,7 @@ func ToInt32Ptr(i int, o ...FieldOption) *int32 {
373370
}
374371

375372
// ToInt32PtrFromIntPtr converts the supplied int pointer for use with the Azure Go SDK.
376-
func ToInt32PtrFromIntPtr(i *int, o ...FieldOption) *int32 {
373+
func ToInt32PtrFromIntPtr(i *int, _ ...FieldOption) *int32 {
377374
if i == nil {
378375
return nil
379376
}
@@ -559,5 +556,5 @@ func endpointToScope(endpoint string) string {
559556
if endpoint[len(endpoint)-1] != '/' {
560557
endpoint += "/"
561558
}
562-
return string(endpoint) + defaultScope
559+
return endpoint + defaultScope
563560
}

pkg/clients/compute/aks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func newManagedCluster(c *v1alpha3.AKSCluster, subscriptionID string) (container
135135
EnableRBAC: to.BoolPtr(!c.Spec.DisableRBAC),
136136
},
137137
}
138-
switch containerservice.ResourceIdentityType(c.Spec.Identity.Type) {
138+
switch containerservice.ResourceIdentityType(c.Spec.Identity.Type) { //nolint:exhaustive
139139
case containerservice.ResourceIdentityTypeSystemAssigned:
140140
p.Identity.Type = containerservice.ResourceIdentityTypeSystemAssigned
141141
case containerservice.ResourceIdentityTypeUserAssigned:

pkg/clients/compute/fake/fake.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ package fake
1919
import (
2020
"context"
2121

22-
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2018-03-31/containerservice"
22+
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2022-01-01/containerservice"
2323

2424
"github.com/crossplane/provider-azure/apis/compute/v1alpha3"
2525
)
2626

2727
// AKSClient is a fake AKS client.
2828
type AKSClient struct {
2929
MockGetManagedCluster func(ctx context.Context, ac *v1alpha3.AKSCluster) (containerservice.ManagedCluster, error)
30-
MockEnsureManagedCluster func(ctx context.Context, ac *v1alpha3.AKSCluster, secret string) error
30+
MockEnsureManagedCluster func(ctx context.Context, ac *v1alpha3.AKSCluster) error
3131
MockDeleteManagedCluster func(ctx context.Context, ac *v1alpha3.AKSCluster) error
3232
MockGetKubeConfig func(ctx context.Context, ac *v1alpha3.AKSCluster) ([]byte, error)
3333
}
@@ -38,8 +38,8 @@ func (c AKSClient) GetManagedCluster(ctx context.Context, ac *v1alpha3.AKSCluste
3838
}
3939

4040
// EnsureManagedCluster calls MockEnsureManagedCluster.
41-
func (c AKSClient) EnsureManagedCluster(ctx context.Context, ac *v1alpha3.AKSCluster, secret string) error {
42-
return c.MockEnsureManagedCluster(ctx, ac, secret)
41+
func (c AKSClient) EnsureManagedCluster(ctx context.Context, ac *v1alpha3.AKSCluster) error {
42+
return c.MockEnsureManagedCluster(ctx, ac)
4343
}
4444

4545
// DeleteManagedCluster calls DeleteManagedCluster.

pkg/controller/compute/managed.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"github.com/crossplane/crossplane-runtime/pkg/event"
3333
"github.com/crossplane/crossplane-runtime/pkg/logging"
3434
"github.com/crossplane/crossplane-runtime/pkg/meta"
35-
"github.com/crossplane/crossplane-runtime/pkg/password"
3635
"github.com/crossplane/crossplane-runtime/pkg/ratelimiter"
3736
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
3837
"github.com/crossplane/crossplane-runtime/pkg/resource"
@@ -44,13 +43,11 @@ import (
4443

4544
// Error strings.
4645
const (
47-
errGenPassword = "cannot generate service principal secret"
4846
errNotAKSCluster = "managed resource is not a AKSCluster"
4947
errCreateAKSCluster = "cannot create AKSCluster"
5048
errGetAKSCluster = "cannot get AKSCluster"
5149
errGetKubeConfig = "cannot get AKSCluster kubeconfig"
5250
errDeleteAKSCluster = "cannot delete AKSCluster"
53-
errGetConnSecret = "cannot get connection secret"
5451
)
5552

5653
// SetupAKSCluster adds a controller that reconciles AKSClusters.
@@ -85,13 +82,12 @@ func (c *connecter) Connect(ctx context.Context, mg resource.Managed) (managed.E
8582
if err != nil {
8683
return nil, err
8784
}
88-
return &external{kube: c.client, client: cl, newPasswordFn: password.Generate}, nil
85+
return &external{kube: c.client, client: cl}, nil
8986
}
9087

9188
type external struct {
92-
kube client.Client
93-
client compute.AKSClient
94-
newPasswordFn func() (password string, err error)
89+
kube client.Client
90+
client compute.AKSClient
9591
}
9692

9793
func (e *external) Observe(ctx context.Context, mg resource.Managed) (managed.ExternalObservation, error) {

pkg/controller/compute/managed_test.go

Lines changed: 6 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,11 @@ import (
2121
"net/http"
2222
"testing"
2323

24-
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2018-03-31/containerservice"
24+
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2022-01-01/containerservice"
2525
"github.com/Azure/go-autorest/autorest"
2626
"github.com/Azure/go-autorest/autorest/to"
27-
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
2827
"github.com/google/go-cmp/cmp"
2928
"github.com/pkg/errors"
30-
v1 "k8s.io/api/core/v1"
31-
"sigs.k8s.io/controller-runtime/pkg/client"
3229

3330
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
3431
"github.com/crossplane/crossplane-runtime/pkg/resource"
@@ -38,11 +35,6 @@ import (
3835
"github.com/crossplane/provider-azure/pkg/clients/compute/fake"
3936
)
4037

41-
const (
42-
testPasswd = "pass123"
43-
testExistingSecret = "existingSecret"
44-
)
45-
4638
type modifier func(*v1alpha3.AKSCluster)
4739

4840
func withState(state string) modifier {
@@ -63,12 +55,6 @@ func withEndpoint(ep string) modifier {
6355
}
6456
}
6557

66-
func withConnectionSecretRef(ref *xpv1.SecretReference) modifier {
67-
return func(c *v1alpha3.AKSCluster) {
68-
c.Spec.WriteConnectionSecretToReference = ref
69-
}
70-
}
71-
7258
func aksCluster(m ...modifier) *v1alpha3.AKSCluster {
7359
ac := &v1alpha3.AKSCluster{}
7460

@@ -84,7 +70,7 @@ func TestObserve(t *testing.T) {
8470
id := "koolAD"
8571
stateSucceeded := "Succeeded"
8672
stateWat := "Wat"
87-
endpoint := "http://wat.example.org"
73+
endpoint := "https://wat.example.org"
8874

8975
type args struct {
9076
ctx context.Context
@@ -241,23 +227,10 @@ func TestCreate(t *testing.T) {
241227
err: errors.New(errNotAKSCluster),
242228
},
243229
},
244-
"ErrGeneratePassword": {
245-
e: &external{
246-
newPasswordFn: func() (string, error) { return "", errBoom },
247-
},
248-
args: args{
249-
ctx: context.Background(),
250-
mg: aksCluster(),
251-
},
252-
want: want{
253-
err: errors.Wrap(errBoom, errGenPassword),
254-
},
255-
},
256230
"ErrEnsureCluster": {
257231
e: &external{
258-
newPasswordFn: func() (string, error) { return "", nil },
259232
client: fake.AKSClient{
260-
MockEnsureManagedCluster: func(_ context.Context, _ *v1alpha3.AKSCluster, _ string) error {
233+
MockEnsureManagedCluster: func(_ context.Context, _ *v1alpha3.AKSCluster) error {
261234
return errBoom
262235
},
263236
},
@@ -268,18 +241,13 @@ func TestCreate(t *testing.T) {
268241
},
269242
want: want{
270243
err: errors.Wrap(errBoom, errCreateAKSCluster),
271-
ec: managed.ExternalCreation{
272-
ConnectionDetails: map[string][]byte{
273-
"password": {},
274-
},
275-
},
244+
ec: managed.ExternalCreation{},
276245
},
277246
},
278247
"SuccessEnsureCluster": {
279248
e: &external{
280-
newPasswordFn: func() (string, error) { return testPasswd, nil },
281249
client: fake.AKSClient{
282-
MockEnsureManagedCluster: func(_ context.Context, _ *v1alpha3.AKSCluster, _ string) error {
250+
MockEnsureManagedCluster: func(_ context.Context, _ *v1alpha3.AKSCluster) error {
283251
return nil
284252
},
285253
},
@@ -289,102 +257,7 @@ func TestCreate(t *testing.T) {
289257
mg: aksCluster(),
290258
},
291259
want: want{
292-
ec: managed.ExternalCreation{
293-
ConnectionDetails: map[string][]byte{
294-
"password": []byte(testPasswd),
295-
},
296-
},
297-
},
298-
},
299-
"SuccessExistingEmptyAppSecret": {
300-
e: &external{
301-
newPasswordFn: func() (string, error) { return testPasswd, nil },
302-
client: fake.AKSClient{
303-
MockEnsureManagedCluster: func(_ context.Context, _ *v1alpha3.AKSCluster, _ string) error {
304-
return nil
305-
},
306-
},
307-
kube: &test.MockClient{
308-
MockGet: func(_ context.Context, _ client.ObjectKey, o client.Object) error {
309-
s, ok := o.(*v1.Secret)
310-
if !ok {
311-
t.Fatalf("not a *v1.Secret")
312-
}
313-
s.Data = map[string][]byte{"password": {}}
314-
return nil
315-
},
316-
},
317-
},
318-
args: args{
319-
ctx: context.Background(),
320-
mg: aksCluster(withConnectionSecretRef(&xpv1.SecretReference{
321-
Name: "test-secret",
322-
Namespace: "test-ns",
323-
})),
324-
},
325-
want: want{
326-
ec: managed.ExternalCreation{
327-
ConnectionDetails: map[string][]byte{
328-
"password": []byte(testPasswd),
329-
},
330-
},
331-
},
332-
},
333-
"SuccessExistingNonEmptyAppSecret": {
334-
e: &external{
335-
newPasswordFn: func() (string, error) { return testPasswd, nil },
336-
client: fake.AKSClient{
337-
MockEnsureManagedCluster: func(_ context.Context, _ *v1alpha3.AKSCluster, _ string) error {
338-
return nil
339-
},
340-
},
341-
kube: &test.MockClient{
342-
MockGet: func(_ context.Context, _ client.ObjectKey, o client.Object) error {
343-
s, ok := o.(*v1.Secret)
344-
if !ok {
345-
t.Fatalf("not a *v1.Secret")
346-
}
347-
s.Data = map[string][]byte{"password": []byte(testExistingSecret)}
348-
return nil
349-
},
350-
},
351-
},
352-
args: args{
353-
ctx: context.Background(),
354-
mg: aksCluster(withConnectionSecretRef(&xpv1.SecretReference{
355-
Name: "test-secret",
356-
Namespace: "test-ns",
357-
})),
358-
},
359-
want: want{
360-
ec: managed.ExternalCreation{
361-
ConnectionDetails: map[string][]byte{
362-
"password": []byte(testExistingSecret),
363-
},
364-
},
365-
},
366-
},
367-
"ErrExistingAppSecret": {
368-
e: &external{
369-
newPasswordFn: func() (string, error) { return testPasswd, nil },
370-
client: fake.AKSClient{
371-
MockEnsureManagedCluster: func(_ context.Context, _ *v1alpha3.AKSCluster, _ string) error {
372-
return nil
373-
},
374-
},
375-
kube: &test.MockClient{
376-
MockGet: test.NewMockGetFn(errBoom),
377-
},
378-
},
379-
args: args{
380-
ctx: context.Background(),
381-
mg: aksCluster(withConnectionSecretRef(&xpv1.SecretReference{
382-
Name: "test-secret",
383-
Namespace: "test-ns",
384-
})),
385-
},
386-
want: want{
387-
err: errors.Wrap(errBoom, errGetConnSecret),
260+
ec: managed.ExternalCreation{},
388261
},
389262
},
390263
}
@@ -425,7 +298,6 @@ func TestDelete(t *testing.T) {
425298
},
426299
"ErrDeleteCluster": {
427300
e: &external{
428-
newPasswordFn: func() (string, error) { return "", nil },
429301
client: fake.AKSClient{
430302
MockDeleteManagedCluster: func(_ context.Context, _ *v1alpha3.AKSCluster) error {
431303
return errBoom

0 commit comments

Comments
 (0)