Skip to content

Commit 56f31a3

Browse files
committed
Add tests for filtering in new and deprecated ways
1 parent 553b7b6 commit 56f31a3

File tree

6 files changed

+137
-22
lines changed

6 files changed

+137
-22
lines changed

tests/python_on_whales/components/test_container.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ def test_context_manager_with_create(ctr_client: DockerClient):
585585

586586

587587
@pytest.mark.parametrize("ctr_client", ["docker", "podman"], indirect=True)
588-
def test_filters(ctr_client: DockerClient):
588+
def test_list_filters(ctr_client: DockerClient):
589589
random_label_value = random_name()
590590

591591
containers_with_labels = []
@@ -614,7 +614,7 @@ def test_filters(ctr_client: DockerClient):
614614
)
615615

616616
expected_containers_with_labels = ctr_client.container.list(
617-
filters=dict(label=f"dodo={random_label_value}")
617+
filters=[("label", f"dodo={random_label_value}")]
618618
)
619619

620620
assert set(expected_containers_with_labels) == set(containers_with_labels)
@@ -623,6 +623,50 @@ def test_filters(ctr_client: DockerClient):
623623
container.kill()
624624

625625

626+
def test_list_filters_old_signature(docker_client: DockerClient):
627+
"""Test backwards compatibility of DockerClient.container.list()."""
628+
random_label_value = random_name()
629+
630+
containers_with_labels = []
631+
for _ in range(3):
632+
containers_with_labels.append(
633+
docker_client.run(
634+
"busybox",
635+
["sleep", "infinity"],
636+
remove=True,
637+
detach=True,
638+
labels=dict(dodo=random_label_value),
639+
)
640+
)
641+
642+
containers_with_wrong_labels = []
643+
for _ in range(3):
644+
containers_with_wrong_labels.append(
645+
docker_client.run(
646+
"busybox",
647+
["sleep", "infinity"],
648+
remove=True,
649+
detach=True,
650+
labels=dict(dodo="something"),
651+
)
652+
)
653+
654+
expected_warning = (
655+
r"Passing filters as a mapping is deprecated, replace with an iterable "
656+
r"of tuples instead, as so:\n"
657+
r"filters=\[\(.*\)\]"
658+
)
659+
with pytest.warns(DeprecationWarning, match=expected_warning):
660+
expected_containers_with_labels = docker_client.container.list(
661+
filters=dict(label=f"dodo={random_label_value}")
662+
)
663+
664+
assert set(expected_containers_with_labels) == set(containers_with_labels)
665+
666+
for container in containers_with_labels + containers_with_wrong_labels:
667+
container.kill()
668+
669+
626670
@pytest.mark.parametrize("ctr_client", ["docker", "podman"], indirect=True)
627671
def test_wait_single_container(ctr_client: DockerClient):
628672
cont_1 = ctr_client.run("busybox", ["sh", "-c", "sleep 2 && exit 8"], detach=True)

tests/python_on_whales/components/test_image.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -110,32 +110,30 @@ def test_save_iterator_bytes_and_load_from_iterator_list_of_images(
110110
pytest.param(
111111
"podman",
112112
marks=pytest.mark.xfail(
113-
reason="podman lists images with registry in image name"
113+
reason="podman includes all repo tags matching images ref"
114114
),
115115
),
116116
],
117117
indirect=True,
118118
)
119-
def test_filter_when_listing(ctr_client: DockerClient):
119+
def test_list_filters(ctr_client: DockerClient):
120120
ctr_client.pull(["hello-world", "busybox"])
121-
images_listed = ctr_client.image.list(filters=dict(reference="hello-world"))
121+
images_listed = ctr_client.image.list(filters=[("reference", "hello-world")])
122122
tags = {tag.split("/")[-1] for image in images_listed for tag in image.repo_tags}
123123
assert tags == {"hello-world:latest"}
124124

125125

126-
def test_filter_when_listing_old_signature(docker_client: DockerClient):
126+
def test_list_filters_old_signature(docker_client: DockerClient):
127127
"""Check backward compatibility of the DockerClient.image.list() API."""
128128
docker_client.pull(["hello-world", "busybox"])
129-
with pytest.warns(DeprecationWarning) as warnings_emmitted:
130-
images_listed = docker_client.image.list({"reference": "hello-world"})
131-
132-
warning_message = str(warnings_emmitted.list[0].message)
133-
assert "docker.image.list({'reference': 'hello-world'}" in warning_message
134-
assert "docker.image.list(filters={'reference': 'hello-world'}" in warning_message
135-
tags = set()
136-
for image in images_listed:
137-
for tag in image.repo_tags:
138-
tags.add(tag)
129+
expected_warning = (
130+
r"Passing filters as a mapping is deprecated, replace with an iterable "
131+
r"of tuples instead, as so:\n"
132+
r"filters=\[\(.*\)\]"
133+
)
134+
with pytest.warns(DeprecationWarning, match=expected_warning):
135+
images_listed = docker_client.image.list(filters={"reference": "hello-world"})
136+
tags = {tag.split("/")[-1] for image in images_listed for tag in image.repo_tags}
139137
assert tags == {"hello-world:latest"}
140138

141139

@@ -146,13 +144,13 @@ def test_filter_when_listing_old_signature(docker_client: DockerClient):
146144
pytest.param(
147145
"podman",
148146
marks=pytest.mark.xfail(
149-
reason="podman lists images with registry in image name"
147+
reason="podman includes all repo tags matching images ref"
150148
),
151149
),
152150
],
153151
indirect=True,
154152
)
155-
def test_use_first_argument_to_filter(ctr_client: DockerClient):
153+
def test_list_filter_by_name(ctr_client: DockerClient):
156154
ctr_client.pull(["hello-world", "busybox"])
157155
images_listed = ctr_client.image.list("hello-world")
158156
tags = {tag.split("/")[-1] for image in images_listed for tag in image.repo_tags}

