<commit_before>import logging
import time

import attr
import pytest
from saltfactories.daemons.container import Container
from saltfactories.utils import random_string
from saltfactories.utils.ports import get_unused_localhost_port

docker = pytest.importorskip("docker")
docker_errors = pytest.importorskip("docker.errors")

log = logging.getLogger(__name__)


@attr.s(kw_only=True, slots=True)
class RabbitMQImage:
    name = attr.ib()
    tag = attr.ib()
    container_id = attr.ib()

    def __str__(self):
        return "{}:{}".format(self.name, self.tag)


@attr.s(kw_only=True, slots=True)
class RabbitMQCombo:
    rabbitmq_name = attr.ib()
    rabbitmq_version = attr.ib()
    rabbitmq_port = attr.ib()

    @rabbitmq_port.default
    def _rabbitmq_port(self):
        return get_unused_localhost_port()


def get_test_versions():
    test_versions = []
    name = "rabbitmq"
    for version in ("3.8",):
        test_versions.append(
            RabbitMQImage(
                name=name,
                tag=version,
                container_id=random_string("rabbitmq-{}-".format(version)),
            )
        )
    return test_versions


def get_test_version_id(value):
    return "container={}".format(value)


@pytest.fixture(scope="module", params=get_test_versions(), ids=get_test_version_id)
def rabbitmq_container(request, salt_factories, modules):

    try:
        docker_client = docker.from_env()
    except docker_errors.DockerException:
        pytest.skip("Failed to get a connection to docker running on the system")
    connectable = Container.client_connectable(docker_client)
    if connectable is not True:  # pragma: no cover
        pytest.skip(connectable)

    rabbitmq_image = request.param

    combo = RabbitMQCombo(
        rabbitmq_name=rabbitmq_image.name, rabbitmq_version=rabbitmq_image.tag,
    )
    container = salt_factories.get_container(
        rabbitmq_image.container_id,
        "{}:{}".format(combo.rabbitmq_name, combo.rabbitmq_version),
        docker_client=docker_client,
    )
    with container.started():
        # Sleep
        time.sleep(10)

        authenticated = False
        login_attempts = 6
        while login_attempts:
            login_attempts -= 1
            ret = container.run("rabbitmqctl ping")
            authenticated = ret.exitcode == 0
            if authenticated:
                break

            time.sleep(10)

        if authenticated:
            yield container
        else:
            pytest.fail(
                "Failed to connect to rabbitmq in container(id: {})".format(
                    rabbitmq_image.container_id
                )
            )


@pytest.fixture(scope="module")
def docker_cmd_run_all_wrapper(rabbitmq_container):
    def run_command(cmd, **kwargs):
        # Update rabbitmqctl location
        if cmd[0] is None:
            cmd[0] = "/opt/rabbitmq/sbin/rabbitmqctl"

        ret = rabbitmq_container.run(cmd)
        res = {"retcode": ret.exitcode, "stdout": ret.stdout, "stderr": ret.stderr}
        return res

    return run_command<commit_msg>Adding some additional debugging to see why container is not ready.<commit_after>import logging
import time

import attr
import pytest
from saltfactories.daemons.container import Container
from saltfactories.utils import random_string
from saltfactories.utils.ports import get_unused_localhost_port

docker = pytest.importorskip("docker")
docker_errors = pytest.importorskip("docker.errors")

log = logging.getLogger(__name__)


@attr.s(kw_only=True, slots=True)
class RabbitMQImage:
    name = attr.ib()
    tag = attr.ib()
    container_id = attr.ib()

    def __str__(self):
        return "{}:{}".format(self.name, self.tag)


@attr.s(kw_only=True, slots=True)
class RabbitMQCombo:
    rabbitmq_name = attr.ib()
    rabbitmq_version = attr.ib()
    rabbitmq_port = attr.ib()

    @rabbitmq_port.default
    def _rabbitmq_port(self):
        return get_unused_localhost_port()


def get_test_versions():
    test_versions = []
    name = "rabbitmq"
    for version in ("3.8",):
        test_versions.append(
            RabbitMQImage(
                name=name,
                tag=version,
                container_id=random_string("rabbitmq-{}-".format(version)),
            )
        )
    return test_versions


def get_test_version_id(value):
    return "container={}".format(value)


@pytest.fixture(scope="module", params=get_test_versions(), ids=get_test_version_id)
def rabbitmq_container(request, salt_factories, modules):

    try:
        docker_client = docker.from_env()
    except docker_errors.DockerException:
        pytest.skip("Failed to get a connection to docker running on the system")
    connectable = Container.client_connectable(docker_client)
    if connectable is not True:  # pragma: no cover
        pytest.skip(connectable)

    rabbitmq_image = request.param

    combo = RabbitMQCombo(
        rabbitmq_name=rabbitmq_image.name, rabbitmq_version=rabbitmq_image.tag,
    )
    container = salt_factories.get_container(
        rabbitmq_image.container_id,
        "{}:{}".format(combo.rabbitmq_name, combo.rabbitmq_version),
        docker_client=docker_client,
    )
    with container.started():
        # Sleep
        log.debug("=== initial sleep")
        time.sleep(10)

        authenticated = False
        login_attempts = 6
        while login_attempts:
            log.debug("=== login attempt %s ===", login_attempts)
            login_attempts -= 1
            ret = container.run("rabbitmqctl ping")
            log.debug("=== ret %s ===", ret)
            authenticated = ret.exitcode == 0
            if authenticated:
                break

            log.debug("=== sleep ===")
            time.sleep(10)

        if authenticated:
            yield container
        else:
            pytest.fail(
                "Failed to connect to rabbitmq in container(id: {})".format(
                    rabbitmq_image.container_id
                )
            )


@pytest.fixture(scope="module")
def docker_cmd_run_all_wrapper(rabbitmq_container):
    def run_command(cmd, **kwargs):
        # Update rabbitmqctl location
        if cmd[0] is None:
            cmd[0] = "/opt/rabbitmq/sbin/rabbitmqctl"

        ret = rabbitmq_container.run(cmd)
        res = {"retcode": ret.exitcode, "stdout": ret.stdout, "stderr": ret.stderr}
        return res

    return run_command
{'fraction_non_alphanumeric': 0.10182726846790567, 'fraction_numerical': 0.003435889426831173, 'mean_word_length': 2.6365701306076095, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1081705', 'n_tokens_mistral': 2036, 'n_tokens_neox': 1893, 'n_words': 397}