pytest-xdocker 0.2.9__tar.gz → 0.3.0__tar.gz

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.
Files changed (59) hide show
  1. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/.github/actions/setup-uv-env/action.yml +1 -1
  2. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/.github/workflows/test.yml +2 -2
  3. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/CHANGES.rst +7 -0
  4. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/PKG-INFO +3 -3
  5. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/docs/pytest_xdocker.rst +8 -0
  6. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/pyproject.toml +2 -2
  7. pytest_xdocker-0.3.0/pytest_xdocker/compose.py +78 -0
  8. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/pytest_xdocker/docker.py +18 -5
  9. pytest_xdocker-0.3.0/tests/integration/test_compose.py +26 -0
  10. pytest_xdocker-0.3.0/tests/unit/test_compose.py +58 -0
  11. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/tests/unit/test_docker.py +42 -12
  12. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/.editorconfig +0 -0
  13. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/.gitattributes +0 -0
  14. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/.github/CODEOWNERS +0 -0
  15. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/.github/workflows/publish.yml +0 -0
  16. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/.gitignore +0 -0
  17. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/.venv.pth +0 -0
  18. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/.vscode/extensions.json +0 -0
  19. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/.vscode/launch.json +0 -0
  20. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/.vscode/settings.json +0 -0
  21. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/CONTRIBUTING.rst +0 -0
  22. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/LICENSE.rst +0 -0
  23. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/Makefile +0 -0
  24. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/README.rst +0 -0
  25. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/STYLE_GUIDE.rst +0 -0
  26. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/docs/changes.rst +0 -0
  27. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/docs/conf.py +0 -0
  28. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/docs/contributing.rst +0 -0
  29. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/docs/index.rst +0 -0
  30. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/docs/license.rst +0 -0
  31. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/docs/modules.rst +0 -0
  32. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/docs/style_guide.rst +0 -0
  33. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/environment.yml +0 -0
  34. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/poetry.toml +0 -0
  35. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/pytest_xdocker/__init__.py +0 -0
  36. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/pytest_xdocker/cache.py +0 -0
  37. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/pytest_xdocker/command.py +0 -0
  38. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/pytest_xdocker/fixtures.py +0 -0
  39. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/pytest_xdocker/lock.py +0 -0
  40. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/pytest_xdocker/network.py +0 -0
  41. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/pytest_xdocker/process.py +0 -0
  42. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/pytest_xdocker/retry.py +0 -0
  43. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/pytest_xdocker/validators.py +0 -0
  44. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/pytest_xdocker/xdocker.py +0 -0
  45. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/setup.py +0 -0
  46. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/tests/integration/__init__.py +0 -0
  47. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/tests/integration/test_process.py +0 -0
  48. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/tests/integration/test_xdocker.py +0 -0
  49. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/tests/unit/__init__.py +0 -0
  50. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/tests/unit/test_cache.py +0 -0
  51. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/tests/unit/test_command.py +0 -0
  52. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/tests/unit/test_fixtures.py +0 -0
  53. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/tests/unit/test_lock.py +0 -0
  54. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/tests/unit/test_network.py +0 -0
  55. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/tests/unit/test_process.py +0 -0
  56. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/tests/unit/test_retry.py +0 -0
  57. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/tests/unit/test_validators.py +0 -0
  58. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/tests/unit/test_xdocker.py +0 -0
  59. {pytest_xdocker-0.2.9 → pytest_xdocker-0.3.0}/uv.lock +0 -0
@@ -5,7 +5,7 @@ inputs:
5
5
  python-version:
6
6
  required: false
7
7
  description: "Python version to use"
8
- default: "3.10"
8
+ default: "3.11"
9
9
  uv-version:
10
10
  required: false
11
11
  description: "uv version to use (empty = use required-version or latest)"
@@ -26,7 +26,7 @@ jobs:
26
26
  strategy:
27
27
  fail-fast: false
28
28
  matrix:
29
- python-version: ['3.10', '3.11', '3.12', '3.13', '3.14']
29
+ python-version: ['3.11', '3.12', '3.13', '3.14', '3.15']
30
30
  steps:
31
31
  - name: Check out
32
32
  uses: actions/checkout@v6
@@ -59,7 +59,7 @@ jobs:
59
59
  - name: Set up the environment
60
60
  uses: ./.github/actions/setup-uv-env
