Skip to content

Commit

Permalink
Annotate more test codes
Browse files Browse the repository at this point in the history
  • Loading branch information
achimnol committed Jun 25, 2024
1 parent 3ca6a6f commit 4ce3d30
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 101 deletions.
8 changes: 6 additions & 2 deletions aiodocker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
from typing import (
IO,
Any,
Dict,
Iterable,
Mapping,
Optional,
Sequence,
Tuple,
Union,
cast,
Expand Down Expand Up @@ -189,13 +191,15 @@ def format_env(key, value: Union[None, bytes, str]) -> str:
return f"{key}={value}"


def clean_networks(networks: Optional[Iterable[str]] = None) -> Optional[Iterable[str]]:
def clean_networks(
networks: Optional[Iterable[str]] = None,
) -> Optional[Sequence[Dict[str, Any]]]:
"""
Cleans the values inside `networks`
Returns a new list
"""
if not networks:
return networks
return []
if not isinstance(networks, list):
raise TypeError("networks parameter must be a list.")

Expand Down
39 changes: 23 additions & 16 deletions tests/test_containers.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import asyncio
import os
import sys

import pytest

from aiodocker.containers import DockerContainer
from aiodocker.docker import Docker
from aiodocker.exceptions import DockerContainerError, DockerError


async def _validate_hello(container):
async def _validate_hello(container: DockerContainer) -> None:
try:
await container.start()
response = await container.wait()
assert response["StatusCode"] == 0
await asyncio.sleep(5) # wait for output in case of slow test container
logs = await container.log(stdout=True)
assert "hello" + os.linesep in logs
assert "hello\n" in logs

with pytest.raises(TypeError):
await container.log()
Expand All @@ -23,7 +24,7 @@ async def _validate_hello(container):


@pytest.mark.asyncio
async def test_run_existing_container(docker, image_name):
async def test_run_existing_container(docker: Docker, image_name: str) -> None:
await docker.pull(image_name)
container = await docker.containers.run(
config={
Expand All @@ -37,7 +38,9 @@ async def test_run_existing_container(docker, image_name):


@pytest.mark.asyncio
async def test_run_container_with_missing_image(docker, image_name):
async def test_run_container_with_missing_image(
docker: Docker, image_name: str
) -> None:
try:
await docker.images.delete(image_name)
except DockerError as e:
Expand All @@ -61,7 +64,7 @@ async def test_run_container_with_missing_image(docker, image_name):


@pytest.mark.asyncio
async def test_run_failing_start_container(docker, image_name):
async def test_run_failing_start_container(docker: Docker, image_name: str) -> None:
try:
await docker.images.delete(image_name)
except DockerError as e:
Expand Down Expand Up @@ -91,7 +94,7 @@ async def test_run_failing_start_container(docker, image_name):


@pytest.mark.asyncio
async def test_restart(docker, image_name):
async def test_restart(docker: Docker, image_name: str) -> None:
# sleep for 10 min to emulate hanging container
container = await docker.containers.run(
config={
Expand All @@ -117,7 +120,7 @@ async def test_restart(docker, image_name):


@pytest.mark.asyncio
async def test_container_stats_list(docker, image_name):
async def test_container_stats_list(docker: Docker, image_name: str) -> None:
container = await docker.containers.run(
config={
"Cmd": ["-c", "print('hello')"],
Expand All @@ -137,7 +140,7 @@ async def test_container_stats_list(docker, image_name):


@pytest.mark.asyncio
async def test_container_stats_stream(docker, image_name):
async def test_container_stats_stream(docker: Docker, image_name: str) -> None:
container = await docker.containers.run(
config={
"Cmd": ["-c", "print('hello')"],
Expand All @@ -161,15 +164,17 @@ async def test_container_stats_stream(docker, image_name):


@pytest.mark.asyncio
async def test_resize(shell_container):
async def test_resize(shell_container: DockerContainer) -> None:
await shell_container.resize(w=120, h=10)


@pytest.mark.skipif(
sys.platform == "win32", reason="Commit unpaused containers doesn't work on Windows"
)
@pytest.mark.asyncio
async def test_commit(docker, image_name, shell_container):
async def test_commit(
docker: Docker, image_name: str, shell_container: DockerContainer
) -> None:
"""
"Container" key was removed in v1.45.
"ContainerConfig" is not present, although this information is now present in "Config"
Expand All @@ -192,7 +197,9 @@ async def test_commit(docker, image_name, shell_container):
sys.platform == "win32", reason="Commit unpaused containers doesn't work on Windows"
)
@pytest.mark.asyncio
async def test_commit_with_changes(docker, image_name, shell_container):
async def test_commit_with_changes(
docker: Docker, image_name: str, shell_container: DockerContainer
) -> None:
ret = await shell_container.commit(changes=["EXPOSE 8000", 'CMD ["py"]'])
img_id = ret["Id"]
img = await docker.images.inspect(img_id)
Expand All @@ -203,7 +210,7 @@ async def test_commit_with_changes(docker, image_name, shell_container):

@pytest.mark.skipif(sys.platform == "win32", reason="Pause doesn't work on Windows")
@pytest.mark.asyncio
async def test_pause_unpause(shell_container):
async def test_pause_unpause(shell_container: DockerContainer) -> None:
await shell_container.pause()
container_info = await shell_container.show()
assert "State" in container_info
Expand All @@ -228,7 +235,7 @@ async def test_pause_unpause(shell_container):


@pytest.mark.asyncio
async def test_capture_log_oneshot(docker, image_name) -> None:
async def test_capture_log_oneshot(docker: Docker, image_name: str) -> None:
container = await docker.containers.run(
config={
"Cmd": [
Expand All @@ -252,7 +259,7 @@ async def test_capture_log_oneshot(docker, image_name) -> None:


@pytest.mark.asyncio
async def test_capture_log_stream(docker, image_name) -> None:
async def test_capture_log_stream(docker: Docker, image_name: str) -> None:
container = await docker.containers.run(
config={
"Cmd": [
Expand All @@ -278,7 +285,7 @@ async def test_capture_log_stream(docker, image_name) -> None:


@pytest.mark.asyncio
async def test_cancel_log(docker) -> None:
async def test_cancel_log(docker: Docker) -> None:
container = docker.containers.container("invalid_container_id")

with pytest.raises(DockerError):
Expand Down
10 changes: 7 additions & 3 deletions tests/test_events.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from __future__ import annotations

import asyncio

import pytest

from aiodocker.docker import Docker


@pytest.mark.asyncio
async def test_events_default_task(docker):
async def test_events_default_task(docker: Docker) -> None:
docker.events.subscribe()
assert docker.events.task is not None
await docker.close()
Expand All @@ -13,7 +17,7 @@ async def test_events_default_task(docker):


@pytest.mark.asyncio
async def test_events_provided_task(docker):
async def test_events_provided_task(docker: Docker) -> None:
task = asyncio.ensure_future(docker.events.run())
docker.events.subscribe(create_task=False)
assert docker.events.task is None
Expand All @@ -25,7 +29,7 @@ async def test_events_provided_task(docker):


@pytest.mark.asyncio
async def test_events_no_task(docker):
async def test_events_no_task(docker: Docker) -> None:
assert docker.events.task is None
await docker.close()
assert docker.events.json_stream is None
Loading

0 comments on commit 4ce3d30

Please sign in to comment.