sigs.k8s.io/cluster-api-provider-aws@v1.5.5/exp/api/v1beta1/awsmachinepool_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 TestAWSMachinePoolDefault(t *testing.T) { 33 m := &AWSMachinePool{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "default"}} 34 t.Run("for AWSCluster", utildefaulting.DefaultValidateTest(m)) 35 m.Default() 36 g := NewWithT(t) 37 g.Expect(m.Spec.DefaultCoolDown.Duration).To(BeNumerically(">=", 0)) 38 } 39 40 func TestAWSMachinePool_ValidateCreate(t *testing.T) { 41 g := NewWithT(t) 42 43 tests := []struct { 44 name string 45 pool *AWSMachinePool 46 wantErr bool 47 }{ 48 { 49 name: "pool with valid tags is accepted", 50 pool: &AWSMachinePool{ 51 Spec: AWSMachinePoolSpec{ 52 AdditionalTags: infrav1.Tags{ 53 "key-1": "value-1", 54 "key-2": "value-2", 55 }, 56 }, 57 }, 58 59 wantErr: false, 60 }, 61 { 62 name: "invalid tags are rejected", 63 pool: &AWSMachinePool{ 64 Spec: AWSMachinePoolSpec{ 65 AdditionalTags: infrav1.Tags{ 66 "key-1": "value-1", 67 "": "value-2", 68 strings.Repeat("CAPI", 33): "value-3", 69 "key-4": strings.Repeat("CAPI", 65), 70 }, 71 }, 72 }, 73 wantErr: true, 74 }, 75 { 76 name: "Should fail if additional security groups are provided with both ID and Filters", 77 pool: &AWSMachinePool{ 78 Spec: AWSMachinePoolSpec{ 79 AWSLaunchTemplate: AWSLaunchTemplate{AdditionalSecurityGroups: []infrav1.AWSResourceReference{{ 80 ID: aws.String("sg-1"), 81 Filters: []infrav1.Filter{ 82 { 83 Name: "sg-1", 84 Values: []string{"test"}, 85 }, 86 }, 87 }}}, 88 }, 89 }, 90 wantErr: true, 91 }, 92 { 93 name: "Should fail if both subnet ID and filters passed in AWSMachinePool spec", 94 pool: &AWSMachinePool{ 95 Spec: AWSMachinePoolSpec{ 96 AdditionalTags: infrav1.Tags{ 97 "key-1": "value-1", 98 "key-2": "value-2", 99 }, 100 Subnets: []infrav1.AWSResourceReference{ 101 { 102 ID: pointer.StringPtr("subnet-id"), 103 Filters: []infrav1.Filter{{Name: "filter_name", Values: []string{"filter_value"}}}, 104 }, 105 }, 106 }, 107 }, 108 wantErr: true, 109 }, 110 { 111 name: "Should pass if either subnet ID or filters passed in AWSMachinePool spec", 112 pool: &AWSMachinePool{ 113 Spec: AWSMachinePoolSpec{ 114 AdditionalTags: infrav1.Tags{ 115 "key-1": "value-1", 116 "key-2": "value-2", 117 }, 118 Subnets: []infrav1.AWSResourceReference{ 119 { 120 ID: pointer.StringPtr("subnet-id"), 121 }, 122 }, 123 }, 124 }, 125 wantErr: false, 126 }, 127 } 128 for _, tt := range tests { 129 t.Run(tt.name, func(t *testing.T) { 130 err := tt.pool.ValidateCreate() 131 if tt.wantErr { 132 g.Expect(err).To(HaveOccurred()) 133 } else { 134 g.Expect(err).To(Succeed()) 135 } 136 }) 137 } 138 } 139 140 func TestAWSMachinePool_ValidateUpdate(t *testing.T) { 141 g := NewWithT(t) 142 143 tests := []struct { 144 name string 145 new *AWSMachinePool 146 old *AWSMachinePool 147 wantErr bool 148 }{ 149 { 150 name: "adding tags is accepted", 151 old: &AWSMachinePool{ 152 Spec: AWSMachinePoolSpec{ 153 AdditionalTags: infrav1.Tags{ 154 "key-1": "value-1", 155 }, 156 }, 157 }, 158 new: &AWSMachinePool{ 159 Spec: AWSMachinePoolSpec{ 160 AdditionalTags: infrav1.Tags{ 161 "key-1": "value-1", 162 "key-2": "value-2", 163 }, 164 }, 165 }, 166 wantErr: false, 167 }, 168 { 169 name: "adding invalid tags is rejected", 170 old: &AWSMachinePool{ 171 Spec: AWSMachinePoolSpec{ 172 AdditionalTags: infrav1.Tags{ 173 "key-1": "value-1", 174 }, 175 }, 176 }, 177 new: &AWSMachinePool{ 178 Spec: AWSMachinePoolSpec{ 179 AdditionalTags: infrav1.Tags{ 180 "key-1": "value-1", 181 "": "value-2", 182 strings.Repeat("CAPI", 33): "value-3", 183 "key-4": strings.Repeat("CAPI", 65), 184 }, 185 }, 186 }, 187 wantErr: true, 188 }, 189 { 190 name: "Should fail update if both subnetID and filters passed in AWSMachinePool spec", 191 old: &AWSMachinePool{ 192 Spec: AWSMachinePoolSpec{ 193 AdditionalTags: infrav1.Tags{ 194 "key-1": "value-1", 195 }, 196 }, 197 }, 198 new: &AWSMachinePool{ 199 Spec: AWSMachinePoolSpec{ 200 AdditionalTags: infrav1.Tags{ 201 "key-1": "value-1", 202 "key-2": "value-2", 203 }, 204 Subnets: []infrav1.AWSResourceReference{ 205 { 206 ID: pointer.StringPtr("subnet-id"), 207 Filters: []infrav1.Filter{{Name: "filter_name", Values: []string{"filter_value"}}}, 208 }, 209 }, 210 }, 211 }, 212 wantErr: true, 213 }, 214 { 215 name: "Should pass update if either subnetID or filters passed in AWSMachinePool spec", 216 old: &AWSMachinePool{ 217 Spec: AWSMachinePoolSpec{ 218 AdditionalTags: infrav1.Tags{ 219 "key-1": "value-1", 220 }, 221 }, 222 }, 223 new: &AWSMachinePool{ 224 Spec: AWSMachinePoolSpec{ 225 AdditionalTags: infrav1.Tags{ 226 "key-1": "value-1", 227 "key-2": "value-2", 228 }, 229 Subnets: []infrav1.AWSResourceReference{ 230 { 231 ID: pointer.StringPtr("subnet-id"), 232 }, 233 }, 234 }, 235 }, 236 wantErr: false, 237 }, 238 } 239 for _, tt := range tests { 240 t.Run(tt.name, func(t *testing.T) { 241 err := tt.new.ValidateUpdate(tt.old.DeepCopy()) 242 if tt.wantErr { 243 g.Expect(err).To(HaveOccurred()) 244 } else { 245 g.Expect(err).To(Succeed()) 246 } 247 }) 248 } 249 }