Пример #1
0
def mysql_container(request, salt_factories, salt_call_cli):

    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)

    mysql_image = request.param

    mysql_user = "******"
    mysql_passwd = "password"

    combo = MySQLCombo(
        mysql_name=mysql_image.name,
        mysql_version=mysql_image.tag,
        mysql_user=mysql_user,
        mysql_passwd=mysql_passwd,
    )
    container = salt_factories.get_container(
        mysql_image.container_id,
        "{}:{}".format(combo.mysql_name, combo.mysql_version),
        docker_client=docker_client,
        check_ports=[combo.mysql_port],
        container_run_kwargs={
            "ports": {"3306/tcp": combo.mysql_port},
            "environment": {
                "MYSQL_ROOT_PASSWORD": mysql_passwd,
                "MYSQL_ROOT_HOST": "%",
            },
        },
    )
    with container.started():
        authenticated = False
        login_attempts = 6
        while login_attempts:
            login_attempts -= 1
            # Make sure "MYSQL" is ready
            ret = salt_call_cli.run(
                "docker.run",
                name=mysql_image.container_id,
                cmd="mysql --user=root --password=password -e 'SELECT 1'",
            )
            authenticated = ret.exitcode == 0
            if authenticated:
                break

            time.sleep(2)

        if authenticated:
            yield combo
        else:
            pytest.fail(
                "Failed to login into mysql server running in container(id: {})".format(
                    mysql_image.container_id
                )
            )
Пример #2
0
def _connectable_docker_client():
    try:
        client = docker.from_env()
        connectable = Container.client_connectable(client)
        if not connectable:
            pytest.skip(connectable)
    except docker.errors.DockerException as exc:
        pytest.skip("Failed to instantiate a docker client: {}".format(exc))
Пример #3
0
def docker_client():
    try:
        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(client)
    if connectable is not True:  # pragma: nocover
        pytest.skip(connectable)
    return client
def test_missing_docker_library():
    with mock.patch(
        "saltfactories.daemons.container.HAS_DOCKER",
        new_callable=mock.PropertyMock(return_value=False),
    ):
        with pytest.raises(RuntimeError) as exc:
            Container(name="foo", image="bar")

        assert str(exc.value) == "The docker python library was not found installed"
Пример #5
0
def docker_client():
    if salt.utils.path.which("docker") is None:
        pytest.skip("The docker binary is not available")
    try:
        client = docker.from_env()
        connectable = Container.client_connectable(client)
        if connectable is not True:  # pragma: no cover
            pytest.skip(connectable)
        return client
    except DockerException:
        pytest.skip(
            "Failed to get a connection to docker running on the system")
Пример #6
0
def test_missing_requests_library():
    with mock.patch(
            "saltfactories.daemons.container.HAS_DOCKER",
            new_callable=mock.PropertyMock(return_value=True),
    ), mock.patch(
            "saltfactories.daemons.container.HAS_REQUESTS",
            new_callable=mock.PropertyMock(return_value=False),
    ):
        with pytest.raises(pytest.fail.Exception) as exc:
            Container(name="foo", image="bar")

        assert str(
            exc.value) == "The requests python library was not found installed"
Пример #7
0
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 status --formatter=json")
            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))