sigs.k8s.io/cluster-api-provider-azure@v1.14.3/controllers/azurejson_machinetemplate_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/google/go-cmp/cmp" 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 "k8s.io/client-go/tools/record" 29 infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" 30 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 31 ctrl "sigs.k8s.io/controller-runtime" 32 "sigs.k8s.io/controller-runtime/pkg/client/fake" 33 ) 34 35 func TestAzureJSONTemplateReconciler(t *testing.T) { 36 scheme, err := newScheme() 37 if err != nil { 38 t.Error(err) 39 } 40 41 cluster := &clusterv1.Cluster{ 42 ObjectMeta: metav1.ObjectMeta{ 43 Name: "my-cluster", 44 }, 45 Spec: clusterv1.ClusterSpec{ 46 InfrastructureRef: &corev1.ObjectReference{ 47 APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", 48 Kind: infrav1.AzureClusterKind, 49 Name: "my-azure-cluster", 50 }, 51 }, 52 } 53 54 azureCluster := &infrav1.AzureCluster{ 55 ObjectMeta: metav1.ObjectMeta{ 56 Name: "my-azure-cluster", 57 OwnerReferences: []metav1.OwnerReference{ 58 { 59 APIVersion: "cluster.x-k8s.io/v1beta1", 60 Kind: "Cluster", 61 Name: "my-cluster", 62 }, 63 }, 64 }, 65 Spec: infrav1.AzureClusterSpec{ 66 AzureClusterClassSpec: infrav1.AzureClusterClassSpec{ 67 SubscriptionID: "123", 68 IdentityRef: &corev1.ObjectReference{ 69 Name: "fake-identity", 70 Namespace: "default", 71 Kind: "AzureClusterIdentity", 72 }, 73 }, 74 }, 75 } 76 77 azureMachineTemplate := &infrav1.AzureMachineTemplate{ 78 ObjectMeta: metav1.ObjectMeta{ 79 Name: "my-json-template", 80 OwnerReferences: []metav1.OwnerReference{ 81 { 82 APIVersion: "cluster.x-k8s.io/v1beta1", 83 Kind: "Cluster", 84 Name: "my-cluster", 85 }, 86 }, 87 }, 88 } 89 90 fakeIdentity := &infrav1.AzureClusterIdentity{ 91 ObjectMeta: metav1.ObjectMeta{ 92 Name: "fake-identity", 93 Namespace: "default", 94 }, 95 Spec: infrav1.AzureClusterIdentitySpec{ 96 Type: infrav1.ServicePrincipal, 97 TenantID: "fake-tenantid", 98 }, 99 } 100 fakeSecret := &corev1.Secret{Data: map[string][]byte{"clientSecret": []byte("fooSecret")}} 101 102 cases := map[string]struct { 103 objects []runtime.Object 104 fail bool 105 err string 106 }{ 107 "should reconcile normally": { 108 objects: []runtime.Object{ 109 cluster, 110 azureCluster, 111 azureMachineTemplate, 112 fakeIdentity, 113 fakeSecret, 114 }, 115 }, 116 "missing azure cluster should return error": { 117 objects: []runtime.Object{ 118 cluster, 119 azureMachineTemplate, 120 }, 121 fail: true, 122 err: "azureclusters.infrastructure.cluster.x-k8s.io \"my-azure-cluster\" not found", 123 }, 124 "infra ref is nil": { 125 objects: []runtime.Object{ 126 &clusterv1.Cluster{ 127 ObjectMeta: metav1.ObjectMeta{ 128 Name: "my-cluster", 129 }, 130 Spec: clusterv1.ClusterSpec{ 131 InfrastructureRef: nil, 132 }, 133 }, 134 azureCluster, 135 azureMachineTemplate, 136 }, 137 fail: false, 138 }, 139 "infra ref is not an azure cluster": { 140 objects: []runtime.Object{ 141 &clusterv1.Cluster{ 142 ObjectMeta: metav1.ObjectMeta{ 143 Name: "my-cluster", 144 }, 145 Spec: clusterv1.ClusterSpec{ 146 InfrastructureRef: &corev1.ObjectReference{ 147 APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", 148 Kind: "FooCluster", 149 Name: "my-foo-cluster", 150 }, 151 }, 152 }, 153 azureCluster, 154 azureMachineTemplate, 155 }, 156 fail: false, 157 }, 158 } 159 160 for name, tc := range cases { 161 t.Run(name, func(t *testing.T) { 162 client := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(tc.objects...).Build() 163 164 reconciler := &AzureJSONTemplateReconciler{ 165 Client: client, 166 Recorder: record.NewFakeRecorder(128), 167 } 168 169 _, err := reconciler.Reconcile(context.Background(), ctrl.Request{ 170 NamespacedName: types.NamespacedName{ 171 Namespace: "", 172 Name: "my-json-template", 173 }, 174 }) 175 if tc.fail { 176 if diff := cmp.Diff(tc.err, err.Error()); diff != "" { 177 t.Error(diff) 178 } 179 } else { 180 if err != nil { 181 t.Errorf("expected success, but got error: %s", err.Error()) 182 } 183 } 184 }) 185 } 186 }