From 4d006304878ef4b3835f4f8a041b383f63ae5727 Mon Sep 17 00:00:00 2001 From: David Ankin Date: Mon, 17 Feb 2025 08:52:37 -0500 Subject: [PATCH 1/4] add compose page back in --- compose.rst | 4 ++++ conf.py | 1 + core/README.rst | 11 +++++++++++ index.rst | 1 + 4 files changed, 17 insertions(+) create mode 100644 compose.rst diff --git a/compose.rst b/compose.rst new file mode 100644 index 00000000..b85303bb --- /dev/null +++ b/compose.rst @@ -0,0 +1,4 @@ +Docker Compose +============== + +Docker compose is described in :ref:`Testcontainers Core`. diff --git a/conf.py b/conf.py index b310e939..1ce94d38 100644 --- a/conf.py +++ b/conf.py @@ -33,6 +33,7 @@ "sphinx.ext.doctest", "sphinx.ext.intersphinx", "sphinx.ext.napoleon", + "sphinx.ext.autosectionlabel", ] # Configure autodoc to avoid excessively long fully-qualified names. diff --git a/core/README.rst b/core/README.rst index 8cc9a278..3492d3f1 100644 --- a/core/README.rst +++ b/core/README.rst @@ -9,6 +9,17 @@ Testcontainers Core .. autoclass:: testcontainers.core.generic.DbContainer +.. raw:: html + +
+ +Compose +------- + +It is also possible to use Docker Compose functionality: + +.. autoclass:: testcontainers.compose.compose.DockerCompose + .. raw:: html
diff --git a/index.rst b/index.rst index 307f934c..e0e28855 100644 --- a/index.rst +++ b/index.rst @@ -16,6 +16,7 @@ testcontainers-python facilitates the use of Docker containers for functional an :maxdepth: 1 core/README + compose modules/index Getting Started From 659a3d939012812ed9ac59cfd2e2afb511e671b9 Mon Sep 17 00:00:00 2001 From: David Ankin Date: Mon, 17 Feb 2025 08:52:56 -0500 Subject: [PATCH 2/4] add compose documentation and at least list the methods in DockerContainer --- core/README.rst | 10 ++++++++-- core/testcontainers/compose/compose.py | 12 ++++++------ core/testcontainers/core/container.py | 6 ++++++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/core/README.rst b/core/README.rst index 3492d3f1..9ccb50fc 100644 --- a/core/README.rst +++ b/core/README.rst @@ -3,7 +3,12 @@ Testcontainers Core :code:`testcontainers-core` is the core functionality for spinning up Docker containers in test environments. -.. autoclass:: testcontainers.core.container.DockerContainer +.. automodule:: testcontainers.core.container + :members: + :undoc-members: + +.. automodule:: testcontainers.core.network + :members: .. autoclass:: testcontainers.core.image.DockerImage @@ -18,7 +23,8 @@ Compose It is also possible to use Docker Compose functionality: -.. autoclass:: testcontainers.compose.compose.DockerCompose +.. automodule:: testcontainers.compose.compose + :members: .. raw:: html diff --git a/core/testcontainers/compose/compose.py b/core/testcontainers/compose/compose.py index e8ce3745..9b847397 100644 --- a/core/testcontainers/compose/compose.py +++ b/core/testcontainers/compose/compose.py @@ -189,7 +189,7 @@ def docker_compose_command(self) -> list[str]: Returns command parts used for the docker compose commands Returns: - cmd: Docker compose command parts. + str: Docker compose command parts. """ return self.compose_command_property @@ -255,8 +255,8 @@ def get_logs(self, *services: str) -> tuple[str, str]: :param services: which services to get the logs for (or omit, for all) Returns: - stdout: Standard output stream. - stderr: Standard error stream. + str: stdout: Standard output stream. + str: stderr: Standard error stream. """ logs_cmd = [*self.compose_command_property, "logs", *services] @@ -362,9 +362,9 @@ def exec_in_container( :param command: the command to run in the container Returns: - stdout: Standard output stream. - stderr: Standard error stream. - exit_code: The command's exit code. + str: stdout: Standard output stream. + str: stderr: Standard error stream. + int: exit_code: The command's exit code. """ if not service_name: service_name = self.get_container().Service diff --git a/core/testcontainers/core/container.py b/core/testcontainers/core/container.py index f677182f..8948c72c 100644 --- a/core/testcontainers/core/container.py +++ b/core/testcontainers/core/container.py @@ -172,6 +172,9 @@ def get_wrapped_container(self) -> "Container": return self._container def get_docker_client(self) -> DockerClient: + """ + :meta private: + """ return self._docker def get_logs(self) -> tuple[bytes, bytes]: @@ -190,6 +193,9 @@ def _configure(self) -> None: class Reaper: + """ + :meta private: + """ _instance: "Optional[Reaper]" = None _container: Optional[DockerContainer] = None _socket: Optional[socket] = None From ce74358e51704153789cf2483e587c7a3722ec5c Mon Sep 17 00:00:00 2001 From: David Ankin Date: Mon, 17 Feb 2025 08:56:45 -0500 Subject: [PATCH 3/4] accidentally a type --- core/testcontainers/compose/compose.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/testcontainers/compose/compose.py b/core/testcontainers/compose/compose.py index 9b847397..c22b7f0f 100644 --- a/core/testcontainers/compose/compose.py +++ b/core/testcontainers/compose/compose.py @@ -189,7 +189,7 @@ def docker_compose_command(self) -> list[str]: Returns command parts used for the docker compose commands Returns: - str: Docker compose command parts. + list[str]: Docker compose command parts. """ return self.compose_command_property From ed7dc23506dc875482680b246fa764a2c37829b1 Mon Sep 17 00:00:00 2001 From: David Ankin Date: Sat, 22 Feb 2025 14:32:48 -0500 Subject: [PATCH 4/4] resolve doc tests? --- core/testcontainers/compose/compose.py | 6 +++--- core/testcontainers/core/container.py | 1 + core/tests/compose_fixtures/basic/hello.yaml | 8 ++++++++ 3 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 core/tests/compose_fixtures/basic/hello.yaml diff --git a/core/testcontainers/compose/compose.py b/core/testcontainers/compose/compose.py index c22b7f0f..d66dd566 100644 --- a/core/testcontainers/compose/compose.py +++ b/core/testcontainers/compose/compose.py @@ -148,11 +148,11 @@ class DockerCompose: >>> from testcontainers.compose import DockerCompose - >>> compose = DockerCompose("compose/tests", compose_file_name="docker-compose-4.yml", + >>> compose = DockerCompose("core/tests/compose_fixtures/basic", compose_file_name="hello.yaml", ... pull=True) >>> with compose: ... stdout, stderr = compose.get_logs() - >>> b"Hello from Docker!" in stdout + >>> "Hello from Docker!" in stdout True .. code-block:: yaml @@ -356,7 +356,7 @@ def exec_in_container( Args: service_name: Name of the docker compose service to run the command in. - command: Command to execute. + command: Command to execute. :param service_name: specify the service name :param command: the command to run in the container diff --git a/core/testcontainers/core/container.py b/core/testcontainers/core/container.py index 8948c72c..a13e3b9e 100644 --- a/core/testcontainers/core/container.py +++ b/core/testcontainers/core/container.py @@ -196,6 +196,7 @@ class Reaper: """ :meta private: """ + _instance: "Optional[Reaper]" = None _container: Optional[DockerContainer] = None _socket: Optional[socket] = None diff --git a/core/tests/compose_fixtures/basic/hello.yaml b/core/tests/compose_fixtures/basic/hello.yaml new file mode 100644 index 00000000..594a5110 --- /dev/null +++ b/core/tests/compose_fixtures/basic/hello.yaml @@ -0,0 +1,8 @@ +services: + hello: + image: alpine:latest + init: true + command: + - sh + - -c + - 'while true; do echo "Hello from Docker!"; sleep 1; done'