Randomize test nodePort to prevent collision

This commit is contained in:
Zihong Zheng 2017-05-18 15:29:22 -07:00
parent 3b26db6d67
commit f0739a5638
2 changed files with 17 additions and 7 deletions

View File

@ -71,6 +71,7 @@ go_test(
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library", "//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/intstr:go_default_library", "//vendor/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/net:go_default_library", "//vendor/k8s.io/apimachinery/pkg/util/net:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/rand:go_default_library",
"//vendor/k8s.io/apiserver/pkg/endpoints/request:go_default_library", "//vendor/k8s.io/apiserver/pkg/endpoints/request:go_default_library",
"//vendor/k8s.io/apiserver/pkg/registry/rest:go_default_library", "//vendor/k8s.io/apiserver/pkg/registry/rest:go_default_library",
"//vendor/k8s.io/apiserver/pkg/util/feature:go_default_library", "//vendor/k8s.io/apiserver/pkg/util/feature:go_default_library",

View File

@ -17,16 +17,19 @@ limitations under the License.
package service package service
import ( import (
"testing"
"fmt"
"net" "net"
"reflect" "reflect"
"strings" "strings"
"testing"
"k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr" "k8s.io/apimachinery/pkg/util/intstr"
utilnet "k8s.io/apimachinery/pkg/util/net" utilnet "k8s.io/apimachinery/pkg/util/net"
"k8s.io/apimachinery/pkg/util/rand"
genericapirequest "k8s.io/apiserver/pkg/endpoints/request" genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/registry/rest" "k8s.io/apiserver/pkg/registry/rest"
utilfeature "k8s.io/apiserver/pkg/util/feature" utilfeature "k8s.io/apiserver/pkg/util/feature"
@ -46,6 +49,10 @@ func init() {
// It is now testing mostly the same things as other resources but // It is now testing mostly the same things as other resources but
// in a completely different way. We should unify it. // in a completely different way. We should unify it.
func generateRandomNodePort() int32 {
return int32(rand.IntnRange(30001, 30999))
}
func NewTestREST(t *testing.T, endpoints *api.EndpointsList) (*REST, *registrytest.ServiceRegistry) { func NewTestREST(t *testing.T, endpoints *api.EndpointsList) (*REST, *registrytest.ServiceRegistry) {
registry := registrytest.NewServiceRegistry() registry := registrytest.NewServiceRegistry()
endpointRegistry := &registrytest.EndpointRegistry{ endpointRegistry := &registrytest.EndpointRegistry{
@ -1034,6 +1041,7 @@ func TestServiceRegistryExternalTrafficHealthCheckNodePortAllocationBeta(t *test
// Validate using the user specified nodePort when ExternalTrafficPolicy is set to Local // Validate using the user specified nodePort when ExternalTrafficPolicy is set to Local
// and type is LoadBalancer. // and type is LoadBalancer.
func TestServiceRegistryExternalTrafficHealthCheckNodePortUserAllocation(t *testing.T) { func TestServiceRegistryExternalTrafficHealthCheckNodePortUserAllocation(t *testing.T) {
randomNodePort := generateRandomNodePort()
ctx := genericapirequest.NewDefaultContext() ctx := genericapirequest.NewDefaultContext()
storage, _ := NewTestREST(t, nil) storage, _ := NewTestREST(t, nil)
svc := &api.Service{ svc := &api.Service{
@ -1048,7 +1056,7 @@ func TestServiceRegistryExternalTrafficHealthCheckNodePortUserAllocation(t *test
TargetPort: intstr.FromInt(6502), TargetPort: intstr.FromInt(6502),
}}, }},
ExternalTrafficPolicy: api.ServiceExternalTrafficPolicyTypeLocal, ExternalTrafficPolicy: api.ServiceExternalTrafficPolicyTypeLocal,
HealthCheckNodePort: int32(30200), HealthCheckNodePort: randomNodePort,
}, },
} }
created_svc, err := storage.Create(ctx, svc) created_svc, err := storage.Create(ctx, svc)
@ -1063,14 +1071,15 @@ func TestServiceRegistryExternalTrafficHealthCheckNodePortUserAllocation(t *test
if port == 0 { if port == 0 {
t.Errorf("Failed to allocate health check node port and set the HealthCheckNodePort") t.Errorf("Failed to allocate health check node port and set the HealthCheckNodePort")
} }
if port != 30200 { if port != randomNodePort {
t.Errorf("Failed to allocate requested nodePort expected 30200, got %d", port) t.Errorf("Failed to allocate requested nodePort expected %d, got %d", randomNodePort, port)
} }
} }
// Validate using the user specified nodePort when ExternalTraffic beta annotation is set to OnlyLocal // Validate using the user specified nodePort when ExternalTraffic beta annotation is set to OnlyLocal
// and type is LoadBalancer. // and type is LoadBalancer.
func TestServiceRegistryExternalTrafficHealthCheckNodePortUserAllocationBeta(t *testing.T) { func TestServiceRegistryExternalTrafficHealthCheckNodePortUserAllocationBeta(t *testing.T) {
randomNodePort := generateRandomNodePort()
ctx := genericapirequest.NewDefaultContext() ctx := genericapirequest.NewDefaultContext()
storage, _ := NewTestREST(t, nil) storage, _ := NewTestREST(t, nil)
svc := &api.Service{ svc := &api.Service{
@ -1078,7 +1087,7 @@ func TestServiceRegistryExternalTrafficHealthCheckNodePortUserAllocationBeta(t *
Name: "external-lb-esipp", Name: "external-lb-esipp",
Annotations: map[string]string{ Annotations: map[string]string{
api.BetaAnnotationExternalTraffic: api.AnnotationValueExternalTrafficLocal, api.BetaAnnotationExternalTraffic: api.AnnotationValueExternalTrafficLocal,
api.BetaAnnotationHealthCheckNodePort: "30200", api.BetaAnnotationHealthCheckNodePort: fmt.Sprintf("%v", randomNodePort),
}, },
}, },
Spec: api.ServiceSpec{ Spec: api.ServiceSpec{
@ -1104,8 +1113,8 @@ func TestServiceRegistryExternalTrafficHealthCheckNodePortUserAllocationBeta(t *
if port == 0 { if port == 0 {
t.Errorf("Failed to allocate health check node port and set the HealthCheckNodePort") t.Errorf("Failed to allocate health check node port and set the HealthCheckNodePort")
} }
if port != 30200 { if port != randomNodePort {
t.Errorf("Failed to allocate requested nodePort expected 30200, got %d", port) t.Errorf("Failed to allocate requested nodePort expected %d, got %d", randomNodePort, port)
} }
} }