testcontainers / testcontainers/testcontainers-python

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

オープン
#407 コメント 8 件 リアクション 2 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

👀 requires attention 📦 package: kafka 🔀 requires triage
主要言語
Python
スター
2.3k
フォーク
386
平均マージ
4時間 40分
マージ済み PR(30日)
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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

提供されたKafkaContainerスニペットを、Testcontainers 3.6.0を使用してWindows 10上のpytestで実行し、その後、トレースバックに示されているDockerClient.portエントリポイントとTC_HOST設定を調査します。コンテナがポートをマッピングした状態で起動し、スクリプトとして実行した場合と同様に、pytestでKafkaプロデューサー/コンシューマーのアサーションが成功すれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
docker, kafka, python, vscode
領域
devops, networking, testing-qa
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
28/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。