From a5aba23813e231dce599811199109d542f840ed9 Mon Sep 17 00:00:00 2001 From: Andrew Sy Kim Date: Mon, 23 Nov 2020 22:29:41 -0500 Subject: [PATCH] test/integration: add helper function serviceHasNodePorts for service load balancer tests Signed-off-by: Andrew Sy Kim --- test/integration/service/loadbalancer_test.go | 37 +++++++------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/test/integration/service/loadbalancer_test.go b/test/integration/service/loadbalancer_test.go index 1a1a7916a3f..4887a849810 100644 --- a/test/integration/service/loadbalancer_test.go +++ b/test/integration/service/loadbalancer_test.go @@ -70,14 +70,7 @@ func Test_ServiceLoadBalancerDisableAllocateNodePorts(t *testing.T) { t.Fatalf("Error creating test service: %v", err) } - foundNodePorts := false - for _, port := range service.Spec.Ports { - if port.NodePort > 0 { - foundNodePorts = true - } - } - - if foundNodePorts { + if serviceHasNodePorts(service) { t.Error("found node ports when none was expected") } } @@ -121,14 +114,7 @@ func Test_ServiceLoadBalancerEnableThenDisableAllocatedNodePorts(t *testing.T) { t.Fatalf("Error creating test service: %v", err) } - foundNodePorts := false - for _, port := range service.Spec.Ports { - if port.NodePort > 0 { - foundNodePorts = true - } - } - - if !foundNodePorts { + if !serviceHasNodePorts(service) { t.Error("expected node ports but found none") } @@ -138,14 +124,17 @@ func Test_ServiceLoadBalancerEnableThenDisableAllocatedNodePorts(t *testing.T) { t.Fatalf("Error updating test service: %v", err) } - foundNodePorts = false - for _, port := range service.Spec.Ports { - if port.NodePort > 0 { - foundNodePorts = true - } - } - - if !foundNodePorts { + if !serviceHasNodePorts(service) { t.Error("node ports were unexpectedly deallocated") } } + +func serviceHasNodePorts(svc *corev1.Service) bool { + for _, port := range svc.Spec.Ports { + if port.NodePort > 0 { + return true + } + } + + return false +}