Skip to content

Commit

Permalink
Merge pull request #19911 from xtexChooser/xtex/impl-container-label-not
Browse files Browse the repository at this point in the history
Add label! filter to container filters
  • Loading branch information
openshift-merge-robot authored Sep 17, 2023
2 parents 3e4e39d + 98e6dda commit 5dc4370
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/source/markdown/podman-ps.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Valid filters are listed below:
| id | [ID] Container's ID (CID prefix match by default; accepts regex) |
| name | [Name] Container's name (accepts regex) |
| label | [Key] or [Key=Value] Label assigned to a container |
| label! | [Key] or [Key=Value] Label NOT assigned to a container |
| exited | [Int] Container's exit code |
| status | [Status] Container's status: 'created', 'exited', 'paused', 'running', 'unknown' |
| ancestor | [ImageName] Image or descendant used to create container (accepts regex) |
Expand Down
4 changes: 4 additions & 0 deletions pkg/domain/filters/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func GenerateContainerFilterFuncs(filter string, filterValues []string, r *libpo
return func(c *libpod.Container) bool {
return filters.MatchLabelFilters(filterValues, c.Labels())
}, nil
case "label!":
return func(c *libpod.Container) bool {
return !filters.MatchLabelFilters(filterValues, c.Labels())
}, nil
case "name":
// we only have to match one name
return func(c *libpod.Container) bool {
Expand Down
5 changes: 5 additions & 0 deletions pkg/domain/filters/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ func GeneratePodFilterFunc(filter string, filterValues []string, r *libpod.Runti
labels := p.Labels()
return filters.MatchLabelFilters(filterValues, labels)
}, nil
case "label!":
return func(p *libpod.Pod) bool {
labels := p.Labels()
return !filters.MatchLabelFilters(filterValues, labels)
}, nil
case "until":
return func(p *libpod.Pod) bool {
until, err := filters.ComputeUntilTimestamp(filterValues)
Expand Down
13 changes: 12 additions & 1 deletion test/apiv2/20-containers.at
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,9 @@ t GET containers/json 200 \
podman stop bar

#compat api list containers sanity checks
podman run -d --rm --name labelcontainer_with $ENV_WORKDIR_IMG top
podman run -d --rm --name labelcontainer_without $IMAGE top

t GET containers/json?filters='garb1age}' 500 \
.cause="invalid character 'g' looking for beginning of value"
t GET containers/json?filters='{"label":["testl' 500 \
Expand Down Expand Up @@ -436,7 +439,15 @@ t POST containers/prune?filters='{"label":["tes' 500 \
t POST libpod/containers/prune?filters='{"label":["tes' 500 \
.cause="unexpected end of JSON input"

t GET libpod/containers/json?filters='{"label":["testlabel"]}' 200 length=0
t GET libpod/containers/json?filters='{"label":["created_by"]}' 200 \
length=1 \
.[0].Names[0]="labelcontainer_with"
t GET libpod/containers/json?filters='{"label!":["created_by"]}' 200 \
length=1 \
.[0].Names[0]="labelcontainer_without"
t GET libpod/containers/json?filters='{"label!":["testlabel"]}' 200 length=2

podman stop -t0 labelcontainer_with labelcontainer_without

# libpod api: do not use list filters for prune
t POST libpod/containers/prune?filters='{"name":["anyname"]}' 500 \
Expand Down
24 changes: 22 additions & 2 deletions test/apiv2/python/rest_api/test_v2_0_0_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,30 @@ def test_list(self):
obj = r.json()
self.assertEqual(len(obj), 1)

def test_list_filters(self):
def test_list_filters_status(self):
r = requests.get(
self.podman_url
+ "/v1.40/containers/json?filters%3D%7B%22status%22%3A%5B%22running%22%5D%7D"
+ "/v1.40/containers/json?filters=%7B%22status%22%3A%5B%22running%22%5D%7D"
)
self.assertEqual(r.status_code, 200, r.text)
payload = r.json()
containerAmnt = len(payload)
self.assertGreater(containerAmnt, 0)

def test_list_filters_label(self):
r = requests.get(
self.podman_url
+ "/v1.40/containers/json?filters=%7B%22label%22%3A%5B%22nonexistlabel%22%5D%7D"
)
self.assertEqual(r.status_code, 200, r.text)
payload = r.json()
containerAmnt = len(payload)
self.assertEqual(containerAmnt, 0)

def test_list_filters_label_not(self):
r = requests.get(
self.podman_url
+ "/v1.40/containers/json?filters=%7B%22label%21%22%3A%5B%22nonexistlabel%22%5D%7D"
)
self.assertEqual(r.status_code, 200, r.text)
payload = r.json()
Expand Down

0 comments on commit 5dc4370

Please sign in to comment.