61
61
  with:
62
- python-version: "3.14"
62
+ python-version: "3.15"
63
63
  extras: test
64
64
 
65
65
  - name: Download coverage data
@@ -1,3 +1,10 @@
1
+ Version 0.3.0
2
+ -------------
3
+
4
+ - Add ComposeServer and find_compose_files.
5
+ - Replace DockerContainer.exposed_ip with DockerContainer.ip.
6
+ - Deprecate Python 3.10, add Python 3.15.
7
+
1
8
  Version 0.2.9
2
9
  -------------
3
10
 
@@ -1,10 +1,10 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.5
2
2
  Name: pytest-xdocker
3
- Version: 0.2.9
3
+ Version: 0.3.0
4
4
  Summary: Pytest fixture to run docker across test runs.
5
5
  Author-email: Marc Tardif <marc@interunion.ca>
6
6
  License-File: LICENSE.rst
7
- Requires-Python: <4.0,>=3.10
7
+ Requires-Python: <4.0,>=3.11
8
8
  Requires-Dist: attrs>=25.3.0
9
9
  Requires-Dist: netifaces2>=0.0.22
10
10
  Requires-Dist: psutil>=6.1.1
@@ -20,6 +20,14 @@ pytest\_xdocker.command module
20
20
  :undoc-members:
21
21
  :show-inheritance:
22
22
 
23
+ pytest\_xdocker.compose module
24
+ ------------------------------
25
+
26
+ .. automodule:: pytest_xdocker.compose
27
+ :members:
28
+ :undoc-members:
29
+ :show-inheritance:
30
+
23
31
  pytest\_xdocker.docker module
24
32
  -----------------------------
25
33
 
@@ -7,7 +7,7 @@ authors = [
7
7
  ]
8
8
  readme = "README.rst"
9
9
  repository = "https://github.com/cr3/pytest-xdocker"
