Add label! filter and tests to containers and pods

Thanks to Ed Santiago <santiago@redhat.com> for tests.

Signed-off-by: xtex <xtexchooser@duck.com>
This commit is contained in:
xtex
2023-09-11 19:53:55 +08:00
parent 51d01dd38e
commit 98e6dda697
5 changed files with 44 additions and 3 deletions

View File

@ -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) |

View File

@ -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 {

View File

@ -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)

View File

@ -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 \
@ -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 \

View File

@ -22,10 +22,30 @@ class ContainerTestCase(APITestCase):
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()