Svc REST: better test checks in new tests

"Has()" was strengthened in the older rest_test, now in the newer.
This commit is contained in:
Tim Hockin 2021-07-02 19:40:37 -07:00
parent e338c9db4b
commit b880d3a149
2 changed files with 22 additions and 25 deletions

View File

@ -288,25 +288,6 @@ func makeIPNet6(t *testing.T) *net.IPNet {
return net 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) { func TestServiceRegistryCreate(t *testing.T) {
testCases := []struct { testCases := []struct {
svc *api.Service svc *api.Service

View File

@ -61,6 +61,25 @@ func makePortAllocator(ports machineryutilnet.PortRange) portallocator.Interface
return al 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) { func newStorage(t *testing.T, ipFamilies []api.IPFamily) (*GenericREST, *StatusREST, *etcd3testing.EtcdTestServer) {
etcdStorage, server := registrytest.NewEtcdStorage(t, "") etcdStorage, server := registrytest.NewEtcdStorage(t, "")
restOptions := generic.RESTOptions{ restOptions := generic.RESTOptions{
@ -5134,7 +5153,7 @@ func TestCreateDryRun(t *testing.T) {
// Ensure the IP allocators are clean. // Ensure the IP allocators are clean.
if !tc.enableDualStack { 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) t.Errorf("expected IP to not be allocated: %v", createdSvc.Spec.ClusterIP)
} }
} else { } else {
@ -5144,7 +5163,7 @@ func TestCreateDryRun(t *testing.T) {
} }
} }
for i, fam := range createdSvc.Spec.IPFamilies { 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]) 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 { if tc.svc.Spec.Type != api.ServiceTypeClusterIP {
for _, p := range createdSvc.Spec.Ports { for _, p := range createdSvc.Spec.Ports {
if p.NodePort == 0 { if portIsAllocated(t, storage.alloc.serviceNodePorts, p.NodePort) {
t.Errorf("expected a NodePort value")
}
if storage.alloc.serviceNodePorts.Has(int(p.NodePort)) {
t.Errorf("expected port to not be allocated: %v", p.NodePort) t.Errorf("expected port to not be allocated: %v", p.NodePort)
} }
} }