Skip to content

Commit

Permalink
Move most of the remaining tests to use docker_client or ctr_client f…
Browse files Browse the repository at this point in the history
…ixture (#535)
  • Loading branch information
LewisGaul authored Jan 15, 2024
1 parent 3c3d0cb commit 26302ea
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 250 deletions.
38 changes: 21 additions & 17 deletions tests/python_on_whales/components/test_config.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
from pathlib import Path

import pytest

from python_on_whales import docker
from python_on_whales import DockerClient


@pytest.mark.usefixtures("swarm_mode")
def test_list_configs():
assert docker.config.list() == []
def test_list_configs(docker_client: DockerClient):
assert docker_client.config.list() == []


@pytest.mark.usefixtures("swarm_mode")
def test_create_delete_config(tmp_path):
def test_create_delete_config(docker_client: DockerClient, tmp_path: Path):
config_file = tmp_path / "config.conf"
config_file.write_text("hello world")
my_conf = docker.config.create("my_conf", config_file)
my_conf = docker_client.config.create("my_conf", config_file)
with my_conf:
assert my_conf.spec.name == "my_conf"
assert docker.config.list() == [my_conf]
assert docker.config.inspect("my_conf") == my_conf
repr(docker.config.list())
assert docker_client.config.list() == [my_conf]
assert docker_client.config.inspect("my_conf") == my_conf
repr(docker_client.config.list())


@pytest.mark.usefixtures("swarm_mode")
def test_labels_config(tmp_path):
def test_labels_config(docker_client: DockerClient, tmp_path: Path):
config_file = tmp_path / "config.conf"
config_file.write_text("hello world")
my_conf = docker.config.create("my_conf", config_file, labels=dict(dodo="dada"))
my_conf = docker_client.config.create(
"my_conf", config_file, labels=dict(dodo="dada")
)
with my_conf:
assert my_conf.spec.name == "my_conf"
assert docker.config.list(filters=dict(label="dodo=dada")) == [my_conf]
assert docker.config.list(filters=dict(label="dodo=dadu")) == []
assert docker_client.config.list(filters=dict(label="dodo=dada")) == [my_conf]
assert docker_client.config.list(filters=dict(label="dodo=dadu")) == []


@pytest.mark.usefixtures("swarm_mode")
def test_remove_empty_config_list(tmp_path):
def test_remove_empty_config_list(docker_client: DockerClient, tmp_path: Path):
config_file = tmp_path / "config.conf"
config_file.write_text("hello world")
with docker.config.create("my_conf", config_file) as my_conf:
assert docker.config.list() == [my_conf]
docker.config.remove([])
assert docker.config.list() == [my_conf]
with docker_client.config.create("my_conf", config_file) as my_conf:
assert docker_client.config.list() == [my_conf]
docker_client.config.remove([])
assert docker_client.config.list() == [my_conf]
40 changes: 21 additions & 19 deletions tests/python_on_whales/components/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from python_on_whales import docker
from python_on_whales import DockerClient
from python_on_whales.components.context.models import ContextInspectResult
from python_on_whales.test_utils import get_all_jsons

Expand All @@ -14,13 +14,13 @@ def test_load_json(json_file):
# we could do more checks here if needed


def test_create_context():
def test_create_context(docker_client: DockerClient):
testname = "testpow"
host = "ssh://test@test.domain"
description = "Python on whales testing context"

all_contexts_before = set(docker.context.list())
with docker.context.create(
all_contexts_before = set(docker_client.context.list())
with docker_client.context.create(
testname, docker=dict(host=host), description=description
) as new_context:
assert new_context.name == testname
Expand All @@ -29,30 +29,32 @@ def test_create_context():

assert new_context not in all_contexts_before

assert new_context in docker.context.list()
assert new_context in docker_client.context.list()


def test_inpect():
default_context = docker.context.inspect()
def test_inpect(docker_client: DockerClient):
default_context = docker_client.context.inspect()
assert default_context.name == "default"
assert default_context == docker.context.inspect("default")
a, b = docker.context.inspect(["default", "default"])
assert default_context == docker_client.context.inspect("default")
a, b = docker_client.context.inspect(["default", "default"])
assert a == b


def test_list_contexts():
assert docker.context.list() == [docker.context.inspect("default")]
def test_list_contexts(docker_client: DockerClient):
assert docker_client.context.list() == [docker_client.context.inspect("default")]


def test_use_context():
docker.context.use("default")
def test_use_context(docker_client: DockerClient):
docker_client.context.use("default")


def test_use_context_returns():
assert docker.context.use("default") == docker.context.inspect("default")
def test_use_context_returns(docker_client: DockerClient):
assert docker_client.context.use("default") == docker_client.context.inspect(
"default"
)


def test_remove_empty_context_list():
all_contexts = set(docker.context.list())
docker.context.remove([])
assert all_contexts == set(docker.context.list())
def test_remove_empty_context_list(docker_client: DockerClient):
all_contexts = set(docker_client.context.list())
docker_client.context.remove([])
assert all_contexts == set(docker_client.context.list())
45 changes: 25 additions & 20 deletions tests/python_on_whales/components/test_manifest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import json
import os
from typing import Generator

import pytest

from python_on_whales import docker
from python_on_whales import DockerClient, Image
from python_on_whales.components.manifest.cli_wrapper import (
ManifestList,
ManifestListInspectResult,
Expand All @@ -12,26 +13,30 @@


@pytest.fixture
def with_manifest():
def manifest(docker_client: DockerClient) -> Generator[ManifestList, None, None]:
manifest_name = random_name()
# utilizing old, pre-manifest-list images
images = ["busybox:1.26", "busybox:1.27.1"]
docker.image.pull(images)
with docker.manifest.create(manifest_name, images) as my_manifest:
docker_client.image.pull(images)
with docker_client.manifest.create(manifest_name, images) as my_manifest:
yield my_manifest
docker.image.remove(images)
docker_client.image.remove(images)


@pytest.fixture
def with_platform_variant_manifest(request):
def platform_variant_manifest(
docker_client: DockerClient, request: pytest.FixtureRequest
) -> Image:
image_with_platform_variant = "arm32v7/busybox:1.35"

def remove_docker_image():
docker.image.remove(image_with_platform_variant)
docker_client.image.remove(image_with_platform_variant)

request.addfinalizer(remove_docker_image)
docker.image.pull(image_with_platform_variant, quiet=True, platform="linux/arm/v7")
return docker.image.inspect(image_with_platform_variant)
docker_client.image.pull(
image_with_platform_variant, quiet=True, platform="linux/arm/v7"
)
return docker_client.image.inspect(image_with_platform_variant)


@pytest.mark.parametrize("json_file", get_all_jsons("manifests"))
Expand All @@ -45,23 +50,23 @@ def test_load_json(json_file):
os.environ.get("CI", "false") == "true",
reason="The creation of manifest doesn't work in github actions for some reason",
)
def test_manifest_create_remove(with_manifest):
assert isinstance(with_manifest, ManifestList)
def test_manifest_create_remove(docker_client: DockerClient, manifest: ManifestList):
assert isinstance(manifest, ManifestList)


@pytest.mark.skipif(
os.environ.get("CI", "false") == "true",
reason="The creation of manifest doesn't work in github actions for some reason",
)
def test_manifest_annotate(with_manifest):
docker.manifest.annotate(
with_manifest.name, "busybox:1.26", os="linux", arch="arm64"
def test_manifest_annotate(docker_client: DockerClient, manifest: ManifestList):
docker_client.manifest.annotate(
manifest.name, "busybox:1.26", os="linux", arch="arm64"
)
assert with_manifest.manifests[0].platform.os == "linux"
assert with_manifest.manifests[0].platform.architecture == "arm64"
assert manifest.manifests[0].platform.os == "linux"
assert manifest.manifests[0].platform.architecture == "arm64"


def test_manifest_platform_variant(with_platform_variant_manifest):
assert "linux" in repr(with_platform_variant_manifest.os)
assert "arm" in repr(with_platform_variant_manifest.architecture)
assert "v7" in repr(with_platform_variant_manifest.variant)
def test_manifest_platform_variant(platform_variant_manifest: Image):
assert "linux" in repr(platform_variant_manifest.os)
assert "arm" in repr(platform_variant_manifest.architecture)
assert "v7" in repr(platform_variant_manifest.variant)
30 changes: 15 additions & 15 deletions tests/python_on_whales/components/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from python_on_whales import docker
from python_on_whales import DockerClient
from python_on_whales.components.node.models import NodeInspectResult
from python_on_whales.test_utils import get_all_jsons

Expand All @@ -24,40 +24,40 @@ def test_load_json(json_file):


@pytest.mark.usefixtures("swarm_mode")
def test_list_nodes():
nodes = docker.node.list()
def test_list_nodes(docker_client: DockerClient):
nodes = docker_client.node.list()
assert nodes[0].id[:12] in repr(nodes)
assert len(nodes) == 1


@pytest.mark.usefixtures("swarm_mode")
def test_add_label():
nodes = docker.node.list()
def test_add_label(docker_client: DockerClient):
nodes = docker_client.node.list()
nodes[0].update(labels_add={"foo": "bar"})
assert nodes[0].spec.labels["foo"] == "bar"


@pytest.mark.usefixtures("swarm_mode")
def test_remove_label():
nodes = docker.node.list()
def test_remove_label(docker_client: DockerClient):
nodes = docker_client.node.list()
nodes[0].update(labels_add={"foo": "bar"})
nodes[0].update(rm_labels=["foo"])
assert "foo" not in nodes[0].spec.labels


@pytest.mark.usefixtures("swarm_mode")
def test_tasks():
service = docker.service.create("busybox", ["sleep", "infinity"])
def test_tasks(docker_client: DockerClient):
service = docker_client.service.create("busybox", ["sleep", "infinity"])

current_node = docker.node.list()[0]
current_node = docker_client.node.list()[0]
tasks = current_node.ps()
assert len(tasks) > 0
assert tasks[0].desired_state == "running"
docker.service.remove(service)
docker_client.service.remove(service)


@pytest.mark.usefixtures("swarm_mode")
def test_list_tasks_node():
with docker.service.create("busybox", ["sleep", "infinity"]) as my_service:
assert docker.node.ps([]) == []
assert set(docker.node.ps()) == set(docker.service.ps(my_service))
def test_list_tasks_node(docker_client: DockerClient):
with docker_client.service.create("busybox", ["sleep", "infinity"]) as my_service:
assert docker_client.node.ps([]) == []
assert set(docker_client.node.ps()) == set(docker_client.service.ps(my_service))
22 changes: 11 additions & 11 deletions tests/python_on_whales/components/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from python_on_whales import docker
from python_on_whales import DockerClient
from python_on_whales.components.plugin.models import PluginInspectResult
from python_on_whales.test_utils import get_all_jsons

Expand All @@ -23,23 +23,23 @@ def test_load_json(json_file):
# we could do more checks here if needed


def test_install_plugin_disable_enable():
with docker.plugin.install(test_plugin_name) as my_plugin:
def test_install_plugin_disable_enable(docker_client: DockerClient):
with docker_client.plugin.install(test_plugin_name) as my_plugin:
my_plugin.disable()
my_plugin.enable()
assert my_plugin in docker.plugin.list()
assert my_plugin in docker_client.plugin.list()
assert "name='vieux/sshfs:latest'" in repr(my_plugin)


def test_plugin_upgrade():
with docker.plugin.install(test_plugin_name) as my_plugin:
def test_plugin_upgrade(docker_client: DockerClient):
with docker_client.plugin.install(test_plugin_name) as my_plugin:
my_plugin.disable()
my_plugin.upgrade()


def test_remove_empty_plugin_list():
with docker.plugin.install(test_plugin_name) as my_plugin:
plugins_set = set(docker.plugin.list())
def test_remove_empty_plugin_list(docker_client: DockerClient):
with docker_client.plugin.install(test_plugin_name) as my_plugin:
plugins_set = set(docker_client.plugin.list())
assert my_plugin in plugins_set
docker.plugin.remove([])
assert set(docker.plugin.list()) == plugins_set
docker_client.plugin.remove([])
assert set(docker_client.plugin.list()) == plugins_set
Loading

0 comments on commit 26302ea

Please sign in to comment.