Skip to content

Commit

Permalink
sidecar-20372 Add e2e test
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Deng <ywdeng@tw.ibm.com>
  • Loading branch information
vincentywdeng committed Dec 3, 2023
1 parent 210cd0e commit f827d95
Showing 1 changed file with 117 additions and 0 deletions.
117 changes: 117 additions & 0 deletions test/e2e/play_kube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"}))))
Expand Down

0 comments on commit f827d95

Please sign in to comment.