gpt4 book ai didi

python - 当单元测试请求到内置Python的Docker容器时,连接中止

转载 作者:行者123 更新时间:2023-12-02 19:07:14 25 4
gpt4 key购买 nike

我的目标是编写一个构建docker容器并尝试执行对新建容器的请求的单元测试。

该测试首先构建一个docker镜像,然后运行该镜像,最后尝试执行对新创建的容器的请求。目前使用py.test运行测试给了我

requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))

而在ipython中运行相同的代码就可以了。

预期的行为: py.test获得成功的结果。

测试
import pytest
import docker
import requests

@pytest.fixture(scope='module')
def docker_env():
return docker.from_env()

@pytest.fixture(scope='module')
def docker_image(docker_env):
client = docker_env
image = client.images.build(path = ".", tag = "python-test:latest")
yield image, client
client.images.remove("python-test:latest")

def test_port(docker_image):
image, client = docker_image
client.containers.run("python-test:latest",
entrypoint="/opt/conda/envs/python-docker-test/bin/gunicorn --config /usr/src/app/gunicorn.conf.py app:api",
detach=True,
ports={'8000/tcp': ('127.0.0.1', 8000)})

resp = requests.get("http://127.0.0.1:8000/hello")
assert resp.status == 200
# Expect success, currently getting an error

Docker文件

我的docker文件如下所示,
FROM phusion/baseimage:0.9.22
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8

RUN apt-get update --fix-missing && apt-get install -y wget bzip2 ca-certificates \
libglib2.0-0 libxext6 libsm6 libxrender1 \
git mercurial subversion

RUN echo 'export PATH=/opt/conda/bin:$PATH' > /etc/profile.d/conda.sh && \
wget --quiet https://repo.continuum.io/miniconda/Miniconda3-4.3.30-Linux-x86_64.sh -O ~/miniconda.sh && \
/bin/bash ~/miniconda.sh -b -p /opt/conda && \
rm ~/miniconda.sh

ENV PATH /opt/conda/bin:$PATH

WORKDIR /usr/src/app
COPY . .

RUN conda env create -f environment.yml

EXPOSE 8000

SHELL ["/bin/bash", "-c"]

ENTRYPOINT source activate python-docker-test && /opt/conda/envs/python-docker-test/bin/gunicorn \
--config /usr/src/app/gunicorn.conf.py app:api

app.py
import falcon

class TestServer:
def on_get(self, req, resp):
resp.content_type = "text/text"

resp.body = "hello world"
resp.status = falcon.HTTP_OK

api = falcon.API()
api.add_route("/hello", TestServer())

环境文件
name: python-docker-test
channels:
- anaconda
- conda-forge
dependencies:
- python==3.6
- gunicorn==19.7.1
- pytest==3.2.5
- gunicorn==19.7.1
- ipython
- falcon==1.3.0
- pip==9.0.1
- pip:
- docker==2.7.0

Gunicorn.conf.py
bind = '0.0.0.0:8000'

系统规格
  • 服务器版本:17.12.0-ce
  • uname:17.3.0达尔文内核版本17.3.0
  • 最佳答案

    正确的测试看起来像

    def test_port(docker_image):
    image, client = docker_image
    import time
    container = client.containers.run("python-test:latest",
    detach=True,
    ports={'8000/tcp': ('127.0.0.1', 8000)})

    time.sleep(1)
    resp = requests.get("http://127.0.0.1:8000/hello")

    container.kill()
    assert resp.status_code == 200

    关于python - 当单元测试请求到内置Python的Docker容器时,连接中止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48514589/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com