pytest-docker
pytest-docker copied to clipboard
Utility to run arbitrary commands on a container
Most projects need some kind of initialization. So far I am copy-pasting this kind of scripts all over the projects
import pytest
import textwrap
@pytest.fixture(scope="session")
def backend_init(docker_services):
def backend_run(command):
docker_services._docker_compose.execute(
"run --rm backend " + textwrap.dedent(command))
backend_run("python -m django migrate")
backend_run("python -m django configure")
backend_run("""\
bash <<EOF
set -exo pipefail
pip install pipenv
pipenv install --dev --deploy
python -m django load-fake-data
EOF
""")
return
This sort of works, but I wonder if it would be worth to have some kind of utility to address this kind of need in pytest-docker.
Also, using docker_services._docker_compose.execute
feels odd.
I realize I am writing this as a workaround on a docker_setup
fixture shortcoming: the docker executor is not available as a fixture and you can't yield multiple compose subcommands interleaved with some python instructions, only a single docker compose subcommand.
Ideally I'd like to be able to write something like
@pytest.fixture(scope="session")
def docker_setup(docker_services):
execute = docker_services._docker_compose.execute
execute("up -d service-a service-b")
docker_services.wait_until_responsive(check=is_service_a_ready)
docker_services.wait_until_responsive(check=is_service_b_ready)
execute(dedent("""\
run --rm service-c my-shell << EOF
something
very
complicated
EOF
"""))
execute("up -d service-c service-d")
but the docker_service
fixture depends on the docker_setup
fixture already!
This seems like a common use case and fairly trivial to implement. Is anyone working on this / are the maintainers open to MRs that would simplify this?