From 0b72391aa00055734f12b37c75daf32579d8f16d Mon Sep 17 00:00:00 2001 From: Fabio Yeon Date: Mon, 23 Feb 2015 14:57:08 -0800 Subject: [PATCH] Move 'defer' call before creation on various e2e tests. --- test/e2e/events.go | 16 +++++++--------- test/e2e/secrets.go | 26 ++++++++++---------------- test/e2e/service.go | 27 ++++++++++++++------------- 3 files changed, 31 insertions(+), 38 deletions(-) diff --git a/test/e2e/events.go b/test/e2e/events.go index 0f7f896f2eafc..c453ba550dac9 100644 --- a/test/e2e/events.go +++ b/test/e2e/events.go @@ -71,18 +71,16 @@ var _ = Describe("Events", func() { } By("submitting the pod to kubernetes") - _, err := podClient.Create(pod) - if err != nil { - Fail(fmt.Sprintf("Failed to create pod: %v", err)) - } defer func() { By("deleting the pod") - defer GinkgoRecover() podClient.Delete(pod.Name) }() + if _, err := podClient.Create(pod); err != nil { + Failf("Failed to create pod: %v", err) + } By("waiting for the pod to start running") - err = waitForPodRunning(c, pod.Name, 300*time.Second) + err := waitForPodRunning(c, pod.Name, 300*time.Second) Expect(err).NotTo(HaveOccurred()) By("verifying the pod is in kubernetes") @@ -92,7 +90,7 @@ var _ = Describe("Events", func() { By("retrieving the pod") podWithUid, err := podClient.Get(pod.Name) if err != nil { - Fail(fmt.Sprintf("Failed to get pod: %v", err)) + Failf("Failed to get pod: %v", err) } fmt.Printf("%+v\n", podWithUid) @@ -108,7 +106,7 @@ var _ = Describe("Events", func() { }.AsSelector(), ) if err != nil { - Fail(fmt.Sprintf("Error while listing events:", err)) + Failf("Error while listing events: %v", err) } Expect(len(events.Items)).ToNot(BeZero(), "scheduler events from running pod") fmt.Println("Saw scheduler event for our pod.") @@ -125,7 +123,7 @@ var _ = Describe("Events", func() { }.AsSelector(), ) if err != nil { - Fail(fmt.Sprintf("Error while listing events:", err)) + Failf("Error while listing events: %v", err) } Expect(len(events.Items)).ToNot(BeZero(), "kubelet events from running pod") fmt.Println("Saw kubelet event for our pod.") diff --git a/test/e2e/secrets.go b/test/e2e/secrets.go index 8778186da89b7..122ed734dacf6 100644 --- a/test/e2e/secrets.go +++ b/test/e2e/secrets.go @@ -56,19 +56,17 @@ var _ = Describe("Secrets", func() { }, } - secret, err := c.Secrets(ns).Create(secret) By(fmt.Sprintf("Creating secret with name %s", secret.Name)) - if err != nil { - Fail(fmt.Sprintf("unable to create test secret %s: %v", secret.Name, err)) - } - - // Clean up secret defer func() { By("Cleaning up the secret") - if err = c.Secrets(ns).Delete(secret.Name); err != nil { - Fail(fmt.Sprintf("unable to delete secret %v: %v", secret.Name, err)) + if err := c.Secrets(ns).Delete(secret.Name); err != nil { + Failf("unable to delete secret %v: %v", secret.Name, err) } }() + var err error + if secret, err = c.Secrets(ns).Create(secret); err != nil { + Failf("unable to create test secret %s: %v", secret.Name, err) + } By(fmt.Sprintf("Creating a pod to consume secret %v", secret.Name)) // Make a client pod that verifies that it has the service environment variables. @@ -112,21 +110,17 @@ var _ = Describe("Secrets", func() { }, } - _, err = c.Pods(ns).Create(clientPod) - if err != nil { - Fail(fmt.Sprintf("Failed to create pod: %v", err)) + defer c.Pods(ns).Delete(clientPod.Name) + if _, err := c.Pods(ns).Create(clientPod); err != nil { + Failf("Failed to create pod: %v", err) } - defer func() { - c.Pods(ns).Delete(clientPod.Name) - }() - // Wait for client pod to complete. expectNoError(waitForPodRunning(c, clientPod.Name, 60*time.Second)) // Grab its logs. Get host first. clientPodStatus, err := c.Pods(ns).Get(clientPod.Name) if err != nil { - Fail(fmt.Sprintf("Failed to get clientPod to know host: %v", err)) + Failf("Failed to get clientPod to know host: %v", err) } By(fmt.Sprintf("Trying to get logs from host %s pod %s container %s: %v", clientPodStatus.Status.Host, clientPodStatus.Name, clientPodStatus.Spec.Containers[0].Name, err)) diff --git a/test/e2e/service.go b/test/e2e/service.go index 30707b2bb8b69..4be152a97149f 100644 --- a/test/e2e/service.go +++ b/test/e2e/service.go @@ -107,24 +107,23 @@ var _ = Describe("Services", func() { } By("submitting the pod to kuberenetes") - _, err := podClient.Create(pod) - if err != nil { - Fail(fmt.Sprintf("Failed to create %s pod: %v", pod.Name, err)) - } defer func() { By("deleting the pod") defer GinkgoRecover() podClient.Delete(pod.Name) }() + if _, err := podClient.Create(pod); err != nil { + Failf("Failed to create %s pod: %v", pod.Name, err) + } By("waiting for the pod to start running") - err = waitForPodRunning(c, pod.Name, 300*time.Second) + err := waitForPodRunning(c, pod.Name, 300*time.Second) Expect(err).NotTo(HaveOccurred()) By("retrieving the pod") pod, err = podClient.Get(pod.Name) if err != nil { - Fail(fmt.Sprintf("Failed to get pod %s: %v", pod.Name, err)) + Failf("Failed to get pod %s: %v", pod.Name, err) } // Try to find results for each expected name. @@ -166,7 +165,7 @@ var _ = Describe("Services", func() { Do(). Into(&svc) if err != nil { - Fail(fmt.Sprintf("unexpected error listing services using ro service: %v", err)) + Failf("unexpected error listing services using ro service: %v", err) } var foundRW, foundRO bool for i := range svc.Items { @@ -210,9 +209,7 @@ var _ = Describe("Services", func() { validateEndpointsOrFail(c, ns, serviceName, expectedPort, []string{}) - name1 := "test1" - addEndpointPodOrFail(c, ns, name1, labels) - names := []string{name1} + var names []string defer func() { for _, name := range names { err := c.Pods(ns).Delete(name) @@ -220,6 +217,10 @@ var _ = Describe("Services", func() { } }() + name1 := "test1" + addEndpointPodOrFail(c, ns, name1, labels) + names = append(names, name1) + validateEndpointsOrFail(c, ns, serviceName, expectedPort, names) name2 := "test2" @@ -251,7 +252,7 @@ func validateIPsOrFail(c *client.Client, ns string, expectedPort int, expectedEn ips := util.StringSet{} for _, ep := range endpoints.Endpoints { if ep.Port != expectedPort { - Fail(fmt.Sprintf("invalid port, expected %d, got %d", expectedPort, ep.Port)) + Failf("invalid port, expected %d, got %d", expectedPort, ep.Port) } ips.Insert(ep.IP) } @@ -259,10 +260,10 @@ func validateIPsOrFail(c *client.Client, ns string, expectedPort int, expectedEn for _, name := range expectedEndpoints { pod, err := c.Pods(ns).Get(name) if err != nil { - Fail(fmt.Sprintf("failed to get pod %s, that's pretty weird. validation failed: %s", name, err)) + Failf("failed to get pod %s, that's pretty weird. validation failed: %s", name, err) } if !ips.Has(pod.Status.PodIP) { - Fail(fmt.Sprintf("ip validation failed, expected: %v, saw: %v", ips, pod.Status.PodIP)) + Failf("ip validation failed, expected: %v, saw: %v", ips, pod.Status.PodIP) } } }