sigs.k8s.io/cluster-api-provider-azure@v1.14.3/exp/controllers/azuremachinepool_controller_test.go (about) 1 /* 2 Copyright 2020 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/ginkgo/v2" 24 . "github.com/onsi/gomega" 25 corev1 "k8s.io/api/core/v1" 26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 "k8s.io/apimachinery/pkg/runtime" 28 "k8s.io/client-go/tools/record" 29 infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" 30 infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1" 31 "sigs.k8s.io/cluster-api-provider-azure/internal/test" 32 "sigs.k8s.io/cluster-api-provider-azure/util/reconciler" 33 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 34 expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1" 35 ctrl "sigs.k8s.io/controller-runtime" 36 "sigs.k8s.io/controller-runtime/pkg/client" 37 "sigs.k8s.io/controller-runtime/pkg/client/fake" 38 ) 39 40 var _ = Describe("AzureMachinePoolReconciler", func() { 41 BeforeEach(func() {}) 42 AfterEach(func() {}) 43 44 Context("Reconcile an AzureMachinePool", func() { 45 It("should not error with minimal set up", func() { 46 reconciler := NewAzureMachinePoolReconciler(testEnv, testEnv.GetEventRecorderFor("azuremachinepool-reconciler"), 47 reconciler.Timeouts{}, "") 48 By("Calling reconcile") 49 instance := &infrav1exp.AzureMachinePool{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "default"}} 50 result, err := reconciler.Reconcile(context.Background(), ctrl.Request{ 51 NamespacedName: client.ObjectKey{ 52 Namespace: instance.Namespace, 53 Name: instance.Name, 54 }, 55 }) 56 Expect(err).NotTo(HaveOccurred()) 57 Expect(result.RequeueAfter).To(BeZero()) 58 }) 59 }) 60 }) 61 62 func TestAzureMachinePoolReconcilePaused(t *testing.T) { 63 g := NewWithT(t) 64 65 ctx := context.Background() 66 67 sb := runtime.NewSchemeBuilder( 68 clusterv1.AddToScheme, 69 infrav1.AddToScheme, 70 expv1.AddToScheme, 71 infrav1exp.AddToScheme, 72 corev1.AddToScheme, 73 ) 74 s := runtime.NewScheme() 75 g.Expect(sb.AddToScheme(s)).To(Succeed()) 76 c := fake.NewClientBuilder(). 77 WithScheme(s). 78 Build() 79 80 recorder := record.NewFakeRecorder(1) 81 82 reconciler := NewAzureMachinePoolReconciler(c, recorder, reconciler.Timeouts{}, "") 83 name := test.RandomName("paused", 10) 84 namespace := "default" 85 86 cluster := &clusterv1.Cluster{ 87 ObjectMeta: metav1.ObjectMeta{ 88 Name: name, 89 Namespace: namespace, 90 }, 91 Spec: clusterv1.ClusterSpec{ 92 Paused: true, 93 InfrastructureRef: &corev1.ObjectReference{ 94 Kind: "AzureCluster", 95 Name: name, 96 Namespace: namespace, 97 }, 98 }, 99 } 100 g.Expect(c.Create(ctx, cluster)).To(Succeed()) 101 102 azCluster := &infrav1.AzureCluster{ 103 ObjectMeta: metav1.ObjectMeta{ 104 Name: name, 105 Namespace: namespace, 106 }, 107 Spec: infrav1.AzureClusterSpec{ 108 AzureClusterClassSpec: infrav1.AzureClusterClassSpec{ 109 SubscriptionID: "something", 110 IdentityRef: &corev1.ObjectReference{ 111 Name: "fake-identity", 112 Namespace: "default", 113 Kind: "AzureClusterIdentity", 114 }, 115 }, 116 }, 117 } 118 g.Expect(c.Create(ctx, azCluster)).To(Succeed()) 119 120 fakeIdentity := &infrav1.AzureClusterIdentity{ 121 ObjectMeta: metav1.ObjectMeta{ 122 Name: "fake-identity", 123 Namespace: "default", 124 }, 125 Spec: infrav1.AzureClusterIdentitySpec{ 126 Type: infrav1.ServicePrincipal, 127 ClientSecret: corev1.SecretReference{ 128 Name: "fooSecret", 129 Namespace: "default", 130 }, 131 TenantID: "fake-tenantid", 132 }, 133 } 134 fakeSecret := &corev1.Secret{ 135 ObjectMeta: metav1.ObjectMeta{ 136 Name: "fooSecret", 137 Namespace: "default", 138 }, 139 Data: map[string][]byte{ 140 "clientSecret": []byte("fooSecret"), 141 }, 142 } 143 g.Expect(c.Create(ctx, fakeIdentity)).To(Succeed()) 144 g.Expect(c.Create(ctx, fakeSecret)).To(Succeed()) 145 146 mp := &expv1.MachinePool{ 147 ObjectMeta: metav1.ObjectMeta{ 148 Name: name, 149 Namespace: namespace, 150 Labels: map[string]string{ 151 clusterv1.ClusterNameLabel: name, 152 }, 153 }, 154 Spec: expv1.MachinePoolSpec{ 155 ClusterName: name, 156 Template: clusterv1.MachineTemplateSpec{ 157 Spec: clusterv1.MachineSpec{ 158 ClusterName: name, 159 }, 160 }, 161 }, 162 } 163 g.Expect(c.Create(ctx, mp)).To(Succeed()) 164 165 instance := &infrav1exp.AzureMachinePool{ 166 ObjectMeta: metav1.ObjectMeta{ 167 Name: name, 168 Namespace: namespace, 169 OwnerReferences: []metav1.OwnerReference{ 170 { 171 Kind: "MachinePool", 172 APIVersion: expv1.GroupVersion.String(), 173 Name: mp.Name, 174 }, 175 }, 176 }, 177 } 178 g.Expect(c.Create(ctx, instance)).To(Succeed()) 179 180 result, err := reconciler.Reconcile(context.Background(), ctrl.Request{ 181 NamespacedName: client.ObjectKey{ 182 Namespace: instance.Namespace, 183 Name: instance.Name, 184 }, 185 }) 186 187 g.Expect(err).NotTo(HaveOccurred()) 188 g.Expect(result.RequeueAfter).To(BeZero()) 189 }