Files
podman/pkg/bindings/test/system_test.go
Valentin Rothberg d856e87f40 events endpoint: fix panic and race condition
Fix a potential panic in the events endpoint when parsing the filters
parameter.  Values of the filters map might be empty, so we need to
account for that instead of uncondtitionally accessing the first item.

Also apply a similar for race conditions as done in commit f4a2d25c0f:

	Fix a race that could cause read errors to be masked.  Masking
	such errors is likely to report red herrings since users don't
	see that reading failed for some reasons but that a given event
	could not be found.

Another race was the handler closing event channel, which could lead to
two kinds of panics: double close, send to close channel.  The backend
takes care of that.  However, make sure that the backend stops working
in case the context has been cancelled.

Fixes: #6899
Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
2020-07-20 11:37:05 +02:00

160 lines
5.1 KiB
Go

package test_bindings
import (
"sync"
"time"
"github.com/containers/libpod/v2/pkg/bindings"
"github.com/containers/libpod/v2/pkg/bindings/containers"
"github.com/containers/libpod/v2/pkg/bindings/pods"
"github.com/containers/libpod/v2/pkg/bindings/system"
"github.com/containers/libpod/v2/pkg/bindings/volumes"
"github.com/containers/libpod/v2/pkg/domain/entities"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
)
var _ = Describe("Podman system", func() {
var (
bt *bindingTest
s *gexec.Session
newpod string
)
BeforeEach(func() {
bt = newBindingTest()
bt.RestoreImagesFromCache()
newpod = "newpod"
bt.Podcreate(&newpod)
s = bt.startAPIService()
time.Sleep(1 * time.Second)
err := bt.NewConnection()
Expect(err).To(BeNil())
})
AfterEach(func() {
s.Kill()
bt.cleanup()
})
It("podman events", func() {
var name = "top"
_, err := bt.RunTopContainer(&name, bindings.PFalse, nil)
Expect(err).To(BeNil())
filters := make(map[string][]string)
filters["container"] = []string{name}
binChan := make(chan entities.Event)
done := sync.Mutex{}
done.Lock()
eventCounter := 0
go func() {
defer done.Unlock()
for range binChan {
eventCounter++
}
}()
err = system.Events(bt.conn, binChan, nil, nil, nil, filters, bindings.PFalse)
Expect(err).To(BeNil())
done.Lock()
Expect(eventCounter).To(BeNumerically(">", 0))
})
It("podman system prune - pod,container stopped", func() {
// Start and stop a pod to enter in exited state.
_, err := pods.Start(bt.conn, newpod)
Expect(err).To(BeNil())
_, err = pods.Stop(bt.conn, newpod, nil)
Expect(err).To(BeNil())
// Start and stop a container to enter in exited state.
var name = "top"
_, err = bt.RunTopContainer(&name, bindings.PFalse, nil)
Expect(err).To(BeNil())
err = containers.Stop(bt.conn, name, nil)
Expect(err).To(BeNil())
systemPruneResponse, err := system.Prune(bt.conn, bindings.PTrue, bindings.PFalse)
Expect(err).To(BeNil())
Expect(len(systemPruneResponse.PodPruneReport)).To(Equal(1))
Expect(len(systemPruneResponse.ContainerPruneReport.ID)).To(Equal(1))
Expect(len(systemPruneResponse.ImagePruneReport.Report.Id)).
To(BeNumerically(">", 0))
Expect(systemPruneResponse.ImagePruneReport.Report.Id).
To(ContainElement("docker.io/library/alpine:latest"))
Expect(len(systemPruneResponse.VolumePruneReport)).To(Equal(0))
})
It("podman system prune running alpine container", func() {
// Start and stop a pod to enter in exited state.
_, err := pods.Start(bt.conn, newpod)
Expect(err).To(BeNil())
_, err = pods.Stop(bt.conn, newpod, nil)
Expect(err).To(BeNil())
// Start and stop a container to enter in exited state.
var name = "top"
_, err = bt.RunTopContainer(&name, bindings.PFalse, nil)
Expect(err).To(BeNil())
err = containers.Stop(bt.conn, name, nil)
Expect(err).To(BeNil())
// Start container and leave in running
var name2 = "top2"
_, err = bt.RunTopContainer(&name2, bindings.PFalse, nil)
Expect(err).To(BeNil())
// Adding an unused volume
_, err = volumes.Create(bt.conn, entities.VolumeCreateOptions{})
Expect(err).To(BeNil())
systemPruneResponse, err := system.Prune(bt.conn, bindings.PTrue, bindings.PFalse)
Expect(err).To(BeNil())
Expect(len(systemPruneResponse.PodPruneReport)).To(Equal(1))
Expect(len(systemPruneResponse.ContainerPruneReport.ID)).To(Equal(1))
Expect(len(systemPruneResponse.ImagePruneReport.Report.Id)).
To(BeNumerically(">", 0))
// Alpine image should not be pruned as used by running container
Expect(systemPruneResponse.ImagePruneReport.Report.Id).
ToNot(ContainElement("docker.io/library/alpine:latest"))
// Though unsed volume is available it should not be pruned as flag set to false.
Expect(len(systemPruneResponse.VolumePruneReport)).To(Equal(0))
})
It("podman system prune running alpine container volume prune", func() {
// Start a pod and leave it running
_, err := pods.Start(bt.conn, newpod)
Expect(err).To(BeNil())
// Start and stop a container to enter in exited state.
var name = "top"
_, err = bt.RunTopContainer(&name, bindings.PFalse, nil)
Expect(err).To(BeNil())
err = containers.Stop(bt.conn, name, nil)
Expect(err).To(BeNil())
// Start second container and leave in running
var name2 = "top2"
_, err = bt.RunTopContainer(&name2, bindings.PFalse, nil)
Expect(err).To(BeNil())
// Adding an unused volume should work
_, err = volumes.Create(bt.conn, entities.VolumeCreateOptions{})
Expect(err).To(BeNil())
systemPruneResponse, err := system.Prune(bt.conn, bindings.PTrue, bindings.PTrue)
Expect(err).To(BeNil())
Expect(len(systemPruneResponse.PodPruneReport)).To(Equal(0))
Expect(len(systemPruneResponse.ContainerPruneReport.ID)).To(Equal(1))
Expect(len(systemPruneResponse.ImagePruneReport.Report.Id)).
To(BeNumerically(">", 0))
// Alpine image should not be pruned as used by running container
Expect(systemPruneResponse.ImagePruneReport.Report.Id).
ToNot(ContainElement("docker.io/library/alpine:latest"))
// Volume should be pruned now as flag set true
Expect(len(systemPruneResponse.VolumePruneReport)).To(Equal(1))
})
})