|
| 1 | +package model |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/go-logr/logr" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + elbv2gw "sigs.k8s.io/aws-load-balancer-controller/apis/gateway/v1beta1" |
| 9 | + elbv2model "sigs.k8s.io/aws-load-balancer-controller/pkg/model/elbv2" |
| 10 | + "sigs.k8s.io/aws-load-balancer-controller/pkg/shared_constants" |
| 11 | +) |
| 12 | + |
| 13 | +func Test_baseModelBuilder_isDeleteProtected(t *testing.T) { |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + lbConf elbv2gw.LoadBalancerConfiguration |
| 17 | + want bool |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "deletion protection enabled", |
| 21 | + lbConf: elbv2gw.LoadBalancerConfiguration{ |
| 22 | + Spec: elbv2gw.LoadBalancerConfigurationSpec{ |
| 23 | + LoadBalancerAttributes: []elbv2gw.LoadBalancerAttribute{ |
| 24 | + { |
| 25 | + Key: shared_constants.LBAttributeDeletionProtection, |
| 26 | + Value: "true", |
| 27 | + }, |
| 28 | + }, |
| 29 | + }, |
| 30 | + }, |
| 31 | + want: true, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "deletion protection disabled", |
| 35 | + lbConf: elbv2gw.LoadBalancerConfiguration{ |
| 36 | + Spec: elbv2gw.LoadBalancerConfigurationSpec{ |
| 37 | + LoadBalancerAttributes: []elbv2gw.LoadBalancerAttribute{ |
| 38 | + { |
| 39 | + Key: shared_constants.LBAttributeDeletionProtection, |
| 40 | + Value: "false", |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + want: false, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "deletion protection not specified", |
| 49 | + lbConf: elbv2gw.LoadBalancerConfiguration{ |
| 50 | + Spec: elbv2gw.LoadBalancerConfigurationSpec{ |
| 51 | + LoadBalancerAttributes: []elbv2gw.LoadBalancerAttribute{}, |
| 52 | + }, |
| 53 | + }, |
| 54 | + want: false, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "deletion protection with invalid value", |
| 58 | + lbConf: elbv2gw.LoadBalancerConfiguration{ |
| 59 | + Spec: elbv2gw.LoadBalancerConfigurationSpec{ |
| 60 | + LoadBalancerAttributes: []elbv2gw.LoadBalancerAttribute{ |
| 61 | + { |
| 62 | + Key: shared_constants.LBAttributeDeletionProtection, |
| 63 | + Value: "invalid", |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + want: false, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "deletion protection among other attributes", |
| 72 | + lbConf: elbv2gw.LoadBalancerConfiguration{ |
| 73 | + Spec: elbv2gw.LoadBalancerConfigurationSpec{ |
| 74 | + LoadBalancerAttributes: []elbv2gw.LoadBalancerAttribute{ |
| 75 | + { |
| 76 | + Key: "idle_timeout.timeout_seconds", |
| 77 | + Value: "60", |
| 78 | + }, |
| 79 | + { |
| 80 | + Key: shared_constants.LBAttributeDeletionProtection, |
| 81 | + Value: "true", |
| 82 | + }, |
| 83 | + { |
| 84 | + Key: "access_logs.s3.enabled", |
| 85 | + Value: "false", |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + want: true, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "empty config", |
| 94 | + lbConf: elbv2gw.LoadBalancerConfiguration{}, |
| 95 | + want: false, |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + for _, tt := range tests { |
| 100 | + t.Run(tt.name, func(t *testing.T) { |
| 101 | + builder := &baseModelBuilder{ |
| 102 | + logger: logr.Discard(), |
| 103 | + } |
| 104 | + got := builder.isDeleteProtected(tt.lbConf) |
| 105 | + assert.Equal(t, tt.want, got) |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func Test_baseModelBuilder_buildLoadBalancerScheme(t *testing.T) { |
| 111 | + internetFacing := elbv2gw.LoadBalancerScheme(elbv2model.LoadBalancerSchemeInternetFacing) |
| 112 | + internal := elbv2gw.LoadBalancerScheme(elbv2model.LoadBalancerSchemeInternal) |
| 113 | + unknown := elbv2gw.LoadBalancerScheme("unknown") |
| 114 | + |
| 115 | + tests := []struct { |
| 116 | + name string |
| 117 | + lbConf elbv2gw.LoadBalancerConfiguration |
| 118 | + defaultScheme elbv2model.LoadBalancerScheme |
| 119 | + want elbv2model.LoadBalancerScheme |
| 120 | + wantErr bool |
| 121 | + }{ |
| 122 | + { |
| 123 | + name: "internet-facing scheme", |
| 124 | + lbConf: elbv2gw.LoadBalancerConfiguration{ |
| 125 | + Spec: elbv2gw.LoadBalancerConfigurationSpec{ |
| 126 | + Scheme: &internetFacing, |
| 127 | + }, |
| 128 | + }, |
| 129 | + defaultScheme: elbv2model.LoadBalancerSchemeInternal, |
| 130 | + want: elbv2model.LoadBalancerSchemeInternetFacing, |
| 131 | + wantErr: false, |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "internal scheme", |
| 135 | + lbConf: elbv2gw.LoadBalancerConfiguration{ |
| 136 | + Spec: elbv2gw.LoadBalancerConfigurationSpec{ |
| 137 | + Scheme: &internal, |
| 138 | + }, |
| 139 | + }, |
| 140 | + defaultScheme: elbv2model.LoadBalancerSchemeInternetFacing, |
| 141 | + want: elbv2model.LoadBalancerSchemeInternal, |
| 142 | + wantErr: false, |
| 143 | + }, |
| 144 | + { |
| 145 | + name: "nil scheme uses default", |
| 146 | + lbConf: elbv2gw.LoadBalancerConfiguration{ |
| 147 | + Spec: elbv2gw.LoadBalancerConfigurationSpec{ |
| 148 | + Scheme: nil, |
| 149 | + }, |
| 150 | + }, |
| 151 | + defaultScheme: elbv2model.LoadBalancerSchemeInternal, |
| 152 | + want: elbv2model.LoadBalancerSchemeInternal, |
| 153 | + wantErr: false, |
| 154 | + }, |
| 155 | + { |
| 156 | + name: "unknown scheme returns error", |
| 157 | + lbConf: elbv2gw.LoadBalancerConfiguration{ |
| 158 | + Spec: elbv2gw.LoadBalancerConfigurationSpec{ |
| 159 | + Scheme: &unknown, |
| 160 | + }, |
| 161 | + }, |
| 162 | + defaultScheme: elbv2model.LoadBalancerSchemeInternal, |
| 163 | + want: "", |
| 164 | + wantErr: true, |
| 165 | + }, |
| 166 | + } |
| 167 | + |
| 168 | + for _, tt := range tests { |
| 169 | + t.Run(tt.name, func(t *testing.T) { |
| 170 | + builder := &baseModelBuilder{ |
| 171 | + defaultLoadBalancerScheme: tt.defaultScheme, |
| 172 | + } |
| 173 | + got, err := builder.buildLoadBalancerScheme(tt.lbConf) |
| 174 | + if tt.wantErr { |
| 175 | + assert.Error(t, err) |
| 176 | + return |
| 177 | + } |
| 178 | + assert.NoError(t, err) |
| 179 | + assert.Equal(t, tt.want, got) |
| 180 | + }) |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +func Test_baseModelBuilder_buildLoadBalancerIPAddressType(t *testing.T) { |
| 185 | + ipv4 := elbv2gw.LoadBalancerIpAddressType(elbv2model.IPAddressTypeIPV4) |
| 186 | + dualStack := elbv2gw.LoadBalancerIpAddressType(elbv2model.IPAddressTypeDualStack) |
| 187 | + dualStackWithoutPublicIPv4 := elbv2gw.LoadBalancerIpAddressType(elbv2model.IPAddressTypeDualStackWithoutPublicIPV4) |
| 188 | + unknown := elbv2gw.LoadBalancerIpAddressType("unknown") |
| 189 | + |
| 190 | + tests := []struct { |
| 191 | + name string |
| 192 | + lbConf elbv2gw.LoadBalancerConfiguration |
| 193 | + defaultType elbv2model.IPAddressType |
| 194 | + want elbv2model.IPAddressType |
| 195 | + wantErr bool |
| 196 | + }{ |
| 197 | + { |
| 198 | + name: "ipv4 address type", |
| 199 | + lbConf: elbv2gw.LoadBalancerConfiguration{ |
| 200 | + Spec: elbv2gw.LoadBalancerConfigurationSpec{ |
| 201 | + IpAddressType: &ipv4, |
| 202 | + }, |
| 203 | + }, |
| 204 | + defaultType: elbv2model.IPAddressTypeDualStack, |
| 205 | + want: elbv2model.IPAddressTypeIPV4, |
| 206 | + wantErr: false, |
| 207 | + }, |
| 208 | + { |
| 209 | + name: "dualstack address type", |
| 210 | + lbConf: elbv2gw.LoadBalancerConfiguration{ |
| 211 | + Spec: elbv2gw.LoadBalancerConfigurationSpec{ |
| 212 | + IpAddressType: &dualStack, |
| 213 | + }, |
| 214 | + }, |
| 215 | + defaultType: elbv2model.IPAddressTypeIPV4, |
| 216 | + want: elbv2model.IPAddressTypeDualStack, |
| 217 | + wantErr: false, |
| 218 | + }, |
| 219 | + { |
| 220 | + name: "dualstack without public ipv4 address type", |
| 221 | + lbConf: elbv2gw.LoadBalancerConfiguration{ |
| 222 | + Spec: elbv2gw.LoadBalancerConfigurationSpec{ |
| 223 | + IpAddressType: &dualStackWithoutPublicIPv4, |
| 224 | + }, |
| 225 | + }, |
| 226 | + defaultType: elbv2model.IPAddressTypeIPV4, |
| 227 | + want: elbv2model.IPAddressTypeDualStackWithoutPublicIPV4, |
| 228 | + wantErr: false, |
| 229 | + }, |
| 230 | + { |
| 231 | + name: "nil address type uses default", |
| 232 | + lbConf: elbv2gw.LoadBalancerConfiguration{ |
| 233 | + Spec: elbv2gw.LoadBalancerConfigurationSpec{ |
| 234 | + IpAddressType: nil, |
| 235 | + }, |
| 236 | + }, |
| 237 | + defaultType: elbv2model.IPAddressTypeIPV4, |
| 238 | + want: elbv2model.IPAddressTypeIPV4, |
| 239 | + wantErr: false, |
| 240 | + }, |
| 241 | + { |
| 242 | + name: "unknown address type returns error", |
| 243 | + lbConf: elbv2gw.LoadBalancerConfiguration{ |
| 244 | + Spec: elbv2gw.LoadBalancerConfigurationSpec{ |
| 245 | + IpAddressType: &unknown, |
| 246 | + }, |
| 247 | + }, |
| 248 | + defaultType: elbv2model.IPAddressTypeIPV4, |
| 249 | + want: "", |
| 250 | + wantErr: true, |
| 251 | + }, |
| 252 | + } |
| 253 | + |
| 254 | + for _, tt := range tests { |
| 255 | + t.Run(tt.name, func(t *testing.T) { |
| 256 | + builder := &baseModelBuilder{ |
| 257 | + defaultIPType: tt.defaultType, |
| 258 | + } |
| 259 | + got, err := builder.buildLoadBalancerIPAddressType(tt.lbConf) |
| 260 | + if tt.wantErr { |
| 261 | + assert.Error(t, err) |
| 262 | + return |
| 263 | + } |
| 264 | + assert.NoError(t, err) |
| 265 | + assert.Equal(t, tt.want, got) |
| 266 | + }) |
| 267 | + } |
| 268 | +} |
0 commit comments