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