tests: Refactors agnhost image pod usage in tests

A previous commit created a few agnhost related functions that creates agnhost
pods / containers for general purposes.

Refactors tests to use those functions.
This commit is contained in:
Claudiu Belu 2019-12-17 17:22:46 +02:00
parent 131f42d263
commit 89eae73323
8 changed files with 53 additions and 211 deletions

View File

@ -35,8 +35,8 @@ import (
utilversion "k8s.io/apimachinery/pkg/util/version" utilversion "k8s.io/apimachinery/pkg/util/version"
"k8s.io/cli-runtime/pkg/printers" "k8s.io/cli-runtime/pkg/printers"
"k8s.io/kubernetes/test/e2e/framework" "k8s.io/kubernetes/test/e2e/framework"
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper" e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
imageutils "k8s.io/kubernetes/test/utils/image"
) )
var serverPrintVersion = utilversion.MustParseSemantic("v1.10.0") var serverPrintVersion = utilversion.MustParseSemantic("v1.10.0")
@ -55,7 +55,7 @@ var _ = SIGDescribe("Servers with support for Table transformation", func() {
podName := "pod-1" podName := "pod-1"
framework.Logf("Creating pod %s", podName) framework.Logf("Creating pod %s", podName)
_, err := c.CoreV1().Pods(ns).Create(context.TODO(), newTablePod(podName), metav1.CreateOptions{}) _, err := c.CoreV1().Pods(ns).Create(context.TODO(), newTablePod(ns, podName), metav1.CreateOptions{})
framework.ExpectNoError(err, "failed to create pod %s in namespace: %s", podName, ns) framework.ExpectNoError(err, "failed to create pod %s in namespace: %s", podName, ns)
table := &metav1beta1.Table{} table := &metav1beta1.Table{}
@ -177,25 +177,10 @@ func printTable(table *metav1beta1.Table) string {
return buf.String() return buf.String()
} }
func newTablePod(podName string) *v1.Pod { func newTablePod(ns, podName string) *v1.Pod {
containerName := fmt.Sprintf("%s-container", podName)
port := 8080 port := 8080
pod := &v1.Pod{ pod := e2epod.NewAgnhostPod(ns, podName, nil, nil, []v1.ContainerPort{{ContainerPort: int32(port)}}, "porter")
ObjectMeta: metav1.ObjectMeta{ pod.Spec.Containers[0].Env = []v1.EnvVar{{Name: fmt.Sprintf("SERVE_PORT_%d", port), Value: "foo"}}
Name: podName, pod.Spec.RestartPolicy = v1.RestartPolicyNever
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: containerName,
Image: imageutils.GetE2EImage(imageutils.Agnhost),
Args: []string{"porter"},
Env: []v1.EnvVar{{Name: fmt.Sprintf("SERVE_PORT_%d", port), Value: "foo"}},
Ports: []v1.ContainerPort{{ContainerPort: int32(port)}},
},
},
RestartPolicy: v1.RestartPolicyNever,
},
}
return pod return pod
} }

View File

