Merge pull request #94440 from claudiubelu/tests/agnhost-usage-refactor-2

tests: Refactor agnhost image pod usage - common (part 1)
This commit is contained in:
Kubernetes Prow Robot 2020-10-16 14:58:12 -07:00 committed by GitHub
commit f738e5b320
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 94 additions and 263 deletions

View File

@ -20,11 +20,9 @@ import (
"github.com/onsi/gomega" "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/uuid" "k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/kubernetes/test/e2e/framework" "k8s.io/kubernetes/test/e2e/framework"
e2epod "k8s.io/kubernetes/test/e2e/framework/pod" e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
imageutils "k8s.io/kubernetes/test/utils/image"
) )
var _ = framework.KubeDescribe("Docker Containers", func() { var _ = framework.KubeDescribe("Docker Containers", func() {
@ -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 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() { 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) 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) framework.ExpectNoError(err, "Expected pod %q to be running, got error: %v", pod.Name, err)
pollLogs := func() (string, error) { 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" // The agnhost's image default entrypoint / args are: "/agnhost pause"
@ -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. 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() { framework.ConformanceIt("should be able to override the image's default arguments (docker cmd) [NodeConformance]", func() {
pod := entrypointTestPod() pod := entrypointTestPod(f.Namespace.Name, "entrypoint-tester", "override", "arguments")
pod.Spec.Containers[0].Args = []string{"entrypoint-tester", "override", "arguments"}
f.TestContainerOutput("override arguments", pod, 0, []string{ f.TestContainerOutput("override arguments", pod, 0, []string{
"[/agnhost entrypoint-tester override arguments]", "[/agnhost entrypoint-tester override arguments]",
}) })
@ -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. 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() { framework.ConformanceIt("should be able to override the image's default command (docker entrypoint) [NodeConformance]", func() {
pod := entrypointTestPod() pod := entrypointTestPod(f.Namespace.Name, "entrypoint-tester")
pod.Spec.Containers[0].Command = []string{"/agnhost-2", "entrypoint-tester"} pod.Spec.Containers[0].Command = []string{"/agnhost-2"}
f.TestContainerOutput("override command", pod, 0, []string{ f.TestContainerOutput("override command", pod, 0, []string{
"[/agnhost-2 entrypoint-tester]", "[/agnhost-2 entrypoint-tester]",
@ -85,9 +82,8 @@ 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. 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() { 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].Command = []string{"/agnhost-2"}
pod.Spec.Containers[0].Args = []string{"entrypoint-tester", "override", "arguments"}
f.TestContainerOutput("override all", pod, 0, []string{ f.TestContainerOutput("override all", pod, 0, []string{
"[/agnhost-2 entrypoint-tester override arguments]", "[/agnhost-2 entrypoint-tester override arguments]",
@ -95,26 +91,13 @@ var _ = framework.KubeDescribe("Docker Containers", func() {
}) })
}) })
const testContainerName = "test-container"
// Return a prototypical entrypoint test pod // Return a prototypical entrypoint test pod
func entrypointTestPod() *v1.Pod { func entrypointTestPod(namespace string, entrypointArgs ...string) *v1.Pod {
podName := "client-containers-" + string(uuid.NewUUID()) podName := "client-containers-" + string(uuid.NewUUID())
pod := e2epod.NewAgnhostPod(namespace, podName, nil, nil, nil, entrypointArgs...)
one := int64(1) one := int64(1)
return &v1.Pod{ pod.Spec.TerminationGracePeriodSeconds = &one
ObjectMeta: metav1.ObjectMeta{ pod.Spec.RestartPolicy = v1.RestartPolicyNever
Name: podName, return pod
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: testContainerName,
Image: imageutils.GetE2EImage(imageutils.Agnhost),
},
},
RestartPolicy: v1.RestartPolicyNever,
TerminationGracePeriodSeconds: &one,
},
}
} }

View File

@ -25,7 +25,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2" "k8s.io/klog/v2"
"k8s.io/kubernetes/test/e2e/framework" "k8s.io/kubernetes/test/e2e/framework"
imageutils "k8s.io/kubernetes/test/utils/image" e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
) )
const ( const (
@ -36,8 +36,6 @@ const (
etcHostsOriginalPath = "/etc/hosts-original" etcHostsOriginalPath = "/etc/hosts-original"
) )
var etcHostsImageName = imageutils.GetE2EImage(imageutils.Agnhost)
type KubeletManagedHostConfig struct { type KubeletManagedHostConfig struct {
hostNetworkPod *v1.Pod hostNetworkPod *v1.Pod
pod *v1.Pod pod *v1.Pod
@ -153,61 +151,28 @@ func (config *KubeletManagedHostConfig) getFileContents(podName, containerName,
func (config *KubeletManagedHostConfig) createPodSpec(podName string) *v1.Pod { func (config *KubeletManagedHostConfig) createPodSpec(podName string) *v1.Pod {
hostPathType := new(v1.HostPathType) hostPathType := new(v1.HostPathType)
*hostPathType = v1.HostPathType(string(v1.HostPathFileOrCreate)) *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{ pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: podName, Name: podName,
}, },
Spec: v1.PodSpec{ Spec: v1.PodSpec{
Containers: []v1.Container{ Containers: []v1.Container{
{ e2epod.NewAgnhostContainer("busybox-1", mounts, nil),
Name: "busybox-1", e2epod.NewAgnhostContainer("busybox-2", mounts, nil),
Image: etcHostsImageName, e2epod.NewAgnhostContainer("busybox-3", multipleMounts, nil),
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,
},
},
},
}, },
Volumes: []v1.Volume{ Volumes: []v1.Volume{
{ {
@ -222,12 +187,19 @@ func (config *KubeletManagedHostConfig) createPodSpec(podName string) *v1.Pod {
}, },
}, },
} }
return pod return pod
} }
func (config *KubeletManagedHostConfig) createPodSpecWithHostNetwork(podName string) *v1.Pod { func (config *KubeletManagedHostConfig) createPodSpecWithHostNetwork(podName string) *v1.Pod {
hostPathType := new(v1.HostPathType) hostPathType := new(v1.HostPathType)
*hostPathType = v1.HostPathType(string(v1.HostPathFileOrCreate)) *hostPathType = v1.HostPathType(string(v1.HostPathFileOrCreate))
mounts := []v1.VolumeMount{
{
Name: "host-etc-hosts",
MountPath: etcHostsOriginalPath,
},
}
pod := &v1.Pod{ pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: podName, Name: podName,
@ -236,36 +208,8 @@ func (config *KubeletManagedHostConfig) createPodSpecWithHostNetwork(podName str
HostNetwork: true, HostNetwork: true,
SecurityContext: &v1.PodSecurityContext{}, SecurityContext: &v1.PodSecurityContext{},
Containers: []v1.Container{ Containers: []v1.Container{
{ e2epod.NewAgnhostContainer("busybox-1", mounts, nil),
Name: "busybox-1", e2epod.NewAgnhostContainer("busybox-2", mounts, nil),
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,
},
},
},
}, },
Volumes: []v1.Volume{ Volumes: []v1.Volume{
{ {

View File

@ -25,6 +25,7 @@ import (
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/kubernetes/test/e2e/framework" "k8s.io/kubernetes/test/e2e/framework"
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
imageutils "k8s.io/kubernetes/test/utils/image" imageutils "k8s.io/kubernetes/test/utils/image"
"github.com/onsi/ginkgo" "github.com/onsi/ginkgo"
@ -41,26 +42,13 @@ var _ = framework.KubeDescribe("Container Lifecycle Hook", func() {
) )
ginkgo.Context("when create a pod with lifecycle hook", func() { ginkgo.Context("when create a pod with lifecycle hook", func() {
var targetIP, targetURL string var targetIP, targetURL string
podHandleHookRequest := &v1.Pod{ ports := []v1.ContainerPort{
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, ContainerPort: 8080,
Protocol: v1.ProtocolTCP, Protocol: v1.ProtocolTCP,
}, },
},
},
},
},
} }
podHandleHookRequest := e2epod.NewAgnhostPod("", "pod-handle-http-request", nil, nil, ports, "netexec")
ginkgo.BeforeEach(func() { ginkgo.BeforeEach(func() {
podClient = f.PodClient() podClient = f.PodClient()
ginkgo.By("create the container to handle the HTTPGet hook request.") ginkgo.By("create the container to handle the HTTPGet hook request.")

View File

@ -125,7 +125,6 @@ var _ = ginkgo.Describe("[sig-storage] Projected configMap", func() {
name := "projected-configmap-test-upd-" + string(uuid.NewUUID()) name := "projected-configmap-test-upd-" + string(uuid.NewUUID())
volumeName := "projected-configmap-volume" volumeName := "projected-configmap-volume"
volumeMountPath := "/etc/projected-configmap-volume" volumeMountPath := "/etc/projected-configmap-volume"
containerName := "projected-configmap-volume-test"
configMap := &v1.ConfigMap{ configMap := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Namespace: f.Namespace.Name, Namespace: f.Namespace.Name,
@ -142,51 +141,14 @@ var _ = ginkgo.Describe("[sig-storage] Projected configMap", func() {
framework.Failf("unable to create test configMap %s: %v", configMap.Name, err) framework.Failf("unable to create test configMap %s: %v", configMap.Name, err)
} }
pod := &v1.Pod{ pod := createProjectedConfigMapMounttestPod(f.Namespace.Name, volumeName, name, volumeMountPath,
ObjectMeta: metav1.ObjectMeta{ "--break_on_expected_content=false", containerTimeoutArg, "--file_content_in_loop=/etc/projected-configmap-volume/data-1")
Name: "pod-projected-configmaps-" + string(uuid.NewUUID()),
},
Spec: v1.PodSpec{
Volumes: []v1.Volume{
{
Name: volumeName,
VolumeSource: v1.VolumeSource{
Projected: &v1.ProjectedVolumeSource{
Sources: []v1.VolumeProjection{
{
ConfigMap: &v1.ConfigMapProjection{
LocalObjectReference: v1.LocalObjectReference{
Name: name,
},
},
},
},
},
},
},
},
Containers: []v1.Container{
{
Name: containerName,
Image: imageutils.GetE2EImage(imageutils.Agnhost),
Args: []string{"mounttest", "--break_on_expected_content=false", containerTimeoutArg, "--file_content_in_loop=/etc/projected-configmap-volume/data-1"},
VolumeMounts: []v1.VolumeMount{
{
Name: volumeName,
MountPath: volumeMountPath,
ReadOnly: true,
},
},
},
},
RestartPolicy: v1.RestartPolicyNever,
},
}
ginkgo.By("Creating the pod") ginkgo.By("Creating the pod")
f.PodClient().CreateSync(pod) f.PodClient().CreateSync(pod)
pollLogs := func() (string, error) { 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)
} }
gomega.Eventually(pollLogs, podLogTimeout, framework.Poll).Should(gomega.ContainSubstring("value-1")) gomega.Eventually(pollLogs, podLogTimeout, framework.Poll).Should(gomega.ContainSubstring("value-1"))
@ -529,49 +491,8 @@ func doProjectedConfigMapE2EWithoutMappings(f *framework.Framework, asUser bool,
framework.Failf("unable to create test configMap %s: %v", configMap.Name, err) framework.Failf("unable to create test configMap %s: %v", configMap.Name, err)
} }
pod := &v1.Pod{ pod := createProjectedConfigMapMounttestPod(f.Namespace.Name, volumeName, name, volumeMountPath,
ObjectMeta: metav1.ObjectMeta{ "--file_content=/etc/projected-configmap-volume/data-1", "--file_mode=/etc/projected-configmap-volume/data-1")
Name: "pod-projected-configmaps-" + string(uuid.NewUUID()),
},
Spec: v1.PodSpec{
SecurityContext: &v1.PodSecurityContext{},
Volumes: []v1.Volume{
{
Name: volumeName,
VolumeSource: v1.VolumeSource{
Projected: &v1.ProjectedVolumeSource{
Sources: []v1.VolumeProjection{
{
ConfigMap: &v1.ConfigMapProjection{
LocalObjectReference: v1.LocalObjectReference{
Name: name,
},
},
},
},
},
},
},
},
Containers: []v1.Container{
{
Name: "projected-configmap-volume-test",
Image: imageutils.GetE2EImage(imageutils.Agnhost),
Args: []string{
"mounttest",
"--file_content=/etc/projected-configmap-volume/data-1",
"--file_mode=/etc/projected-configmap-volume/data-1"},
VolumeMounts: []v1.VolumeMount{
{
Name: volumeName,
MountPath: volumeMountPath,
},
},
},
},
RestartPolicy: v1.RestartPolicyNever,
},
}
if asUser { if asUser {
setPodNonRootUser(pod) setPodNonRootUser(pod)
@ -611,55 +532,13 @@ func doProjectedConfigMapE2EWithMappings(f *framework.Framework, asUser bool, fs
framework.Failf("unable to create test configMap %s: %v", configMap.Name, err) framework.Failf("unable to create test configMap %s: %v", configMap.Name, err)
} }
pod := &v1.Pod{ pod := createProjectedConfigMapMounttestPod(f.Namespace.Name, volumeName, name, volumeMountPath,
ObjectMeta: metav1.ObjectMeta{ "--file_content=/etc/projected-configmap-volume/path/to/data-2", "--file_mode=/etc/projected-configmap-volume/path/to/data-2")
Name: "pod-projected-configmaps-" + string(uuid.NewUUID()), pod.Spec.Volumes[0].VolumeSource.Projected.Sources[0].ConfigMap.Items = []v1.KeyToPath{
},
Spec: v1.PodSpec{
SecurityContext: &v1.PodSecurityContext{},
Volumes: []v1.Volume{
{
Name: volumeName,
VolumeSource: v1.VolumeSource{
Projected: &v1.ProjectedVolumeSource{
Sources: []v1.VolumeProjection{
{
ConfigMap: &v1.ConfigMapProjection{
LocalObjectReference: v1.LocalObjectReference{
Name: name,
},
Items: []v1.KeyToPath{
{ {
Key: "data-2", Key: "data-2",
Path: "path/to/data-2", Path: "path/to/data-2",
}, },
},
},
},
},
},
},
},
},
Containers: []v1.Container{
{
Name: "projected-configmap-volume-test",
Image: imageutils.GetE2EImage(imageutils.Agnhost),
Args: []string{
"mounttest",
"--file_content=/etc/projected-configmap-volume/path/to/data-2",
"--file_mode=/etc/projected-configmap-volume/path/to/data-2"},
VolumeMounts: []v1.VolumeMount{
{
Name: volumeName,
MountPath: volumeMountPath,
ReadOnly: true,
},
},
},
},
RestartPolicy: v1.RestartPolicyNever,
},
} }
if asUser { if asUser {
@ -686,3 +565,29 @@ func doProjectedConfigMapE2EWithMappings(f *framework.Framework, asUser bool, fs
} }
f.TestContainerOutputRegexp("consume configMaps", pod, 0, output) f.TestContainerOutputRegexp("consume configMaps", pod, 0, output)
} }
func createProjectedConfigMapMounttestPod(namespace, volumeName, referenceName, mountPath string, mounttestArgs ...string) *v1.Pod {
volumes := []v1.Volume{
{
Name: volumeName,
VolumeSource: v1.VolumeSource{
Projected: &v1.ProjectedVolumeSource{
Sources: []v1.VolumeProjection{
{
ConfigMap: &v1.ConfigMapProjection{
LocalObjectReference: v1.LocalObjectReference{
Name: referenceName,
},
},
},
},
},
},
},
}
podName := "pod-projected-configmaps-" + string(uuid.NewUUID())
mounttestArgs = append([]string{"mounttest"}, mounttestArgs...)
pod := e2epod.NewAgnhostPod(namespace, podName, volumes, createMounts(volumeName, mountPath, true), nil, mounttestArgs...)
pod.Spec.RestartPolicy = v1.RestartPolicyNever
return pod
}

View File

@ -241,3 +241,14 @@ func getFileModeRegex(filePath string, mask *int32) string {
return fmt.Sprintf("(%s|%s)", linuxOutput, windowsOutput) return fmt.Sprintf("(%s|%s)", linuxOutput, windowsOutput)
} }
// createMounts creates a v1.VolumeMount list with a single element.
func createMounts(volumeName, volumeMountPath string, readOnly bool) []v1.VolumeMount {
return []v1.VolumeMount{
{
Name: volumeName,
MountPath: volumeMountPath,
ReadOnly: readOnly,
},
}
}