用 Python 和 Conu 测试容器

发布时间:2024-05-17 点击:86
越来越多的开发人员使用容器开发和部署他们的应用。这意味着可以轻松地测试容器也变得很重要。conu (container utilities 的简写) 是一个 python 库,让你编写容器测试变得简单。本文向你介绍如何使用它测试容器。
开始吧
首先,你需要一个容器程序来测试。为此,以下命令创建一个包含一个容器的 dockerfile 和一个被容器伺服的 flask 应用程序的文件夹。
$ mkdir container_test $ cd container_test $ touch dockerfile $ touch app.py将以下代码复制到 app.py 文件中。这是惯常的基本 flask 应用,它返回字符串 “hello container world!”。
from flask import flask app = flask(__name__) @app.route('/') def hello_world(): return 'hello container world!' if __name__ == '__main__': app.run(debug=true,host='0.0.0.0')创建和构建测试容器
为了构建测试容器,将以下指令添加到 dockerfile。
from registry.fedoraproject.org/fedora-minimal:latest run microdnf -y install python3-flask && microdnf clean all add ./app.py /srv cmd [python3, /srv/app.py]然后使用 docker cli 工具构建容器。
$ sudo dnf -y install docker $ sudo systemctl start docker $ sudo docker build . -t flaskapp_container提示:只有在系统上未安装 docker 时才需要前两个命令。
构建之后使用以下命令运行容器。
$ sudo docker run -p 5000:5000 --rm flaskapp_container * running on http://0.0.0.0:5000/ (press ctrl+c to quit) * restarting with stat * debugger is active! * debugger pin: 473-505-51最后,使用 curl 检查 flask 应用程序是否在容器内正确运行:
$ curl http://127.0.0.1:5000 hello container world!现在,flaskapp_container 正在运行并准备好进行测试,你可以使用 ctrl+c 将其停止。
创建测试脚本
在编写测试脚本之前,必须安装 conu。在先前创建的 container_test 目录中,运行以下命令。
$ python3 -m venv .venv $ source .venv/bin/activate (.venv)$ pip install --upgrade pip (.venv)$ pip install conu $ touch test_container.py然后将以下脚本复制并保存在 test_container.py 文件中。
import conu port = 5000 with conu.dockerbackend() as backend: image = backend.imageclass(flaskapp_container) options = [-p, 5000:5000] container = image.run_via_binary(additional_opts=options) try: # check that the container is running and wait for the flask application to start. assert container.is_running() container.wait_for_port(port) # run a get request on / port 5000. http_response = container.http_request(path=/, port=port) # check the response status code is 200 assert http_response.ok # get the response content response_content = http_response.content.decode(utf-8) # check that the hello container world! string is served. assert hello container world! in response_content # get the logs from the container logs = [line for line in container.logs()] # check the the flask application saw the get request. assert b'get / http/1.1 200 -' in logs[-1] finally: container.stop() container.delete()测试设置
这个脚本首先设置 conu 使用 docker 作为后端来运行容器。然后它设置容器镜像以使用你在本教程第一部分中构建的 flaskapp_container。
下一步是配置运行容器所需的选项。在此示例中,flask 应用在端口5000上提供内容。于是你需要暴露此端口并将其映射到主机上的同一端口。
最后,用这个脚本启动容器,现在可以测试了。
测试方法
在测试容器之前,检查容器是否正在运行并准备就绪。示范脚本使用 container.is_running 和 container.wait_for_port。这些方法可确保容器正在运行,并且服务在预设端口上可用。
container.http_request 是 request 库的包装器,可以方便地在测试期间发送 http 请求。这个方法返回requests.responseobject,因此可以轻松地访问响应的内容以进行测试。
conu 还可以访问容器日志。又一次,这在测试期间非常有用。在上面的示例中,container.logs 方法返回容器日志。你可以使用它们断言打印了特定日志,或者,例如在测试期间没有异常被引发。
conu 提供了许多与容器接合的有用方法。文档中提供了完整的 api 列表。你还可以参考 github 上提供的示例。
运行本教程所需的所有代码和文件也可以在 github 上获得。 对于想要进一步采用这个例子的读者,你可以看看使用 pytest 来运行测试并构建一个容器测试套件。
via: https://fedoramagazine.org/test-containers-python-conu/
作者:clément verna 选题:lujun9972 译者:graveaccent 校对:wxy


谷歌云服务器如何建站点信息
ecs云服务器显示数据不足
“新基建”新契机,数据中心如何加速奔跑?
我的域名提交备案的时候-备案平台
Wi-Fi信号差怎么办?
win2008阿里云服务器怎么建站
重装系统后远程桌面链接不了了
机械磁盘读写特别慢怎么办?加快机械磁盘读写速度的方法