sigs.k8s.io/cluster-api-provider-aws@v1.5.5/exp/api/v1beta1/awsmanagedmachinepool_webhook_test.go (about) 1 /* 2 Copyright 2021 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 "strings" 21 "testing" 22 23 "github.com/aws/aws-sdk-go/aws" 24 . "github.com/onsi/gomega" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 "k8s.io/utils/pointer" 27 28 infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta1" 29 utildefaulting "sigs.k8s.io/cluster-api/util/defaulting" 30 ) 31 32 func TestAWSManagedMachinePoolDefault(t *testing.T) { 33 fargate := &AWSManagedMachinePool{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "default"}} 34 t.Run("for AWSManagedMachinePool", utildefaulting.DefaultValidateTest(fargate)) 35 fargate.Default() 36 } 37 38 func TestAWSManagedMachinePool_ValidateCreate(t *testing.T) { 39 g := NewWithT(t) 40 41 tests := []struct { 42 name string 43 pool *AWSManagedMachinePool 44 wantErr bool 45 }{ 46 { 47 name: "pool requires a EKS Node group name", 48 pool: &AWSManagedMachinePool{ 49 Spec: AWSManagedMachinePoolSpec{ 50 EKSNodegroupName: "", 51 }, 52 }, 53 54 wantErr: true, 55 }, 56 { 57 name: "pool with valid EKS Node group name", 58 pool: &AWSManagedMachinePool{ 59 Spec: AWSManagedMachinePoolSpec{ 60 EKSNodegroupName: "eks-node-group-1", 61 }, 62 }, 63 64 wantErr: false, 65 }, 66 { 67 name: "pool with valid tags is accepted", 68 pool: &AWSManagedMachinePool{ 69 Spec: AWSManagedMachinePoolSpec{ 70 EKSNodegroupName: "eks-node-group-2", 71 AdditionalTags: infrav1.Tags{ 72 "key-1": "value-1", 73 "key-2": "value-2", 74 }, 75 }, 76 }, 77 78 wantErr: false, 79 }, 80 { 81 name: "invalid tags are rejected", 82 pool: &AWSManagedMachinePool{ 83 Spec: AWSManagedMachinePoolSpec{ 84 EKSNodegroupName: "eks-node-group-3", 85 AdditionalTags: infrav1.Tags{ 86 "key-1": "value-1", 87 "": "value-2", 88 strings.Repeat("CAPI", 33): "value-3", 89 "key-4": strings.Repeat("CAPI", 65), 90 }, 91 }, 92 }, 93 wantErr: true, 94 }, 95 { 96 name: "valid update config", 97 pool: &AWSManagedMachinePool{ 98 Spec: AWSManagedMachinePoolSpec{ 99 EKSNodegroupName: "eks-node-group-3", 100 UpdateConfig: &UpdateConfig{ 101 MaxUnavailable: aws.Int(1), 102 }, 103 }, 104 }, 105 wantErr: false, 106 }, 107 { 108 name: "update config with no values", 109 pool: &AWSManagedMachinePool{ 110 Spec: AWSManagedMachinePoolSpec{ 111 EKSNodegroupName: "eks-node-group-3", 112 UpdateConfig: &UpdateConfig{}, 113 }, 114 }, 115 wantErr: true, 116 }, 117 { 118 name: "update config with both values", 119 pool: &AWSManagedMachinePool{ 120 Spec: AWSManagedMachinePoolSpec{ 121 EKSNodegroupName: "eks-node-group-3", 122 UpdateConfig: &UpdateConfig{ 123 MaxUnavailable: aws.Int(1), 124 MaxUnavailablePercentage: aws.Int(10), 125 }, 126 }, 127 }, 128 wantErr: true, 129 }, 130 { 131 name: "minSize 0 is accepted", 132 pool: &AWSManagedMachinePool{ 133 Spec: AWSManagedMachinePoolSpec{ 134 EKSNodegroupName: "eks-node-group-3", 135 Scaling: &ManagedMachinePoolScaling{ 136 MinSize: pointer.Int32(0), 137 }, 138 }, 139 }, 140 wantErr: false, 141 }, 142 } 143 for _, tt := range tests { 144 t.Run(tt.name, func(t *testing.T) { 145 err := tt.pool.ValidateCreate() 146 if tt.wantErr { 147 g.Expect(err).To(HaveOccurred()) 148 } else { 149 g.Expect(err).To(Succeed()) 150 } 151 }) 152 } 153 } 154 155 func TestAWSManagedMachinePool_ValidateUpdate(t *testing.T) { 156 g := NewWithT(t) 157 158 tests := []struct { 159 name string 160 new *AWSManagedMachinePool 161 old *AWSManagedMachinePool 162 wantErr bool 163 }{ 164 { 165 name: "update EKS node groups name is rejected", 166 old: &AWSManagedMachinePool{ 167 Spec: AWSManagedMachinePoolSpec{ 168 EKSNodegroupName: "eks-node-group-1", 169 }, 170 }, 171 new: &AWSManagedMachinePool{ 172 Spec: AWSManagedMachinePoolSpec{ 173 EKSNodegroupName: "eks-node-group-2", 174 }, 175 }, 176 wantErr: true, 177 }, 178 { 179 name: "adding tags is accepted", 180 old: &AWSManagedMachinePool{ 181 Spec: AWSManagedMachinePoolSpec{ 182 EKSNodegroupName: "eks-node-group-1", 183 AdditionalTags: infrav1.Tags{ 184 "key-1": "value-1", 185 }, 186 }, 187 }, 188 new: &AWSManagedMachinePool{ 189 Spec: AWSManagedMachinePoolSpec{ 190 EKSNodegroupName: "eks-node-group-1", 191 AdditionalTags: infrav1.Tags{ 192 "key-1": "value-1", 193 "key-2": "value-2", 194 }, 195 }, 196 }, 197 wantErr: false, 198 }, 199 { 200 name: "adding invalid tags is rejected", 201 old: &AWSManagedMachinePool{ 202 Spec: AWSManagedMachinePoolSpec{ 203 EKSNodegroupName: "eks-node-group-3", 204 AdditionalTags: infrav1.Tags{ 205 "key-1": "value-1", 206 }, 207 }, 208 }, 209 new: &AWSManagedMachinePool{ 210 Spec: AWSManagedMachinePoolSpec{ 211 EKSNodegroupName: "eks-node-group-3", 212 AdditionalTags: infrav1.Tags{ 213 "key-1": "value-1", 214 "": "value-2", 215 strings.Repeat("CAPI", 33): "value-3", 216 "key-4": strings.Repeat("CAPI", 65), 217 }, 218 }, 219 }, 220 wantErr: true, 221 }, 222 { 223 name: "adding update config is accepted", 224 old: &AWSManagedMachinePool{ 225 Spec: AWSManagedMachinePoolSpec{ 226 EKSNodegroupName: "eks-node-group-1", 227 }, 228 }, 229 new: &AWSManagedMachinePool{ 230 Spec: AWSManagedMachinePoolSpec{ 231 EKSNodegroupName: "eks-node-group-1", 232 UpdateConfig: &UpdateConfig{ 233 MaxUnavailablePercentage: aws.Int(10), 234 }, 235 }, 236 }, 237 wantErr: false, 238 }, 239 { 240 name: "removing update config is accepted", 241 old: &AWSManagedMachinePool{ 242 Spec: AWSManagedMachinePoolSpec{ 243 EKSNodegroupName: "eks-node-group-1", 244 UpdateConfig: &UpdateConfig{ 245 MaxUnavailablePercentage: aws.Int(10), 246 }, 247 }, 248 }, 249 new: &AWSManagedMachinePool{ 250 Spec: AWSManagedMachinePoolSpec{ 251 EKSNodegroupName: "eks-node-group-1", 252 }, 253 }, 254 wantErr: false, 255 }, 256 } 257 for _, tt := range tests { 258 t.Run(tt.name, func(t *testing.T) { 259 err := tt.new.ValidateUpdate(tt.old.DeepCopy()) 260 if tt.wantErr { 261 g.Expect(err).To(HaveOccurred()) 262 } else { 263 g.Expect(err).To(Succeed()) 264 } 265 }) 266 } 267 }