forked from testcontainers/testcontainers-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
66 lines (51 loc) · 2.02 KB
/
conftest.py
File metadata and controls
66 lines (51 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from pathlib import Path
import pytest
from typing import Callable
from testcontainers.core.docker_client import DockerClient
from pprint import pprint
import sys
PROJECT_DIR = Path(__file__).parent.parent.parent.resolve()
def pytest_configure(config: pytest.Config) -> None:
"""
Add configuration for custom pytest markers.
"""
config.addinivalue_line(
"markers",
"inside_docker_check: test used to validate DinD/DooD are working as expected",
)
@pytest.fixture(scope="session")
def python_testcontainer_image() -> str:
"""Build an image with test containers python for DinD and DooD tests"""
py_version = ".".join(map(str, sys.version_info[:2]))
image_name = f"testcontainers-python:{py_version}"
client = DockerClient()
client.build(
path=str(PROJECT_DIR),
tag=image_name,
rm=False,
buildargs={"PYTHON_VERSION": py_version},
)
return image_name
@pytest.fixture
def check_for_image() -> Callable[[str, bool], None]:
"""Warp the check_for_image function in a fixture"""
def _check_for_image(image_short_id: str, cleaned: bool) -> None:
"""
Validates if the image is present or not.
:param image_short_id: The short id of the image
:param cleaned: True if the image should not be present, False otherwise
"""
client = DockerClient()
images = client.client.images.list()
found = any(image.short_id.endswith(image_short_id) for image in images)
assert found is not cleaned, f"Image {image_short_id} was {'found' if cleaned else 'not found'}"
return _check_for_image
@pytest.fixture
def show_container_attributes() -> Callable[..., None]:
"""Wrap the show_container_attributes function in a fixture"""
def _show_container_attributes(container_id: str) -> None:
"""Print the attributes of a container"""
client = DockerClient().client
data = client.containers.get(container_id).attrs
pprint(data)
return _show_container_attributes