diff options
Diffstat (limited to 'test/e2e/play_kube_test.go')
-rw-r--r-- | test/e2e/play_kube_test.go | 138 |
1 files changed, 122 insertions, 16 deletions
diff --git a/test/e2e/play_kube_test.go b/test/e2e/play_kube_test.go index 5e01971cb..7a5aebcc2 100644 --- a/test/e2e/play_kube_test.go +++ b/test/e2e/play_kube_test.go @@ -30,9 +30,14 @@ apiVersion: v1 kind: Pod metadata: creationTimestamp: "2019-07-17T14:44:08Z" + name: {{ .Name }} labels: app: {{ .Name }} - name: {{ .Name }} +{{ with .Labels }} + {{ range $key, $value := . }} + {{ $key }}: {{ $value }} + {{ end }} +{{ end }} {{ with .Annotations }} annotations: {{ range $key, $value := . }} @@ -41,6 +46,7 @@ metadata: {{ end }} spec: + restartPolicy: {{ .RestartPolicy }} hostname: {{ .Hostname }} hostAliases: {{ range .HostAliases }} @@ -125,9 +131,14 @@ apiVersion: v1 kind: Deployment metadata: creationTimestamp: "2019-07-17T14:44:08Z" + name: {{ .Name }} labels: app: {{ .Name }} - name: {{ .Name }} +{{ with .Labels }} + {{ range $key, $value := . }} + {{ $key }}: {{ $value }} + {{ end }} +{{ end }} {{ with .Annotations }} annotations: {{ range $key, $value := . }} @@ -145,13 +156,17 @@ spec: metadata: labels: app: {{ .Name }} - {{ with .Annotations }} - annotations: - {{ range $key, $value := . }} - {{ $key }}: {{ $value }} - {{ end }} - {{ end }} + {{- with .Labels }}{{ range $key, $value := . }} + {{ $key }}: {{ $value }} + {{- end }}{{ end }} + {{- with .Annotations }} + annotations: + {{- range $key, $value := . }} + {{ $key }}: {{ $value }} + {{- end }} + {{- end }} spec: + restartPolicy: {{ .RestartPolicy }} hostname: {{ .Hostname }} containers: {{ with .Ctrs }} @@ -261,12 +276,14 @@ func generateDeploymentKubeYaml(deployment *Deployment, fileName string) error { // Pod describes the options a kube yaml can be configured at pod level type Pod struct { - Name string - Hostname string - HostAliases []HostAlias - Ctrs []*Ctr - Volumes []*Volume - Annotations map[string]string + Name string + RestartPolicy string + Hostname string + HostAliases []HostAlias + Ctrs []*Ctr + Volumes []*Volume + Labels map[string]string + Annotations map[string]string } type HostAlias struct { @@ -278,7 +295,16 @@ type HostAlias struct { // and the configured options // if no containers are added, it will add the default container func getPod(options ...podOption) *Pod { - p := Pod{defaultPodName, "", nil, make([]*Ctr, 0), make([]*Volume, 0), make(map[string]string)} + p := Pod{ + Name: defaultPodName, + RestartPolicy: "Never", + Hostname: "", + HostAliases: nil, + Ctrs: make([]*Ctr, 0), + Volumes: make([]*Volume, 0), + Labels: make(map[string]string), + Annotations: make(map[string]string), + } for _, option := range options { option(&p) } @@ -290,6 +316,12 @@ func getPod(options ...podOption) *Pod { type podOption func(*Pod) +func withPodName(name string) podOption { + return func(pod *Pod) { + pod.Name = name + } +} + func withHostname(h string) podOption { return func(pod *Pod) { pod.Hostname = h @@ -311,6 +343,18 @@ func withCtr(c *Ctr) podOption { } } +func withRestartPolicy(policy string) podOption { + return func(pod *Pod) { + pod.RestartPolicy = policy + } +} + +func withLabel(k, v string) podOption { + return func(pod *Pod) { + pod.Labels[k] = v + } +} + func withAnnotation(k, v string) podOption { return func(pod *Pod) { pod.Annotations[k] = v @@ -327,12 +371,19 @@ func withVolume(v *Volume) podOption { type Deployment struct { Name string Replicas int32 + Labels map[string]string Annotations map[string]string PodTemplate *Pod } func getDeployment(options ...deploymentOption) *Deployment { - d := Deployment{defaultDeploymentName, 1, make(map[string]string), getPod()} + d := Deployment{ + Name: defaultDeploymentName, + Replicas: 1, + Labels: make(map[string]string), + Annotations: make(map[string]string), + PodTemplate: getPod(), + } for _, option := range options { option(&d) } @@ -342,6 +393,12 @@ func getDeployment(options ...deploymentOption) *Deployment { type deploymentOption func(*Deployment) +func withDeploymentLabel(k, v string) deploymentOption { + return func(deployment *Deployment) { + deployment.Labels[k] = v + } +} + func withDeploymentAnnotation(k, v string) deploymentOption { return func(deployment *Deployment) { deployment.Annotations[k] = v @@ -608,6 +665,30 @@ var _ = Describe("Podman generate kube", func() { Expect(inspect.OutputToString()).To(ContainSubstring(`[echo hello world]`)) }) + It("podman play kube test restartPolicy", func() { + // podName, set, expect + testSli := [][]string{ + {"testPod1", "", "always"}, // Default eqaul to always + {"testPod2", "Always", "always"}, + {"testPod3", "OnFailure", "on-failure"}, + {"testPod4", "Never", "no"}, + } + for _, v := range testSli { + pod := getPod(withPodName(v[0]), withRestartPolicy(v[1])) + err := generatePodKubeYaml(pod, kubeYaml) + Expect(err).To(BeNil()) + + kube := podmanTest.Podman([]string{"play", "kube", kubeYaml}) + kube.WaitWithDefaultTimeout() + Expect(kube.ExitCode()).To(Equal(0)) + + inspect := podmanTest.Podman([]string{"inspect", getCtrNameInPod(pod), "--format", "{{.HostConfig.RestartPolicy.Name}}"}) + inspect.WaitWithDefaultTimeout() + Expect(inspect.ExitCode()).To(Equal(0)) + Expect(inspect.OutputToString()).To(Equal(v[2])) + } + }) + It("podman play kube test hostname", func() { pod := getPod() err := generatePodKubeYaml(pod, kubeYaml) @@ -1077,4 +1158,29 @@ spec: correct := fmt.Sprintf("%s:%s:%s", hostPathLocation, hostPathLocation, "ro") Expect(inspect.OutputToString()).To(ContainSubstring(correct)) }) + + It("podman play kube applies labels to pods", func() { + SkipIfRemote() + var numReplicas int32 = 5 + expectedLabelKey := "key1" + expectedLabelValue := "value1" + deployment := getDeployment( + withReplicas(numReplicas), + withPod(getPod(withLabel(expectedLabelKey, expectedLabelValue))), + ) + err := generateDeploymentKubeYaml(deployment, kubeYaml) + Expect(err).To(BeNil()) + + kube := podmanTest.Podman([]string{"play", "kube", kubeYaml}) + kube.WaitWithDefaultTimeout() + Expect(kube.ExitCode()).To(Equal(0)) + + correctLabels := expectedLabelKey + ":" + expectedLabelValue + for _, pod := range getPodNamesInDeployment(deployment) { + inspect := podmanTest.Podman([]string{"pod", "inspect", pod.Name, "--format", "'{{ .Labels }}'"}) + inspect.WaitWithDefaultTimeout() + Expect(inspect.ExitCode()).To(Equal(0)) + Expect(inspect.OutputToString()).To(ContainSubstring(correctLabels)) + } + }) }) |