10
- requires-python = ">=3.10,<4.0"
10
+ requires-python = ">=3.11,<4.0"
11
11
  dependencies = [
12
12
  "attrs>=25.3.0",
13
13
  "netifaces2>=0.0.22",
@@ -53,7 +53,7 @@ build-backend = "hatchling.build"
53
53
  source = "vcs"
54
54
 
55
55
  [tool.ruff]
56
- target-version = "py310"
56
+ target-version = "py311"
57
57
  line-length = 120
58
58
  fix = true
59
59
  lint.select = [
@@ -0,0 +1,78 @@
1
+ """Docker compose server."""
2
+
3
+ from contextlib import contextmanager
4
+ from pathlib import Path
5
+
6
+ from pytest_xdocker.docker import DockerContainer
7
+ from pytest_xdocker.process import ProcessData, ProcessServer
8
+ from pytest_xdocker.xdocker import xdocker
9
+
10
+ COMPOSE_FILENAMES = ["docker-compose.yml", "compose.yaml", "compose.yml"]
11
+
12
+
13
+ def find_compose_files(directory):
14
+ """Find the compose file and its overrides, looking up from directory.
15
+
16
+ :param directory: Directory where to start looking up.
17
+ :return: Compose files ordered so overrides come after the base file,
18
+ ie compose.yml then compose.override.yml.
19
+ """
20
+ directory = Path(directory)
21
+ for parent in [directory, *directory.parents]:
22
+ for filename in COMPOSE_FILENAMES:
23
+ path = parent / filename
24
+ if path.exists():
25
+ return sorted(parent.glob(f"{path.stem}.*"), key=lambda p: len(p.name))
26
+
27
+ raise FileNotFoundError(f"Docker compose file not found from {directory}")
28
+
29
+
30
+ class ComposeServer(ProcessServer):
31
+ """Server for a docker compose service.
32
+
33
+ :param pattern: Pattern in the service output when it is ready.
34
+ :param project: Compose project name.
35
+ :param env_file: Optional compose env file.
36
+ :param compose_files: Optional compose files, see `find_compose_files`.
37
+ :param timeout: Seconds to wait for the pattern.
38
+ """
39
+
40
+ def __init__(self, pattern, project="test", env_file=None, compose_files=None, timeout=180, **kwargs):
41
+ """Init."""
42
+ super().__init__(**kwargs)
43
+ self.pattern = pattern
44
+ self.project = project
45
+ self.env_file = env_file
46
+ self.compose_files = compose_files
47
+ self.timeout = timeout
48
+
49
+ def __repr__(self):
50
+ return f"{self.__class__.__name__}(pattern={self.pattern!r}, project={self.project!r})"
51
+
52
+ def full_name(self, name):
53
+ """Return the container name of the service."""
54
+ return f"{self.project}-{name}-1"
55
+
56
+ def prepare_func(self, controldir):
57
+ """Prepare the function to run the compose service."""
58
+ compose = xdocker.compose().with_project_name(self.project)
59
+ if self.env_file:
60
+ compose = compose.with_env_file(self.env_file)
61
+ for file in self.compose_files or []:
62
+ compose = compose.with_file(file)
63
+
64
+ command = (
65
+ compose.run(controldir.basename)
66
+ .with_name(self.full_name(controldir.basename))
67
+ .with_build()
68
+ .with_remove()
69
+ .with_optionals("--use-aliases")
70
+ )
71
+
72
+ return ProcessData(self.pattern, command, timeout=self.timeout)
73
+
74
+ @contextmanager
75
+ def run(self, name, restart=None):
76
+ """Run the compose service and yield its `DockerContainer`."""
77
+ with super().run(name, restart):
78
+ yield DockerContainer(self.full_name(name), network=f"{self.project}_default")
@@ -330,13 +330,19 @@ class DockerRemoveCommand(Command):
330
330
  class DockerContainer:
331
331
  """Manager a docker container."""
332
332
 
333
- def __init__(self, name, inspect=None):
334
- """Inint."""
333
+ def __init__(self, name, inspect=None, network=None):
334
+ """Inint.
335
+
336
+ :param name: Container name.
337
+ :param inspect: Optional `DockerInspect`, defaults to inspecting name.
338
+ :param network: Optional network name used to resolve the IP.
339
+ """
335
340
  if inspect is None:
336
341
  inspect = DockerInspect(name)
337
342
 
338
343
  self.name = name
339
344
  self.inspect = inspect
345
+ self.network = network
340
346
 
341
347
  def _port_to_int(self, port):
342
348
  pattern = r"(?P<port>\d+)/tcp"
@@ -358,9 +364,16 @@ class DockerContainer:
358
364
  return {}
359
365
 
360
366
  @property
361
- def exposed_ip(self):
362
- """Return the IP exposed to other containers."""
363
- return self.inspect.get("NetworkSettings", "IPAddress")
367
+ def ip(self):
368
+ """Return the IP on the network, or on the only network."""
369
+ networks = self.inspect.get("NetworkSettings", "Networks") or {}
370
+ if self.network in networks:
371
+ return networks[self.network]["IPAddress"]
372
+
373
+ if len(networks) != 1:
374
+ raise AssertionError(f"Expecting network {self.network!r} or one network, got {list(networks)}")
375
+
376
+ return next(iter(networks.values()))["IPAddress"]
364
377
 
365
378
  @property
366
379
  def exposed_port(self):
@@ -0,0 +1,26 @@
1
+ """Integration tests for the compose module."""
2
+
3
+ from pytest_xdocker.cache import NullCache
4
+ from pytest_xdocker.compose import ComposeServer, find_compose_files
5
+ from pytest_xdocker.process import Process, ProcessConfig
6
+
7
+
8
+ def test_compose_server_run(tmp_path, unique):
9
+ """Running a compose server should yield the running container."""
10
+ (tmp_path / "compose.yml").write_text(
11
+ "services:\n"
12
+ " app:\n"
13
+ " image: alpine:3.21\n"
14
+ " command: sh -c 'echo ready; sleep 600'\n"
15
+ )
16
+ process = Process(config=ProcessConfig(cache=NullCache()))
17
+ server = ComposeServer(
18
+ "ready",
19
+ project=unique("text"),
20
+ compose_files=find_compose_files(tmp_path),
21
+ process=process,
22
+ )
23
+
24
+ with server.run("app") as container:
25
+ assert container.isrunning
26
+ assert container.ip
@@ -0,0 +1,58 @@
1
+ """Unit tests for the compose module."""
2
+
3
+ from unittest.mock import Mock
4
+
5
+ import pytest
6
+
7
+ from pytest_xdocker.compose import ComposeServer, find_compose_files
8
+ from pytest_xdocker.xdocker import xdocker
9
+
10
+
11
+ def test_find_compose_files_overrides(tmp_path):
12
+ """Finding compose files should order overrides after the base file."""
13
+ for filename in ["compose.override.yml", "compose.yml", "other.yml"]:
14
+ (tmp_path / filename).touch()
15
+
16
+ result = find_compose_files(tmp_path)
17
+
18
+ assert result == [tmp_path / "compose.yml", tmp_path / "compose.override.yml"]
19
+
20
+
21
+ def test_find_compose_files_parent(tmp_path):
22
+ """Finding compose files should look up in parent directories."""
23
+ (tmp_path / "docker-compose.yml").touch()
24
+ child = tmp_path / "a" / "b"
25
+ child.mkdir(parents=True)
26
+
27
+ result = find_compose_files(child)
28
+
29
+ assert result == [tmp_path / "docker-compose.yml"]
30
+
31
+
32
+ def test_find_compose_files_not_found(tmp_path):
33
+ """Finding compose files should raise when there are none."""
34
+ with pytest.raises(FileNotFoundError):
35
+ find_compose_files(tmp_path)
36
+
37
+
38
+ def test_compose_server_prepare_func():
39
+ """Preparing a compose server should run the service by full name."""
40
+ server = ComposeServer("ready", project="p", env_file="env", compose_files=["a.yml", "b.yml"], timeout=1)
41
+
42
+ result = server.prepare_func(Mock(basename="svc"))
43
+
44
+ assert list(result.args) == [
45
+ *xdocker.compose(),
46
+ "--project-name", "p",
47
+ "--env-file", "env",
48
+ "--file", "a.yml",
49
+ "--file", "b.yml",
50
+ "run",
51
+ "--name", "p-svc-1",
52
+ "--build",
53
+ "--rm",
54
+ "--use-aliases",
55
+ "svc",
56
+ ]
57
+ assert result.pattern == "ready"
58
+ assert result.timeout == 1
@@ -344,19 +344,49 @@ def test_container_env():
344
344
  assert container.env["foo"] == "bar"
345
345
 
346
346
 
347
- def test_container_exposed_ip():
348
- """A container exposed IP should return NetworkSettings/IPaddress."""
349
- ip = "1.2.3.4"
350
- inspect = DockerInspect(
351
- "name",
352
- {
353
- "NetworkSettings": {
354
- "IPAddress": ip,
355
- },
356
- },
357
- )
347
+ @pytest.mark.parametrize(
348
+ "network, networks, expected",
349
+ [
350
+ pytest.param(
351
+ "b",
352
+ {"a": {"IPAddress": "1.1.1.1"}, "b": {"IPAddress": "2.2.2.2"}},
353
+ "2.2.2.2",
354
+ id="named network",
355
+ ),
356
+ pytest.param(
357
+ None,
358
+ {"a": {"IPAddress": "1.1.1.1"}},
359
+ "1.1.1.1",
360
+ id="only network",
361
+ ),
362
+ pytest.param(
363
+ "missing",
364
+ {"a": {"IPAddress": "1.1.1.1"}},
365
+ "1.1.1.1",
366
+ id="missing network falls back to only network",
367
+ ),
368
+ ],
369
+ )
370
+ def test_container_ip(network, networks, expected):
371
+ """A container IP should come from the named network or the only one."""
372
+ inspect = DockerInspect("name", {"NetworkSettings": {"Networks": networks}})
373
+ container = DockerContainer("name", inspect, network=network)
374
+ assert container.ip == expected
375
+
376
+
377
+ @pytest.mark.parametrize(
378
+ "networks",
379
+ [
380
+ pytest.param({}, id="no network"),
381
+ pytest.param({"a": {"IPAddress": "1.1.1.1"}, "b": {"IPAddress": "2.2.2.2"}}, id="many networks"),
382
+ ],
383
+ )
384
+ def test_container_ip_error(networks):
385
+ """A container IP should raise without a single network to choose."""
386
+ inspect = DockerInspect("name", {"NetworkSettings": {"Networks": networks}})
358
387
  container = DockerContainer("name", inspect)
359
- assert container.exposed_ip == ip
388
+ with pytest.raises(AssertionError):
389
+ container.ip # noqa: B018
360
390
 
361
391
 
362
392
  def test_container_exposed_port():
File without changes
File without changes
File without changes
File without changes