Skip to content

Commit 0757c58

Browse files
authored
Merge pull request #4884 from troy0820/troy0820/add-unit-tests-to-amcr
Create reconcile test for azuremanagedcluster_controller
2 parents 81329b8 + 6d0b244 commit 0757c58

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
Copyright 2024 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+
. "github.com/onsi/gomega"
24+
corev1 "k8s.io/api/core/v1"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
"k8s.io/apimachinery/pkg/runtime"
27+
"k8s.io/apimachinery/pkg/types"
28+
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
29+
"sigs.k8s.io/cluster-api-provider-azure/util/reconciler"
30+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
31+
ctrl "sigs.k8s.io/controller-runtime"
32+
"sigs.k8s.io/controller-runtime/pkg/client"
33+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
34+
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
35+
)
36+
37+
func TestAzureManagedClusterController(t *testing.T) {
38+
g := NewWithT(t)
39+
ctx := context.Background()
40+
scheme := runtime.NewScheme()
41+
g.Expect(infrav1.AddToScheme(scheme)).To(Succeed())
42+
g.Expect(clusterv1.AddToScheme(scheme)).To(Succeed())
43+
44+
cluster := &clusterv1.Cluster{
45+
ObjectMeta: metav1.ObjectMeta{
46+
Name: "fake-capi-cluster",
47+
Namespace: "fake-namespace",
48+
},
49+
Spec: clusterv1.ClusterSpec{
50+
ControlPlaneRef: &corev1.ObjectReference{
51+
Name: "fake-control-plane",
52+
},
53+
},
54+
}
55+
cp := &infrav1.AzureManagedControlPlane{
56+
ObjectMeta: metav1.ObjectMeta{
57+
Name: "fake-control-plane",
58+
Namespace: "fake-namespace",
59+
},
60+
Spec: infrav1.AzureManagedControlPlaneSpec{
61+
ControlPlaneEndpoint: clusterv1.APIEndpoint{
62+
Host: "fake-host",
63+
Port: int32(8080),
64+
},
65+
},
66+
}
67+
aksCluster := &infrav1.AzureManagedCluster{
68+
ObjectMeta: metav1.ObjectMeta{
69+
Name: "fake-cluster",
70+
Namespace: "fake-namespace",
71+
},
72+
}
73+
74+
g.Expect(controllerutil.SetOwnerReference(cluster, aksCluster, scheme)).To(Succeed())
75+
fakeclient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(cluster, cp, aksCluster).WithStatusSubresource(cp, cluster, aksCluster).Build()
76+
req := ctrl.Request{
77+
NamespacedName: types.NamespacedName{
78+
Name: "fake-cluster",
79+
Namespace: "fake-namespace"},
80+
}
81+
to := reconciler.Timeouts{}
82+
rec := &AzureManagedClusterReconciler{
83+
Timeouts: to,
84+
Client: fakeclient,
85+
}
86+
87+
_, err := rec.Reconcile(ctx, req)
88+
g.Expect(err).ToNot(HaveOccurred())
89+
g.Expect(rec.Get(ctx, client.ObjectKeyFromObject(aksCluster), aksCluster)).To(Succeed())
90+
g.Expect(aksCluster.Spec.ControlPlaneEndpoint).To(Equal(cp.Spec.ControlPlaneEndpoint))
91+
g.Expect(aksCluster.Status.Ready).To(BeTrue())
92+
}

0 commit comments

Comments
 (0)