tests/python_on_whales/components/test_network.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,25 @@ def test_network_exists(ctr_client: DockerClient):
9292
# Outside with clause, network should be removed and no longer exist
9393
assert not ctr_client.network.exists(network)
9494
assert not network.exists()
95+
96+
97+
@pytest.mark.parametrize("ctr_client", ["docker", "podman"], indirect=True)
98+
def test_list_filters(ctr_client: DockerClient):
99+
name = random_name()
100+
with ctr_client.network.create(name) as network:
101+
networks_listed = ctr_client.network.list(filters=[("name", name)])
102+
assert networks_listed == [network]
103+
104+
105+
def test_list_filters_old_signature(docker_client: DockerClient):
106+
"""Check backward compatibility of the DockerClient.network.list() API."""
107+
name = random_name()
108+
expected_warning = (
109+
r"Passing filters as a mapping is deprecated, replace with an iterable "
110+
r"of tuples instead, as so:\n"
111+
r"filters=\[\(.*\)\]"
112+
)
113+
with docker_client.network.create(name) as network:
114+
with pytest.warns(DeprecationWarning, match=expected_warning):
115+
networks_listed = docker_client.network.list(filters={"name": name})
116+
assert networks_listed == [network]

tests/python_on_whales/components/test_pod.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,31 @@ def test_list_multiple(podman_client: DockerClient):
7878
assert pod2_name in listed_pod_names
7979

8080

81+
def test_list_filters(podman_client: DockerClient):
82+
pod1_name = random_name()
83+
pod2_name = random_name()
84+
select_label = random_name()
85+
with podman_client.pod.create(pod1_name), podman_client.pod.create(
86+
pod2_name, labels={select_label: None}
87+
):
88+
listed_pods = podman_client.pod.list(filters=[("label", select_label)])
89+
assert [p.name for p in listed_pods] == [pod2_name]
90+
91+
92+
def test_list_filters_old_signature(podman_client: DockerClient):
93+
"""Check backward compatibility of the DockerClient.pod.list() API."""
94+
pod_name = random_name()
95+
expected_warning = (
96+
r"Passing filters as a mapping is deprecated, replace with an iterable "
97+
r"of tuples instead, as so:\n"
98+
r"filters=\[\(.*\)\]"
99+
)
100+
with podman_client.pod.create(pod_name):
101+
with pytest.warns(DeprecationWarning, match=expected_warning):
102+
listed_pods = podman_client.pod.list(filters={"name": pod_name})
103+
assert [p.name for p in listed_pods] == [pod_name]
104+
105+
81106
def test_does_not_exist(podman_client: DockerClient):
82107
assert podman_client.pod.exists("pod-that-does-not-exist") is False
83108

tests/python_on_whales/components/test_service.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_get_list_of_services(docker_client: DockerClient):
4646

4747

4848
@pytest.mark.usefixtures("swarm_mode")
49-
def test_filters(docker_client: DockerClient):
49+
def test_list_filters(docker_client: DockerClient):
5050
random_label_value = random_name()
5151
with docker_client.service.create(
5252
"busybox",
@@ -59,11 +59,11 @@ def test_filters(docker_client: DockerClient):
5959
labels=dict(dodo="something"),
6060
):
6161
expected_services = docker_client.service.list(
62-
filters=dict(label=f"dodo={random_label_value}")
62+
filters=[("label", f"dodo={random_label_value}")]
6363
)
6464
assert expected_services == [my_service]
6565
no_expected_services = docker_client.service.list(
66-
filters=dict(label=f"dodo={random_name()}")
66+
filters=[("label", f"dodo={random_name()}")]
6767
)
6868
assert len(no_expected_services) == 0
6969

tests/python_on_whales/components/test_volume.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from python_on_whales import DockerClient
88
from python_on_whales.components.volume.models import VolumeInspectResult
99
from python_on_whales.exceptions import NoSuchVolume
10-
from python_on_whales.test_utils import get_all_jsons
10+
from python_on_whales.test_utils import get_all_jsons, random_name
1111

1212

1313
@pytest.mark.parametrize("json_file", get_all_jsons("volumes"))
@@ -84,6 +84,32 @@ def test_list(ctr_client: DockerClient):
8484
assert v in all_volumes
8585

8686

87+
@pytest.mark.parametrize("ctr_client", ["docker", "podman"], indirect=True)
88+
def test_list_filters(ctr_client: DockerClient):
89+
select_label = random_name()
90+
with ctr_client.volume.create(), ctr_client.volume.create(
91+
labels={select_label: None}
92+
) as vol2:
93+
listed_volumes = ctr_client.volume.list(filters=[("label", select_label)])
94+
assert listed_volumes == [vol2]
95+
96+
97+
def test_list_filters_old_signature(docker_client: DockerClient):
98+
"""Check backward compatibility of the DockerClient.pod.list() API."""
99+
expected_warning = (
100+
r"Passing filters as a mapping is deprecated, replace with an iterable "
101+
r"of tuples instead, as so:\n"
102+
r"filters=\[\(.*\)\]"
103+
)
104+
select_label = random_name()
105+
with docker_client.volume.create(), docker_client.volume.create(
106+
labels={select_label: None}
107+
) as vol2:
108+
with pytest.warns(DeprecationWarning, match=expected_warning):
109+
listed_volumes = docker_client.volume.list(filters={"label": select_label})
110+
assert listed_volumes == [vol2]
111+
112+
87113
@pytest.mark.parametrize(
88114
"ctr_client",
89115
[

0 commit comments

Comments
 (0)