Skip to content

Commit 0389651

Browse files
Merge pull request #26704 from shiavm006/fix-ancestor-filter-docker-compatibility
Fix ancestor filter to support Docker-compatible substring matching
2 parents 7a3d99c + b5d18e8 commit 0389651

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

pkg/domain/filters/containers.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,13 @@ func GenerateContainerFilterFuncs(filter string, filterValues []string, r *libpo
107107
imageTag = tag
108108
}
109109

110-
if (rootfsImageID == filterValue) ||
111-
util.StringMatchRegexSlice(rootfsImageName, filterValues) ||
110+
// Check for substring match on image ID (Docker compatibility)
111+
if strings.Contains(rootfsImageID, filterValue) {
112+
return true
113+
}
114+
115+
// Check for regex match (advanced use cases)
116+
if util.StringMatchRegexSlice(rootfsImageName, filterValues) ||
112117
(util.StringMatchRegexSlice(imageNameWithoutTag, filterValues) && imageTag == "latest") {
113118
return true
114119
}
@@ -363,8 +368,13 @@ func GenerateExternalContainerFilterFuncs(filter string, filterValues []string,
363368
imageTag = tag
364369
}
365370

366-
if (listContainer.ImageID == filterValue) ||
367-
util.StringMatchRegexSlice(listContainer.Image, filterValues) ||
371+
// Check for substring match on image ID (Docker compatibility)
372+
if strings.Contains(listContainer.ImageID, filterValue) {
373+
return true
374+
}
375+
376+
// Check for regex match (advanced use cases)
377+
if util.StringMatchRegexSlice(listContainer.Image, filterValues) ||
368378
(util.StringMatchRegexSlice(imageNameWithoutTag, filterValues) && imageTag == "latest") {
369379
return true
370380
}

test/e2e/ps_test.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -334,25 +334,38 @@ var _ = Describe("Podman ps", func() {
334334
Expect(result.OutputToString()).To(Equal(""))
335335
})
336336

337-
It("podman ps id filter flag", func() {
338-
_, ec, fullCid := podmanTest.RunLsContainer("")
337+
It("podman ps ancestor filter with substring matching (Docker compatibility)", func() {
338+
// Create a container to test with
339+
_, ec, cid := podmanTest.RunLsContainer("test1")
339340
Expect(ec).To(Equal(0))
340341

341-
result := podmanTest.Podman([]string{"ps", "-a", "--filter", fmt.Sprintf("id=%s", fullCid)})
342-
result.WaitWithDefaultTimeout()
343-
Expect(result).Should(ExitCleanly())
342+
// Get the full image ID to test substring matching
343+
inspect := podmanTest.PodmanExitCleanly("inspect", cid, "--format", "{{.Image}}")
344+
fullImageID := inspect.OutputToString()
345+
346+
// Test with prefix substring of image ID (Docker compatibility, new functionality)
347+
imageIDPrefix := fullImageID[:12]
348+
result := podmanTest.PodmanExitCleanly("ps", "-q", "--no-trunc", "-a", "--filter", "ancestor="+imageIDPrefix)
349+
Expect(result.OutputToString()).To(Equal(cid))
350+
351+
// Test with non-prefix substring of image ID (Docker compatibility)
352+
imageIDSubstr := fullImageID[4:16]
353+
result = podmanTest.PodmanExitCleanly("ps", "-q", "--no-trunc", "-a", "--filter", "ancestor="+imageIDSubstr)
354+
Expect(result.OutputToString()).To(Equal(cid))
355+
356+
// Test with non-existent substring (should not match)
357+
result = podmanTest.PodmanExitCleanly("ps", "-q", "--no-trunc", "-a", "--filter", "ancestor=nonexistent")
358+
Expect(result.OutputToString()).To(Equal(""))
344359
})
345360

346361
It("podman ps id filter flag", func() {
347-
session := podmanTest.RunTopContainer("")
348-
session.WaitWithDefaultTimeout()
349-
Expect(session).Should(ExitCleanly())
350-
fullCid := session.OutputToString()
362+
_, ec, fullCid := podmanTest.RunLsContainer("")
363+
Expect(ec).To(Equal(0))
351364

352-
result := podmanTest.Podman([]string{"ps", "-aq", "--no-trunc", "--filter", "status=running"})
365+
result := podmanTest.Podman([]string{"ps", "-aq", "--no-trunc", "--filter", fmt.Sprintf("id=%s", fullCid)})
353366
result.WaitWithDefaultTimeout()
354367
Expect(result).Should(ExitCleanly())
355-
Expect(result.OutputToStringArray()[0]).To(Equal(fullCid))
368+
Expect(result.OutputToString()).To(Equal(fullCid))
356369
})
357370

358371
It("podman ps multiple filters", func() {
@@ -1033,4 +1046,5 @@ var _ = Describe("Podman ps", func() {
10331046
Expect(output).To(HaveLen(1))
10341047
Expect(output).Should(ContainElement(ContainSubstring("late")))
10351048
})
1049+
10361050
})

0 commit comments

Comments
 (0)