【发布时间】:2020-06-29 11:42:24
【问题描述】:
环境: 平台 qnx——Python 2.7.12、pytest-4.6.9、py-1.8.1、pluggy-0.13.1 插件:json-report-1.2.1、shell-0.2.3
注意: 我知道 Python2.7 很旧且不受支持,但目前 QNX 没有其他版本可用。
问题:
我正在运行一个测试,当某个关键字出现在其日志中时,它应该终止服务。为此,我需要它在后台运行。 为此,我使用以下 shell 命令:
def test_kill_process():
expected_output="XXXXXXXXX"
expected_rc=0
check_kill_process(expected_output, expected_rc)
import os
def check_kill_process(expected_output, expected_rc):
test_log = File(r"/path/to/log")
erase_log_entry = "Action"
service=MyService()
service.start()
sleep(2)
kill_command = "tail -f " + test_log.file_path + " | grep --line-buffered " + erase_log_entry + \
" | while read ; do kill " + service.pid + " ; done &"
os.popen(kill_command)
service.action()
f = open(test_log.file_path, "r")
output = f.read()
assert re.search(expected_output, output)
================================================ ==========================
没有 Pytest 甚至 Python,就像一个魅力。
如果我尝试使用 subprocess 模块运行命令,测试会无限期冻结。 如果我尝试使用 os.popen 或 os.system,命令以错误结束:
tail: read failed in '/path/to/logfile' (Invalid argument)
此外,如果我尝试同样的事情,只有一只“猫”我会得到这个:
--stdout--: Broken pipe
如果有人有任何想法,请提前感谢!
【问题讨论】:
-
如果我尝试使用子进程 -
subprocess在空环境下运行,因此LOGFILE和KEYWORD都不会被定义。使用env参数传递环境。 -
对不起,我在这里写的不是很好。我直接传递参数,而不是作为 shell 变量。我重写了这个问题。感谢您的及时答复,但事实并非如此......
-
请分享完整的源代码