From a47fe37a97b93911b4509636888018fa18de500d Mon Sep 17 00:00:00 2001
From: Joel Smith <joelsmith@redhat.com>
Date: Tue, 3 Nov 2020 14:59:03 -0700
Subject: [PATCH] Use regex for "pod ps" name filter to match "ps" behavior

Signed-off-by: Joel Smith <joelsmith@redhat.com>
---
 libpod/filters/pods.go  |  7 ++++++-
 test/e2e/pod_ps_test.go | 22 ++++++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/libpod/filters/pods.go b/libpod/filters/pods.go
index adce9784c6..0caa941dd3 100644
--- a/libpod/filters/pods.go
+++ b/libpod/filters/pods.go
@@ -1,6 +1,7 @@
 package lpfilters
 
 import (
+	"regexp"
 	"strconv"
 	"strings"
 
@@ -78,7 +79,11 @@ func GeneratePodFilterFunc(filter, filterValue string) (
 		}, nil
 	case "name":
 		return func(p *libpod.Pod) bool {
-			return strings.Contains(p.Name(), filterValue)
+			match, err := regexp.MatchString(filterValue, p.Name())
+			if err != nil {
+				return false
+			}
+			return match
 		}, nil
 	case "status":
 		if !util.StringInSlice(filterValue, []string{"stopped", "running", "paused", "exited", "dead", "created"}) {
diff --git a/test/e2e/pod_ps_test.go b/test/e2e/pod_ps_test.go
index a299d3cf2d..5d63d59854 100644
--- a/test/e2e/pod_ps_test.go
+++ b/test/e2e/pod_ps_test.go
@@ -107,6 +107,28 @@ var _ = Describe("Podman ps", func() {
 		Expect(result.ExitCode()).To(Equal(0))
 	})
 
+	It("podman pod ps filter name regexp", func() {
+		_, ec, podid := podmanTest.CreatePod("mypod")
+		Expect(ec).To(Equal(0))
+		_, ec2, _ := podmanTest.CreatePod("mypod1")
+		Expect(ec2).To(Equal(0))
+
+		result := podmanTest.Podman([]string{"pod", "ps", "-q", "--no-trunc", "--filter", "name=mypod"})
+		result.WaitWithDefaultTimeout()
+		Expect(result.ExitCode()).To(Equal(0))
+
+		output := result.OutputToStringArray()
+		Expect(len(output)).To(Equal(2))
+
+		result = podmanTest.Podman([]string{"pod", "ps", "-q", "--no-trunc", "--filter", "name=mypod$"})
+		result.WaitWithDefaultTimeout()
+		Expect(result.ExitCode()).To(Equal(0))
+
+		output = result.OutputToStringArray()
+		Expect(len(output)).To(Equal(1))
+		Expect(output[0]).To(Equal(podid))
+	})
+
 	It("podman pod ps mutually exclusive flags", func() {
 		session := podmanTest.Podman([]string{"pod", "ps", "-q", "--format", "{{.ID}}"})
 		session.WaitWithDefaultTimeout()