Skip to content

Commit

Permalink
tests: Refactor agnhost image pod usage - common (part 1)
Browse files Browse the repository at this point in the history
A previous commit added  a few agnhost related functions that creates agnhost
pods / containers for general purposes.

Refactors tests to use those functions.
  • Loading branch information
claudiubelu committed Sep 26, 2020
1 parent 131f42d commit c99b185
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 263 deletions.
43 changes: 13 additions & 30 deletions test/e2e/common/docker_containers.go
Expand Up @@ -20,11 +20,9 @@ import (
"github.com/onsi/gomega"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/kubernetes/test/e2e/framework"
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
imageutils "k8s.io/kubernetes/test/utils/image"
)

var _ = framework.KubeDescribe("Docker Containers", func() {
Expand All @@ -36,12 +34,13 @@ var _ = framework.KubeDescribe("Docker Containers", func() {
Description: Default command and arguments from the docker image entrypoint MUST be used when Pod does not specify the container command
*/
framework.ConformanceIt("should use the image defaults if command and args are blank [NodeConformance]", func() {
pod := f.PodClient().Create(entrypointTestPod())
pod := entrypointTestPod(f.Namespace.Name)
pod.Spec.Containers[0].Args = nil
pod = f.PodClient().Create(pod)
err := e2epod.WaitForPodNameRunningInNamespace(f.ClientSet, pod.Name, f.Namespace.Name)
framework.ExpectNoError(err, "Expected pod %q to be running, got error: %v", pod.Name, err)

pollLogs := func() (string, error) {
return e2epod.GetPodLogs(f.ClientSet, f.Namespace.Name, pod.Name, containerName)
return e2epod.GetPodLogs(f.ClientSet, f.Namespace.Name, pod.Name, pod.Spec.Containers[0].Name)
}

// The agnhost's image default entrypoint / args are: "/agnhost pause"
Expand All @@ -55,9 +54,7 @@ var _ = framework.KubeDescribe("Docker Containers", func() {
Description: Default command and from the docker image entrypoint MUST be used when Pod does not specify the container command but the arguments from Pod spec MUST override when specified.
*/
framework.ConformanceIt("should be able to override the image's default arguments (docker cmd) [NodeConformance]", func() {
pod := entrypointTestPod()
pod.Spec.Containers[0].Args = []string{"entrypoint-tester", "override", "arguments"}

pod := entrypointTestPod(f.Namespace.Name, "entrypoint-tester", "override", "arguments")
f.TestContainerOutput("override arguments", pod, 0, []string{
"[/agnhost entrypoint-tester override arguments]",
})
Expand All @@ -71,8 +68,8 @@ var _ = framework.KubeDescribe("Docker Containers", func() {
Description: Default command from the docker image entrypoint MUST NOT be used when Pod specifies the container command. Command from Pod spec MUST override the command in the image.
*/
framework.ConformanceIt("should be able to override the image's default command (docker entrypoint) [NodeConformance]", func() {
pod := entrypointTestPod()
pod.Spec.Containers[0].Command = []string{"/agnhost-2", "entrypoint-tester"}
pod := entrypointTestPod(f.Namespace.Name, "entrypoint-tester")
pod.Spec.Containers[0].Command = []string{"/agnhost-2"}

f.TestContainerOutput("override command", pod, 0, []string{
"[/agnhost-2 entrypoint-tester]",
Expand All @@ -85,36 +82,22 @@ var _ = framework.KubeDescribe("Docker Containers", func() {
Description: Default command and arguments from the docker image entrypoint MUST NOT be used when Pod specifies the container command and arguments. Command and arguments from Pod spec MUST override the command and arguments in the image.
*/
framework.ConformanceIt("should be able to override the image's default command and arguments [NodeConformance]", func() {
pod := entrypointTestPod()
pod := entrypointTestPod(f.Namespace.Name, "entrypoint-tester", "override", "arguments")
pod.Spec.Containers[0].Command = []string{"/agnhost-2"}
pod.Spec.Containers[0].Args = []string{"entrypoint-tester", "override", "arguments"}

f.TestContainerOutput("override all", pod, 0, []string{
"[/agnhost-2 entrypoint-tester override arguments]",
})
})
})

const testContainerName = "test-container"

// Return a prototypical entrypoint test pod
func entrypointTestPod() *v1.Pod {
func entrypointTestPod(namespace string, entrypointArgs ...string) *v1.Pod {
podName := "client-containers-" + string(uuid.NewUUID())
pod := e2epod.NewAgnhostPod(namespace, podName, nil, nil, nil, entrypointArgs...)

one := int64(1)
return &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: testContainerName,
Image: imageutils.GetE2EImage(imageutils.Agnhost),
},
},
RestartPolicy: v1.RestartPolicyNever,
TerminationGracePeriodSeconds: &one,
},
}
pod.Spec.TerminationGracePeriodSeconds = &one
pod.Spec.RestartPolicy = v1.RestartPolicyNever
return pod
}
108 changes: 26 additions & 82 deletions test/e2e/common/kubelet_etc_hosts.go
Expand Up @@ -25,7 +25,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
"k8s.io/kubernetes/test/e2e/framework"
imageutils "k8s.io/kubernetes/test/utils/image"
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
)

const (
Expand All @@ -36,8 +36,6 @@ const (
etcHostsOriginalPath = "/etc/hosts-original"
)

var etcHostsImageName = imageutils.GetE2EImage(imageutils.Agnhost)

type KubeletManagedHostConfig struct {
hostNetworkPod *v1.Pod
pod *v1.Pod
Expand Down Expand Up @@ -153,61 +151,28 @@ func (config *KubeletManagedHostConfig) getFileContents(podName, containerName,
func (config *KubeletManagedHostConfig) createPodSpec(podName string) *v1.Pod {
hostPathType := new(v1.HostPathType)
*hostPathType = v1.HostPathType(string(v1.HostPathFileOrCreate))
mounts := []v1.VolumeMount{
{
Name: "host-etc-hosts",
MountPath: etcHostsOriginalPath,
},
}
multipleMounts := []v1.VolumeMount{
mounts[0],
{
Name: "host-etc-hosts",
MountPath: etcHostsPath,
},
}
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "busybox-1",
Image: etcHostsImageName,
ImagePullPolicy: v1.PullIfNotPresent,
Command: []string{
"sleep",
"900",
},
VolumeMounts: []v1.VolumeMount{
{
Name: "host-etc-hosts",
MountPath: etcHostsOriginalPath,
},
},
},
{
Name: "busybox-2",
Image: etcHostsImageName,
ImagePullPolicy: v1.PullIfNotPresent,
Command: []string{
"sleep",
"900",
},
VolumeMounts: []v1.VolumeMount{
{
Name: "host-etc-hosts",
MountPath: etcHostsOriginalPath,
},
},
},
{
Name: "busybox-3",
Image: etcHostsImageName,
ImagePullPolicy: v1.PullIfNotPresent,
Command: []string{
"sleep",
"900",
},
VolumeMounts: []v1.VolumeMount{
{
Name: "host-etc-hosts",
MountPath: etcHostsPath,
},
{
Name: "host-etc-hosts",
MountPath: etcHostsOriginalPath,
},
},
},
e2epod.NewAgnhostContainer("busybox-1", mounts, nil),
e2epod.NewAgnhostContainer("busybox-2", mounts, nil),
e2epod.NewAgnhostContainer("busybox-3", multipleMounts, nil),
},
Volumes: []v1.Volume{
{
Expand All @@ -222,12 +187,19 @@ func (config *KubeletManagedHostConfig) createPodSpec(podName string) *v1.Pod {
},
},
}

return pod
}

func (config *KubeletManagedHostConfig) createPodSpecWithHostNetwork(podName string) *v1.Pod {
hostPathType := new(v1.HostPathType)
*hostPathType = v1.HostPathType(string(v1.HostPathFileOrCreate))
mounts := []v1.VolumeMount{
{
Name: "host-etc-hosts",
MountPath: etcHostsOriginalPath,
},
}
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Expand All @@ -236,36 +208,8 @@ func (config *KubeletManagedHostConfig) createPodSpecWithHostNetwork(podName str
HostNetwork: true,
SecurityContext: &v1.PodSecurityContext{},
Containers: []v1.Container{
{
Name: "busybox-1",
Image: etcHostsImageName,
ImagePullPolicy: v1.PullIfNotPresent,
Command: []string{
"sleep",
"900",
},
VolumeMounts: []v1.VolumeMount{
{
Name: "host-etc-hosts",
MountPath: etcHostsOriginalPath,
},
},
},
{
Name: "busybox-2",
Image: etcHostsImageName,
ImagePullPolicy: v1.PullIfNotPresent,
Command: []string{
"sleep",
"900",
},
VolumeMounts: []v1.VolumeMount{
{
Name: "host-etc-hosts",
MountPath: etcHostsOriginalPath,
},
},
},
e2epod.NewAgnhostContainer("busybox-1", mounts, nil),
e2epod.NewAgnhostContainer("busybox-2", mounts, nil),
},
Volumes: []v1.Volume{
{
Expand Down
24 changes: 6 additions & 18 deletions test/e2e/common/lifecycle_hook.go
Expand Up @@ -25,6 +25,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/kubernetes/test/e2e/framework"
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
imageutils "k8s.io/kubernetes/test/utils/image"

"github.com/onsi/ginkgo"
Expand All @@ -41,26 +42,13 @@ var _ = framework.KubeDescribe("Container Lifecycle Hook", func() {
)
ginkgo.Context("when create a pod with lifecycle hook", func() {
var targetIP, targetURL string
podHandleHookRequest := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod-handle-http-request",
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "pod-handle-http-request",
Image: imageutils.GetE2EImage(imageutils.Agnhost),
Args: []string{"netexec"},
Ports: []v1.ContainerPort{
{
ContainerPort: 8080,
Protocol: v1.ProtocolTCP,
},
},
},
},
ports := []v1.ContainerPort{
{
ContainerPort: 8080,
Protocol: v1.ProtocolTCP,
},
}
podHandleHookRequest := e2epod.NewAgnhostPod("", "pod-handle-http-request", nil, nil, ports, "netexec")
ginkgo.BeforeEach(func() {
podClient = f.PodClient()
ginkgo.By("create the container to handle the HTTPGet hook request.")
Expand Down

0 comments on commit c99b185

Please sign in to comment.