From e548c16f7b02a48632251334cac78b7d4423d1bc Mon Sep 17 00:00:00 2001 From: jayunit100 Date: Fri, 17 Apr 2015 11:22:29 -0400 Subject: [PATCH] Generalized namespace creation pattern for test utils --- test/e2e/networking.go | 46 ++++++++++++++++-------------------------- test/e2e/util.go | 16 +++++++++++++++ 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/test/e2e/networking.go b/test/e2e/networking.go index e16cf8d9c75..1a7a0d7fc31 100644 --- a/test/e2e/networking.go +++ b/test/e2e/networking.go @@ -23,6 +23,8 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/client" + //"github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd" + //"github.com/GoogleCloudPlatform/kubernetes/pkg/clientauth" "github.com/GoogleCloudPlatform/kubernetes/pkg/fields" "github.com/GoogleCloudPlatform/kubernetes/pkg/labels" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" @@ -77,7 +79,7 @@ func LaunchNetTestPodPerNode(nodes *api.NodeList, name string, c *client.Client, var _ = Describe("Networking", func() { //This namespace is modified throughout the course of the test. - var namespaceObj *api.Namespace + var namespace *api.Namespace var svcname = "nettest" var c *client.Client = nil @@ -94,30 +96,19 @@ var _ = Describe("Networking", func() { Failf("Unexpected error code, expected 200, got, %v (%v)", resp.StatusCode, resp) } - By("Building a namespace api object") - var ns = svcname + "-" + randomSuffix() - - namespaceObj = &api.Namespace{ - ObjectMeta: api.ObjectMeta{ - Name: ns, - Namespace: "", - }, - Status: api.NamespaceStatus{}, - } - By("Creating a kubernetes client") c, err = loadClient() Expect(err).NotTo(HaveOccurred()) - By("Creating a namespace for this test suite") - _, err = c.Namespaces().Create(namespaceObj) + By("Building a namespace api object") + namespace, err = createTestingNS("nettest", c) Expect(err).NotTo(HaveOccurred()) }) AfterEach(func() { - By("Destroying namespace for this suite") - if err := c.Namespaces().Delete(namespaceObj.Name); err != nil { + By(fmt.Sprintf("Destroying namespace for this suite %v", namespace.Name)) + if err := c.Namespaces().Delete(namespace.Name); err != nil { Failf("Couldn't delete ns %s", err) } }) @@ -134,7 +125,7 @@ var _ = Describe("Networking", func() { for _, test := range tests { By(fmt.Sprintf("testing: %s", test.path)) data, err := c.RESTClient.Get(). - Namespace(api.NamespaceDefault). + Namespace(namespace.Name). AbsPath(test.path). DoRaw() if err != nil { @@ -143,9 +134,6 @@ var _ = Describe("Networking", func() { } }) - // Create a unique namespace for this test. - ns := "nettest-" + randomSuffix() - //Now we can proceed with the test. It("should function for intra-pod communication", func() { @@ -154,8 +142,8 @@ var _ = Describe("Networking", func() { return } - By(fmt.Sprintf("Creating a service named [%s] in namespace %s", svcname, ns)) - svc, err := c.Services(ns).Create(&api.Service{ + By(fmt.Sprintf("Creating a service named [%s] in namespace %s", svcname, namespace.Name)) + svc, err := c.Services(namespace.Name).Create(&api.Service{ ObjectMeta: api.ObjectMeta{ Name: svcname, Labels: map[string]string{ @@ -181,7 +169,7 @@ var _ = Describe("Networking", func() { defer func() { defer GinkgoRecover() By("Cleaning up the service") - if err = c.Services(ns).Delete(svc.Name); err != nil { + if err = c.Services(namespace.Name).Delete(svc.Name); err != nil { Failf("unable to delete svc %v: %v", svc.Name, err) } }() @@ -193,14 +181,14 @@ var _ = Describe("Networking", func() { Failf("Failed to list nodes: %v", err) } - podNames := LaunchNetTestPodPerNode(nodes, svcname, c, ns) + podNames := LaunchNetTestPodPerNode(nodes, svcname, c, namespace.Name) // Clean up the pods defer func() { defer GinkgoRecover() By("Cleaning up the webserver pods") for _, podName := range podNames { - if err = c.Pods(ns).Delete(podName); err != nil { + if err = c.Pods(namespace.Name).Delete(podName); err != nil { Logf("Failed to delete pod %s: %v", podName, err) } } @@ -208,7 +196,7 @@ var _ = Describe("Networking", func() { By("Waiting for the webserver pods to transition to Running state") for _, podName := range podNames { - err = waitForPodRunningInNamespace(c, podName, ns) + err = waitForPodRunningInNamespace(c, podName, namespace.Name) Expect(err).NotTo(HaveOccurred()) } @@ -224,7 +212,7 @@ var _ = Describe("Networking", func() { Logf("About to make a proxy status call") start := time.Now() body, err = c.Get(). - Namespace(ns). + Namespace(namespace.Name). Prefix("proxy"). Resource("services"). Name(svc.Name). @@ -246,7 +234,7 @@ var _ = Describe("Networking", func() { break case "fail": if body, err = c.Get(). - Namespace(ns).Prefix("proxy"). + Namespace(namespace.Name).Prefix("proxy"). Resource("services"). Name(svc.Name).Suffix("read"). DoRaw(); err != nil { @@ -260,7 +248,7 @@ var _ = Describe("Networking", func() { if !passed { if body, err = c.Get(). - Namespace(ns). + Namespace(namespace.Name). Prefix("proxy"). Resource("services"). Name(svc.Name). diff --git a/test/e2e/util.go b/test/e2e/util.go index 7f024ad301e..09088899245 100644 --- a/test/e2e/util.go +++ b/test/e2e/util.go @@ -81,6 +81,19 @@ func waitForPodCondition(c *client.Client, ns, podName, desc string, condition p return fmt.Errorf("gave up waiting for pod %s to be %s after %.2f seconds", podName, desc, podStartTimeout.Seconds()) } +// createNS should be used by every test, note that we append a common prefix to the provided test name. +func createTestingNS(baseName string, c *client.Client) (*api.Namespace, error) { + namespaceObj := &api.Namespace{ + ObjectMeta: api.ObjectMeta{ + Name: fmt.Sprintf("e2e-tests-%v-%v", baseName, randomSuffix()), + Namespace: "", + }, + Status: api.NamespaceStatus{}, + } + _, err := c.Namespaces().Create(namespaceObj) + return namespaceObj, err +} + func waitForPodRunningInNamespace(c *client.Client, podName string, namespace string) error { return waitForPodCondition(c, namespace, podName, "running", func(pod *api.Pod) (bool, error) { return (pod.Status.Phase == api.PodRunning), nil @@ -179,6 +192,8 @@ func loadClient() (*client.Client, error) { return c, nil } +// randomSuffix provides a random string to append to pods,services,rcs. +// NOTE: For test's namespaces, instead use createGinkgoNS(..). // 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 @@ -269,6 +284,7 @@ func validateController(c *client.Client, containerImage string, replicas int, c Failf("Timed out after %v seconds waiting for %s pods to reach valid state", podStartTimeout.Seconds(), testname) } +// kubectlCmd runs the kubectl executable. // kubectlCmd runs the kubectl executable. func kubectlCmd(args ...string) *exec.Cmd { defaultArgs := []string{}