Pytest container
A simple pytest plugin to test Container Images via pytest fixtures and testinfra.
This module provides a set of fixtures and helper functions to ease testing of container images leveraging testinfra. Assuming you want to automatically spin up a container for a test, then the container fixture will do exactly that (plus it will cleanup after itself):
LEAP = Container(url="registry.opensuse.org/opensuse/leap:latest")
@pytest.mark.parametrize("container", [LEAP], indirect=["container"])
def test_leap(container):
assert container.connection.file("/etc/os-release").exists
In the above example we created a :py:class:`pytest_container.container.Container` object which is just a container that is directly pulled from registry.opensuse.org. The container fixture then receives this container via pytest's parametrization and returns a :py:class:`pytest_container.container.ContainerData` to the test function. In the test function itself, we can leverage testinfra to run some basic tests inside the container itself, e.g. check whether files are there, packages are installed, etc.pp.
You can also customize the container to be used, e.g. build it from a
Containerfile
or specify an entry point:
BUSYBOX_WITH_ENTRYPOINT = Container(
url="registry.opensuse.org/opensuse/busybox:latest",
custom_entry_point="/bin/sh",
)
@pytest.mark.parametrize(
"container", [BUSYBOX_WITH_ENTRYPOINT], indirect=["container"]
)
def test_custom_entry_point(container):
container.connection.run_expect([0], "true")