From 5f29c7bf989110acb25031a61b6839b4397389b9 Mon Sep 17 00:00:00 2001 From: Gunjan Vyas Date: Thu, 18 May 2023 17:30:01 +0530 Subject: [PATCH] fix: podman event --filter volume=vol-name should compare the event name with volume name Fixes: https://github.com/containers/podman/issues/18618 Signed-off-by: Gunjan Vyas --- libpod/events/filters.go | 3 ++- test/e2e/common_test.go | 13 +++++++++++++ test/e2e/events_test.go | 20 ++++++++++++++++++++ test/system/090-events.bats | 15 +++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/libpod/events/filters.go b/libpod/events/filters.go index 9057c9b79c..6d08dc4ed3 100644 --- a/libpod/events/filters.go +++ b/libpod/events/filters.go @@ -52,7 +52,8 @@ func generateEventFilter(filter, filterValue string) (func(e *Event) bool, error if e.Type != Volume { return false } - return strings.HasPrefix(e.ID, filterValue) + // Prefix match with name for consistency with docker + return strings.HasPrefix(e.Name, filterValue) }, nil case "TYPE": return func(e *Event) bool { diff --git a/test/e2e/common_test.go b/test/e2e/common_test.go index 483f68e120..cf11c4eebf 100644 --- a/test/e2e/common_test.go +++ b/test/e2e/common_test.go @@ -718,6 +718,19 @@ func (p *PodmanTestIntegration) CreatePod(options map[string][]string) (*PodmanS return session, session.ExitCode(), session.OutputToString() } +func (p *PodmanTestIntegration) CreateVolume(options map[string][]string) (*PodmanSessionIntegration, int, string) { + var args = []string{"volume", "create"} + for k, values := range options { + for _, v := range values { + args = append(args, k+"="+v) + } + } + + session := p.Podman(args) + session.WaitWithDefaultTimeout() + return session, session.ExitCode(), session.OutputToString() +} + func (p *PodmanTestIntegration) RunTopContainerInPod(name, pod string) *PodmanSessionIntegration { return p.RunTopContainerWithArgs(name, []string{"--pod", pod}) } diff --git a/test/e2e/events_test.go b/test/e2e/events_test.go index 61e8fc14c2..31ab1872e3 100644 --- a/test/e2e/events_test.go +++ b/test/e2e/events_test.go @@ -38,6 +38,26 @@ var _ = Describe("Podman events", func() { date := time.Now().Format("2006-01-02") Expect(result.OutputToStringArray()).To(ContainElement(HavePrefix(date)), "event log has correct timestamp") }) + It("podman events with a volume filter", func() { + _, ec, vname := podmanTest.CreateVolume(nil) + Expect(ec).To(Equal(0)) + + // Run two event commands - one with the full volume name and the second with the prefix + result := podmanTest.Podman([]string{"events", "--stream=false", "--filter", fmt.Sprintf("volume=%s", vname)}) + resultPrefix := podmanTest.Podman([]string{"events", "--stream=false", "--filter", fmt.Sprintf("volume=%s", vname[:5])}) + + result.WaitWithDefaultTimeout() + Expect(result).Should(Exit(0)) + events := result.OutputToStringArray() + Expect(events).To(HaveLen(1), "number of events") + Expect(events[0]).To(ContainSubstring(vname), "event log includes volume name") + + resultPrefix.WaitWithDefaultTimeout() + Expect(resultPrefix).Should(Exit(0)) + events = resultPrefix.OutputToStringArray() + Expect(events).To(HaveLen(1), "number of events") + Expect(events[0]).To(ContainSubstring(vname), "event log includes volume name") + }) It("podman events with an event filter and container=cid", func() { _, ec, cid := podmanTest.RunLsContainer("") diff --git a/test/system/090-events.bats b/test/system/090-events.bats index 4dc3434b51..7577d2d54f 100644 --- a/test/system/090-events.bats +++ b/test/system/090-events.bats @@ -382,3 +382,18 @@ EOF --stream=false is "${lines[0]}" ".* container died .* (image=$IMAGE, name=$cname, .*)" } + +@test "events - volume events" { + local vname=v$(random_string 10) + run_podman volume create $vname + run_podman volume rm $vname + + run_podman events --since=1m --stream=false --filter volume=$vname + notrunc_results="$output" + assert "${lines[0]}" =~ ".* volume create $vname" + assert "${lines[1]}" =~ ".* volume remove $vname" + + # Prefix test + run_podman events --since=1m --stream=false --filter volume=${vname:0:5} + assert "$output" = "$notrunc_results" +}