@ -24,11 +24,11 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/pkg/cluster/ports" "k8s.io/kubernetes/pkg/cluster/ports"
"k8s.io/kubernetes/test/e2e/framework" "k8s.io/kubernetes/test/e2e/framework"
imageutils "k8s.io/kubernetes/test/utils/image"
"github.com/onsi/ginkgo" "github.com/onsi/ginkgo"
"github.com/onsi/gomega" "github.com/onsi/gomega"
e2enode "k8s.io/kubernetes/test/e2e/framework/node" e2enode "k8s.io/kubernetes/test/e2e/framework/node"
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
) )
var _ = SIGDescribe("[Feature:NodeAuthenticator]", func() { var _ = SIGDescribe("[Feature:NodeAuthenticator]", func() {
@ -93,19 +93,7 @@ var _ = SIGDescribe("[Feature:NodeAuthenticator]", func() {
}) })
func createNodeAuthTestPod(f *framework.Framework) *v1.Pod { func createNodeAuthTestPod(f *framework.Framework) *v1.Pod {
pod := &v1.Pod{ pod := e2epod.NewAgnhostPod(f.Namespace.Name, "agnhost-pod", nil, nil, nil)
ObjectMeta: metav1.ObjectMeta{ pod.ObjectMeta.GenerateName = "test-node-authn-"
GenerateName: "test-node-authn-",
},
Spec: v1.PodSpec{
Containers: []v1.Container{{
Name: "test-node-authn",
Image: imageutils.GetE2EImage(imageutils.Agnhost),
Command: []string{"sleep", "3600"},
}},
RestartPolicy: v1.RestartPolicyNever,
},
}
return f.PodClient().CreateSync(pod) return f.PodClient().CreateSync(pod)
} }

View File

@ -3587,11 +3587,7 @@ func createPausePodDeployment(cs clientset.Interface, name, ns string, replicas
labels := map[string]string{"deployment": "agnhost-pause"} labels := map[string]string{"deployment": "agnhost-pause"}
pauseDeployment := e2edeployment.NewDeployment(name, int32(replicas), labels, "", "", appsv1.RollingUpdateDeploymentStrategyType) pauseDeployment := e2edeployment.NewDeployment(name, int32(replicas), labels, "", "", appsv1.RollingUpdateDeploymentStrategyType)
pauseDeployment.Spec.Template.Spec.Containers[0] = v1.Container{ pauseDeployment.Spec.Template.Spec.Containers[0] = e2epod.NewAgnhostContainer("agnhost-pause", nil, nil, "pause")
Name: "agnhost-pause",
Image: imageutils.GetE2EImage(imageutils.Agnhost),
Args: []string{"pause"},
}
pauseDeployment.Spec.Template.Spec.Affinity = &v1.Affinity{ pauseDeployment.Spec.Template.Spec.Affinity = &v1.Affinity{
PodAntiAffinity: &v1.PodAntiAffinity{ PodAntiAffinity: &v1.PodAntiAffinity{
RequiredDuringSchedulingIgnoredDuringExecution: []v1.PodAffinityTerm{ RequiredDuringSchedulingIgnoredDuringExecution: []v1.PodAffinityTerm{
@ -3612,25 +3608,12 @@ func createPausePodDeployment(cs clientset.Interface, name, ns string, replicas
// createPodOrFail creates a pod with the specified containerPorts. // createPodOrFail creates a pod with the specified containerPorts.
func createPodOrFail(c clientset.Interface, ns, name string, labels map[string]string, containerPorts []v1.ContainerPort) { func createPodOrFail(c clientset.Interface, ns, name string, labels map[string]string, containerPorts []v1.ContainerPort) {
ginkgo.By(fmt.Sprintf("Creating pod %s in namespace %s", name, ns)) ginkgo.By(fmt.Sprintf("Creating pod %s in namespace %s", name, ns))
pod := &v1.Pod{ pod := e2epod.NewAgnhostPod(ns, name, nil, nil, containerPorts)
ObjectMeta: metav1.ObjectMeta{ pod.ObjectMeta.Labels = labels
Name: name,
Labels: labels,
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "pause",
Image: imageutils.GetE2EImage(imageutils.Agnhost),
Args: []string{"pause"},
Ports: containerPorts,
// Add a dummy environment variable to work around a docker issue. // Add a dummy environment variable to work around a docker issue.
// https://github.com/docker/docker/issues/14203 // https://github.com/docker/docker/issues/14203
Env: []v1.EnvVar{{Name: "FOO", Value: " "}}, pod.Spec.Containers[0].Env = []v1.EnvVar{{Name: "FOO", Value: " "}}
},
},
},
}
_, err := c.CoreV1().Pods(ns).Create(context.TODO(), pod, metav1.CreateOptions{}) _, err := c.CoreV1().Pods(ns).Create(context.TODO(), pod, metav1.CreateOptions{})
framework.ExpectNoError(err, "failed to create pod %s in namespace %s", name, ns) framework.ExpectNoError(err, "failed to create pod %s in namespace %s", name, ns)
} }

View File

@ -63,23 +63,10 @@ var _ = SIGDescribe("Pods Extended", func() {
ginkgo.By("creating the pod") ginkgo.By("creating the pod")
name := "pod-submit-remove-" + string(uuid.NewUUID()) name := "pod-submit-remove-" + string(uuid.NewUUID())
value := strconv.Itoa(time.Now().Nanosecond()) value := strconv.Itoa(time.Now().Nanosecond())
pod := &v1.Pod{ pod := e2epod.NewAgnhostPod(f.Namespace.Name, name, nil, nil, nil)
ObjectMeta: metav1.ObjectMeta{ pod.ObjectMeta.Labels = map[string]string{
Name: name,
Labels: map[string]string{
"name": "foo", "name": "foo",
"time": value, "time": value,
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "agnhost",
Image: imageutils.GetE2EImage(imageutils.Agnhost),
Args: []string{"pause"},
},
},
},
} }
ginkgo.By("setting up selector") ginkgo.By("setting up selector")

View File

@ -44,21 +44,7 @@ type State struct {
func testPreStop(c clientset.Interface, ns string) { func testPreStop(c clientset.Interface, ns string) {
// This is the server that will receive the preStop notification // This is the server that will receive the preStop notification
podDescr := &v1.Pod{ podDescr := e2epod.NewAgnhostPod(ns, "server", nil, nil, []v1.ContainerPort{{ContainerPort: 8080}}, "nettest")
ObjectMeta: metav1.ObjectMeta{
Name: "server",
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "server",
Image: imageutils.GetE2EImage(imageutils.Agnhost),
Args: []string{"nettest"},
Ports: []v1.ContainerPort{{ContainerPort: 8080}},
},
},
},
}
ginkgo.By(fmt.Sprintf("Creating server pod %s in namespace %s", podDescr.Name, ns)) ginkgo.By(fmt.Sprintf("Creating server pod %s in namespace %s", podDescr.Name, ns))
podDescr, err := c.CoreV1().Pods(ns).Create(context.TODO(), podDescr, metav1.CreateOptions{}) podDescr, err := c.CoreV1().Pods(ns).Create(context.TODO(), podDescr, metav1.CreateOptions{})
framework.ExpectNoError(err, fmt.Sprintf("creating pod %s", podDescr.Name)) framework.ExpectNoError(err, fmt.Sprintf("creating pod %s", podDescr.Name))

View File

@ -214,25 +214,8 @@ func createGitServer(f *framework.Framework) (gitURL string, gitRepo string, cle
labels := map[string]string{"name": gitServerPodName} labels := map[string]string{"name": gitServerPodName}
gitServerPod := &v1.Pod{ gitServerPod := e2epod.NewAgnhostPod(f.Namespace.Name, gitServerPodName, nil, nil, []v1.ContainerPort{{ContainerPort: int32(containerPort)}}, "fake-gitserver")
ObjectMeta: metav1.ObjectMeta{ gitServerPod.ObjectMeta.Labels = labels
Name: gitServerPodName,
Labels: labels,
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "git-repo",
Image: imageutils.GetE2EImage(imageutils.Agnhost),
Args: []string{"fake-gitserver"},
ImagePullPolicy: "IfNotPresent",
Ports: []v1.ContainerPort{
{ContainerPort: int32(containerPort)},
},
},
},
},
}
f.PodClient().CreateSync(gitServerPod) f.PodClient().CreateSync(gitServerPod)
// Portal IP and port // Portal IP and port

View File

@ -508,6 +508,28 @@ func SubpathTestPod(f *framework.Framework, subpath, volumeType string, source *
probeVolumeName = "liveness-probe-volume" probeVolumeName = "liveness-probe-volume"
seLinuxOptions = &v1.SELinuxOptions{Level: "s0:c0,c1"} seLinuxOptions = &v1.SELinuxOptions{Level: "s0:c0,c1"}
) )
volumeMount := v1.VolumeMount{Name: volumeName, MountPath: volumePath}
volumeSubpathMount := v1.VolumeMount{Name: volumeName, MountPath: volumePath, SubPath: subpath}
probeMount := v1.VolumeMount{Name: probeVolumeName, MountPath: probeVolumePath}
initSubpathContainer := e2epod.NewAgnhostContainer(
fmt.Sprintf("test-init-subpath-%s", suffix),
[]v1.VolumeMount{volumeSubpathMount, probeMount}, nil, "mounttest")
initSubpathContainer.SecurityContext = e2evolume.GenerateSecurityContext(privilegedSecurityContext)
initVolumeContainer := e2epod.NewAgnhostContainer(
fmt.Sprintf("test-init-volume-%s", suffix),
[]v1.VolumeMount{volumeMount, probeMount}, nil, "mounttest")
initVolumeContainer.SecurityContext = e2evolume.GenerateSecurityContext(privilegedSecurityContext)
subpathContainer := e2epod.NewAgnhostContainer(
fmt.Sprintf("test-container-subpath-%s", suffix),
[]v1.VolumeMount{volumeSubpathMount, probeMount}, nil, "mounttest")
subpathContainer.SecurityContext = e2evolume.GenerateSecurityContext(privilegedSecurityContext)
volumeContainer := e2epod.NewAgnhostContainer(
fmt.Sprintf("test-container-volume-%s", suffix),
[]v1.VolumeMount{volumeMount, probeMount}, nil, "mounttest")
volumeContainer.SecurityContext = e2evolume.GenerateSecurityContext(privilegedSecurityContext)
return &v1.Pod{ return &v1.Pod{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("pod-subpath-test-%s", suffix), Name: fmt.Sprintf("pod-subpath-test-%s", suffix),
@ -518,86 +540,15 @@ func SubpathTestPod(f *framework.Framework, subpath, volumeType string, source *
{ {
Name: fmt.Sprintf("init-volume-%s", suffix), Name: fmt.Sprintf("init-volume-%s", suffix),
Image: e2evolume.GetTestImage(imageutils.GetE2EImage(imageutils.BusyBox)), Image: e2evolume.GetTestImage(imageutils.GetE2EImage(imageutils.BusyBox)),
VolumeMounts: []v1.VolumeMount{ VolumeMounts: []v1.VolumeMount{volumeMount, probeMount},
{
Name: volumeName,
MountPath: volumePath,
},
{
Name: probeVolumeName,
MountPath: probeVolumePath,
},
},
SecurityContext: e2evolume.GenerateSecurityContext(privilegedSecurityContext),
},
{
Name: fmt.Sprintf("test-init-subpath-%s", suffix),
Image: mountImage,
Args: []string{"mounttest"},
VolumeMounts: []v1.VolumeMount{
{
Name: volumeName,
MountPath: volumePath,
SubPath: subpath,
},
{
Name: probeVolumeName,
MountPath: probeVolumePath,
},
},
SecurityContext: e2evolume.GenerateSecurityContext(privilegedSecurityContext),
},
{
Name: fmt.Sprintf("test-init-volume-%s", suffix),
Image: mountImage,
Args: []string{"mounttest"},
VolumeMounts: []v1.VolumeMount{
{
Name: volumeName,
MountPath: volumePath,
},
{
Name: probeVolumeName,
MountPath: probeVolumePath,
},
},
SecurityContext: e2evolume.GenerateSecurityContext(privilegedSecurityContext), SecurityContext: e2evolume.GenerateSecurityContext(privilegedSecurityContext),
}, },
initSubpathContainer,
initVolumeContainer,
}, },
Containers: []v1.Container{ Containers: []v1.Container{
{ subpathContainer,
Name: fmt.Sprintf("test-container-subpath-%s", suffix), volumeContainer,
Image: mountImage,
Args: []string{"mounttest"},
VolumeMounts: []v1.VolumeMount{
{
Name: volumeName,
MountPath: volumePath,
SubPath: subpath,
},
{
Name: probeVolumeName,
MountPath: probeVolumePath,
},
},
SecurityContext: e2evolume.GenerateSecurityContext(privilegedSecurityContext),
},
{
Name: fmt.Sprintf("test-container-volume-%s", suffix),
Image: mountImage,
Args: []string{"mounttest"},
VolumeMounts: []v1.VolumeMount{
{
Name: volumeName,
MountPath: volumePath,
},
{
Name: probeVolumeName,
MountPath: probeVolumePath,
},
},
SecurityContext: e2evolume.GenerateSecurityContext(privilegedSecurityContext),
},
}, },
RestartPolicy: v1.RestartPolicyNever, RestartPolicy: v1.RestartPolicyNever,
TerminationGracePeriodSeconds: &gracePeriod, TerminationGracePeriodSeconds: &gracePeriod,

View File

@ -26,7 +26,6 @@ import (
"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"
e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper" e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
imageutils "k8s.io/kubernetes/test/utils/image"
"github.com/onsi/ginkgo" "github.com/onsi/ginkgo"
) )
@ -45,7 +44,7 @@ var _ = SIGDescribe("DNS", func() {
testSearchPath := "resolv.conf.local" testSearchPath := "resolv.conf.local"
ginkgo.By("Creating a pod with dnsPolicy=None and customized dnsConfig...") ginkgo.By("Creating a pod with dnsPolicy=None and customized dnsConfig...")
testUtilsPod := generateDNSUtilsPod() testUtilsPod := e2epod.NewAgnhostPod(f.Namespace.Name, "e2e-dns-utils", nil, nil, nil)
testUtilsPod.Spec.DNSPolicy = v1.DNSNone testUtilsPod.Spec.DNSPolicy = v1.DNSNone
testUtilsPod.Spec.DNSConfig = &v1.PodDNSConfig{ testUtilsPod.Spec.DNSConfig = &v1.PodDNSConfig{
Nameservers: []string{testInjectedIP}, Nameservers: []string{testInjectedIP},
@ -90,23 +89,3 @@ var _ = SIGDescribe("DNS", func() {
// TODO: Add more test cases for other DNSPolicies. // TODO: Add more test cases for other DNSPolicies.
}) })
}) })
func generateDNSUtilsPod() *v1.Pod {
return &v1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
},
ObjectMeta: metav1.ObjectMeta{
GenerateName: "e2e-dns-utils-",
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "util",
Image: imageutils.GetE2EImage(imageutils.Agnhost),
Command: []string{"sleep", "10000"},
},
},
},
}
}