e2e-node: refactor lifecycle test to avoid selinux issues

This commit is contained in:
Seth Jennings 2017-03-20 11:56:10 -05:00
parent 38055983e0
commit 5156f582fe

View File

@ -17,12 +17,10 @@ limitations under the License.
package e2e_node package e2e_node
import ( import (
"fmt"
"time" "time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr" "k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/kubernetes/pkg/api/v1" "k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/test/e2e/framework" "k8s.io/kubernetes/test/e2e/framework"
@ -35,179 +33,108 @@ var _ = framework.KubeDescribe("Container Lifecycle Hook", func() {
var podClient *framework.PodClient var podClient *framework.PodClient
const ( const (
podCheckInterval = 1 * time.Second podCheckInterval = 1 * time.Second
podWaitTimeout = 3 * time.Minute
postStartWaitTimeout = 2 * time.Minute postStartWaitTimeout = 2 * time.Minute
preStopWaitTimeout = 30 * time.Second preStopWaitTimeout = 30 * time.Second
) )
Context("when create a pod with lifecycle hook", func() { Context("when create a pod with lifecycle hook", func() {
var targetIP string
podHandleHookRequest := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod-handle-http-request",
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "pod-handle-http-request",
Image: "gcr.io/google_containers/netexec:1.7",
Ports: []v1.ContainerPort{
{
ContainerPort: 8080,
Protocol: v1.ProtocolTCP,
},
},
},
},
},
}
BeforeEach(func() { BeforeEach(func() {
podClient = f.PodClient() podClient = f.PodClient()
By("create the container to handle the HTTPGet hook request.")
newPod := podClient.CreateSync(podHandleHookRequest)
targetIP = newPod.Status.PodIP
}) })
testPodWithHook := func(podWithHook *v1.Pod) {
Context("when it is exec hook", func() { By("create the pod with lifecycle hook")
var file string podClient.CreateSync(podWithHook)
testPodWithExecHook := func(podWithHook *v1.Pod) { if podWithHook.Spec.Containers[0].Lifecycle.PostStart != nil {
podCheckHook := getExecHookTestPod("pod-check-hook", By("check poststart hook")
// Wait until the file is created. Eventually(func() error {
[]string{"sh", "-c", fmt.Sprintf("while [ ! -e %s ]; do sleep 1; done", file)}, return podClient.MatchContainerOutput(podHandleHookRequest.Name, podHandleHookRequest.Spec.Containers[0].Name,
) `GET /echo\?msg=poststart`)
By("create the pod with lifecycle hook") }, postStartWaitTimeout, podCheckInterval).Should(BeNil())
podClient.CreateSync(podWithHook)
if podWithHook.Spec.Containers[0].Lifecycle.PostStart != nil {
By("create the hook check pod")
podClient.Create(podCheckHook)
By("wait for the hook check pod to success")
podClient.WaitForSuccess(podCheckHook.Name, postStartWaitTimeout)
}
By("delete the pod with lifecycle hook")
podClient.DeleteSync(podWithHook.Name, metav1.NewDeleteOptions(15), framework.DefaultPodDeletionTimeout)
if podWithHook.Spec.Containers[0].Lifecycle.PreStop != nil {
By("create the hook check pod")
podClient.Create(podCheckHook)
By("wait for the prestop check pod to success")
podClient.WaitForSuccess(podCheckHook.Name, preStopWaitTimeout)
}
} }
By("delete the pod with lifecycle hook")
BeforeEach(func() { podClient.DeleteSync(podWithHook.Name, metav1.NewDeleteOptions(15), framework.DefaultPodDeletionTimeout)
file = "/tmp/test-" + string(uuid.NewUUID()) if podWithHook.Spec.Containers[0].Lifecycle.PreStop != nil {
}) By("check prestop hook")
Eventually(func() error {
AfterEach(func() { return podClient.MatchContainerOutput(podHandleHookRequest.Name, podHandleHookRequest.Spec.Containers[0].Name,
By("cleanup the temporary file created in the test.") `GET /echo\?msg=prestop`)
cleanupPod := getExecHookTestPod("pod-clean-up", []string{"rm", file}) }, preStopWaitTimeout, podCheckInterval).Should(BeNil())
podClient.Create(cleanupPod) }
podClient.WaitForSuccess(cleanupPod.Name, podWaitTimeout) }
}) It("should execute poststart exec hook properly [Conformance]", func() {
lifecycle := &v1.Lifecycle{
It("should execute poststart exec hook properly [Conformance]", func() { PostStart: &v1.Handler{
podWithHook := getExecHookTestPod("pod-with-poststart-exec-hook", Exec: &v1.ExecAction{
// Block forever Command: []string{"sh", "-c", "curl http://" + targetIP + ":8080/echo?msg=poststart"},
[]string{"tail", "-f", "/dev/null"},
)
podWithHook.Spec.Containers[0].Lifecycle = &v1.Lifecycle{
PostStart: &v1.Handler{
Exec: &v1.ExecAction{Command: []string{"touch", file}},
},
}
testPodWithExecHook(podWithHook)
})
It("should execute prestop exec hook properly [Conformance]", func() {
podWithHook := getExecHookTestPod("pod-with-prestop-exec-hook",
// Block forever
[]string{"tail", "-f", "/dev/null"},
)
podWithHook.Spec.Containers[0].Lifecycle = &v1.Lifecycle{
PreStop: &v1.Handler{
Exec: &v1.ExecAction{Command: []string{"touch", file}},
},
}
testPodWithExecHook(podWithHook)
})
})
Context("when it is http hook", func() {
var targetIP string
podHandleHookRequest := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod-handle-http-request",
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "pod-handle-http-request",
Image: "gcr.io/google_containers/netexec:1.7",
Ports: []v1.ContainerPort{
{
ContainerPort: 8080,
Protocol: v1.ProtocolTCP,
},
},
},
}, },
}, },
} }
BeforeEach(func() { podWithHook := getPodWithHook("pod-with-poststart-exec-hook", "gcr.io/google_containers/hostexec:1.2", lifecycle)
By("create the container to handle the HTTPGet hook request.") testPodWithHook(podWithHook)
newPod := podClient.CreateSync(podHandleHookRequest) })
targetIP = newPod.Status.PodIP It("should execute prestop exec hook properly [Conformance]", func() {
}) lifecycle := &v1.Lifecycle{
testPodWithHttpHook := func(podWithHook *v1.Pod) { PreStop: &v1.Handler{
By("create the pod with lifecycle hook") Exec: &v1.ExecAction{
podClient.CreateSync(podWithHook) Command: []string{"sh", "-c", "curl http://" + targetIP + ":8080/echo?msg=prestop"},
if podWithHook.Spec.Containers[0].Lifecycle.PostStart != nil { },
By("check poststart hook") },
Eventually(func() error {
return podClient.MatchContainerOutput(podHandleHookRequest.Name, podHandleHookRequest.Spec.Containers[0].Name,
`GET /echo\?msg=poststart`)
}, postStartWaitTimeout, podCheckInterval).Should(BeNil())
}
By("delete the pod with lifecycle hook")
podClient.DeleteSync(podWithHook.Name, metav1.NewDeleteOptions(15), framework.DefaultPodDeletionTimeout)
if podWithHook.Spec.Containers[0].Lifecycle.PreStop != nil {
By("check prestop hook")
Eventually(func() error {
return podClient.MatchContainerOutput(podHandleHookRequest.Name, podHandleHookRequest.Spec.Containers[0].Name,
`GET /echo\?msg=prestop`)
}, preStopWaitTimeout, podCheckInterval).Should(BeNil())
}
} }
It("should execute poststart http hook properly [Conformance]", func() { podWithHook := getPodWithHook("pod-with-prestop-exec-hook", "gcr.io/google_containers/hostexec:1.2", lifecycle)
podWithHook := &v1.Pod{ testPodWithHook(podWithHook)
ObjectMeta: metav1.ObjectMeta{ })
Name: "pod-with-poststart-http-hook", It("should execute poststart http hook properly [Conformance]", func() {
lifecycle := &v1.Lifecycle{
PostStart: &v1.Handler{
HTTPGet: &v1.HTTPGetAction{
Path: "/echo?msg=poststart",
Host: targetIP,
Port: intstr.FromInt(8080),
}, },
Spec: v1.PodSpec{ },
Containers: []v1.Container{ }
{ podWithHook := getPodWithHook("pod-with-poststart-http-hook", framework.GetPauseImageNameForHostArch(), lifecycle)
Name: "pod-with-poststart-http-hook", testPodWithHook(podWithHook)
Image: framework.GetPauseImageNameForHostArch(), })
Lifecycle: &v1.Lifecycle{ It("should execute prestop http hook properly [Conformance]", func() {
PostStart: &v1.Handler{ lifecycle := &v1.Lifecycle{
HTTPGet: &v1.HTTPGetAction{ PreStop: &v1.Handler{
Path: "/echo?msg=poststart", HTTPGet: &v1.HTTPGetAction{
Host: targetIP, Path: "/echo?msg=prestop",
Port: intstr.FromInt(8080), Host: targetIP,
}, Port: intstr.FromInt(8080),
},
},
},
},
}, },
} },
testPodWithHttpHook(podWithHook) }
}) podWithHook := getPodWithHook("pod-with-prestop-http-hook", framework.GetPauseImageNameForHostArch(), lifecycle)
It("should execute prestop http hook properly [Conformance]", func() { testPodWithHook(podWithHook)
podWithHook := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod-with-prestop-http-hook",
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "pod-with-prestop-http-hook",
Image: framework.GetPauseImageNameForHostArch(),
Lifecycle: &v1.Lifecycle{
PreStop: &v1.Handler{
HTTPGet: &v1.HTTPGetAction{
Path: "/echo?msg=prestop",
Host: targetIP,
Port: intstr.FromInt(8080),
},
},
},
},
},
},
}
testPodWithHttpHook(podWithHook)
})
}) })
}) })
}) })
func getExecHookTestPod(name string, cmd []string) *v1.Pod { func getPodWithHook(name string, image string, lifecycle *v1.Lifecycle) *v1.Pod {
return &v1.Pod{ return &v1.Pod{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: name, Name: name,
@ -215,22 +142,9 @@ func getExecHookTestPod(name string, cmd []string) *v1.Pod {
Spec: v1.PodSpec{ Spec: v1.PodSpec{
Containers: []v1.Container{ Containers: []v1.Container{
{ {
Name: name, Name: name,
Image: "gcr.io/google_containers/busybox:1.24", Image: image,
VolumeMounts: []v1.VolumeMount{ Lifecycle: lifecycle,
{
Name: "tmpfs",
MountPath: "/tmp",
},
},
Command: cmd,
},
},
RestartPolicy: v1.RestartPolicyNever,
Volumes: []v1.Volume{
{
Name: "tmpfs",
VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{Path: "/tmp"}},
}, },
}, },
}, },