Skip to content

Commit f103e8b

Browse files
committed
add test coverage for agentpooladopt controller
Signed-off-by: Troy Connor <[email protected]>
1 parent eb42392 commit f103e8b

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package controllers
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
asocontainerservicev1 "github.com/Azure/azure-service-operator/v2/api/containerservice/v1api20231001"
24+
"github.com/Azure/azure-service-operator/v2/pkg/genruntime"
25+
. "github.com/onsi/gomega"
26+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/apimachinery/pkg/types"
28+
"k8s.io/utils/ptr"
29+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
30+
ctrl "sigs.k8s.io/controller-runtime"
31+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
32+
33+
infrav1alpha "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha1"
34+
)
35+
36+
func TestAgentPoolAdoptController(t *testing.T) {
37+
g := NewWithT(t)
38+
req := ctrl.Request{NamespacedName: types.NamespacedName{Name: "fake-agent-pool", Namespace: "fake-ns"}}
39+
ctx := context.Background()
40+
scheme, err := newScheme()
41+
g.Expect(err).ToNot(HaveOccurred())
42+
43+
agentPool := &asocontainerservicev1.ManagedClustersAgentPool{
44+
ObjectMeta: metav1.ObjectMeta{
45+
Name: "fake-agent-pool",
46+
Namespace: "fake-ns",
47+
Annotations: map[string]string{
48+
adoptAnnotation: adoptAnnotationValue,
49+
},
50+
OwnerReferences: []metav1.OwnerReference{
51+
{
52+
Kind: infrav1alpha.AzureASOManagedMachinePoolKind,
53+
APIVersion: infrav1alpha.GroupVersion.Identifier(),
54+
},
55+
},
56+
},
57+
Spec: asocontainerservicev1.ManagedClustersAgentPool_Spec{
58+
Count: ptr.To(1),
59+
Owner: &genruntime.KnownResourceReference{
60+
Name: "fake-managed-cluster",
61+
},
62+
},
63+
}
64+
mc := &asocontainerservicev1.ManagedCluster{
65+
ObjectMeta: metav1.ObjectMeta{
66+
Name: "fake-managed-cluster",
67+
Namespace: "fake-ns",
68+
OwnerReferences: []metav1.OwnerReference{
69+
{
70+
Kind: infrav1alpha.AzureASOManagedControlPlaneKind,
71+
APIVersion: infrav1alpha.GroupVersion.Identifier(),
72+
Name: "fake-managed-cluster",
73+
},
74+
},
75+
},
76+
}
77+
asoManagedControlPlane := &infrav1alpha.AzureASOManagedControlPlane{
78+
ObjectMeta: metav1.ObjectMeta{
79+
Name: "fake-managed-cluster",
80+
Namespace: "fake-ns",
81+
Labels: map[string]string{
82+
clusterv1.ClusterNameLabel: "cluster-name",
83+
},
84+
},
85+
}
86+
87+
err = asocontainerservicev1.AddToScheme(scheme)
88+
g.Expect(err).ToNot(HaveOccurred())
89+
err = infrav1alpha.AddToScheme(scheme)
90+
g.Expect(err).ToNot(HaveOccurred())
91+
client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(agentPool, mc, asoManagedControlPlane).WithStatusSubresource(mc, agentPool, asoManagedControlPlane).Build()
92+
aprec := &AgentPoolAdoptReconciler{
93+
Client: client,
94+
}
95+
_, err = aprec.Reconcile(ctx, req)
96+
g.Expect(err).ToNot(HaveOccurred())
97+
}

controllers/azurejson_machinepool_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ func TestAzureJSONPoolReconcilerUserAssignedIdentities(t *testing.T) {
382382
CredentialCache: azure.NewCredentialCache(),
383383
}
384384
id := "azure:///subscriptions/123/resourceGroups/test-rg/providers/Microsoft.ManagedIdentity/userAssignedIdentities/fake-provider-id"
385-
getClient = func(auth azure.Authorizer) (identities.Client, error) {
385+
getClient = func(_ azure.Authorizer) (identities.Client, error) {
386386
mockClient := mock_identities.NewMockClient(ctrlr)
387387
mockClient.EXPECT().GetClientID(gomock.Any(), gomock.Any()).Return(id, nil)
388388
return mockClient, nil

0 commit comments

Comments
 (0)