sigs.k8s.io/cluster-api-provider-azure@v1.17.0/api/v1beta1/azuremanagedcluster_webhook_test.go (about) 1 /* 2 Copyright 2023 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 v1beta1 18 19 import ( 20 "testing" 21 22 . "github.com/onsi/gomega" 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 utilfeature "k8s.io/component-base/featuregate/testing" 25 "k8s.io/utils/ptr" 26 "sigs.k8s.io/cluster-api-provider-azure/feature" 27 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 28 capifeature "sigs.k8s.io/cluster-api/feature" 29 ) 30 31 func TestAzureManagedCluster_ValidateUpdate(t *testing.T) { 32 tests := []struct { 33 name string 34 oldAMC *AzureManagedCluster 35 amc *AzureManagedCluster 36 wantErr bool 37 }{ 38 { 39 name: "ControlPlaneEndpoint.Port update (AKS API-derived update scenario)", 40 oldAMC: &AzureManagedCluster{ 41 ObjectMeta: metav1.ObjectMeta{}, 42 Spec: AzureManagedClusterSpec{ 43 ControlPlaneEndpoint: clusterv1.APIEndpoint{ 44 Host: "aks-8622-h4h26c44.hcp.eastus.azmk8s.io", 45 }, 46 }, 47 }, 48 amc: &AzureManagedCluster{ 49 ObjectMeta: metav1.ObjectMeta{}, 50 Spec: AzureManagedClusterSpec{ 51 ControlPlaneEndpoint: clusterv1.APIEndpoint{ 52 Host: "aks-8622-h4h26c44.hcp.eastus.azmk8s.io", 53 Port: 443, 54 }, 55 }, 56 }, 57 wantErr: false, 58 }, 59 { 60 name: "ControlPlaneEndpoint.Host update (AKS API-derived update scenario)", 61 oldAMC: &AzureManagedCluster{ 62 ObjectMeta: metav1.ObjectMeta{}, 63 Spec: AzureManagedClusterSpec{ 64 ControlPlaneEndpoint: clusterv1.APIEndpoint{ 65 Port: 443, 66 }, 67 }, 68 }, 69 amc: &AzureManagedCluster{ 70 ObjectMeta: metav1.ObjectMeta{}, 71 Spec: AzureManagedClusterSpec{ 72 ControlPlaneEndpoint: clusterv1.APIEndpoint{ 73 Host: "aks-8622-h4h26c44.hcp.eastus.azmk8s.io", 74 Port: 443, 75 }, 76 }, 77 }, 78 wantErr: false, 79 }, 80 } 81 for _, tc := range tests { 82 t.Run(tc.name, func(t *testing.T) { 83 g := NewWithT(t) 84 _, err := tc.amc.ValidateUpdate(tc.oldAMC) 85 if tc.wantErr { 86 g.Expect(err).To(HaveOccurred()) 87 } else { 88 g.Expect(err).NotTo(HaveOccurred()) 89 } 90 }) 91 } 92 } 93 94 func TestAzureManagedCluster_ValidateCreate(t *testing.T) { 95 tests := []struct { 96 name string 97 oldAMC *AzureManagedCluster 98 amc *AzureManagedCluster 99 wantErr bool 100 }{ 101 { 102 name: "can set Spec.ControlPlaneEndpoint.Host during create (clusterctl move scenario)", 103 amc: &AzureManagedCluster{ 104 Spec: AzureManagedClusterSpec{ 105 ControlPlaneEndpoint: clusterv1.APIEndpoint{ 106 Host: "my-host", 107 }, 108 }, 109 }, 110 wantErr: false, 111 }, 112 { 113 name: "can set Spec.ControlPlaneEndpoint.Port during create (clusterctl move scenario)", 114 amc: &AzureManagedCluster{ 115 Spec: AzureManagedClusterSpec{ 116 ControlPlaneEndpoint: clusterv1.APIEndpoint{ 117 Port: 4443, 118 }, 119 }, 120 }, 121 wantErr: false, 122 }, 123 } 124 for _, tc := range tests { 125 t.Run(tc.name, func(t *testing.T) { 126 g := NewWithT(t) 127 _, err := tc.amc.ValidateCreate() 128 if tc.wantErr { 129 g.Expect(err).To(HaveOccurred()) 130 } else { 131 g.Expect(err).NotTo(HaveOccurred()) 132 } 133 }) 134 } 135 } 136 137 func TestAzureManagedCluster_ValidateCreateFailure(t *testing.T) { 138 tests := []struct { 139 name string 140 amc *AzureManagedCluster 141 featureGateEnabled *bool 142 expectError bool 143 }{ 144 { 145 name: "feature gate explicitly disabled", 146 amc: getKnownValidAzureManagedCluster(), 147 featureGateEnabled: ptr.To(false), 148 expectError: true, 149 }, 150 { 151 name: "feature gate implicitly enabled", 152 amc: getKnownValidAzureManagedCluster(), 153 featureGateEnabled: nil, 154 expectError: false, 155 }, 156 } 157 for _, tc := range tests { 158 t.Run(tc.name, func(t *testing.T) { 159 if tc.featureGateEnabled != nil { 160 defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, capifeature.MachinePool, *tc.featureGateEnabled)() 161 } 162 g := NewWithT(t) 163 _, err := tc.amc.ValidateCreate() 164 if tc.expectError { 165 g.Expect(err).To(HaveOccurred()) 166 } else { 167 g.Expect(err).NotTo(HaveOccurred()) 168 } 169 }) 170 } 171 } 172 173 func getKnownValidAzureManagedCluster() *AzureManagedCluster { 174 return &AzureManagedCluster{} 175 }