Update pod bindings and Add test to validate prune pod apiv2 binding.

Modify the pod inspect bindings to hold current pod status.
Includes test to validate on pod status and added test to check
no or few pods are pruned,if the pods are in exited state.

Signed-off-by: Sujil02 <sushah@redhat.com>
This commit is contained in:
Sujil02 2020-02-24 17:07:11 -05:00
parent afd5cbff1e
commit bbda410526
3 changed files with 72 additions and 2 deletions

View File

@ -88,6 +88,7 @@ type PodInspect struct {
type PodInspectState struct {
CgroupPath string `json:"cgroupPath"`
InfraContainerID string `json:"infraContainerID"`
Status string `json:"status"`
}
// PodContainerInfo keeps information on a container in a pod

View File

@ -407,7 +407,10 @@ func (p *Pod) Status() (map[string]define.ContainerStatus, error) {
if err != nil {
return nil, err
}
return containerStatusFromContainers(allCtrs)
}
func containerStatusFromContainers(allCtrs []*Container) (map[string]define.ContainerStatus, error) {
// We need to lock all the containers
for _, ctr := range allCtrs {
ctr.lock.Lock()
@ -443,6 +446,14 @@ func (p *Pod) Inspect() (*PodInspect, error) {
if err != nil {
return &PodInspect{}, err
}
ctrStatuses, err := containerStatusFromContainers(containers)
if err != nil {
return nil, err
}
status, err := CreatePodStatusResults(ctrStatuses)
if err != nil {
return nil, err
}
for _, c := range containers {
containerStatus := "unknown"
// Ignoring possible errors here because we don't want this to be
@ -468,6 +479,7 @@ func (p *Pod) Inspect() (*PodInspect, error) {
State: &PodInspectState{
CgroupPath: p.state.CgroupPath,
InfraContainerID: infraContainerID,
Status: status,
},
Containers: podContainers,
}

View File

@ -13,7 +13,7 @@ import (
"github.com/onsi/gomega/gexec"
)
var _ = Describe("Podman images", func() {
var _ = Describe("Podman pods", func() {
var (
bt *bindingTest
s *gexec.Session
@ -120,6 +120,7 @@ var _ = Describe("Podman images", func() {
err = pods.Pause(connText, newpod)
Expect(err).To(BeNil())
response, err = pods.Inspect(connText, newpod)
Expect(response.State.Status).To(Equal(define.PodStatePaused))
for _, i := range response.Containers {
Expect(define.StringToContainerStatus(i.State)).
To(Equal(define.ContainerStatePaused))
@ -129,6 +130,7 @@ var _ = Describe("Podman images", func() {
err = pods.Unpause(connText, newpod)
Expect(err).To(BeNil())
response, err = pods.Inspect(connText, newpod)
Expect(response.State.Status).To(Equal(define.PodStateRunning))
for _, i := range response.Containers {
Expect(define.StringToContainerStatus(i.State)).
To(Equal(define.ContainerStateRunning))
@ -159,6 +161,7 @@ var _ = Describe("Podman images", func() {
Expect(err).To(BeNil())
response, err := pods.Inspect(connText, newpod)
Expect(response.State.Status).To(Equal(define.PodStateRunning))
for _, i := range response.Containers {
Expect(define.StringToContainerStatus(i.State)).
To(Equal(define.ContainerStateRunning))
@ -172,6 +175,7 @@ var _ = Describe("Podman images", func() {
err = pods.Stop(connText, newpod, nil)
Expect(err).To(BeNil())
response, _ = pods.Inspect(connText, newpod)
Expect(response.State.Status).To(Equal(define.PodStateExited))
for _, i := range response.Containers {
Expect(define.StringToContainerStatus(i.State)).
To(Equal(define.ContainerStateStopped))
@ -184,13 +188,66 @@ var _ = Describe("Podman images", func() {
err = pods.Restart(connText, newpod)
Expect(err).To(BeNil())
response, _ = pods.Inspect(connText, newpod)
Expect(response.State.Status).To(Equal(define.PodStateRunning))
for _, i := range response.Containers {
Expect(define.StringToContainerStatus(i.State)).
To(Equal(define.ContainerStateRunning))
}
})
// Remove all stopped pods and their container to be implemented.
// Test to validate all the pods in the stopped/exited state are pruned sucessfully.
It("prune pod", func() {
// Add a new pod
var newpod2 string = "newpod2"
bt.Podcreate(&newpod2)
// No pods pruned since no pod in exited state
err = pods.Prune(connText)
Expect(err).To(BeNil())
podSummary, err := pods.List(connText, nil)
Expect(err).To(BeNil())
Expect(len(podSummary)).To(Equal(2))
// Prune only one pod which is in exited state.
// Start then stop a pod.
// pod moves to exited state one pod should be pruned now.
err = pods.Start(connText, newpod)
Expect(err).To(BeNil())
err = pods.Stop(connText, newpod, nil)
Expect(err).To(BeNil())
response, err := pods.Inspect(connText, newpod)
Expect(response.State.Status).To(Equal(define.PodStateExited))
err = pods.Prune(connText)
Expect(err).To(BeNil())
podSummary, err = pods.List(connText, nil)
Expect(err).To(BeNil())
Expect(len(podSummary)).To(Equal(1))
// Test prune all pods in exited state.
bt.Podcreate(&newpod)
err = pods.Start(connText, newpod)
Expect(err).To(BeNil())
err = pods.Start(connText, newpod2)
Expect(err).To(BeNil())
err = pods.Stop(connText, newpod, nil)
Expect(err).To(BeNil())
response, err = pods.Inspect(connText, newpod)
Expect(response.State.Status).To(Equal(define.PodStateExited))
for _, i := range response.Containers {
Expect(define.StringToContainerStatus(i.State)).
To(Equal(define.ContainerStateStopped))
}
err = pods.Stop(connText, newpod2, nil)
Expect(err).To(BeNil())
response, err = pods.Inspect(connText, newpod2)
Expect(response.State.Status).To(Equal(define.PodStateExited))
for _, i := range response.Containers {
Expect(define.StringToContainerStatus(i.State)).
To(Equal(define.ContainerStateStopped))
}
err = pods.Prune(connText)
Expect(err).To(BeNil())
podSummary, err = pods.List(connText, nil)
Expect(err).To(BeNil())
Expect(len(podSummary)).To(Equal(0))
})
})