From 27eaa1b5bcbe3b84dce49f3b63787cbf09964c55 Mon Sep 17 00:00:00 2001 From: Alper Rifat Ulucinar Date: Fri, 25 Mar 2022 10:23:34 +0300 Subject: [PATCH] Fix tests Signed-off-by: Alper Rifat Ulucinar --- apis/compute/v1alpha3/types.go | 2 + pkg/clients/azure.go | 31 +++--- pkg/clients/compute/aks.go | 2 +- pkg/clients/compute/fake/fake.go | 8 +- pkg/controller/compute/managed.go | 10 +- pkg/controller/compute/managed_test.go | 140 ++----------------------- 6 files changed, 30 insertions(+), 163 deletions(-) diff --git a/apis/compute/v1alpha3/types.go b/apis/compute/v1alpha3/types.go index ac57ea2e..60690657 100644 --- a/apis/compute/v1alpha3/types.go +++ b/apis/compute/v1alpha3/types.go @@ -87,6 +87,8 @@ type AKSClusterParameters struct { Identity Identity `json:"identity"` } +// Identity represents a system-assigned or user-assigned managed identities +// for the control-plane of the AKS cluster. type Identity struct { // Type specifies the type of the managed identity to be used by // the control-plane. Allowed values are: `SystemAssigned` or diff --git a/pkg/clients/azure.go b/pkg/clients/azure.go index 980a99c3..b61e3372 100644 --- a/pkg/clients/azure.go +++ b/pkg/clients/azure.go @@ -32,12 +32,13 @@ import ( "github.com/Azure/go-autorest/autorest/azure" "github.com/Azure/go-autorest/autorest/azure/auth" "github.com/Azure/go-autorest/autorest/to" - xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" + xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" + "github.com/crossplane/crossplane-runtime/pkg/resource" "github.com/crossplane/provider-azure/apis/v1alpha3" @@ -86,16 +87,12 @@ const ( // Credentials Secret content is a json whose keys are below. const ( - CredentialsKeyClientID = "clientId" - CredentialsKeyClientSecret = "clientSecret" - CredentialsKeyTenantID = "tenantId" - CredentialsKeySubscriptionID = "subscriptionId" - CredentialsKeyActiveDirectoryEndpointURL = "activeDirectoryEndpointUrl" - CredentialsKeyResourceManagerEndpointURL = "resourceManagerEndpointUrl" - CredentialsKeyActiveDirectoryGraphResourceID = "activeDirectoryGraphResourceId" - CredentialsKeySQLManagementEndpointURL = "sqlManagementEndpointUrl" - CredentialsKeyGalleryEndpointURL = "galleryEndpointUrl" - CredentialsManagementEndpointURL = "managementEndpointUrl" + CredentialsKeyClientID = "clientId" + CredentialsKeyClientSecret = "clientSecret" + CredentialsKeyTenantID = "tenantId" + CredentialsKeySubscriptionID = "subscriptionId" + CredentialsKeyActiveDirectoryEndpointURL = "activeDirectoryEndpointUrl" + CredentialsKeyResourceManagerEndpointURL = "resourceManagerEndpointUrl" ) // 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 // UseProviderConfig to return the necessary information to construct an Azure // client. -func UseProviderConfig(ctx context.Context, c client.Client, mg resource.Managed) (string, autorest.Authorizer, error) { +func UseProviderConfig(ctx context.Context, c client.Client, mg resource.Managed) (string, autorest.Authorizer, error) { //nolint:gocyclo pc := &v1beta1.ProviderConfig{} t := resource.NewProviderConfigUsageTracker(c, &v1beta1.ProviderConfigUsage{}) if err := t.Track(ctx, mg); err != nil { @@ -147,10 +144,10 @@ func UseProviderConfig(ctx context.Context, c client.Client, mg resource.Managed var authorizer autorest.Authorizer var err error subscriptionID := "" - switch pc.Spec.Credentials.Source { + switch pc.Spec.Credentials.Source { //nolint:exhaustive case xpv1.CredentialsSourceSecret: - m, err := getCredentialsMap(ctx, pc, c) - if err != nil { + m, mErr := getCredentialsMap(ctx, pc, c) + if mErr != nil { return "", nil, err } subscriptionID = m[CredentialsKeySubscriptionID] @@ -373,7 +370,7 @@ func ToInt32Ptr(i int, o ...FieldOption) *int32 { } // ToInt32PtrFromIntPtr converts the supplied int pointer for use with the Azure Go SDK. -func ToInt32PtrFromIntPtr(i *int, o ...FieldOption) *int32 { +func ToInt32PtrFromIntPtr(i *int, _ ...FieldOption) *int32 { if i == nil { return nil } @@ -559,5 +556,5 @@ func endpointToScope(endpoint string) string { if endpoint[len(endpoint)-1] != '/' { endpoint += "/" } - return string(endpoint) + defaultScope + return endpoint + defaultScope } diff --git a/pkg/clients/compute/aks.go b/pkg/clients/compute/aks.go index 92c575cc..230a78d1 100644 --- a/pkg/clients/compute/aks.go +++ b/pkg/clients/compute/aks.go @@ -135,7 +135,7 @@ func newManagedCluster(c *v1alpha3.AKSCluster, subscriptionID string) (container EnableRBAC: to.BoolPtr(!c.Spec.DisableRBAC), }, } - switch containerservice.ResourceIdentityType(c.Spec.Identity.Type) { + switch containerservice.ResourceIdentityType(c.Spec.Identity.Type) { //nolint:exhaustive case containerservice.ResourceIdentityTypeSystemAssigned: p.Identity.Type = containerservice.ResourceIdentityTypeSystemAssigned case containerservice.ResourceIdentityTypeUserAssigned: diff --git a/pkg/clients/compute/fake/fake.go b/pkg/clients/compute/fake/fake.go index 5f23a694..27aed03a 100644 --- a/pkg/clients/compute/fake/fake.go +++ b/pkg/clients/compute/fake/fake.go @@ -19,7 +19,7 @@ package fake import ( "context" - "github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2018-03-31/containerservice" + "github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2022-01-01/containerservice" "github.com/crossplane/provider-azure/apis/compute/v1alpha3" ) @@ -27,7 +27,7 @@ import ( // AKSClient is a fake AKS client. type AKSClient struct { MockGetManagedCluster func(ctx context.Context, ac *v1alpha3.AKSCluster) (containerservice.ManagedCluster, error) - MockEnsureManagedCluster func(ctx context.Context, ac *v1alpha3.AKSCluster, secret string) error + MockEnsureManagedCluster func(ctx context.Context, ac *v1alpha3.AKSCluster) error MockDeleteManagedCluster func(ctx context.Context, ac *v1alpha3.AKSCluster) error MockGetKubeConfig func(ctx context.Context, ac *v1alpha3.AKSCluster) ([]byte, error) } @@ -38,8 +38,8 @@ func (c AKSClient) GetManagedCluster(ctx context.Context, ac *v1alpha3.AKSCluste } // EnsureManagedCluster calls MockEnsureManagedCluster. -func (c AKSClient) EnsureManagedCluster(ctx context.Context, ac *v1alpha3.AKSCluster, secret string) error { - return c.MockEnsureManagedCluster(ctx, ac, secret) +func (c AKSClient) EnsureManagedCluster(ctx context.Context, ac *v1alpha3.AKSCluster) error { + return c.MockEnsureManagedCluster(ctx, ac) } // DeleteManagedCluster calls DeleteManagedCluster. diff --git a/pkg/controller/compute/managed.go b/pkg/controller/compute/managed.go index f6f666ec..5436619c 100644 --- a/pkg/controller/compute/managed.go +++ b/pkg/controller/compute/managed.go @@ -32,7 +32,6 @@ import ( "github.com/crossplane/crossplane-runtime/pkg/event" "github.com/crossplane/crossplane-runtime/pkg/logging" "github.com/crossplane/crossplane-runtime/pkg/meta" - "github.com/crossplane/crossplane-runtime/pkg/password" "github.com/crossplane/crossplane-runtime/pkg/ratelimiter" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/pkg/resource" @@ -44,13 +43,11 @@ import ( // Error strings. const ( - errGenPassword = "cannot generate service principal secret" errNotAKSCluster = "managed resource is not a AKSCluster" errCreateAKSCluster = "cannot create AKSCluster" errGetAKSCluster = "cannot get AKSCluster" errGetKubeConfig = "cannot get AKSCluster kubeconfig" errDeleteAKSCluster = "cannot delete AKSCluster" - errGetConnSecret = "cannot get connection secret" ) // SetupAKSCluster adds a controller that reconciles AKSClusters. @@ -85,13 +82,12 @@ func (c *connecter) Connect(ctx context.Context, mg resource.Managed) (managed.E if err != nil { return nil, err } - return &external{kube: c.client, client: cl, newPasswordFn: password.Generate}, nil + return &external{kube: c.client, client: cl}, nil } type external struct { - kube client.Client - client compute.AKSClient - newPasswordFn func() (password string, err error) + kube client.Client + client compute.AKSClient } func (e *external) Observe(ctx context.Context, mg resource.Managed) (managed.ExternalObservation, error) { diff --git a/pkg/controller/compute/managed_test.go b/pkg/controller/compute/managed_test.go index 21427a6b..49949546 100644 --- a/pkg/controller/compute/managed_test.go +++ b/pkg/controller/compute/managed_test.go @@ -21,14 +21,11 @@ import ( "net/http" "testing" - "github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2018-03-31/containerservice" + "github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2022-01-01/containerservice" "github.com/Azure/go-autorest/autorest" "github.com/Azure/go-autorest/autorest/to" - xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" - v1 "k8s.io/api/core/v1" - "sigs.k8s.io/controller-runtime/pkg/client" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/pkg/resource" @@ -38,11 +35,6 @@ import ( "github.com/crossplane/provider-azure/pkg/clients/compute/fake" ) -const ( - testPasswd = "pass123" - testExistingSecret = "existingSecret" -) - type modifier func(*v1alpha3.AKSCluster) func withState(state string) modifier { @@ -63,12 +55,6 @@ func withEndpoint(ep string) modifier { } } -func withConnectionSecretRef(ref *xpv1.SecretReference) modifier { - return func(c *v1alpha3.AKSCluster) { - c.Spec.WriteConnectionSecretToReference = ref - } -} - func aksCluster(m ...modifier) *v1alpha3.AKSCluster { ac := &v1alpha3.AKSCluster{} @@ -84,7 +70,7 @@ func TestObserve(t *testing.T) { id := "koolAD" stateSucceeded := "Succeeded" stateWat := "Wat" - endpoint := "http://wat.example.org" + endpoint := "https://wat.example.org" type args struct { ctx context.Context @@ -241,23 +227,10 @@ func TestCreate(t *testing.T) { err: errors.New(errNotAKSCluster), }, }, - "ErrGeneratePassword": { - e: &external{ - newPasswordFn: func() (string, error) { return "", errBoom }, - }, - args: args{ - ctx: context.Background(), - mg: aksCluster(), - }, - want: want{ - err: errors.Wrap(errBoom, errGenPassword), - }, - }, "ErrEnsureCluster": { e: &external{ - newPasswordFn: func() (string, error) { return "", nil }, client: fake.AKSClient{ - MockEnsureManagedCluster: func(_ context.Context, _ *v1alpha3.AKSCluster, _ string) error { + MockEnsureManagedCluster: func(_ context.Context, _ *v1alpha3.AKSCluster) error { return errBoom }, }, @@ -268,18 +241,13 @@ func TestCreate(t *testing.T) { }, want: want{ err: errors.Wrap(errBoom, errCreateAKSCluster), - ec: managed.ExternalCreation{ - ConnectionDetails: map[string][]byte{ - "password": {}, - }, - }, + ec: managed.ExternalCreation{}, }, }, "SuccessEnsureCluster": { e: &external{ - newPasswordFn: func() (string, error) { return testPasswd, nil }, client: fake.AKSClient{ - MockEnsureManagedCluster: func(_ context.Context, _ *v1alpha3.AKSCluster, _ string) error { + MockEnsureManagedCluster: func(_ context.Context, _ *v1alpha3.AKSCluster) error { return nil }, }, @@ -289,102 +257,7 @@ func TestCreate(t *testing.T) { mg: aksCluster(), }, want: want{ - ec: managed.ExternalCreation{ - ConnectionDetails: map[string][]byte{ - "password": []byte(testPasswd), - }, - }, - }, - }, - "SuccessExistingEmptyAppSecret": { - e: &external{ - newPasswordFn: func() (string, error) { return testPasswd, nil }, - client: fake.AKSClient{ - MockEnsureManagedCluster: func(_ context.Context, _ *v1alpha3.AKSCluster, _ string) error { - return nil - }, - }, - kube: &test.MockClient{ - MockGet: func(_ context.Context, _ client.ObjectKey, o client.Object) error { - s, ok := o.(*v1.Secret) - if !ok { - t.Fatalf("not a *v1.Secret") - } - s.Data = map[string][]byte{"password": {}} - return nil - }, - }, - }, - args: args{ - ctx: context.Background(), - mg: aksCluster(withConnectionSecretRef(&xpv1.SecretReference{ - Name: "test-secret", - Namespace: "test-ns", - })), - }, - want: want{ - ec: managed.ExternalCreation{ - ConnectionDetails: map[string][]byte{ - "password": []byte(testPasswd), - }, - }, - }, - }, - "SuccessExistingNonEmptyAppSecret": { - e: &external{ - newPasswordFn: func() (string, error) { return testPasswd, nil }, - client: fake.AKSClient{ - MockEnsureManagedCluster: func(_ context.Context, _ *v1alpha3.AKSCluster, _ string) error { - return nil - }, - }, - kube: &test.MockClient{ - MockGet: func(_ context.Context, _ client.ObjectKey, o client.Object) error { - s, ok := o.(*v1.Secret) - if !ok { - t.Fatalf("not a *v1.Secret") - } - s.Data = map[string][]byte{"password": []byte(testExistingSecret)} - return nil - }, - }, - }, - args: args{ - ctx: context.Background(), - mg: aksCluster(withConnectionSecretRef(&xpv1.SecretReference{ - Name: "test-secret", - Namespace: "test-ns", - })), - }, - want: want{ - ec: managed.ExternalCreation{ - ConnectionDetails: map[string][]byte{ - "password": []byte(testExistingSecret), - }, - }, - }, - }, - "ErrExistingAppSecret": { - e: &external{ - newPasswordFn: func() (string, error) { return testPasswd, nil }, - client: fake.AKSClient{ - MockEnsureManagedCluster: func(_ context.Context, _ *v1alpha3.AKSCluster, _ string) error { - return nil - }, - }, - kube: &test.MockClient{ - MockGet: test.NewMockGetFn(errBoom), - }, - }, - args: args{ - ctx: context.Background(), - mg: aksCluster(withConnectionSecretRef(&xpv1.SecretReference{ - Name: "test-secret", - Namespace: "test-ns", - })), - }, - want: want{ - err: errors.Wrap(errBoom, errGetConnSecret), + ec: managed.ExternalCreation{}, }, }, } @@ -425,7 +298,6 @@ func TestDelete(t *testing.T) { }, "ErrDeleteCluster": { e: &external{ - newPasswordFn: func() (string, error) { return "", nil }, client: fake.AKSClient{ MockDeleteManagedCluster: func(_ context.Context, _ *v1alpha3.AKSCluster) error { return errBoom