sigs.k8s.io/cluster-api-provider-azure@v1.14.3/exp/controllers/azuremachinepool_controller_unit_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 "testing" 21 22 . "github.com/onsi/gomega" 23 "go.uber.org/mock/gomock" 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/utils/ptr" 28 infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" 29 "sigs.k8s.io/cluster-api-provider-azure/azure/mock_azure" 30 "sigs.k8s.io/cluster-api-provider-azure/azure/scope" 31 infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1" 32 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 33 expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1" 34 ) 35 36 func Test_newAzureMachinePoolService(t *testing.T) { 37 mockCtrl := gomock.NewController(t) 38 defer mockCtrl.Finish() 39 40 cluster := newAzureCluster("fakeCluster") 41 cluster.Spec.ResourceGroup = "resourceGroup" 42 cluster.Spec.Location = "test-location" 43 cluster.Spec.ResourceGroup = "my-rg" 44 cluster.Spec.SubscriptionID = "123" 45 cluster.Spec.NetworkSpec = infrav1.NetworkSpec{ 46 Vnet: infrav1.VnetSpec{Name: "my-vnet", ResourceGroup: "my-rg"}, 47 } 48 49 clusterMock := mock_azure.NewMockClusterScoper(mockCtrl) 50 clusterMock.EXPECT().SubscriptionID().AnyTimes() 51 clusterMock.EXPECT().BaseURI().AnyTimes() 52 clusterMock.EXPECT().CloudEnvironment().AnyTimes() 53 clusterMock.EXPECT().Token().AnyTimes() 54 clusterMock.EXPECT().Location().Return(cluster.Spec.Location) 55 clusterMock.EXPECT().HashKey().Return("fakeCluster") 56 clusterMock.EXPECT().CloudEnvironment().AnyTimes() 57 clusterMock.EXPECT().Token().AnyTimes() 58 clusterMock.EXPECT().DefaultedAzureCallTimeout().AnyTimes() 59 60 mps := &scope.MachinePoolScope{ 61 ClusterScoper: clusterMock, 62 MachinePool: newMachinePool("fakeCluster", "poolName"), 63 AzureMachinePool: &infrav1exp.AzureMachinePool{ 64 ObjectMeta: metav1.ObjectMeta{ 65 Name: "poolName", 66 }, 67 }, 68 } 69 70 subject, err := newAzureMachinePoolService(mps) 71 g := NewWithT(t) 72 g.Expect(err).NotTo(HaveOccurred()) 73 g.Expect(subject).NotTo(BeNil()) 74 g.Expect(subject.services).NotTo(BeEmpty()) 75 g.Expect(subject.skuCache).NotTo(BeNil()) 76 } 77 78 func newScheme(g *GomegaWithT) *runtime.Scheme { 79 scheme := runtime.NewScheme() 80 for _, f := range []func(*runtime.Scheme) error{ 81 clusterv1.AddToScheme, 82 expv1.AddToScheme, 83 infrav1.AddToScheme, 84 infrav1exp.AddToScheme, 85 } { 86 g.Expect(f(scheme)).To(Succeed()) 87 } 88 return scheme 89 } 90 91 func newMachinePool(clusterName, poolName string) *expv1.MachinePool { 92 return &expv1.MachinePool{ 93 ObjectMeta: metav1.ObjectMeta{ 94 Labels: map[string]string{ 95 clusterv1.ClusterNameLabel: clusterName, 96 }, 97 Name: poolName, 98 Namespace: "default", 99 }, 100 Spec: expv1.MachinePoolSpec{ 101 Replicas: ptr.To[int32](2), 102 }, 103 } 104 } 105 106 func newAzureMachinePool(clusterName, poolName string) *infrav1exp.AzureMachinePool { 107 return &infrav1exp.AzureMachinePool{ 108 ObjectMeta: metav1.ObjectMeta{ 109 Labels: map[string]string{ 110 clusterv1.ClusterNameLabel: clusterName, 111 }, 112 Name: poolName, 113 Namespace: "default", 114 }, 115 } 116 } 117 118 func newMachinePoolWithInfrastructureRef(clusterName, poolName string) *expv1.MachinePool { 119 m := newMachinePool(clusterName, poolName) 120 m.Spec.Template.Spec.InfrastructureRef = corev1.ObjectReference{ 121 Kind: infrav1.AzureMachinePoolKind, 122 Namespace: m.Namespace, 123 Name: "azure" + poolName, 124 APIVersion: infrav1exp.GroupVersion.String(), 125 } 126 return m 127 } 128 129 func newAzureCluster(clusterName string) *infrav1.AzureCluster { 130 return &infrav1.AzureCluster{ 131 ObjectMeta: metav1.ObjectMeta{ 132 Name: "azure" + clusterName, 133 Namespace: "default", 134 OwnerReferences: []metav1.OwnerReference{ 135 { 136 APIVersion: clusterv1.GroupVersion.String(), 137 Kind: "Cluster", 138 Name: clusterName, 139 }, 140 }, 141 }, 142 } 143 } 144 145 func newCluster(name string) *clusterv1.Cluster { 146 return &clusterv1.Cluster{ 147 ObjectMeta: metav1.ObjectMeta{ 148 Name: name, 149 Namespace: "default", 150 }, 151 } 152 }