From 8f1101df131a4d3e03b377738507d88b745831c0 Mon Sep 17 00:00:00 2001 From: "Yiting.Li" Date: Tue, 22 Dec 2015 17:11:12 -0800 Subject: Upload the contribution of vstf as bottleneck network framework. End to End Performance test JIRA:BOTTLENECK-29 Change-Id: Ib2c553c8b60d6cda9e7a7b52b737c9139f706ebd Signed-off-by: Yiting.Li --- vstf/vstf/agent/env/basic/commandline.py | 46 ++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100755 vstf/vstf/agent/env/basic/commandline.py (limited to 'vstf/vstf/agent/env/basic/commandline.py') diff --git a/vstf/vstf/agent/env/basic/commandline.py b/vstf/vstf/agent/env/basic/commandline.py new file mode 100755 index 00000000..0df037d8 --- /dev/null +++ b/vstf/vstf/agent/env/basic/commandline.py @@ -0,0 +1,46 @@ +import subprocess +import threading +import logging +from vstf.common import constants + +LOG = logging.getLogger(__name__) + + +class CommandLine(object): + def __init__(self): + super(CommandLine, self).__init__() + self.proc = None + self.is_timeout = False + + def __kill_proc(self): + self.is_timeout = True + self.proc.kill() + + def execute(self, cmd, timeout=constants.TIMEOUT, shell=False): + """this func call subprocess.Popen(), + here setup a timer to deal with timeout. + :param cmd: cmd list like ['ls', 'home'] + :param timeout: for timer count for timeout + :return: (ret, output) the output (stdout+'\n'+stderr) + """ + # reset the timeout flag + self.is_timeout = False + self.proc = subprocess.Popen(cmd, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + shell=shell) + + timer = threading.Timer(timeout, self.__kill_proc, []) + timer.start() + stdout, stderr = self.proc.communicate() + timer.cancel() + + if self.proc.returncode or self.is_timeout: + if self.is_timeout: + LOG.error("run cmd<%(cmd)s> timeout", {"cmd": cmd}) + ret = False + output = "".join([stderr, stdout]) + else: + ret = True + output = stdout + return ret, output -- cgit 1.2.3-korg