与python异步运行docker容器

时间:2018-05-04 14:21:12

标签: python docker

我已将测试框架移动到docker容器中。我的目标之一是制作它,这样我们就可以每次测试旋转1个容器,然后报告结果。

我使用以下内容[请记住这是一个WIP]: api_test_runner.py

import subprocess
from api_test_list import single_test_list

for test in single_test_list:

    command = subprocess.check_output(['docker', 'run', '-e', 'USER=root', '-e', 'TEST=' + test, 'api_test_container'])
    print(command)

这会运行一个测试列表并为每个测试旋转一个容器,但是在进入下一个测试之前等待一个容器停止:

(py3_api_automation) remmac05674:api_automation$ python api_test_runner.py
b'============================= test session starts ==============================\nplatform linux -- Python 3.6.4, pytest-3.2.5, py-1.5.2, pluggy-0.4.0\nrootdir: /app, inifile:\nplugins: xdist-1.20.1, forked-0.2, cloud-2.0.0\ncollected 1 item\n\ntest_1.py .\n\n=========================== 1 passed in 1.81 seconds ===========================\n'
b'============================= test session starts ==============================\nplatform linux -- Python 3.6.4, pytest-3.2.5, py-1.5.2, pluggy-0.4.0\nrootdir: /app, inifile:\nplugins: xdist-1.20.1, forked-0.2, cloud-2.0.0\ncollected 1 item\n\ntest_2.py .

我想要解决的是如何让python运行测试列表并为每个测试列表启动一个容器,而不是等待其他容器完成。

2 个答案:

答案 0 :(得分:0)

对于那些感兴趣的人,我更新了我的脚本以使用.Popen

for test in single_test_list:

    command = subprocess.Popen(['docker', 'run', '-e', 'USER=root', '-e', 'TEST=' + test, 'api_test_container'])
    print(command)

答案 1 :(得分:0)

如果您不想等待每个,则必须使用-d运行每个'docker run'命令(并将输出写入文件)或者在测试脚本上独立运行每个docker