用 Python 和 Conu 测试容器 | Linux 中国
2018-11-26 14:19
791 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/F8qG7f9YD02Pe/article/details/84543353
Conu (container utilities 的简写) 是一个 Python 库,让你编写容器测试变得简单。-- Clément Verna
https://fedoramagazine.org/test-containers-python-conu/
作者 | Clément Verna
译者 | GraveAccent 🌟🌟共计翻译:5.0 篇 贡献时间:63 天
https://fedoramagazine.org/test-containers-python-conu/
作者 | Clément Verna
译者 | GraveAccent 🌟🌟共计翻译:5.0 篇 贡献时间:63 天
越来越多的开发人员使用容器开发和部署他们的应用。这意味着可以轻松地测试容器也变得很重要。Conu[1] (container utilities 的简写) 是一个 Python 库,让你编写容器测试变得简单。本文向你介绍如何使用它测试容器。开始吧首先,你需要一个容器程序来测试。为此,以下命令创建一个包含一个容器的 Dockerfile 和一个被容器伺服的 Flask 应用程序的文件夹。
- [p]
$ 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')
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"]
$ sudo dnf -y install docker
$ sudo systemctl start docker
$ sudo docker build . -t flaskapp_container
$ 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!
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[2] 库的包装器,可以方便地在测试期间发送 HTTP 请求。这个方法返回requests.Responseobject[3],因此可以轻松地访问响应的内容以进行测试。
conu还可以访问容器日志。又一次,这在测试期间非常有用。在上面的示例中,
container.logs方法返回容器日志。你可以使用它们断言打印了特定日志,或者,例如在测试期间没有异常被引发。
conu提供了许多与容器接合的有用方法。文档[4]中提供了完整的 API 列表。你还可以参考 GitHub[5] 上提供的示例。运行本教程所需的所有代码和文件也可以在 GitHub[6] 上获得。 对于想要进一步采用这个例子的读者,你可以看看使用 pytest[7] 来运行测试并构建一个容器测试套件。[/p]
via: https://fedoramagazine.org/test-containers-python-conu/作者:Clément Verna[9] 选题:lujun9972 译者:GraveAccent 校对:wxy本文由 LCTT 原创编译,Linux中国 荣誉推出
相关文章推荐
- linux下测试python程序执行时间
- Windows 上面搭建 Spark + Pycharm/idea scala/python 本地编写Spark程序,测试通过后再提交到Linux集群上
- Python 版的 Nmon 分析器:让你远离 excel 宏 | Linux 中国
- linux 下redis 安装,python 链接测试
- Linux上如何测试、运行python脚本
- linux VM与容器的存储IO性能测试
- Python编写的Linux网络设置脚本,Debian Wheezy上测试通过
- 用 Ansible Container 去管理 Linux 容器 | Linux 中国
- 使用 Python 的 urlliib.parse 库解析 URL | Linux 中国
- linux VM与容器的网络IO性能测试
- SPARTA:用于网络渗透测试的 GUI 工具套件 | Linux 中国
- 如何使用 cloud-init 来预配置 LXD 容器 | Linux 中国
- Linux配置Selenium+Chrome+Python实现自动化测试
- 使用 Python 在 Linux 上实现一键回归测试
- 用 Python 构建你自己的 RSS 提示系统 | Linux 中国
- 初识 Python:Hello World 和字符串操作 | Linux 中国
- 微服务和容器:需要去防范的 5 个“坑” | Linux 中国
- 什么是容器?为什么我们关注它? | Linux 中国
- python实现递归遍历目录(linux下测试通过)