From f827d958ace6fc409bad2e121223f346a3e772e2 Mon Sep 17 00:00:00 2001 From: Vincent Deng Date: Sun, 3 Dec 2023 14:58:44 +0800 Subject: [PATCH] sidecar-20372 Add e2e test Signed-off-by: Vincent Deng --- test/e2e/play_kube_test.go | 117 +++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/test/e2e/play_kube_test.go b/test/e2e/play_kube_test.go index 77aed75e860d..7ab72dbe2613 100644 --- a/test/e2e/play_kube_test.go +++ b/test/e2e/play_kube_test.go @@ -582,6 +582,21 @@ spec: {{ end }} image: {{ .Image }} name: {{ .Name }} +{{ if .RestartPolicy }} + restartPolicy: {{ .RestartPolicy }} + {{ if .StartupProbe }} + startupProbe: + exec: + command: + {{ range .StartupProbe.Cmd }} + - {{.}} + {{ end }} + {{ if .StartupProbe.TimeoutSeconds }} + timeoutSeconds: {{ .StartupProbe.TimeoutSeconds }} + {{ end }} + {{ end }} +{{ end }} + {{ end }} {{ end }} {{ if .SecurityContext }} @@ -1644,6 +1659,40 @@ func getPodNameInDeployment(d *Deployment) Pod { return p } +// HealthcheckProbe describes the options for a health check probe +type HealthCheckProbe struct { + Cmd []string + InitialDelaySeconds int + PeriodSeconds int + TimeoutSeconds int +} + +func getHealthCheckProbe(options ...healthProbeOption) *HealthCheckProbe { + probe := HealthCheckProbe{ + InitialDelaySeconds: 1, + PeriodSeconds: 1, + } + + for _, option := range options { + option(&probe) + } + return &probe +} + +type healthProbeOption func(*HealthCheckProbe) + +func WithHealthCheckCommand(cmd []string) healthProbeOption { + return func(h *HealthCheckProbe) { + h.Cmd = cmd + } +} + +func WithHeathCheckTimeout(timeout int) healthProbeOption { + return func(h *HealthCheckProbe) { + h.TimeoutSeconds = timeout + } +} + // Ctr describes the options a kube yaml can be configured at container level type Ctr struct { Name string @@ -1669,8 +1718,10 @@ type Ctr struct { Env []Env EnvFrom []EnvFrom InitCtrType string + RestartPolicy string RunAsUser string RunAsGroup string + StartupProbe *HealthCheckProbe } // getCtr takes a list of ctrOptions and returns a Ctr with sane defaults @@ -1717,6 +1768,18 @@ func withInitCtr() ctrOption { } } +func withRestartableInitCtr() ctrOption { + return func(c *Ctr) { + c.RestartPolicy = string(v1.RestartPolicyAlways) + } +} + +func withStartupProbe(probe *HealthCheckProbe) ctrOption { + return func(c *Ctr) { + c.StartupProbe = probe + } +} + func withCmd(cmd []string) ctrOption { return func(c *Ctr) { c.Cmd = cmd @@ -2498,6 +2561,60 @@ var _ = Describe("Podman kube play", func() { Expect(inspect.OutputToString()).To(ContainSubstring("running")) }) + // If always restart init container didn't define define startup probe, it is started immediately and the Pod is starting regular container + It("test with always restart init container without startup probe started immediately", func() { + pod := getPod(withPodInitCtr(getCtr(withImage(CITEST_IMAGE), withCmd([]string{"top"}), withRestartableInitCtr(), withName("sidecar-container"))), withCtr(getCtr(withImage(CITEST_IMAGE), withCmd([]string{"top"})))) + err := generateKubeYaml("pod", pod, kubeYaml) + Expect(err).ToNot(HaveOccurred()) + + kube := podmanTest.Podman([]string{"kube", "play", kubeYaml}) + kube.WaitWithDefaultTimeout() + Expect(kube).Should(ExitCleanly()) + + // Expect the number of containers created to be 3, infra, restartable init container and regular container + numOfCtrs := podmanTest.NumberOfContainers() + Expect(numOfCtrs).To(Equal(3)) + + // Init container should keep running + inspect := podmanTest.Podman([]string{"inspect", "--format", "{{.State.Status}}", "testPod-" + "sidecar-container"}) + inspect.WaitWithDefaultTimeout() + Expect(inspect).Should(ExitCleanly()) + Expect(inspect.OutputToString()).To(ContainSubstring("running")) + + // Regular container should be in running state + inspect = podmanTest.Podman([]string{"inspect", "--format", "{{.State.Status}}", "testPod-" + defaultCtrName}) + inspect.WaitWithDefaultTimeout() + Expect(inspect).Should(ExitCleanly()) + Expect(inspect.OutputToString()).To(ContainSubstring("running")) + }) + + It("test with always restart init container startup probe continue Pod creation after init container started", func() { + startupProbe := getHealthCheckProbe(WithHealthCheckCommand([]string{"sleep", "'1'"}), WithHeathCheckTimeout(3)) + pod := getPod(withPodInitCtr(getCtr(withImage(CITEST_IMAGE), withCmd([]string{"top"}), withRestartableInitCtr(), withName("sidecar-container"), withStartupProbe(startupProbe))), withCtr(getCtr(withImage(CITEST_IMAGE), withCmd([]string{"top"})))) + err := generateKubeYaml("pod", pod, kubeYaml) + Expect(err).ToNot(HaveOccurred()) + + kube := podmanTest.Podman([]string{"kube", "play", kubeYaml}) + kube.WaitWithDefaultTimeout() + Expect(kube).Should(ExitCleanly()) + + // Expect the number of containers created to be 3, infra, restartable init container and regular container + numOfCtrs := podmanTest.NumberOfContainers() + Expect(numOfCtrs).To(Equal(3)) + + // Init container should keep running + inspect := podmanTest.Podman([]string{"inspect", "--format", "{{.State.Status}}", "testPod-" + "sidecar-container"}) + inspect.WaitWithDefaultTimeout() + Expect(inspect).Should(ExitCleanly()) + Expect(inspect.OutputToString()).To(ContainSubstring("running")) + + // Regular container should be in running state + inspect = podmanTest.Podman([]string{"inspect", "--format", "{{.State.Status}}", "testPod-" + defaultCtrName}) + inspect.WaitWithDefaultTimeout() + Expect(inspect).Should(ExitCleanly()) + Expect(inspect.OutputToString()).To(ContainSubstring("running")) + }) + // If you supply only args for a Container, the default Entrypoint defined in the Docker image is run with the args that you supplied. It("test correct command with only set args in yaml file", func() { pod := getPod(withCtr(getCtr(withImage(REGISTRY_IMAGE), withCmd(nil), withArg([]string{"echo", "hello"}))))