sigs.k8s.io/cluster-api-provider-aws@v1.5.5/pkg/cloud/scope/shared_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 scope 18 19 import ( 20 "testing" 21 22 "github.com/go-logr/logr" 23 . "github.com/onsi/gomega" 24 "k8s.io/klog/v2/klogr" 25 26 infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta1" 27 ) 28 29 func TestSubnetPlacement(t *testing.T) { 30 testCases := []struct { 31 name string 32 specSubnetIDs []string 33 specAZs []string 34 parentAZs []string 35 controlPlaneSubnets infrav1.Subnets 36 logger logr.Logger 37 expectedSubnetIDs []string 38 expectError bool 39 }{ 40 { 41 name: "spec subnets expected", 42 specSubnetIDs: []string{"az1"}, 43 specAZs: []string{"eu-west-1b"}, 44 parentAZs: []string{"eu-west-1c"}, 45 controlPlaneSubnets: infrav1.Subnets{ 46 infrav1.SubnetSpec{ 47 ID: "az1", 48 AvailabilityZone: "eu-west-1a", 49 }, 50 infrav1.SubnetSpec{ 51 ID: "az2", 52 AvailabilityZone: "eu-west-1b", 53 }, 54 infrav1.SubnetSpec{ 55 ID: "az3", 56 AvailabilityZone: "eu-west-1c", 57 }, 58 }, 59 logger: klogr.New(), 60 expectedSubnetIDs: []string{"az1"}, 61 expectError: false, 62 }, 63 { 64 name: "spec azs expected", 65 specSubnetIDs: []string{}, 66 specAZs: []string{"eu-west-1b"}, 67 parentAZs: []string{"eu-west-1c"}, 68 controlPlaneSubnets: infrav1.Subnets{ 69 infrav1.SubnetSpec{ 70 ID: "az1", 71 AvailabilityZone: "eu-west-1a", 72 }, 73 infrav1.SubnetSpec{ 74 ID: "az2", 75 AvailabilityZone: "eu-west-1b", 76 }, 77 infrav1.SubnetSpec{ 78 ID: "az3", 79 AvailabilityZone: "eu-west-1c", 80 }, 81 }, 82 logger: klogr.New(), 83 expectedSubnetIDs: []string{"az2"}, 84 expectError: false, 85 }, 86 { 87 name: "parent azs expected", 88 specSubnetIDs: []string{}, 89 specAZs: []string{}, 90 parentAZs: []string{"eu-west-1c"}, 91 controlPlaneSubnets: infrav1.Subnets{ 92 infrav1.SubnetSpec{ 93 ID: "az1", 94 AvailabilityZone: "eu-west-1a", 95 }, 96 infrav1.SubnetSpec{ 97 ID: "az2", 98 AvailabilityZone: "eu-west-1b", 99 }, 100 infrav1.SubnetSpec{ 101 ID: "az3", 102 AvailabilityZone: "eu-west-1c", 103 }, 104 }, 105 logger: klogr.New(), 106 expectedSubnetIDs: []string{"az3"}, 107 expectError: false, 108 }, 109 { 110 name: "use control plane subnets", 111 specSubnetIDs: []string{}, 112 specAZs: []string{}, 113 parentAZs: []string{}, 114 controlPlaneSubnets: infrav1.Subnets{ 115 infrav1.SubnetSpec{ 116 ID: "az1", 117 AvailabilityZone: "eu-west-1a", 118 IsPublic: false, 119 }, 120 infrav1.SubnetSpec{ 121 ID: "az2", 122 AvailabilityZone: "eu-west-1b", 123 IsPublic: false, 124 }, 125 infrav1.SubnetSpec{ 126 ID: "az3", 127 AvailabilityZone: "eu-west-1c", 128 IsPublic: true, 129 }, 130 }, 131 logger: klogr.New(), 132 expectedSubnetIDs: []string{"az1", "az2"}, 133 expectError: false, 134 }, 135 { 136 name: "no placement", 137 specSubnetIDs: []string{}, 138 specAZs: []string{}, 139 parentAZs: []string{}, 140 controlPlaneSubnets: infrav1.Subnets{}, 141 logger: klogr.New(), 142 expectedSubnetIDs: []string{}, 143 expectError: true, 144 }, 145 } 146 147 for _, tc := range testCases { 148 t.Run(tc.name, func(t *testing.T) { 149 g := NewGomegaWithT(t) 150 151 strategy, err := newDefaultSubnetPlacementStrategy(&tc.logger) 152 g.Expect(err).NotTo(HaveOccurred()) 153 154 actualSubnetIDs, err := strategy.Place(&placementInput{ 155 SpecSubnetIDs: tc.specSubnetIDs, 156 SpecAvailabilityZones: tc.specAZs, 157 ParentAvailabilityZones: tc.parentAZs, 158 ControlplaneSubnets: tc.controlPlaneSubnets, 159 }) 160 161 if tc.expectError { 162 g.Expect(err).ToNot(BeNil()) 163 return 164 } 165 166 g.Expect(err).To(BeNil()) 167 g.Expect(actualSubnetIDs).To(Equal(tc.expectedSubnetIDs)) 168 }) 169 } 170 }