sigs.k8s.io/cluster-api-provider-aws@v1.5.5/exp/api/v1beta1/awsfargateprofile_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/onsi/gomega" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 26 infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta1" 27 "sigs.k8s.io/cluster-api-provider-aws/pkg/eks" 28 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" 29 utildefaulting "sigs.k8s.io/cluster-api/util/defaulting" 30 ) 31 32 func TestAWSFargateProfileDefault(t *testing.T) { 33 fargate := &AWSFargateProfile{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "default"}, 34 Spec: FargateProfileSpec{ 35 ClusterName: "clustername", 36 }, 37 } 38 t.Run("for AWSFargateProfile", utildefaulting.DefaultValidateTest(fargate)) 39 fargate.Default() 40 g := NewWithT(t) 41 g.Expect(fargate.GetLabels()[clusterv1.ClusterLabelName]).To(BeEquivalentTo(fargate.Spec.ClusterName)) 42 name, err := eks.GenerateEKSName(fargate.Name, fargate.Namespace, maxProfileNameLength) 43 g.Expect(err).NotTo(HaveOccurred()) 44 g.Expect(fargate.Spec.ProfileName).To(BeEquivalentTo(name)) 45 } 46 47 func TestAWSFargateProfileValidateRoleNameUpdate(t *testing.T) { 48 g := NewWithT(t) 49 50 before := &AWSFargateProfile{ 51 ObjectMeta: metav1.ObjectMeta{ 52 Name: "foo", 53 Namespace: "default", 54 }, 55 Spec: FargateProfileSpec{ 56 ClusterName: "clustername", 57 ProfileName: "profilename", 58 }, 59 } 60 61 invalidRoleNameUpdate := before.DeepCopy() 62 invalidRoleNameUpdate.Spec.RoleName = "invalid-role-name" 63 64 invalidTagsUpdate := before.DeepCopy() 65 invalidTagsUpdate.Spec.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 defaultRoleNameUpdate := before.DeepCopy() 73 defaultRoleNameUpdate.Spec.RoleName = DefaultEKSFargateRole 74 75 validRoleNameUpdate := before.DeepCopy() 76 validRoleNameUpdate.Spec.RoleName = "clustername-profilename_fargate" 77 78 beforeWithDifferentRoleName := before.DeepCopy() 79 beforeWithDifferentRoleName.Spec.RoleName = "different-role-name" 80 81 tests := []struct { 82 name string 83 expectErr bool 84 before *AWSFargateProfile 85 fargateProfile *AWSFargateProfile 86 }{ 87 { 88 name: "update roleName should fail when existing roleName is empty and the new roleName is not the generated name", 89 expectErr: true, 90 before: before, 91 fargateProfile: invalidRoleNameUpdate, 92 }, 93 { 94 name: "update roleName should succeed when existing roleName is empty and the new roleName is the default name", 95 expectErr: false, 96 before: before, 97 fargateProfile: defaultRoleNameUpdate, 98 }, 99 { 100 name: "update roleName should succeed when existing roleName is empty and the new roleName is the generated name", 101 expectErr: false, 102 before: before, 103 fargateProfile: validRoleNameUpdate, 104 }, 105 { 106 name: "update roleName should fail when existing roleName is different from the new roleName", 107 expectErr: true, 108 before: beforeWithDifferentRoleName, 109 fargateProfile: validRoleNameUpdate, 110 }, 111 { 112 name: "update tags should fail when invalid tags are present", 113 expectErr: true, 114 before: before, 115 fargateProfile: invalidTagsUpdate, 116 }, 117 } 118 119 for _, tt := range tests { 120 t.Run(tt.name, func(t *testing.T) { 121 err := tt.fargateProfile.ValidateUpdate(tt.before.DeepCopy()) 122 if tt.expectErr { 123 g.Expect(err).To(HaveOccurred()) 124 } else { 125 g.Expect(err).To(Succeed()) 126 } 127 }) 128 } 129 } 130 131 func TestAWSFargateProfile_ValidateCreate(t *testing.T) { 132 g := NewWithT(t) 133 134 tests := []struct { 135 name string 136 profile *AWSFargateProfile 137 wantErr bool 138 }{ 139 { 140 name: "profile with name is accepted", 141 profile: &AWSFargateProfile{ 142 Spec: FargateProfileSpec{ 143 ClusterName: "cluster-1", 144 }, 145 }, 146 147 wantErr: false, 148 }, 149 { 150 name: "profile with valid tags is accepted", 151 profile: &AWSFargateProfile{ 152 Spec: FargateProfileSpec{ 153 ClusterName: "cluster-1", 154 AdditionalTags: infrav1.Tags{ 155 "key-1": "value-1", 156 "key-2": "value-2", 157 }, 158 }, 159 }, 160 161 wantErr: false, 162 }, 163 { 164 name: "invalid tags are rejected", 165 profile: &AWSFargateProfile{ 166 Spec: FargateProfileSpec{ 167 ClusterName: "cluster-2", 168 AdditionalTags: infrav1.Tags{ 169 "key-1": "value-1", 170 "": "value-2", 171 strings.Repeat("CAPI", 33): "value-3", 172 "key-4": strings.Repeat("CAPI", 65), 173 }, 174 }, 175 }, 176 wantErr: true, 177 }, 178 } 179 for _, tt := range tests { 180 t.Run(tt.name, func(t *testing.T) { 181 err := tt.profile.ValidateCreate() 182 if tt.wantErr { 183 g.Expect(err).To(HaveOccurred()) 184 } else { 185 g.Expect(err).To(Succeed()) 186 } 187 }) 188 } 189 }