testcontainers / testcontainers/testcontainers-python

Testcontainers work as expected, fail to map Docker port when run with pytest in VS Code

未关闭
#407 8 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

👀 requires attention 📦 package: kafka 🔀 requires triage
主要语言
Python
星标
2.3k
派生
386
平均合并
4 小时 40 分钟
30 天内合并 PR
1

描述

Describe the bug
I copied this code from the python-kafka tests in this repo.
I am able to run it successfully as a script. However, when I run in pytest, the docker container doesn't even start because it can't map to a port.

This behavior only happens when I run pytest.

The error code is as follows:

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <testcontainers.core.docker_client.DockerClient object at 0x0000016333B4DE90>
container_id = '0100dda1fb4811efbdf5743faf5208c2113f7952824e04799df9c3b497bd9864'
port = 9093

    def port(self, container_id, port):
        port_mappings = self.client.api.port(container_id, port)
        if not port_mappings:
>           raise RuntimeError(f'port mapping for container {container_id} and port {port} is not '
                               'available')
E           RuntimeError: port mapping for container 0100dda1fb4811efbdf5743faf5208c2113f7952824e04799df9c3b497bd9864 and port 9093 is not available

I believe this is a windows issue as I had to add os.environ['TC_HOST'] = 'localhost' because Windows has issues connecting to the localnpipe named pipe.

To Reproduce

from kafka import KafkaConsumer, KafkaProducer, TopicPartition
from testcontainers.kafka import KafkaContainer
import os

def test_kafka_confluent_7_1_3():
    kafka_container = KafkaContainer("confluentinc/cp-kafka:7.1.3")
    # set the advertised listeners to be the container's IP address
    with kafka_container as container:
        container.get_bootstrap_server()
        produce_and_consume_kafka_message(container)

def produce_and_consume_kafka_message(container):
    topic = 'test-topic'
    bootstrap_server = container.get_bootstrap_server()

    producer = KafkaProducer(bootstrap_servers=[bootstrap_server])
    producer.send(topic, b"verification message")
    producer.flush()
    producer.close()

    consumer = KafkaConsumer(bootstrap_servers=[bootstrap_server])
    tp = TopicPartition(topic, 0)
    consumer.assign([tp])
    consumer.seek_to_beginning()
    assert consumer.end_offsets([tp])[tp] == 1, \
        "Expected exactly one test message to be present on test topic !"

if __name__ == '__main__':
    test_kafka_confluent_7_1_3()

Runtime environment

Windows 10
Testcontainers 3.6.0
kafka-python 2.0.2

I am running the snippet above in pipenv, so it is not Docker in Docker.

Will update if I find a solution.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

在 Windows 10 上使用 Testcontainers 3.6.0,通过 pytest 运行提供的 KafkaContainer 代码片段,然后检查 traceback 中显示的 DockerClient.port 入口点和 TC_HOST 设置。完成的标准是容器启动时其端口已完成映射,并且 Kafka 生产者/消费者断言在 pytest 下通过,就像作为脚本运行时一样。

由索引模型根据 Issue 内容生成。

评估

技术栈
docker, kafka, python, vscode
领域
devops, networking, testing-qa
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
需要澄清
新手友好度
28/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。