Skip to content

Commit 8f489e7

Browse files
add unit tests
1 parent 1027221 commit 8f489e7

File tree

4 files changed

+110
-4
lines changed

4 files changed

+110
-4
lines changed

pkg/consts/consts.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,7 @@ const (
382382
// LoadBalancerBackendPoolConfigurationTypeNodeIP is the lb backend pool config type node ip
383383
LoadBalancerBackendPoolConfigurationTypeNodeIP = "nodeIP"
384384
// LoadBalancerBackendPoolConfigurationTypePODIP is the lb backend pool config type pod ip
385-
// TODO (nilo19): support pod IP in the future
386-
LoadBalancerBackendPoolConfigurationTypePODIP = "podIP"
385+
LoadBalancerBackendPoolConfigurationTypePodIP = "podIP"
387386
)
388387

389388
// error messages

pkg/provider/azure.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,13 @@ func (az *Cloud) InitializeCloudFromConfig(ctx context.Context, config *config.C
294294

295295
if config.LoadBalancerBackendPoolConfigurationType == "" ||
296296
// TODO(nilo19): support pod IP mode in the future
297-
strings.EqualFold(config.LoadBalancerBackendPoolConfigurationType, consts.LoadBalancerBackendPoolConfigurationTypePODIP) {
297+
strings.EqualFold(config.LoadBalancerBackendPoolConfigurationType, consts.LoadBalancerBackendPoolConfigurationTypePodIP) {
298298
config.LoadBalancerBackendPoolConfigurationType = consts.LoadBalancerBackendPoolConfigurationTypeNodeIPConfiguration
299299
} else {
300300
supportedLoadBalancerBackendPoolConfigurationTypes := utilsets.NewString(
301301
strings.ToLower(consts.LoadBalancerBackendPoolConfigurationTypeNodeIPConfiguration),
302302
strings.ToLower(consts.LoadBalancerBackendPoolConfigurationTypeNodeIP),
303-
strings.ToLower(consts.LoadBalancerBackendPoolConfigurationTypePODIP))
303+
strings.ToLower(consts.LoadBalancerBackendPoolConfigurationTypePodIP))
304304
if !supportedLoadBalancerBackendPoolConfigurationTypes.Has(strings.ToLower(config.LoadBalancerBackendPoolConfigurationType)) {
305305
return fmt.Errorf("loadBalancerBackendPoolConfigurationType %s is not supported, supported values are %v", config.LoadBalancerBackendPoolConfigurationType, supportedLoadBalancerBackendPoolConfigurationTypes.UnsortedList())
306306
}

pkg/provider/azure_loadbalancer_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -3027,9 +3027,43 @@ func TestReconcileLoadBalancerRuleCommon(t *testing.T) {
30273027
expectedProbes: probes,
30283028
expectedRules: rules1DualStack,
30293029
})
3030+
3031+
testCases = append(testCases, []struct {
3032+
desc string
3033+
service v1.Service
3034+
loadBalancerSKU string
3035+
probeProtocol string
3036+
probePath string
3037+
expectedProbes map[bool][]*armnetwork.Probe
3038+
expectedRules map[bool][]*armnetwork.LoadBalancingRule
3039+
expectedErr bool
3040+
}{
3041+
{
3042+
desc: "LB backend pool of type PodIP - getExpectedLBRules should return error when the service is dual-stack",
3043+
service: getTestServiceDualStack("test", v1.ProtocolTCP, nil, 80),
3044+
loadBalancerSKU: "standardV2",
3045+
expectedErr: true,
3046+
},
3047+
{
3048+
desc: "LB backend pool of type PodIP - getExpectedLBRules should return error when the service port is named",
3049+
service: getTestServiceWithNamedTargetPorts("test", v1.ProtocolTCP, nil, false, 8080, "http-web-svc"),
3050+
loadBalancerSKU: "standardV2",
3051+
expectedErr: true,
3052+
},
3053+
{
3054+
desc: "LB backend pool of type PodIP - getExpectedLBRules should return expected rules and probes",
3055+
service: getTestServiceWithIntTargetPorts("test1", v1.ProtocolTCP, nil, false, 8080, 1234),
3056+
loadBalancerSKU: "standardV2",
3057+
expectedRules: getTestRuleCLB(false, 8080, 1234, false),
3058+
expectedProbes: nil,
3059+
},
3060+
}...)
30303061
for _, test := range testCases {
30313062
t.Run(test.desc, func(t *testing.T) {
30323063
az := GetTestCloud(ctrl)
3064+
if test.loadBalancerSKU == "standardV2" {
3065+
az.LoadBalancerBackendPoolConfigurationType = consts.LoadBalancerBackendPoolConfigurationTypePodIP
3066+
}
30333067
az.Config.LoadBalancerSKU = test.loadBalancerSKU
30343068
service := test.service
30353069
firstPort := service.Spec.Ports[0]
@@ -3130,6 +3164,17 @@ func getTCPResetTestRules(enableTCPReset bool) map[bool][]*armnetwork.LoadBalanc
31303164
}
31313165
}
31323166

3167+
func getTestRuleCLB(enableTCPReset bool, servicePort int32, targetPort int32, isIPv6 bool) map[bool][]*armnetwork.LoadBalancingRule {
3168+
rule := getTestRule(enableTCPReset, servicePort, isIPv6)
3169+
rule.Properties.EnableFloatingIP = to.Ptr(false)
3170+
rule.Properties.Probe = nil
3171+
rule.Properties.BackendPort = &targetPort
3172+
3173+
return map[bool][]*armnetwork.LoadBalancingRule{
3174+
false: {rule},
3175+
}
3176+
}
3177+
31333178
// getTestRule returns a rule for dualStack.
31343179
func getTestRule(enableTCPReset bool, port int32, isIPv6 bool) *armnetwork.LoadBalancingRule {
31353180
suffix := ""

pkg/provider/azure_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
v1 "k8s.io/api/core/v1"
3737
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3838
"k8s.io/apimachinery/pkg/types"
39+
"k8s.io/apimachinery/pkg/util/intstr"
3940
"k8s.io/client-go/informers"
4041
"k8s.io/client-go/kubernetes/fake"
4142
cloudprovider "k8s.io/cloud-provider"
@@ -1259,6 +1260,67 @@ func getTestServiceCommon(identifier string, proto v1.Protocol, annotations map[
12591260
return svc
12601261
}
12611262

1263+
func getTestServiceWithNamedTargetPorts(identifier string, proto v1.Protocol, annotations map[string]string, isIPv6 bool, servicePort int32, namedTargetPorts ...string) v1.Service {
1264+
targetPorts := []intstr.IntOrString{}
1265+
for _, port := range namedTargetPorts {
1266+
targetPorts = append(targetPorts, intstr.FromString(port))
1267+
}
1268+
svc := getTestServiceWithTargetPortsCommon(identifier, proto, annotations, servicePort, targetPorts...)
1269+
svc.Spec.ClusterIP = "10.0.0.2"
1270+
svc.Spec.IPFamilies = []v1.IPFamily{v1.IPv4Protocol}
1271+
if isIPv6 {
1272+
svc.Spec.ClusterIP = "fd00::1907"
1273+
svc.Spec.IPFamilies = []v1.IPFamily{v1.IPv6Protocol}
1274+
}
1275+
1276+
return svc
1277+
}
1278+
1279+
func getTestServiceWithIntTargetPorts(identifier string, proto v1.Protocol, annotations map[string]string, isIPv6 bool, servicePort int32, intTargetPorts ...int32) v1.Service {
1280+
targetPorts := []intstr.IntOrString{}
1281+
for _, port := range intTargetPorts {
1282+
targetPorts = append(targetPorts, intstr.FromInt(int(port)))
1283+
}
1284+
svc := getTestServiceWithTargetPortsCommon(identifier, proto, annotations, servicePort, targetPorts...)
1285+
svc.Spec.ClusterIP = "10.0.0.2"
1286+
svc.Spec.IPFamilies = []v1.IPFamily{v1.IPv4Protocol}
1287+
if isIPv6 {
1288+
svc.Spec.ClusterIP = "fd00::1907"
1289+
svc.Spec.IPFamilies = []v1.IPFamily{v1.IPv6Protocol}
1290+
}
1291+
1292+
return svc
1293+
}
1294+
1295+
func getTestServiceWithTargetPortsCommon(identifier string, proto v1.Protocol, annotations map[string]string, servicePort int32, targetPorts ...intstr.IntOrString) v1.Service {
1296+
ports := []v1.ServicePort{}
1297+
for _, port := range targetPorts {
1298+
ports = append(ports, v1.ServicePort{
1299+
Name: "target-port",
1300+
Protocol: proto,
1301+
TargetPort: port,
1302+
Port: servicePort,
1303+
})
1304+
}
1305+
1306+
svc := v1.Service{
1307+
Spec: v1.ServiceSpec{
1308+
Type: v1.ServiceTypeLoadBalancer,
1309+
Ports: ports,
1310+
},
1311+
}
1312+
svc.Name = identifier
1313+
svc.Namespace = "default"
1314+
svc.UID = types.UID(identifier)
1315+
if annotations == nil {
1316+
svc.Annotations = make(map[string]string)
1317+
} else {
1318+
svc.Annotations = annotations
1319+
}
1320+
1321+
return svc
1322+
}
1323+
12621324
func getInternalTestService(identifier string, requestedPorts ...int32) v1.Service {
12631325
return getTestServiceWithAnnotation(identifier, map[string]string{consts.ServiceAnnotationLoadBalancerInternal: consts.TrueAnnotationValue}, false, requestedPorts...)
12641326
}

0 commit comments

Comments
 (0)