From 2d1ee816a1c29bb1a14f071195d77ba38267abe2 Mon Sep 17 00:00:00 2001 From: Satnam Singh Date: Fri, 23 Jan 2015 17:06:13 -0800 Subject: [PATCH] Change TestNetwork to allow parallel execution --- test/e2e/network.go | 60 ++++++++++++++++++++++++++++++++++++++------- test/e2e/util.go | 11 +++++++++ 2 files changed, 62 insertions(+), 9 deletions(-) diff --git a/test/e2e/network.go b/test/e2e/network.go index 2d778d912eb..dcc6a6a7512 100644 --- a/test/e2e/network.go +++ b/test/e2e/network.go @@ -21,6 +21,7 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/client" + "github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/golang/glog" ) @@ -31,11 +32,27 @@ func TestNetwork(c *client.Client) bool { } ns := api.NamespaceDefault - svc, err := c.Services(ns).Create(loadObjectOrDie(assetPath( - "contrib", "for-tests", "network-tester", "service.json", - )).(*api.Service)) + // TODO(satnam6502): Replace call of randomSuffix with call to NewUUID when service + // names have the same form as pod and replication controller names. + name := "nettest-" + randomSuffix() + svc, err := c.Services(ns).Create(&api.Service{ + ObjectMeta: api.ObjectMeta{ + Name: name, + Labels: map[string]string{ + "name": name, + }, + }, + Spec: api.ServiceSpec{ + Port: 8080, + ContainerPort: util.NewIntOrStringFromInt(8080), + Selector: map[string]string{ + "name": name, + }, + }, + }) + glog.Infof("Creating service with name %s", svc.Name) if err != nil { - glog.Errorf("unable to create test service: %v", err) + glog.Errorf("unable to create test service %s: %v", svc.Name, err) return false } // Clean up service @@ -44,10 +61,35 @@ func TestNetwork(c *client.Client) bool { glog.Errorf("unable to delete svc %v: %v", svc.Name, err) } }() - - rc, err := c.ReplicationControllers(ns).Create(loadObjectOrDie(assetPath( - "contrib", "for-tests", "network-tester", "rc.json", - )).(*api.ReplicationController)) + rc, err := c.ReplicationControllers(ns).Create(&api.ReplicationController{ + ObjectMeta: api.ObjectMeta{ + Name: name, + Labels: map[string]string{ + "name": name, + }, + }, + Spec: api.ReplicationControllerSpec{ + Replicas: 8, + Selector: map[string]string{ + "name": name, + }, + Template: &api.PodTemplateSpec{ + ObjectMeta: api.ObjectMeta{ + Labels: map[string]string{"name": name}, + }, + Spec: api.PodSpec{ + Containers: []api.Container{ + { + Name: "webserver", + Image: "kubernetes/nettest:latest", + Command: []string{"-service=" + name}, + Ports: []api.Port{{ContainerPort: 8080}}, + }, + }, + }, + }, + }, + }) if err != nil { glog.Errorf("unable to create test rc: %v", err) return false @@ -66,7 +108,7 @@ func TestNetwork(c *client.Client) bool { }() const maxAttempts = 60 for i := 0; i < maxAttempts; i++ { - time.Sleep(time.Second) + time.Sleep(2 * time.Second) body, err := c.Get().Prefix("proxy").Resource("services").Name(svc.Name).Suffix("status").Do().Raw() if err != nil { glog.Infof("Attempt %v/%v: service/pod still starting. (error: '%v')", i, maxAttempts, err) diff --git a/test/e2e/util.go b/test/e2e/util.go index 03baac6ba44..07befd14dcf 100644 --- a/test/e2e/util.go +++ b/test/e2e/util.go @@ -18,7 +18,9 @@ package e2e import ( "io/ioutil" + "math/rand" "path/filepath" + "strconv" "time" "github.com/GoogleCloudPlatform/kubernetes/pkg/api" @@ -183,3 +185,12 @@ func parseServiceOrDie(json string) *api.Service { } return service } + +// TODO: Allow service names to have the same form as names +// for pods and replication controllers so we don't +// need to use such a function and can instead +// use the UUID utilty function. +func randomSuffix() string { + r := rand.New(rand.NewSource(time.Now().UnixNano())) + return strconv.Itoa(r.Int() % 10000) +}