Merge pull request #6224 from pmorie/e2e-refactor

Extract testContainerOutput method from e2e tests
This commit is contained in:
Victor Marmol 2015-03-31 14:45:02 -07:00
commit d80ac514df
4 changed files with 70 additions and 157 deletions

View File

@ -17,10 +17,6 @@ limitations under the License.
package e2e package e2e
import ( import (
"fmt"
"strings"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client" "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util" "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() { 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]", "[/ep default arguments]",
}) })
}) })
@ -48,7 +44,7 @@ var _ = Describe("Docker Containers", func() {
pod := entrypointTestPod() pod := entrypointTestPod()
pod.Spec.Containers[0].Args = []string{"override", "arguments"} pod.Spec.Containers[0].Args = []string{"override", "arguments"}
runEntrypointTest("override arguments", c, pod, []string{ testContainerOutput("override arguments", c, pod, []string{
"[/ep override arguments]", "[/ep override arguments]",
}) })
}) })
@ -59,7 +55,7 @@ var _ = Describe("Docker Containers", func() {
pod := entrypointTestPod() pod := entrypointTestPod()
pod.Spec.Containers[0].Command = []string{"/ep-2"} pod.Spec.Containers[0].Command = []string{"/ep-2"}
runEntrypointTest("override command", c, pod, []string{ testContainerOutput("override command", c, pod, []string{
"[/ep-2]", "[/ep-2]",
}) })
}) })
@ -69,7 +65,7 @@ var _ = Describe("Docker Containers", func() {
pod.Spec.Containers[0].Command = []string{"/ep-2"} pod.Spec.Containers[0].Command = []string{"/ep-2"}
pod.Spec.Containers[0].Args = []string{"override", "arguments"} pod.Spec.Containers[0].Args = []string{"override", "arguments"}
runEntrypointTest("override all", c, pod, []string{ testContainerOutput("override all", c, pod, []string{
"[/ep-2 override arguments]", "[/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)
}
}

View File

@ -19,7 +19,6 @@ package e2e
import ( import (
"fmt" "fmt"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
@ -322,65 +321,26 @@ var _ = Describe("Pods", func() {
Fail(fmt.Sprintf("Failed to create service: %v", err)) 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. // Make a client pod that verifies that it has the service environment variables.
clientName := "client-envvars-" + string(util.NewUUID()) podName := "client-envvars-" + string(util.NewUUID())
clientPod := &api.Pod{ pod := &api.Pod{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{
Name: clientName, Name: podName,
Labels: map[string]string{"name": clientName}, Labels: map[string]string{"name": podName},
}, },
Spec: api.PodSpec{ Spec: api.PodSpec{
Containers: []api.Container{ Containers: []api.Container{
{ {
Name: "env3cont", Name: "env3cont",
Image: "busybox", Image: "busybox",
Command: []string{"sh", "-c", "env; sleep 600"}, Command: []string{"sh", "-c", "env"},
}, },
}, },
RestartPolicy: api.RestartPolicyNever, 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)) testContainerOutput("service env", c, pod, []string{
// 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{
"FOOSERVICE_SERVICE_HOST=", "FOOSERVICE_SERVICE_HOST=",
"FOOSERVICE_SERVICE_PORT=", "FOOSERVICE_SERVICE_PORT=",
"FOOSERVICE_PORT=", "FOOSERVICE_PORT=",
@ -388,13 +348,7 @@ var _ = Describe("Pods", func() {
"FOOSERVICE_PORT_8765_TCP_PROTO=", "FOOSERVICE_PORT_8765_TCP_PROTO=",
"FOOSERVICE_PORT_8765_TCP=", "FOOSERVICE_PORT_8765_TCP=",
"FOOSERVICE_PORT_8765_TCP_ADDR=", "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() { It("should be restarted with a docker exec \"cat /tmp/health\" liveness probe", func() {

View File

@ -18,8 +18,6 @@ package e2e
import ( import (
"fmt" "fmt"
"strings"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client" "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) 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{ pod := &api.Pod{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{
Name: "pod-secrets-" + string(util.NewUUID()), Name: "pod-secrets-" + string(util.NewUUID()),
@ -103,51 +99,8 @@ var _ = Describe("Secrets", func() {
}, },
} }
defer c.Pods(ns).Delete(pod.Name) testContainerOutput("consume secrets", c, pod, []string{
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{
"value-1", "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.
}) })
}) })

View File

@ -302,3 +302,60 @@ func runKubectl(args ...string) string {
// TODO: trimspace should be unnecessary after switching to use kubectl binary directly // TODO: trimspace should be unnecessary after switching to use kubectl binary directly
return strings.TrimSpace(stdout.String()) 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)
}
}