def test_filter_when_listing(): docker.pull("hello-world") images_listed = docker.image.list(filters=dict(reference="hello-world")) tags = set() for image in images_listed: for tag in image.repo_tags: tags.add(tag) assert tags == {"hello-world:latest"}
def test_buildx_build_push_registry(tmp_path, docker_registry): (tmp_path / "Dockerfile").write_text(dockerfile_content1) with docker.buildx.create(use=True, driver_options=dict(network="host")): output = docker.buildx.build(tmp_path, push=True, tags=f"{docker_registry}/dodo") assert output is None docker.pull(f"{docker_registry}/dodo")
def test_multiarch_build(tmp_path, docker_registry): (tmp_path / "Dockerfile").write_text(dockerfile_content1) tags = [f"{docker_registry}/dodo:1"] with docker.buildx.create(use=True, driver_options=dict(network="host")): output = docker.buildx.build( tmp_path, push=True, tags=tags, platforms=["linux/amd64", "linux/arm64"] ) assert output is None docker.pull(f"{docker_registry}/dodo:1")
def test_pull_not_quiet_multiple_images_break(): images_names = ["busybox:1", "hellstuff"] try: docker.image.remove(images_names) except DockerException: pass with pytest.raises(DockerException) as err: docker.pull(images_names) assert "docker image pull hellstuff" in str(err.value)
def test_cache_invalidity(tmp_path): (tmp_path / "Dockerfile").write_text(dockerfile_content1) with set_cache_validity_period(100): image = docker.buildx.build(tmp_path, tags=["hello1", "hello2"]) docker.image.remove("hello1") docker.pull("hello-world") docker.tag("hello-world", "hello1") image.reload() assert "hello1:latest" not in image.repo_tags assert "hello2:latest" in image.repo_tags
def test_many_images(): for tag in [ "ubuntu:16.04", "ubuntu:18.04", "ubuntu:20.04", "busybox:1", "traefik:v2.3.2", "redis:alpine3.12", "docker:19.03.13", ]: docker.pull(tag)._get_inspect_result()
def test_login_logout(docker_registry_without_login): busybox_image = docker.pull("busybox") busybox_image.tag(f"{docker_registry_without_login}/my_busybox") with pytest.raises(DockerException): docker.push(f"{docker_registry_without_login}/my_busybox") docker.login(docker_registry_without_login, username="******", password="******") assert (docker_registry_without_login in (Path.home() / ".docker" / "config.json").read_text()) docker.push(f"{docker_registry_without_login}/my_busybox") docker.pull(f"{docker_registry_without_login}/my_busybox") docker.logout(docker_registry_without_login) assert (docker_registry_without_login not in (Path.home() / ".docker" / "config.json").read_text())
def test_pull_not_quiet_multiple_images(): images_names = ["busybox:1", "hello-world:latest"] try: docker.image.remove(images_names) except DockerException: pass images = docker.pull(images_names) for image_name, image in zip(images_names, images): assert image_name in image.repo_tags
def test_copy_from_and_to_directory(tmp_path): my_image = docker.pull("busybox:1") (tmp_path / "dodo.txt").write_text("Hello world!") new_image_name = random_name() my_image.copy_to(tmp_path, "/some_path", new_tag=new_image_name) new_image_name = docker.image.inspect(new_image_name) new_image_name.copy_from("/some_path", tmp_path / "some_path") assert "Hello world!" == (tmp_path / "some_path" / "dodo.txt").read_text()
def test_copy_from_and_to(tmp_path): my_image = docker.pull("busybox:1") (tmp_path / "dodo.txt").write_text("Hello world!") new_image_name = random_name() my_image.copy_to(tmp_path / "dodo.txt", "/dada.txt", new_tag=new_image_name) new_image_name = docker.image.inspect(new_image_name) new_image_name.copy_from("/dada.txt", tmp_path / "dudu.txt") assert (tmp_path / "dodo.txt").read_text() == (tmp_path / "dudu.txt").read_text()
def test_prune_prunes_image(): # TODO: Test dangling image for container in docker.container.list(filters={"ancestor": "busybox"}): docker.container.remove(container, force=True) image = docker.pull("busybox") assert image in docker.image.list() # image not pruned because not dangling docker.system.prune() assert image in docker.image.list() # image not pruned because it does not have dne label docker.system.prune(all=True, filters={"label": "dne"}) assert image in docker.image.list() # image pruned docker.system.prune(all=True) assert image not in docker.image.list()
def test_exists(): my_image = docker.pull("busybox") assert my_image.exists() assert docker.image.exists("busybox") assert not docker.image.exists("dudurghurozgiozpfezjigfoeioengizeonig")
def test_prune(): docker.pull("busybox") docker.image.prune(all=True) with pytest.raises(DockerException): docker.image.inspect("busybox")
def test_disk_free(): docker.pull("busybox") docker.pull("busybox:1") docker_items_summary = docker.system.disk_free() assert docker_items_summary.images.active > 1 assert docker_items_summary.images.size > 2000