Skip to content

Commit

Permalink
create test to cover path of azureMachinePool.Spec.UserAssignedIdenti…
Browse files Browse the repository at this point in the history
…ties

Signed-off-by: Troy Connor <[email protected]>
  • Loading branch information
troy0820 committed Nov 15, 2023
1 parent fc261f0 commit ce82395
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 7 deletions.
2 changes: 1 addition & 1 deletion azure/services/identities/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type AzureClient struct {
}

// NewClient creates a new MSI client from an authorizer.
func NewClient(auth azure.Authorizer) (*AzureClient, error) {
func NewClient(auth azure.Authorizer) (Client, error) {
opts, err := azure.ARMClientOptions(auth.CloudEnvironment())
if err != nil {
return nil, errors.Wrap(err, "failed to create identities client options")
Expand Down
8 changes: 6 additions & 2 deletions controllers/azurejson_machinepool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"k8s.io/client-go/tools/record"
"k8s.io/utils/ptr"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/identities"
infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/util/reconciler"
Expand Down Expand Up @@ -114,7 +115,6 @@ func (r *AzureJSONMachinePoolReconciler) Reconcile(ctx context.Context, req ctrl
}
return reconcile.Result{}, err
}

// Fetch the CAPI MachinePool.
machinePool, err := GetOwnerMachinePool(ctx, r.Client, azureMachinePool.ObjectMeta)
if err != nil {
Expand Down Expand Up @@ -149,7 +149,7 @@ func (r *AzureJSONMachinePoolReconciler) Reconcile(ctx context.Context, req ctrl
// Construct secret for this machine
userAssignedIdentityIfExists := ""
if len(azureMachinePool.Spec.UserAssignedIdentities) > 0 {
idsClient, err := identities.NewClient(clusterScope)
idsClient, err := getClient(clusterScope)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to create identities client")
}
Expand Down Expand Up @@ -194,3 +194,7 @@ func (r *AzureJSONMachinePoolReconciler) Reconcile(ctx context.Context, req ctrl

return ctrl.Result{}, nil
}

var getClient = func(auth azure.Authorizer) (identities.Client, error) {

Check failure on line 198 in controllers/azurejson_machinepool_controller.go

View workflow job for this annotation

GitHub Actions / coverage

unlambda: replace `func(auth azure.Authorizer) (identities.Client, error) {
return identities.NewClient(auth)
}
152 changes: 148 additions & 4 deletions controllers/azurejson_machinepool_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,24 @@ package controllers

import (
"context"
"os"
"testing"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/go-autorest/autorest/azure/auth"
"github.com/google/go-cmp/cmp"
. "github.com/onsi/gomega"
"go.uber.org/mock/gomock"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/utils/ptr"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/identities"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/identities/mock_identities"
infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
Expand Down Expand Up @@ -178,9 +185,9 @@ func TestAzureJSONPoolReconciler(t *testing.T) {
},
}

os.Setenv(auth.ClientID, "fooClient")
os.Setenv(auth.ClientSecret, "fooSecret")
os.Setenv(auth.TenantID, "fooTenant")
t.Setenv(auth.ClientID, "fooClient")
t.Setenv(auth.ClientSecret, "fooSecret")
t.Setenv(auth.TenantID, "fooTenant")

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
Expand Down Expand Up @@ -210,3 +217,140 @@ func TestAzureJSONPoolReconciler(t *testing.T) {
})
}
}

func TestAzureJSONPoolReconcilerUserAssignedIdentities(t *testing.T) {
g := NewWithT(t)
ctrlr := gomock.NewController(t)
defer ctrlr.Finish()
req := ctrl.Request{NamespacedName: types.NamespacedName{Name: "fake-machine-pool", Namespace: "fake-ns"}}
ctx := context.Background()
scheme := runtime.NewScheme()

err := infrav1exp.AddToScheme(scheme)
g.Expect(err).ToNot(HaveOccurred())

err = expv1.AddToScheme(scheme)
g.Expect(err).ToNot(HaveOccurred())

err = corev1.AddToScheme(scheme)
g.Expect(err).ToNot(HaveOccurred())

err = clusterv1.AddToScheme(scheme)
g.Expect(err).ToNot(HaveOccurred())

err = infrav1.AddToScheme(scheme)
g.Expect(err).ToNot(HaveOccurred())

azureMP := &infrav1exp.AzureMachinePool{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-machine-pool",
Namespace: "fake-ns",
Labels: map[string]string{
clusterv1.ClusterNameLabel: "fake-cluster",
},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: expv1.GroupVersion.Group + "/" + expv1.GroupVersion.Version,
Kind: "MachinePool",
Name: "fake-other-machine-pool",
Controller: to.Ptr(true),
},
},
},
Spec: infrav1exp.AzureMachinePoolSpec{
UserAssignedIdentities: []infrav1.UserAssignedIdentity{
{
ProviderID: "fake-id",
},
},
},
}

clusterA := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-cluster",
Namespace: "fake-ns",
},
Spec: clusterv1.ClusterSpec{
InfrastructureRef: &corev1.ObjectReference{
Kind: "AzureCluster",
Name: "fake-azure-cluster",
Namespace: "fake-ns",
},
},
}

ownerMP := &expv1.MachinePool{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-other-machine-pool",
Namespace: "fake-ns",
Labels: map[string]string{
clusterv1.ClusterNameLabel: "fake-cluster",
},
},
}

azureCluster := &infrav1.AzureCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-azure-cluster",
Namespace: "fake-ns",
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "cluster.x-k8s.io/v1beta1",
Kind: "Cluster",
Name: "my-cluster",
},
},
},
Spec: infrav1.AzureClusterSpec{
AzureClusterClassSpec: infrav1.AzureClusterClassSpec{
SubscriptionID: "123",
},
NetworkSpec: infrav1.NetworkSpec{
Subnets: infrav1.Subnets{
{
SubnetClassSpec: infrav1.SubnetClassSpec{
Name: "node",
Role: infrav1.SubnetNode,
},
},
},
},
},
}
apiVersion, kind := infrav1.GroupVersion.WithKind("AzureMachinePool").ToAPIVersionAndKind()

sec := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: azureMP.Name,
Namespace: "fake-ns",
Labels: map[string]string{
"fake-cluster": string(infrav1.ResourceLifecycleOwned),
},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: apiVersion,
Kind: kind,
Name: azureMP.GetName(),
Controller: ptr.To(true),
},
},
},
}

client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(azureMP, ownerMP, clusterA, azureCluster, sec).Build()
rec := AzureJSONMachinePoolReconciler{
Client: client,
Recorder: record.NewFakeRecorder(42),
ReconcileTimeout: 20 * time.Second,
}
id := "fake-id"
getClient = func(auth azure.Authorizer) (identities.Client, error) {
mockClient := mock_identities.NewMockClient(ctrlr)
mockClient.EXPECT().GetClientID(gomock.Any(), gomock.Any()).Return(id, nil)
return mockClient, nil
}

_, err = rec.Reconcile(ctx, req)
g.Expect(err).ToNot(HaveOccurred())
}

0 comments on commit ce82395

Please sign in to comment.