自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

用Python和Conu測試容器

開發(fā) 后端
越來越多的開發(fā)人員使用容器開發(fā)和部署他們的應(yīng)用。這意味著可以輕松地測試容器也變得很重要。Conu (container utilities 的簡寫) 是一個 Python 庫,讓你編寫容器測試變得簡單。本文向你介紹如何使用它測試容器。

[[250679]]

越來越多的開發(fā)人員使用容器開發(fā)和部署他們的應(yīng)用。這意味著可以輕松地測試容器也變得很重要。Conu (container utilities 的簡寫) 是一個 Python 庫,讓你編寫容器測試變得簡單。本文向你介紹如何使用它測試容器。

開始吧

首先,你需要一個容器程序來測試。為此,以下命令創(chuàng)建一個包含一個容器的 Dockerfile 和一個被容器伺服的 Flask 應(yīng)用程序的文件夾。

  1. $ mkdir container_test
  2. $ cd container_test
  3. $ touch Dockerfile
  4. $ touch app.py

將以下代碼復(fù)制到 app.py 文件中。這是慣常的基本 Flask 應(yīng)用,它返回字符串 “Hello Container World!”。

  1. from flask import Flask
  2. app = Flask(__name__)
  3.  
  4. @app.route('/')
  5. def hello_world():
  6. return 'Hello Container World!'
  7.  
  8. if __name__ == '__main__':
  9. app.run(debug=True,host='0.0.0.0')

創(chuàng)建和構(gòu)建測試容器

為了構(gòu)建測試容器,將以下指令添加到 Dockerfile。

  1. FROM registry.fedoraproject.org/fedora-minimal:latest
  2. RUN microdnf -y install python3-flask && microdnf clean all
  3. ADD ./app.py /srv
  4. CMD ["python3", "/srv/app.py"]

然后使用 Docker CLI 工具構(gòu)建容器。

  1. $ sudo dnf -y install docker
  2. $ sudo systemctl start docker
  3. $ sudo docker build . -t flaskapp_container

提示:只有在系統(tǒng)上未安裝 Docker 時才需要前兩個命令。

構(gòu)建之后使用以下命令運行容器。

  1. $ sudo docker run -p 5000:5000 --rm flaskapp_container
  2. * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
  3. * Restarting with stat
  4. * Debugger is active!
  5. * Debugger PIN: 473-505-51

***,使用 curl 檢查 Flask 應(yīng)用程序是否在容器內(nèi)正確運行:

  1. $ curl http://127.0.0.1:5000
  2. Hello Container World!

現(xiàn)在,flaskapp_container 正在運行并準(zhǔn)備好進(jìn)行測試,你可以使用 Ctrl+C 將其停止。

創(chuàng)建測試腳本

在編寫測試腳本之前,必須安裝 conu。在先前創(chuàng)建的 container_test 目錄中,運行以下命令。

  1. $ python3 -m venv .venv
  2. $ source .venv/bin/activate
  3. (.venv)$ pip install --upgrade pip
  4. (.venv)$ pip install conu
  5. $ touch test_container.py

然后將以下腳本復(fù)制并保存在 test_container.py 文件中。

  1. import conu
  2.  
  3. PORT = 5000
  4.  
  5. with conu.DockerBackend() as backend:
  6. image = backend.ImageClass("flaskapp_container")
  7. options = ["-p", "5000:5000"]
  8. container = image.run_via_binary(additional_opts=options)
  9. try:
  10. # Check that the container is running and wait for the flask application to start.
  11. assert container.is_running()
  12. container.wait_for_port(PORT)
  13. # Run a GET request on / port 5000.
  14. http_response = container.http_request(path="/", port=PORT)
  15. # Check the response status code is 200
  16. assert http_response.ok
  17. # Get the response content
  18. response_content = http_response.content.decode("utf-8")
  19.  
  20. # Check that the "Hello Container World!" string is served.
  21. assert "Hello Container World!" in response_content
  22.  
  23. # Get the logs from the container
  24. logs = [line for line in container.logs()]
  25. # Check the the Flask application saw the GET request.
  26. assert b'"GET / HTTP/1.1" 200 -' in logs[-1]
  27.  
  28. finally:
  29. container.stop()
  30. container.delete()

測試設(shè)置

這個腳本首先設(shè)置 conu 使用 Docker 作為后端來運行容器。然后它設(shè)置容器鏡像以使用你在本教程***部分中構(gòu)建的 flaskapp_container。

下一步是配置運行容器所需的選項。在此示例中,F(xiàn)lask 應(yīng)用在端口5000上提供內(nèi)容。于是你需要暴露此端口并將其映射到主機(jī)上的同一端口。

***,用這個腳本啟動容器,現(xiàn)在可以測試了。

測試方法

在測試容器之前,檢查容器是否正在運行并準(zhǔn)備就緒。示范腳本使用 container.is_runningcontainer.wait_for_port。這些方法可確保容器正在運行,并且服務(wù)在預(yù)設(shè)端口上可用。

container.http_requestrequest 庫的包裝器,可以方便地在測試期間發(fā)送 HTTP 請求。這個方法返回requests.Responseobject,因此可以輕松地訪問響應(yīng)的內(nèi)容以進(jìn)行測試。

conu 還可以訪問容器日志。又一次,這在測試期間非常有用。在上面的示例中,container.logs 方法返回容器日志。你可以使用它們斷言打印了特定日志,或者,例如在測試期間沒有異常被引發(fā)。

conu 提供了許多與容器接合的有用方法。文檔中提供了完整的 API 列表。你還可以參考 GitHub 上提供的示例。

運行本教程所需的所有代碼和文件也可以在 GitHub 上獲得。 對于想要進(jìn)一步采用這個例子的讀者,你可以看看使用 pytest 來運行測試并構(gòu)建一個容器測試套件。 

責(zé)任編輯:龐桂玉 來源: Linux中國
相關(guān)推薦

2021-08-17 09:00:00

架構(gòu)PythonWeb

2023-10-08 13:47:33

Docker容器

2009-07-06 17:08:19

測試JSP容器

2016-08-23 09:16:46

Docker鏡像容器

2023-05-11 08:33:17

測試和調(diào)試Python

2022-08-19 11:19:49

單元測試Python

2021-01-15 13:54:04

Portainer.iDocker運維

2021-01-15 13:37:43

Portainer.iDocker運維

2011-04-19 09:51:27

PythonNautilus

2012-02-22 14:18:06

測試測試人員

2022-03-02 10:13:01

SELinux開源

2021-02-17 09:39:41

PodmanDockerLinux

2022-07-01 08:00:00

自動處理Mockoon測試

2020-09-07 15:00:48

Python偏度峰度

2017-08-22 10:52:35

容器DockerLinux

2019-02-13 12:05:57

編程容器開發(fā)

2018-05-11 08:29:10

Python自動化測試數(shù)據(jù)驅(qū)動

2018-05-11 13:39:05

PythonCSV接口測試

2017-10-09 09:33:55

2024-02-23 08:36:34

Python鼠標(biāo)鍵盤
點贊
收藏

51CTO技術(shù)棧公眾號