mirror of
https://github.com/containers/podman.git
synced 2025-05-19 16:18:51 +08:00
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:
@ -50,6 +50,7 @@ Valid filters are listed below:
|
|||||||
| id | [ID] Container's ID (CID prefix match by default; accepts regex) |
|
| id | [ID] Container's ID (CID prefix match by default; accepts regex) |
|
||||||
| name | [Name] Container's name (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 assigned to a container |
|
||||||
|
| label! | [Key] or [Key=Value] Label NOT assigned to a container |
|
||||||
| exited | [Int] Container's exit code |
|
| exited | [Int] Container's exit code |
|
||||||
| status | [Status] Container's status: 'created', 'exited', 'paused', 'running', 'unknown' |
|
| status | [Status] Container's status: 'created', 'exited', 'paused', 'running', 'unknown' |
|
||||||
| ancestor | [ImageName] Image or descendant used to create container (accepts regex) |
|
| ancestor | [ImageName] Image or descendant used to create container (accepts regex) |
|
||||||
|
@ -25,6 +25,10 @@ func GenerateContainerFilterFuncs(filter string, filterValues []string, r *libpo
|
|||||||
return func(c *libpod.Container) bool {
|
return func(c *libpod.Container) bool {
|
||||||
return filters.MatchLabelFilters(filterValues, c.Labels())
|
return filters.MatchLabelFilters(filterValues, c.Labels())
|
||||||
}, nil
|
}, nil
|
||||||
|
case "label!":
|
||||||
|
return func(c *libpod.Container) bool {
|
||||||
|
return !filters.MatchLabelFilters(filterValues, c.Labels())
|
||||||
|
}, nil
|
||||||
case "name":
|
case "name":
|
||||||
// we only have to match one name
|
// we only have to match one name
|
||||||
return func(c *libpod.Container) bool {
|
return func(c *libpod.Container) bool {
|
||||||
|
@ -119,6 +119,11 @@ func GeneratePodFilterFunc(filter string, filterValues []string, r *libpod.Runti
|
|||||||
labels := p.Labels()
|
labels := p.Labels()
|
||||||
return filters.MatchLabelFilters(filterValues, labels)
|
return filters.MatchLabelFilters(filterValues, labels)
|
||||||
}, nil
|
}, nil
|
||||||
|
case "label!":
|
||||||
|
return func(p *libpod.Pod) bool {
|
||||||
|
labels := p.Labels()
|
||||||
|
return !filters.MatchLabelFilters(filterValues, labels)
|
||||||
|
}, nil
|
||||||
case "until":
|
case "until":
|
||||||
return func(p *libpod.Pod) bool {
|
return func(p *libpod.Pod) bool {
|
||||||
until, err := filters.ComputeUntilTimestamp(filterValues)
|
until, err := filters.ComputeUntilTimestamp(filterValues)
|
||||||
|
@ -409,6 +409,9 @@ t GET containers/json 200 \
|
|||||||
podman stop bar
|
podman stop bar
|
||||||
|
|
||||||
#compat api list containers sanity checks
|
#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 \
|
t GET containers/json?filters='garb1age}' 500 \
|
||||||
.cause="invalid character 'g' looking for beginning of value"
|
.cause="invalid character 'g' looking for beginning of value"
|
||||||
t GET containers/json?filters='{"label":["testl' 500 \
|
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 \
|
t POST libpod/containers/prune?filters='{"label":["tes' 500 \
|
||||||
.cause="unexpected end of JSON input"
|
.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
|
# libpod api: do not use list filters for prune
|
||||||
t POST libpod/containers/prune?filters='{"name":["anyname"]}' 500 \
|
t POST libpod/containers/prune?filters='{"name":["anyname"]}' 500 \
|
||||||
|
@ -22,10 +22,30 @@ class ContainerTestCase(APITestCase):
|
|||||||
obj = r.json()
|
obj = r.json()
|
||||||
self.assertEqual(len(obj), 1)
|
self.assertEqual(len(obj), 1)
|
||||||
|
|
||||||
def test_list_filters(self):
|
def test_list_filters_status(self):
|
||||||
r = requests.get(
|
r = requests.get(
|
||||||
self.podman_url
|
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)
|
self.assertEqual(r.status_code, 200, r.text)
|
||||||
payload = r.json()
|
payload = r.json()
|
||||||
|
Reference in New Issue
Block a user