Skip to main content

Spawn epehemeral containers in pytest

Project description

pytest-ephemeral-container

This is a bit of code to allow test suites (namely pytest) to spawn and destroy containers tied to fixtures.

This uses the Docker Python library, not the Docker CLI, and should be compatible with a wide variety of platforms and implementations.

Example

For example, to create an S3 based on localstack:

from pytest_ephemeral_container import spawn_container, discover_ports, wait_for_http


@pytest.fixture(scope="session")
def _localstack_s3():
    with spawn_container(
        image="docker.io/localstack/localstack:s3-latest",
        ports={"4566/tcp": None},
        environment={
            "DEBUG": "0",
        },
    ) as container:
        ip, port = next(discover_ports(container, "4566/tcp"))

        wait_for_http(ip, port)

        yield f"{ip}:{port}"

Reference

Jamie doesn't feel like doing a full docs site yet, so have quick reference.

There are features of the library not discussed here; read the source for more information.

def spawn_container(**props) -> ContextManager[Container]

Start a container, as a context manager. Cleans itself up.

props are the container properties passed to client.containers.run(). The image parameter is required, and you probably want to pass a ports (see discover_ports()). name is left to be autogenerated by Docker, and you probably want to keep that.

def discover_ports(container: Container, port: str) -> typing.Iterable[tuple[str, int]]

Docker has the option to automatically pick ports for you. This is very valuable in the context of ephemeral containers, since you can avoid conflicting with anything else the user is running.

This function will find the ports that Docker picked, and tell you about them.

In most cases, the first port given is fine, so you can wrap this in next().

def wait_for_http(addr: str, port: int, *, timeout: float = 30.0) -> None

Uses urllib to repeatedly make HTTP requests at the given port until success.

timeout is for the total wait time.

pytest fixture dockerclient ()

Gets the Docker client that is in use, in case you want to do anything advanced.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_ephemeral_container-0.1.1.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_ephemeral_container-0.1.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file pytest_ephemeral_container-0.1.1.tar.gz.

File metadata

  • Download URL: pytest_ephemeral_container-0.1.1.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.14.0 Linux/6.17.8-300.fc43.x86_64

File hashes

Hashes for pytest_ephemeral_container-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8e9ca275d045f81f7af92fabbca95fbb499af55d9923a68cff44451e3d1c2b79
MD5 85e90bd59c0737e39d2be4eed3a5ed19
BLAKE2b-256 810d62bc09ad225230794be5e25563b2156000b37d753a44b016a67c354deda4

See more details on using hashes here.

File details

Details for the file pytest_ephemeral_container-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_ephemeral_container-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3c2f4dd2996c2afa41e11c08e09ba3772625b79c4c5756e72c69ffc2150c95ba
MD5 9a0bb64ba3a9b2a33b01ce6bc43f3601
BLAKE2b-256 bb82fbb80c8c7969d4a75a04b64f3beb16a5322df0bc2df7a6accecead3be431

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page