Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Alper Rifat Ulucinar <[email protected]>
  • Loading branch information
ulucinar committed Mar 25, 2022
1 parent 081d584 commit 27eaa1b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 163 deletions.
2 changes: 2 additions & 0 deletions apis/compute/v1alpha3/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 14 additions & 17 deletions pkg/clients/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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]
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -559,5 +556,5 @@ func endpointToScope(endpoint string) string {
if endpoint[len(endpoint)-1] != '/' {
endpoint += "/"
}
return string(endpoint) + defaultScope
return endpoint + defaultScope
}
2 changes: 1 addition & 1 deletion pkg/clients/compute/aks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions pkg/clients/compute/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ 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"
)

// 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)
}
Expand All @@ -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.
Expand Down
10 changes: 3 additions & 7 deletions pkg/controller/compute/managed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
140 changes: 6 additions & 134 deletions pkg/controller/compute/managed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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{}

Expand All @@ -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
Expand Down Expand Up @@ -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
},
},
Expand All @@ -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
},
},
Expand All @@ -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{},
},
},
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 27eaa1b

Please sign in to comment.