/pytest_container

Collection of pytest helpers and fixtures to test container images

Primary LanguagePythonGNU Lesser General Public License v2.1LGPL-2.1

Pytest container

https://codecov.io/gh/dcermak/pytest_container/branch/main/graph/badge.svg?token=D16Q2PGL67 https://app.fossa.com/api/projects/git%2Bgithub.com%2Fdcermak%2Fpytest_container.svg?type=shield PyPI

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")