pydocks 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pydocks/__init__.py +16 -0
- pydocks/nginx.py +16 -0
- pydocks/plugin.py +63 -0
- pydocks/postgresql.py +143 -0
- pydocks/py.typed +0 -0
- pydocks-0.0.1.dist-info/METADATA +86 -0
- pydocks-0.0.1.dist-info/RECORD +10 -0
- pydocks-0.0.1.dist-info/WHEEL +4 -0
- pydocks-0.0.1.dist-info/entry_points.txt +2 -0
- pydocks-0.0.1.dist-info/licenses/LICENSE +21 -0
pydocks/__init__.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
__version__ = "0.0.1"
|
|
2
|
+
__all__ = (
|
|
3
|
+
"__version__",
|
|
4
|
+
"postgresql_clean_all_containers",
|
|
5
|
+
"postgresql_container",
|
|
6
|
+
"postgresql_container_session",
|
|
7
|
+
"docker",
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
# pytest_plugins = ["pydocks.conftest"]
|
|
11
|
+
from pydocks.plugin import docker
|
|
12
|
+
from pydocks.postgresql import (
|
|
13
|
+
postgresql_clean_all_containers,
|
|
14
|
+
postgresql_container,
|
|
15
|
+
postgresql_container_session,
|
|
16
|
+
)
|
pydocks/nginx.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""
|
|
2
|
+
# https://hub.docker.com/r/nginx/tags
|
|
3
|
+
TEST_NGINX_DOCKER_IMAGE: str = "docker.io/nginx:1.27.1-alpine"
|
|
4
|
+
|
|
5
|
+
# https://hub.docker.com/r/hashicorp/terraform/tags
|
|
6
|
+
TEST_TERRAFORM_DOCKER_IMAGE: str = "docker.io/hashicorp/terraform:1.5"
|
|
7
|
+
|
|
8
|
+
# https://hub.docker.com/r/chainguard/opentofu/tags
|
|
9
|
+
TEST_OPENTOFU_DOCKER_IMAGE: str = "docker.io/chainguard/opentofu:latest-dev"
|
|
10
|
+
|
|
11
|
+
# https://hub.docker.com/_/redis/tags
|
|
12
|
+
TEST_REDIS_DOCKER_IMAGE: str = "docker.io/redis:7.4.0"
|
|
13
|
+
|
|
14
|
+
# https://hub.docker.com/r/hashicorp/vault/tags
|
|
15
|
+
TEST_VAULT_DOCKER_IMAGE: str = "docker.io/hashicorp/vault:1.17.3"
|
|
16
|
+
"""
|
pydocks/plugin.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
import os
|
|
4
|
+
|
|
5
|
+
import socket
|
|
6
|
+
|
|
7
|
+
# from asgi_lifespan import LifespanManager
|
|
8
|
+
import anyio
|
|
9
|
+
from python_on_whales import DockerClient
|
|
10
|
+
from reattempt import reattempt
|
|
11
|
+
import logging
|
|
12
|
+
|
|
13
|
+
logger = logging.getLogger("pydocks")
|
|
14
|
+
logger.addHandler(logging.NullHandler())
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@pytest.fixture(scope="session", autouse=True)
|
|
18
|
+
def docker():
|
|
19
|
+
if "DOCKER_SOCK" in os.environ:
|
|
20
|
+
yield DockerClient(host=os.environ["DOCKER_SOCK"])
|
|
21
|
+
elif "CI" in os.environ:
|
|
22
|
+
# Github Actions, GitLab CI, ...
|
|
23
|
+
yield DockerClient()
|
|
24
|
+
else:
|
|
25
|
+
# local with colima
|
|
26
|
+
home: str = str(Path.home())
|
|
27
|
+
yield DockerClient(host=f"unix://{home}/.colima/default/docker.sock")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@reattempt(max_retries=30, min_time=0.1, max_time=0.5)
|
|
31
|
+
async def socket_test_connection(host, port):
|
|
32
|
+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
33
|
+
s.connect((host, port))
|
|
34
|
+
await anyio.wait_socket_writable(s)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
async def clean_containers(docker: DockerClient, name: str):
|
|
38
|
+
containers = docker.ps(all=True, filters={"name": f"^{name}"})
|
|
39
|
+
|
|
40
|
+
for container in containers:
|
|
41
|
+
if container.state.running:
|
|
42
|
+
docker.kill(container)
|
|
43
|
+
logger.info(f"remove container {container.name}")
|
|
44
|
+
docker.remove(container)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
async def wait_and_run_container(docker, container, name: str):
|
|
48
|
+
logger.debug(f"start {name}")
|
|
49
|
+
try:
|
|
50
|
+
yield container
|
|
51
|
+
finally:
|
|
52
|
+
logger.debug(f"kill container {name} and remove the docker container")
|
|
53
|
+
if container.state.status == "running":
|
|
54
|
+
logger.debug(f"container {name} is running")
|
|
55
|
+
docker.kill(container.id)
|
|
56
|
+
logger.debug(f"killed container {name}")
|
|
57
|
+
else:
|
|
58
|
+
logger.debug(f"container {name} is not running")
|
|
59
|
+
|
|
60
|
+
# keep the container id, to keep the docker images
|
|
61
|
+
# docker has a garbage collector, when all docker container are deleted the
|
|
62
|
+
# image related is deleted
|
|
63
|
+
# docker.remove(container.id)
|
pydocks/postgresql.py
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
import pytest_asyncio
|
|
6
|
+
|
|
7
|
+
# from asgi_lifespan import LifespanManager
|
|
8
|
+
import asyncpg
|
|
9
|
+
import anyio
|
|
10
|
+
from python_on_whales import docker as libdocker
|
|
11
|
+
from reattempt import reattempt
|
|
12
|
+
import logging
|
|
13
|
+
import struct
|
|
14
|
+
from anyio.abc import SocketStream
|
|
15
|
+
import uuid
|
|
16
|
+
|
|
17
|
+
from pydocks.plugin import (
|
|
18
|
+
clean_containers,
|
|
19
|
+
socket_test_connection,
|
|
20
|
+
wait_and_run_container,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
logger = logging.getLogger("pydocks")
|
|
25
|
+
logger.addHandler(logging.NullHandler())
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
# https://hub.docker.com/_/postgres/tags
|
|
29
|
+
# TEST_POSTGRES_DOCKER_IMAGE: str = "docker.io/postgres:16.3"
|
|
30
|
+
TEST_POSTGRESQL_DOCKER_IMAGE: str = "docker.io/postgres:17rc1-alpine3.20"
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@pytest_asyncio.fixture(scope="session", loop_scope="session")
|
|
34
|
+
async def postgresql_clean_all_containers(docker):
|
|
35
|
+
container_name: str = "test-postgresql"
|
|
36
|
+
# clean before
|
|
37
|
+
|
|
38
|
+
await clean_containers(docker, container_name)
|
|
39
|
+
yield
|
|
40
|
+
# clean after
|
|
41
|
+
await clean_containers(docker, container_name)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@pytest.fixture(scope="function")
|
|
45
|
+
async def postgresql_container(docker: libdocker, mocker): # type: ignore
|
|
46
|
+
mocker.patch(
|
|
47
|
+
"logging.exception",
|
|
48
|
+
lambda *args, **kwargs: logger.warning(f"Exception raised {args}"),
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
container_name = f"test-postgresql-{uuid.uuid4()}"
|
|
52
|
+
# await clean_containers(docker, container_name)
|
|
53
|
+
|
|
54
|
+
async for container in setup_postgresql_container(docker, container_name):
|
|
55
|
+
yield container
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@pytest_asyncio.fixture(scope="session", loop_scope="session")
|
|
59
|
+
async def postgresql_container_session(docker: libdocker, session_mocker): # type: ignore
|
|
60
|
+
session_mocker.patch(
|
|
61
|
+
"logging.exception",
|
|
62
|
+
lambda *args, **kwargs: logger.warning(f"Exception raised {args}"),
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
await clean_containers(docker, "test-postgresql-session")
|
|
66
|
+
|
|
67
|
+
container_name = f"test-postgresql-session-{uuid.uuid4()}"
|
|
68
|
+
|
|
69
|
+
async for container in setup_postgresql_container(docker, container_name):
|
|
70
|
+
yield container
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
async def setup_postgresql_container(docker: libdocker, container_name): # type: ignore
|
|
74
|
+
postgresql_image = (
|
|
75
|
+
TEST_POSTGRESQL_DOCKER_IMAGE
|
|
76
|
+
if "TEST_POSTGRESQL_DOCKER_IMAGE" not in os.environ
|
|
77
|
+
else os.environ["TEST_POSTGRESQL_DOCKER_IMAGE"]
|
|
78
|
+
)
|
|
79
|
+
logger.debug(f"[PYDOCKS] Pull docker image : {postgresql_image}")
|
|
80
|
+
|
|
81
|
+
def run_container(container_name: str):
|
|
82
|
+
return docker.run(
|
|
83
|
+
image=postgresql_image,
|
|
84
|
+
name=container_name,
|
|
85
|
+
detach=True,
|
|
86
|
+
envs={
|
|
87
|
+
"POSTGRES_PASSWORD": "postgres",
|
|
88
|
+
},
|
|
89
|
+
publish=[(5433, 5432)],
|
|
90
|
+
expose=[5433],
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
# Select the container with the given name if exists, else create a new one
|
|
94
|
+
containers = docker.ps(all=True, filters={"name": f"^{container_name}$"})
|
|
95
|
+
if containers and len(containers) > 0:
|
|
96
|
+
container = containers[0] # type: ignore
|
|
97
|
+
logger.debug(f"[PYDOCKS] Found existing container: {container_name}")
|
|
98
|
+
else:
|
|
99
|
+
logger.debug(
|
|
100
|
+
f"[PYDOCKS] No existing container found, creating new one: {container_name}"
|
|
101
|
+
)
|
|
102
|
+
container = run_container(container_name)
|
|
103
|
+
|
|
104
|
+
await postgresql_test_connection(
|
|
105
|
+
host="127.0.0.1",
|
|
106
|
+
port=5433,
|
|
107
|
+
username="postgres",
|
|
108
|
+
password="postgres",
|
|
109
|
+
db_name="postgres",
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
async for instance in wait_and_run_container(docker, container, container_name):
|
|
113
|
+
yield instance
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
@reattempt(max_retries=30, min_time=0.1, max_time=0.5)
|
|
117
|
+
async def postgresql_test_connection(
|
|
118
|
+
host: str, port: int, username: str, password: str, db_name: str
|
|
119
|
+
):
|
|
120
|
+
await socket_test_connection(host, port)
|
|
121
|
+
|
|
122
|
+
stream: SocketStream = await anyio.connect_tcp(host, port)
|
|
123
|
+
|
|
124
|
+
# Send a startup packet
|
|
125
|
+
startup_packet = f"user=fake-user password=fake-password dbname=fake-db host={host} port={port}\x00".encode()
|
|
126
|
+
await stream.send(struct.pack(">I", len(startup_packet)))
|
|
127
|
+
await stream.send(b"\x00\x03\x00\x00") # frontend protocol version 3.0
|
|
128
|
+
await stream.send(startup_packet)
|
|
129
|
+
await stream.send(b"\x00") # terminator byte
|
|
130
|
+
|
|
131
|
+
await stream.receive()
|
|
132
|
+
logger.info("[PYDOCKS] Connection successful.")
|
|
133
|
+
await stream.aclose()
|
|
134
|
+
|
|
135
|
+
conn = await asyncpg.connect(
|
|
136
|
+
user=username, password=password, database=db_name, host=host, port=port
|
|
137
|
+
)
|
|
138
|
+
try:
|
|
139
|
+
# Execute the SELECT 1 query
|
|
140
|
+
result = await conn.fetchval("SELECT 1")
|
|
141
|
+
logger.info(f"[PYDOCKS] Query result: {result}")
|
|
142
|
+
finally:
|
|
143
|
+
await conn.close()
|
pydocks/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: pydocks
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Pytest fixures for running tests with Docker containers
|
|
5
|
+
Project-URL: homepage, https://github.com/sylvainmouquet/pydocks
|
|
6
|
+
Project-URL: documentation, https://github.com/sylvainmouquet/pydocks
|
|
7
|
+
Project-URL: repository, https://github.com/sylvainmouquet/ppydocks
|
|
8
|
+
Project-URL: changelog, https://github.com/sylvainmouquet/pydocks/releases
|
|
9
|
+
Author-email: Sylvain Mouquet <sylvain.mouquet@gmail.com>
|
|
10
|
+
License: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: containers,docker,pydocks,pytest,test
|
|
13
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
14
|
+
Classifier: Framework :: Pytest
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
17
|
+
Classifier: Programming Language :: Python
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
24
|
+
Requires-Python: >=3.9
|
|
25
|
+
Requires-Dist: asyncpg>=0.29.0
|
|
26
|
+
Requires-Dist: uvloop>=0.20.0
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# PyDocks
|
|
30
|
+
|
|
31
|
+
PyDocks is a group of pytest fixures for running tests with Docker containers
|
|
32
|
+
|
|
33
|
+
### Demonstration:
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
...
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Table of Contents
|
|
40
|
+
|
|
41
|
+
- [PyDocks](#PyDocks)
|
|
42
|
+
- [Table of Contents](#table-of-contents)
|
|
43
|
+
- [Description](#description)
|
|
44
|
+
- [Installation](#installation)
|
|
45
|
+
- [Usage](#usage)
|
|
46
|
+
- [License](#license)
|
|
47
|
+
- [Contact](#contact)
|
|
48
|
+
|
|
49
|
+
## Description
|
|
50
|
+
|
|
51
|
+
PyDocks is a Python library that provides a set of pytest fixtures for running tests with Docker containers. It simplifies the process of setting up, managing, and tearing down Docker containers during test execution.
|
|
52
|
+
|
|
53
|
+
Key features include:
|
|
54
|
+
- Easy integration with pytest
|
|
55
|
+
- Support for PostgreSQL containers
|
|
56
|
+
- Automatic container cleanup
|
|
57
|
+
- Configurable container settings
|
|
58
|
+
- Reusable session-scoped containers for improved test performance
|
|
59
|
+
|
|
60
|
+
PyDocks is designed to make testing with Docker containers more efficient and less error-prone, allowing developers to focus on writing tests rather than managing infrastructure.
|
|
61
|
+
|
|
62
|
+
## Installation
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
# Install the dependency
|
|
66
|
+
pip install pydocks
|
|
67
|
+
uv add pydocks
|
|
68
|
+
poetry add pydocks
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Usage
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from pydocks import pydocks
|
|
75
|
+
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
## License
|
|
80
|
+
|
|
81
|
+
PyDocks is released under the MIT License. See the [LICENSE](LICENSE) file for more details.
|
|
82
|
+
|
|
83
|
+
## Contact
|
|
84
|
+
|
|
85
|
+
For questions, suggestions, or issues related to ReAttempt, please open an issue on the GitHub repository.
|
|
86
|
+
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
pydocks/__init__.py,sha256=W6CvJFv97kKviM7P4V77BwQhbHtvXhq_52Ya-yzbgM0,379
|
|
2
|
+
pydocks/nginx.py,sha256=_cJaeUMG4IBBxf2M7D00fDIPQinlD2x6Gn3Mu3Acg2I,572
|
|
3
|
+
pydocks/plugin.py,sha256=7RPsJs15aqu0o2JPI0qhw87Om06PlTvnKKcx76MycYI,1969
|
|
4
|
+
pydocks/postgresql.py,sha256=o940-Snobfu5HyEs4QQ7jjfbXo4PCtCUfKDeVge0Bn8,4463
|
|
5
|
+
pydocks/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
6
|
+
pydocks-0.0.1.dist-info/METADATA,sha256=OUrktuk4zb0CZ50BAdZRdPWKRng-EsKuY_Ryd-LNcrE,2588
|
|
7
|
+
pydocks-0.0.1.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
|
|
8
|
+
pydocks-0.0.1.dist-info/entry_points.txt,sha256=xM0EPrcMDdbClZ8xZedU09BiL6dyq1B70eZuS2Cb_6M,29
|
|
9
|
+
pydocks-0.0.1.dist-info/licenses/LICENSE,sha256=apw6aefq1u6QAh-QZbpNI5ZDDfjM5Uv3hjDPAQsrsC0,1064
|
|
10
|
+
pydocks-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 PyDocks
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|