Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add expectNoError helper, minor cleanups #4392

Merged
merged 3 commits into from
Feb 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 10 additions & 20 deletions test/e2e/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ import (
)

func runLivenessTest(c *client.Client, podDescr *api.Pod) {
defer GinkgoRecover()
ns := "e2e-test-" + string(util.NewUUID())

By(fmt.Sprintf("Creating pod %s in namespace %s", podDescr.Name, ns))
_, err := c.Pods(ns).Create(podDescr)
Expect(err).NotTo(HaveOccurred(), fmt.Sprintf("creating pod %s", podDescr.Name))
expectNoError(err, fmt.Sprintf("creating pod %s", podDescr.Name))

// At the end of the test, clean up by removing the pod.
defer func() {
Expand All @@ -48,14 +47,14 @@ func runLivenessTest(c *client.Client, podDescr *api.Pod) {
// 'Pending' other than checking for 'Running', since when failures occur, we go to
// 'Terminated' which can cause indefinite blocking.)
By("waiting for the pod to be something other than pending")
err = waitForPodNotPending(c, ns, podDescr.Name, 60*time.Second)
Expect(err).NotTo(HaveOccurred(), fmt.Sprintf("starting pod %s in namespace %s", podDescr.Name, ns))
expectNoError(waitForPodNotPending(c, ns, podDescr.Name, 60*time.Second),
fmt.Sprintf("starting pod %s in namespace %s", podDescr.Name, ns))
By(fmt.Sprintf("Started pod %s in namespace %s", podDescr.Name, ns))

// Check the pod's current state and verify that restartCount is present.
By("checking the pod's current state and verifying that restartCount is present")
pod, err := c.Pods(ns).Get(podDescr.Name)
Expect(err).NotTo(HaveOccurred(), fmt.Sprintf("getting pod %s in namespace %s", podDescr.Name, ns))
expectNoError(err, fmt.Sprintf("getting pod %s in namespace %s", podDescr.Name, ns))
initialRestartCount := pod.Status.Info["liveness"].RestartCount
By(fmt.Sprintf("Initial restart count of pod %s is %d", podDescr.Name, initialRestartCount))

Expand All @@ -65,7 +64,7 @@ func runLivenessTest(c *client.Client, podDescr *api.Pod) {
// Wait until restartCount is incremented.
time.Sleep(5 * time.Second)
pod, err = c.Pods(ns).Get(podDescr.Name)
Expect(err).NotTo(HaveOccurred(), fmt.Sprintf("getting pod %s", podDescr.Name))
expectNoError(err, fmt.Sprintf("getting pod %s", podDescr.Name))
restartCount := pod.Status.Info["liveness"].RestartCount
By(fmt.Sprintf("Restart count of pod %s in namespace %s is now %d", podDescr.Name, ns, restartCount))
if restartCount > initialRestartCount {
Expand All @@ -86,7 +85,7 @@ var _ = Describe("Pods", func() {
BeforeEach(func() {
var err error
c, err = loadClient()
Expect(err).NotTo(HaveOccurred())
expectNoError(err)
})

It("should be submitted and removed", func() {
Expand Down Expand Up @@ -132,7 +131,6 @@ var _ = Describe("Pods", func() {
// We call defer here in case there is a problem with
// the test so we can ensure that we clean up after
// ourselves
defer GinkgoRecover()
podClient.Delete(pod.Name)
}()

Expand Down Expand Up @@ -190,13 +188,11 @@ var _ = Describe("Pods", func() {
}
defer func() {
By("deleting the pod")
defer GinkgoRecover()
podClient.Delete(pod.Name)
}()

By("waiting for the pod to start running")
err = waitForPodRunning(c, pod.Name, 300*time.Second)
Expect(err).NotTo(HaveOccurred())
expectNoError(waitForPodRunning(c, pod.Name, 300*time.Second))

By("verifying the pod is in kubernetes")
pods, err := podClient.List(labels.SelectorFromSet(labels.Set(map[string]string{"time": value})))
Expand All @@ -219,8 +215,7 @@ var _ = Describe("Pods", func() {
}

By("waiting for the updated pod to start running")
err = waitForPodRunning(c, pod.Name, 300*time.Second)
Expect(err).NotTo(HaveOccurred())
expectNoError(waitForPodRunning(c, pod.Name, 300*time.Second))

By("verifying the updated pod is in kubernetes")
pods, err = podClient.List(labels.SelectorFromSet(labels.Set(map[string]string{"time": value})))
Expand Down Expand Up @@ -252,11 +247,9 @@ var _ = Describe("Pods", func() {
Fail(fmt.Sprintf("Failed to create serverPod: %v", err))
}
defer func() {
defer GinkgoRecover()
c.Pods(api.NamespaceDefault).Delete(serverPod.Name)
}()
err = waitForPodRunning(c, serverPod.Name, 300*time.Second)
Expect(err).NotTo(HaveOccurred())
expectNoError(waitForPodRunning(c, serverPod.Name, 300*time.Second))

// This service exposes port 8080 of the test pod as a service on port 8765
// TODO(filbranden): We would like to use a unique service name such as:
Expand Down Expand Up @@ -287,7 +280,6 @@ var _ = Describe("Pods", func() {
Fail(fmt.Sprintf("Failed to create service: %v", err))
}
defer func() {
defer GinkgoRecover()
c.Services(api.NamespaceDefault).Delete(svc.Name)
}()

Expand Down Expand Up @@ -318,13 +310,11 @@ var _ = Describe("Pods", func() {
Fail(fmt.Sprintf("Failed to create pod: %v", err))
}
defer func() {
defer GinkgoRecover()
c.Pods(api.NamespaceDefault).Delete(clientPod.Name)
}()

// Wait for client pod to complete.
err = waitForPodSuccess(c, clientPod.Name, clientPod.Spec.Containers[0].Name, 60*time.Second)
Expect(err).NotTo(HaveOccurred())
expectNoError(waitForPodSuccess(c, clientPod.Name, clientPod.Spec.Containers[0].Name, 60*time.Second))

// Grab its logs. Get host first.
clientPodStatus, err := c.Pods(api.NamespaceDefault).Get(clientPod.Name)
Expand Down
7 changes: 6 additions & 1 deletion test/e2e/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/clientauth"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

type testContextType struct {
Expand All @@ -45,7 +46,7 @@ func Logf(format string, a ...interface{}) {
}

func Failf(format string, a ...interface{}) {
Fail(fmt.Sprintf(format, a...))
Fail(fmt.Sprintf(format, a...), 1)
}

func waitForPodRunning(c *client.Client, id string, tryFor time.Duration) error {
Expand Down Expand Up @@ -152,3 +153,7 @@ func randomSuffix() string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return strconv.Itoa(r.Int() % 10000)
}

func expectNoError(err error, explain ...interface{}) {
ExpectWithOffset(1, err).NotTo(HaveOccurred(), explain...)
}