Exemple #1
0
import pytest

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


@pytest.mark.parametrize("json_file", get_all_jsons("nodes"))
def test_load_json(json_file):
    json_as_txt = json_file.read_text()
    a: NodeInspectResult = NodeInspectResult.parse_raw(json_as_txt)
    if json_file.name == "1.json":
        assert (a.description.resources.generic_resources[0].
                named_resource_spec.kind == "gpu")
        assert (a.description.resources.generic_resources[0].
                named_resource_spec.value == "gpu-0")
        assert a.description.resources.nano_cpus == 4000000001


@pytest.mark.usefixtures("swarm_mode")
def test_list_nodes():
    nodes = docker.node.list()
    assert len(nodes) == 1


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

    current_node = docker.node.list()[0]
    tasks = current_node.ps()
    assert docker.volume.exists("components_dodo")
    docker.compose.down()
    assert docker.volume.exists("components_dodo")
    docker.compose.down(volumes=True)
    assert not docker.volume.exists("components_dodo")


def test_compose_config_from_rc1():
    config = ComposeConfig.parse_file(
        Path(__file__).parent / "strange_compose_config_rc1.json")

    assert config.services[
        "myservice"].deploy.resources.reservations.cpus == "'0.25'"


@pytest.mark.parametrize("json_file", get_all_jsons("compose"))
def test_load_json(json_file):
    json_as_txt = json_file.read_text()
    config: ComposeConfig = ComposeConfig.parse_raw(json_as_txt)
    if json_file.name == "0.json":
        assert config.services["traefik"].labels["traefik.enable"] == "true"


def test_compose_run_simple():
    result = docker.compose.run("alpine", ["echo", "dodo"],
                                remove=True,
                                tty=False)
    assert result == "dodo"


def test_compose_run_detach():
import pytest

from python_on_whales import docker
from python_on_whales.components.network.cli_wrapper import NetworkInspectResult
from python_on_whales.exceptions import DockerException
from python_on_whales.test_utils import get_all_jsons, random_name


@pytest.mark.parametrize("json_file", get_all_jsons("networks"))
def test_load_json(json_file):
    json_as_txt = json_file.read_text()
    NetworkInspectResult.parse_raw(json_as_txt)
    # we could do more checks here if needed


def test_network_create_remove():
    my_name = random_name()
    with docker.network.create(my_name) as my_network:
        assert my_network.name == my_name


def test_network_create_with_labels():
    my_name = random_name()
    labels = {"hello": "world", "meme": "meme-label"}
    with docker.network.create(my_name, labels=labels) as my_network:
        assert my_network.name == my_name
        for key, value in labels.items():
            assert my_network.labels[key] == value


def test_context_manager():
Exemple #4
0
from pathlib import Path

import pytest

import python_on_whales
from python_on_whales import Image, docker
from python_on_whales.components.container.cli_wrapper import ContainerStats
from python_on_whales.components.container.models import (
    ContainerInspectResult,
    ContainerState,
)
from python_on_whales.exceptions import DockerException, NoSuchContainer
from python_on_whales.test_utils import get_all_jsons, random_name


@pytest.mark.parametrize("json_file", get_all_jsons("containers"))
def test_load_json(json_file):
    json_as_txt = json_file.read_text()
    ContainerInspectResult.parse_raw(json_as_txt)
    # we could do more checks here if needed


def test_simple_command():
    output = docker.run("hello-world", remove=True)
    assert "Hello from Docker!" in output


def test_simple_command_create_start():
    output = docker.container.create("hello-world",
                                     remove=True).start(attach=True)
    assert "Hello from Docker!" in output
Exemple #5
0
import pytest

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

test_plugin_name = "vieux/sshfs:latest"


def get_all_plugins_jsons() -> List[Path]:
    jsons_directory = Path(__file__).parent / "plugins"
    return sorted(list(jsons_directory.iterdir()))


@pytest.mark.parametrize("json_file", get_all_jsons("plugins"))
def test_load_json(json_file):
    json_as_txt = json_file.read_text()
    PluginInspectResult.parse_raw(json_as_txt)
    # we could do more checks here if needed


def test_install_plugin_disable_enable():
    with docker.plugin.install(test_plugin_name) as my_plugin:
        my_plugin.disable()
        my_plugin.enable()


def test_plugin_upgrade():
    with docker.plugin.install(test_plugin_name) as my_plugin:
        my_plugin.disable()
Exemple #6
0
from python_on_whales.components.system import DockerEvent, SystemInfo
from python_on_whales.test_utils import get_all_jsons


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


def test_info():
    info = docker.system.info()
    assert "local" in info.plugins.volume


@pytest.mark.parametrize("json_file", get_all_jsons("system_info"))
def test_load_json(json_file):
    json_as_txt = json_file.read_text()
    SystemInfo.parse_raw(json_as_txt)
    # we could do more checks here if needed


def test_parsing_events():
    json_file = Path(__file__).parent / "jsons/events/0.json"
    events = json.loads(json_file.read_text())["events"]
    for event in events:
        parsed: DockerEvent = DockerEvent.parse_obj(event)
        assert parsed.time.date() == date(2020, 12, 28)
import time

import pytest

from python_on_whales import docker
from python_on_whales.components.service import ServiceInspectResult
from python_on_whales.test_utils import get_all_jsons


@pytest.mark.parametrize("json_file", get_all_jsons("services"))
def test_load_json(json_file):
    json_as_txt = json_file.read_text()
    ServiceInspectResult.parse_raw(json_as_txt)
    # we could do more checks here if needed


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

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


@pytest.mark.usefixtures("swarm_mode")
def test_service_scale():
    service = docker.service.create("busybox", ["sleep", "infinity"])
    service.scale(3)
    time.sleep(0.4)
Exemple #8
0
import pytest

from python_on_whales.components.buildx.imagetools.models import Manifest
from python_on_whales.test_utils import get_all_jsons


@pytest.mark.parametrize("json_file", get_all_jsons("manifests"))
def test_load_json(json_file):
    Manifest.parse_file(json_file)
Exemple #9
0
import pytest

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


@pytest.mark.parametrize("json_file", get_all_jsons("contexts"))
def test_load_json(json_file):
    json_as_txt = json_file.read_text()
    ContextInspectResult.parse_raw(json_as_txt)
    # we could do more checks here if needed


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


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


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