From 0ff7f6b1a9a48483f547102754be3cd0bdc0d8bf Mon Sep 17 00:00:00 2001 From: Urvashi Mohnani Date: Mon, 15 Jan 2024 09:20:41 -0500 Subject: [PATCH] Fix image filters parsing Fix the image filter parsing in the common libraries to follow an AND logic for all filters passed in ensuring compatibility with Docker behavior. Also fix the filter parsing on the tunnel side so that we grab all the filters given by the user and not only the last filter in the list. Add tests for the fixes. Signed-off-by: Urvashi Mohnani --- pkg/domain/infra/tunnel/images.go | 4 +-- test/e2e/images_test.go | 49 +++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/pkg/domain/infra/tunnel/images.go b/pkg/domain/infra/tunnel/images.go index 9851259790..c0468c9e07 100644 --- a/pkg/domain/infra/tunnel/images.go +++ b/pkg/domain/infra/tunnel/images.go @@ -37,8 +37,8 @@ func (ir *ImageEngine) Remove(ctx context.Context, imagesArg []string, opts enti func (ir *ImageEngine) List(ctx context.Context, opts entities.ImageListOptions) ([]*entities.ImageSummary, error) { filters := make(map[string][]string, len(opts.Filter)) for _, filter := range opts.Filter { - f := strings.Split(filter, "=") - filters[f[0]] = f[1:] + f := strings.SplitN(filter, "=", 2) + filters[f[0]] = append(filters[f[0]], f[1]) } options := new(images.ListOptions).WithAll(opts.All).WithFilters(filters) psImages, err := images.List(ir.ClientCtx, options) diff --git a/test/e2e/images_test.go b/test/e2e/images_test.go index 9813062645..7bb08fdf0f 100644 --- a/test/e2e/images_test.go +++ b/test/e2e/images_test.go @@ -496,4 +496,53 @@ RUN > file2 }) + It("podman images filter should be AND logic", func() { + dockerfile := `FROM quay.io/libpod/alpine:latest +LABEL abc="" +LABEL xyz="" +` + podmanTest.BuildImage(dockerfile, "test-abc-xyz", "true") + + dockerfile2 := `FROM quay.io/libpod/alpine:latest +LABEL xyz="bar" +` + podmanTest.BuildImage(dockerfile2, "test-xyz", "true") + + session := podmanTest.Podman([]string{"images", "-f", "label=xyz"}) + session.WaitWithDefaultTimeout() + Expect(session).Should(ExitCleanly()) + Expect(session.OutputToStringArray()).To(HaveLen(3)) + Expect(session.OutputToString()).To(ContainSubstring("test-abc-xyz")) + Expect(session.OutputToString()).To(ContainSubstring("test-xyz")) + + session = podmanTest.Podman([]string{"images", "-f", "label=xyz=bar"}) + session.WaitWithDefaultTimeout() + Expect(session).Should(ExitCleanly()) + Expect(session.OutputToStringArray()).To(HaveLen(2)) + Expect(session.OutputToString()).To(ContainSubstring("test-xyz")) + + session = podmanTest.Podman([]string{"images", "-f", "label=abc"}) + session.WaitWithDefaultTimeout() + Expect(session).Should(ExitCleanly()) + Expect(session.OutputToStringArray()).To(HaveLen(2)) + Expect(session.OutputToString()).To(ContainSubstring("test-abc-xyz")) + + session = podmanTest.Podman([]string{"images", "-f", "label=abc", "-f", "label=xyz"}) + session.WaitWithDefaultTimeout() + Expect(session).Should(ExitCleanly()) + Expect(session.OutputToStringArray()).To(HaveLen(2)) + Expect(session.OutputToString()).To(ContainSubstring("test-abc-xyz")) + + session = podmanTest.Podman([]string{"images", "-f", "label=xyz=bar", "-f", "label=abc"}) + session.WaitWithDefaultTimeout() + Expect(session).Should(ExitCleanly()) + Expect(session.OutputToStringArray()).To(HaveLen(1)) + + session = podmanTest.Podman([]string{"images", "-f", "label=xyz", "-f", "reference=test-abc-xyz"}) + session.WaitWithDefaultTimeout() + Expect(session).Should(ExitCleanly()) + Expect(session.OutputToStringArray()).To(HaveLen(2)) + Expect(session.OutputToString()).To(ContainSubstring("test-abc-xyz")) + }) + })