Skip to content

Commit

Permalink
Extract testContainerOutput method from e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pmorie committed Mar 31, 2015
1 parent 9bbf0b1 commit 0efe5e7
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 157 deletions.
59 changes: 4 additions & 55 deletions test/e2e/docker_containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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]",
})
})
Expand All @@ -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]",
})
})
Expand All @@ -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]",
})
})
Expand All @@ -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]",
})
})
Expand All @@ -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)
}
}
60 changes: 7 additions & 53 deletions test/e2e/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package e2e
import (
"fmt"
"strconv"
"strings"
"time"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
Expand Down Expand Up @@ -320,79 +319,34 @@ 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=",
"FOOSERVICE_PORT_8765_TCP_PORT=",
"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() {
Expand Down
51 changes: 2 additions & 49 deletions test/e2e/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package e2e

import (
"fmt"
"strings"
"time"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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.
})
})
})
57 changes: 57 additions & 0 deletions test/e2e/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 0efe5e7

Please sign in to comment.