Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utility to run arbitrary commands on a container #93

Open
n1ngu opened this issue Jun 23, 2023 · 2 comments
Open

Utility to run arbitrary commands on a container #93

n1ngu opened this issue Jun 23, 2023 · 2 comments

Comments

@n1ngu
Copy link
Contributor

n1ngu commented Jun 23, 2023

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.

@n1ngu
Copy link
Contributor Author

n1ngu commented Jun 23, 2023

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!

@SRv6d
Copy link

SRv6d commented Sep 28, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants