diff --git a/pkg/registry/core/service/storage/rest_test.go b/pkg/registry/core/service/storage/rest_test.go index d00aa81a5bf..edcc8157bcb 100644 --- a/pkg/registry/core/service/storage/rest_test.go +++ b/pkg/registry/core/service/storage/rest_test.go @@ -288,25 +288,6 @@ func makeIPNet6(t *testing.T) *net.IPNet { return net } -func ipIsAllocated(t *testing.T, alloc ipallocator.Interface, ipstr string) bool { - t.Helper() - ip := netutils.ParseIPSloppy(ipstr) - if ip == nil { - t.Errorf("error parsing IP %q", ipstr) - return false - } - return alloc.Has(ip) -} - -func portIsAllocated(t *testing.T, alloc portallocator.Interface, port int32) bool { - t.Helper() - if port == 0 { - t.Errorf("port is 0") - return false - } - return alloc.Has(int(port)) -} - func TestServiceRegistryCreate(t *testing.T) { testCases := []struct { svc *api.Service diff --git a/pkg/registry/core/service/storage/storage_test.go b/pkg/registry/core/service/storage/storage_test.go index a8fc1f47e1a..6397beb9e39 100644 --- a/pkg/registry/core/service/storage/storage_test.go +++ b/pkg/registry/core/service/storage/storage_test.go @@ -61,6 +61,25 @@ func makePortAllocator(ports machineryutilnet.PortRange) portallocator.Interface return al } +func ipIsAllocated(t *testing.T, alloc ipallocator.Interface, ipstr string) bool { + t.Helper() + ip := netutils.ParseIPSloppy(ipstr) + if ip == nil { + t.Errorf("error parsing IP %q", ipstr) + return false + } + return alloc.Has(ip) +} + +func portIsAllocated(t *testing.T, alloc portallocator.Interface, port int32) bool { + t.Helper() + if port == 0 { + t.Errorf("port is 0") + return false + } + return alloc.Has(int(port)) +} + func newStorage(t *testing.T, ipFamilies []api.IPFamily) (*GenericREST, *StatusREST, *etcd3testing.EtcdTestServer) { etcdStorage, server := registrytest.NewEtcdStorage(t, "") restOptions := generic.RESTOptions{ @@ -5134,7 +5153,7 @@ func TestCreateDryRun(t *testing.T) { // Ensure the IP allocators are clean. if !tc.enableDualStack { - if storage.alloc.serviceIPAllocatorsByFamily[api.IPv4Protocol].Has(netutils.ParseIPSloppy(createdSvc.Spec.ClusterIP)) { + if ipIsAllocated(t, storage.alloc.serviceIPAllocatorsByFamily[api.IPv4Protocol], createdSvc.Spec.ClusterIP) { t.Errorf("expected IP to not be allocated: %v", createdSvc.Spec.ClusterIP) } } else { @@ -5144,7 +5163,7 @@ func TestCreateDryRun(t *testing.T) { } } for i, fam := range createdSvc.Spec.IPFamilies { - if storage.alloc.serviceIPAllocatorsByFamily[fam].Has(netutils.ParseIPSloppy(createdSvc.Spec.ClusterIPs[i])) { + if ipIsAllocated(t, storage.alloc.serviceIPAllocatorsByFamily[fam], createdSvc.Spec.ClusterIPs[i]) { t.Errorf("expected IP to not be allocated: %v", createdSvc.Spec.ClusterIPs[i]) } } @@ -5152,10 +5171,7 @@ func TestCreateDryRun(t *testing.T) { if tc.svc.Spec.Type != api.ServiceTypeClusterIP { for _, p := range createdSvc.Spec.Ports { - if p.NodePort == 0 { - t.Errorf("expected a NodePort value") - } - if storage.alloc.serviceNodePorts.Has(int(p.NodePort)) { + if portIsAllocated(t, storage.alloc.serviceNodePorts, p.NodePort) { t.Errorf("expected port to not be allocated: %v", p.NodePort) } }