lovelysystems/lovely-pytest-docker

"no container found" error with docker-compose 2.2

Opened this issue · 0 comments

Hello !

I've run into an issue, which I think may be caused by my very recent version of docker-compose (2.2.0).

Minimal reproduction

I created a project in /tmp/foo with following files:

tests/docker-compose.yml

version: "3"
services:
  elasticsearch:
    image: elasticsearch:7.14.2
    ports:
      - "9200"

tests/conftest.py

import pytest

@pytest.fixture(scope='session')
def docker_app(docker_services):
    docker_services.start('elasticsearch')
    public_port = docker_services.wait_for_service("elasticsearch", 9200)

tests/test_foo.py

def test_foo(docker_app):
    assert False

Result

pytest fails with the following message:

docker_services = <lovely.pytest.docker.compose.Services object at 0x7fed40b556d0>

    @pytest.fixture(scope='session')
    def docker_app(docker_services):
        docker_services.start('elasticsearch')
>       public_port = docker_services.wait_for_service("elasticsearch", 9200)

tests/conftest.py:6:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/home/remi/.local/share/virtualenvs/foo-r-OLon9H/lib/python3.9/site-packages/lovely/pytest/docker/compose.py:98: in wait_for_service
    public_port = self.port_for(service, private_port)
/home/remi/.local/share/virtualenvs/foo-r-OLon9H/lib/python3.9/site-packages/lovely/pytest/docker/compose.py:117: in port_for
    output = self._docker_compose.execute(
/home/remi/.local/share/virtualenvs/foo-r-OLon9H/lib/python3.9/site-packages/lovely/pytest/docker/compose.py:166: in execute
    return execute(command)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

command = ['docker-compose', '--project-directory', '/tmp/foo/tests', '-f', '/tmp/foo/tests/docker-compose.yml', '-p', ...], success_codes = (0,)

    def execute(command, success_codes=(0,)):
        """Run a shell command."""
        try:
            output = subprocess.check_output(
                command,
                stderr=subprocess.STDOUT,
                shell=False,
            )
            status = 0
        except subprocess.CalledProcessError as error:
            output = error.output or b''
            status = error.returncode
            command = error.cmd
        output = output.decode('utf-8')
        if status not in success_codes:
>           raise Exception(
                'Command %r returned %d: """%s""".' % (command, status, output)
            )
E           Exception: Command ['docker-compose', '--project-directory', '/tmp/foo/tests', '-f', '/tmp/foo/tests/docker-compose.yml', '-p', 'pytest/tmp/foo', 'port', 'elasticsearch', '9200'] returned 1: """no container found for elasticsearch_1
E           """.

The error seems to simply come from how the project name is defined (-p pytest/tmp/foo) as the slashes should be trimmed. The actual name of the created container is pytesttmpfoo-elasticsearch-1 so the project name should be pytesttmpfoo. The following commands exits normally:

$ docker-compose --project-directory /tmp/foo/tests -f /tmp/foo/tests/docker-compose.yml -p "pytest/tmp/foo" port elasticsearch 9200
no container found for elasticsearch_1

I'm not sure whether this unexpected behavior comes from docker-compose or this project, so I stick with reporting it in an issue even though it seems easy to fix here: https://github.com/lovelysystems/lovely-pytest-docker/blob/master/src/lovely/pytest/docker/compose.py#L210.