我有一组有趣的要求,我正在尝试使用 Python 子进程模块和 docker-compose 来执行这些要求。整个设置可以在一个 docker-compose 中完成,但由于要求,这就是我想要设置的:
- 使用 python 子进程调用 docker-compose 来激活
测试服务器
- 打印上面 docker-compose 运行的所有标准输出。
- 一旦测试服务器通过 docker-compose 启动并运行;调用该服务器的测试脚本。
这是我的 docker-compose.py 的样子:
import subprocess
from subprocess import PIPE
import os
from datetime import datetime
class MyLog:
def my_log(self, message):
date_now = datetime.today().strftime('%d-%m-%Y %H:%M:%S')
print("{0} || {1}".format(date_now, message))
class DockercomposeRun:
log = MyLog()
def __init__(self):
dir_name, _ = os.path.split(os.path.abspath(__file__))
self.dirname = dir_name
def run_docker_compose(self, filename):
command_name = ["docker-compose", "-f", self.dirname + filename, "up"]
popen = subprocess.Popen(command_name, stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
return popen
现在在我的 test.py 中
一旦我的标准输出为空,我想打破打印循环并在 test.py 中运行测试的其余部分。
docker_compose_run = DockercomposeRun()
rc = docker_compose_run.run_docker_compose('/docker-compose.yml.sas-viya-1')
for line in iter(rc.stdout.readline, ''):
print(line, end='')
if line == '':
break
popen.stdout.close()
# start here actual test cases
.......
但对我来说,即使 docker-compose 的标准输出在服务器启动并运行后变为空白,循环也永远不会中断。并且,测试用例永远不会被执行。
这是正确的方法还是我如何才能实现这一目标?