mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 04:33:26 +00:00
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:
parent
e338c9db4b
commit
b880d3a149
@ -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
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user