From 0efe5e7872dddfa817f90dfc4ca666c6d63469c1 Mon Sep 17 00:00:00 2001 From: Paul Morie Date: Tue, 31 Mar 2015 13:25:14 -0400 Subject: [PATCH] Extract testContainerOutput method from e2e tests --- test/e2e/docker_containers.go | 59 +++------------------------------- test/e2e/pods.go | 60 ++++------------------------------- test/e2e/secrets.go | 51 ++--------------------------- test/e2e/util.go | 57 +++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 157 deletions(-) diff --git a/test/e2e/docker_containers.go b/test/e2e/docker_containers.go index eb00133fbb8..425de9fa1bb 100644 --- a/test/e2e/docker_containers.go +++ b/test/e2e/docker_containers.go @@ -17,10 +17,6 @@ limitations under the License. package e2e import ( - "fmt" - "strings" - "time" - "github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/client" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" @@ -39,7 +35,7 @@ var _ = Describe("Docker Containers", func() { }) It("should use the image defaults if command and args are blank", func() { - runEntrypointTest("use defaults", c, entrypointTestPod(), []string{ + testContainerOutput("use defaults", c, entrypointTestPod(), []string{ "[/ep default arguments]", }) }) @@ -48,7 +44,7 @@ var _ = Describe("Docker Containers", func() { pod := entrypointTestPod() pod.Spec.Containers[0].Args = []string{"override", "arguments"} - runEntrypointTest("override arguments", c, pod, []string{ + testContainerOutput("override arguments", c, pod, []string{ "[/ep override arguments]", }) }) @@ -59,7 +55,7 @@ var _ = Describe("Docker Containers", func() { pod := entrypointTestPod() pod.Spec.Containers[0].Command = []string{"/ep-2"} - runEntrypointTest("override command", c, pod, []string{ + testContainerOutput("override command", c, pod, []string{ "[/ep-2]", }) }) @@ -69,7 +65,7 @@ var _ = Describe("Docker Containers", func() { pod.Spec.Containers[0].Command = []string{"/ep-2"} pod.Spec.Containers[0].Args = []string{"override", "arguments"} - runEntrypointTest("override all", c, pod, []string{ + testContainerOutput("override all", c, pod, []string{ "[/ep-2 override arguments]", }) }) @@ -96,50 +92,3 @@ func entrypointTestPod() *api.Pod { }, } } - -// pod must have a container named 'test-container' -func runEntrypointTest(scenarioName string, c *client.Client, pod *api.Pod, expectedOutput []string) { - ns := api.NamespaceDefault - By(fmt.Sprintf("Creating a pod to test %v", scenarioName)) - - defer c.Pods(ns).Delete(pod.Name) - if _, err := c.Pods(ns).Create(pod); err != nil { - Failf("Failed to create pod: %v", err) - } - // Wait for client pod to complete. - expectNoError(waitForPodSuccess(c, pod.Name, testContainerName)) - - // Grab its logs. Get host first. - podStatus, err := c.Pods(ns).Get(pod.Name) - if err != nil { - Failf("Failed to get pod to know host: %v", err) - } - By(fmt.Sprintf("Trying to get logs from host %s pod %s container %s: %v", - podStatus.Status.Host, podStatus.Name, podStatus.Spec.Containers[0].Name, err)) - var logs []byte - start := time.Now() - - // Sometimes the actual containers take a second to get started, try to get logs for 60s - for time.Now().Sub(start) < (60 * time.Second) { - logs, err = c.Get(). - Prefix("proxy"). - Resource("minions"). - Name(podStatus.Status.Host). - Suffix("containerLogs", ns, podStatus.Name, podStatus.Spec.Containers[0].Name). - Do(). - Raw() - fmt.Sprintf("pod logs:%v\n", string(logs)) - By(fmt.Sprintf("pod logs:%v\n", string(logs))) - if strings.Contains(string(logs), "Internal Error") { - By(fmt.Sprintf("Failed to get logs from host %s pod %s container %s: %v", - podStatus.Status.Host, podStatus.Name, podStatus.Spec.Containers[0].Name, string(logs))) - time.Sleep(5 * time.Second) - continue - } - break - } - - for _, m := range expectedOutput { - Expect(string(logs)).To(ContainSubstring(m), "%q in container output", m) - } -} diff --git a/test/e2e/pods.go b/test/e2e/pods.go index 9d16a950bad..80d0e8239ac 100644 --- a/test/e2e/pods.go +++ b/test/e2e/pods.go @@ -19,7 +19,6 @@ package e2e import ( "fmt" "strconv" - "strings" "time" "github.com/GoogleCloudPlatform/kubernetes/pkg/api" @@ -320,65 +319,26 @@ var _ = Describe("Pods", func() { Fail(fmt.Sprintf("Failed to create service: %v", err)) } - // TODO: we don't have a way to wait for a service to be "running". // If this proves flaky, then we will need to retry the clientPod or insert a sleep. - // Make a client pod that verifies that it has the service environment variables. - clientName := "client-envvars-" + string(util.NewUUID()) - clientPod := &api.Pod{ + podName := "client-envvars-" + string(util.NewUUID()) + pod := &api.Pod{ ObjectMeta: api.ObjectMeta{ - Name: clientName, - Labels: map[string]string{"name": clientName}, + Name: podName, + Labels: map[string]string{"name": podName}, }, Spec: api.PodSpec{ Containers: []api.Container{ { Name: "env3cont", Image: "busybox", - Command: []string{"sh", "-c", "env; sleep 600"}, + Command: []string{"sh", "-c", "env"}, }, }, RestartPolicy: api.RestartPolicyNever, }, } - defer c.Pods(api.NamespaceDefault).Delete(clientPod.Name) - _, err = c.Pods(api.NamespaceDefault).Create(clientPod) - if err != nil { - Fail(fmt.Sprintf("Failed to create pod: %v", err)) - } - expectNoError(waitForPodRunning(c, clientPod.Name)) - - // Grab its logs. Get host first. - clientPodStatus, err := c.Pods(api.NamespaceDefault).Get(clientPod.Name) - if err != nil { - Fail(fmt.Sprintf("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)) - var logs []byte - start := time.Now() - - // Sometimes the actual containers take a second to get started, try to get logs for 60s - for time.Now().Sub(start) < (60 * time.Second) { - logs, err = c.Get(). - Prefix("proxy"). - Resource("minions"). - Name(clientPodStatus.Status.Host). - Suffix("containerLogs", api.NamespaceDefault, clientPodStatus.Name, clientPodStatus.Spec.Containers[0].Name). - Do(). - Raw() - fmt.Sprintf("clientPod logs:%v\n", string(logs)) - By(fmt.Sprintf("clientPod logs:%v\n", string(logs))) - if strings.Contains(string(logs), "Internal Error") { - By(fmt.Sprintf("Failed to get logs from host %s pod %s container %s: %v", - clientPodStatus.Status.Host, clientPodStatus.Name, clientPodStatus.Spec.Containers[0].Name, string(logs))) - time.Sleep(5 * time.Second) - continue - } - break - } - - toFind := []string{ + testContainerOutput("service env", c, pod, []string{ "FOOSERVICE_SERVICE_HOST=", "FOOSERVICE_SERVICE_PORT=", "FOOSERVICE_PORT=", @@ -386,13 +346,7 @@ var _ = Describe("Pods", func() { "FOOSERVICE_PORT_8765_TCP_PROTO=", "FOOSERVICE_PORT_8765_TCP=", "FOOSERVICE_PORT_8765_TCP_ADDR=", - } - - for _, m := range toFind { - Expect(string(logs)).To(ContainSubstring(m), "%q in client env vars", m) - } - - // We could try a wget the service from the client pod. But services.sh e2e test covers that pretty well. + }) }) It("should be restarted with a docker exec \"cat /tmp/health\" liveness probe", func() { diff --git a/test/e2e/secrets.go b/test/e2e/secrets.go index 20ab361a2b2..8443879486c 100644 --- a/test/e2e/secrets.go +++ b/test/e2e/secrets.go @@ -18,8 +18,6 @@ package e2e import ( "fmt" - "strings" - "time" "github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/client" @@ -68,8 +66,6 @@ var _ = Describe("Secrets", func() { 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 pod that verifies that it has the service environment variables. pod := &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: "pod-secrets-" + string(util.NewUUID()), @@ -103,51 +99,8 @@ var _ = Describe("Secrets", func() { }, } - defer c.Pods(ns).Delete(pod.Name) - if _, err := c.Pods(ns).Create(pod); err != nil { - Failf("Failed to create pod: %v", err) - } - // Wait for client pod to complete. - expectNoError(waitForPodSuccess(c, pod.Name, "catcont")) - - // Grab its logs. Get host first. - podStatus, err := c.Pods(ns).Get(pod.Name) - if err != nil { - Failf("Failed to get pod to know host: %v", err) - } - By(fmt.Sprintf("Trying to get logs from host %s pod %s container %s: %v", - podStatus.Status.Host, podStatus.Name, podStatus.Spec.Containers[0].Name, err)) - var logs []byte - start := time.Now() - - // Sometimes the actual containers take a second to get started, try to get logs for 60s - for time.Now().Sub(start) < (60 * time.Second) { - logs, err = c.Get(). - Prefix("proxy"). - Resource("minions"). - Name(podStatus.Status.Host). - Suffix("containerLogs", ns, podStatus.Name, podStatus.Spec.Containers[0].Name). - Do(). - Raw() - fmt.Sprintf("pod logs:%v\n", string(logs)) - By(fmt.Sprintf("pod logs:%v\n", string(logs))) - if strings.Contains(string(logs), "Internal Error") { - By(fmt.Sprintf("Failed to get logs from host %s pod %s container %s: %v", - podStatus.Status.Host, podStatus.Name, podStatus.Spec.Containers[0].Name, string(logs))) - time.Sleep(5 * time.Second) - continue - } - break - } - - toFind := []string{ + testContainerOutput("consume secrets", c, pod, []string{ "value-1", - } - - for _, m := range toFind { - Expect(string(logs)).To(ContainSubstring(m), "%q in secret data", m) - } - - // We could try a wget the service from the client pod. But services.sh e2e test covers that pretty well. + }) }) }) diff --git a/test/e2e/util.go b/test/e2e/util.go index ca63c53ccee..554ed2ccfad 100644 --- a/test/e2e/util.go +++ b/test/e2e/util.go @@ -302,3 +302,60 @@ func runKubectl(args ...string) string { // TODO: trimspace should be unnecessary after switching to use kubectl binary directly return strings.TrimSpace(stdout.String()) } + +// testContainerOutput runs testContainerOutputInNamespace with the default namespace. +func testContainerOutput(scenarioName string, c *client.Client, pod *api.Pod, expectedOutput []string) { + testContainerOutputInNamespace(api.NamespaceDefault, scenarioName, c, pod, expectedOutput) +} + +// testContainerOutputInNamespace runs the given pod in the given namespace and waits +// for the first container in the podSpec to move into the 'Success' status. It retrieves +// the container log and searches for lines of expected output. +func testContainerOutputInNamespace(ns, scenarioName string, c *client.Client, pod *api.Pod, expectedOutput []string) { + By(fmt.Sprintf("Creating a pod to test %v", scenarioName)) + + defer c.Pods(ns).Delete(pod.Name) + if _, err := c.Pods(ns).Create(pod); err != nil { + Failf("Failed to create pod: %v", err) + } + + containerName := pod.Spec.Containers[0].Name + + // Wait for client pod to complete. + expectNoError(waitForPodSuccess(c, pod.Name, containerName)) + + // Grab its logs. Get host first. + podStatus, err := c.Pods(ns).Get(pod.Name) + if err != nil { + Failf("Failed to get pod status: %v", err) + } + + By(fmt.Sprintf("Trying to get logs from host %s pod %s container %s: %v", + podStatus.Status.Host, podStatus.Name, containerName, err)) + var logs []byte + start := time.Now() + + // Sometimes the actual containers take a second to get started, try to get logs for 60s + for time.Now().Sub(start) < (60 * time.Second) { + logs, err = c.Get(). + Prefix("proxy"). + Resource("minions"). + Name(podStatus.Status.Host). + Suffix("containerLogs", ns, podStatus.Name, containerName). + Do(). + Raw() + fmt.Sprintf("pod logs:%v\n", string(logs)) + By(fmt.Sprintf("pod logs:%v\n", string(logs))) + if strings.Contains(string(logs), "Internal Error") { + By(fmt.Sprintf("Failed to get logs from host %q pod %q container %q: %v", + podStatus.Status.Host, podStatus.Name, containerName, string(logs))) + time.Sleep(5 * time.Second) + continue + } + break + } + + for _, m := range expectedOutput { + Expect(string(logs)).To(ContainSubstring(m), "%q in container output", m) + } +}