diff options
author | Cédric Ollivier <cedric.ollivier@orange.com> | 2018-12-16 17:50:38 +0100 |
---|---|---|
committer | Cédric Ollivier <cedric.ollivier@orange.com> | 2018-12-27 15:42:43 +0100 |
commit | ebe4291c4457d84cb8425999cfa12371c1c7ce40 (patch) | |
tree | 8f55b8c11591a32048486feb9402703dc7b239a1 /xtesting/core/feature.py | |
parent | 07d8b10d394d1632742c16e4f1f45a29879cf7c1 (diff) |
Allow printing bash cmd output in console
It switches to Popen to print real-time console.
Console has to be enabled per testcase (testcases.yaml).
Change-Id: Id36b42c8409262f7c443e98ae2bcc465984b287f
Signed-off-by: Cédric Ollivier <cedric.ollivier@orange.com>
Diffstat (limited to 'xtesting/core/feature.py')
-rw-r--r-- | xtesting/core/feature.py | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/xtesting/core/feature.py b/xtesting/core/feature.py index 2cb36bd6..2730179f 100644 --- a/xtesting/core/feature.py +++ b/xtesting/core/feature.py @@ -15,7 +15,9 @@ helpers to run any python method or any bash command. import abc import logging +import os import subprocess +import sys import time import six @@ -86,8 +88,8 @@ class BashFeature(Feature): def __init__(self, **kwargs): super(BashFeature, self).__init__(**kwargs) - dir_results = "/var/lib/xtesting/results" - self.result_file = "{}/{}.log".format(dir_results, self.case_name) + self.res_dir = "/var/lib/xtesting/results/{}".format(self.case_name) + self.result_file = "{}/{}.log".format(self.res_dir, self.case_name) def execute(self, **kwargs): """Execute the cmd passed as arg @@ -101,12 +103,22 @@ class BashFeature(Feature): """ try: cmd = kwargs["cmd"] - with open(self.result_file, 'w+') as f_stdout: - subprocess.check_call( - cmd, shell=True, stdout=f_stdout, stderr=subprocess.STDOUT) - return 0 + console = kwargs["console"] if "console" in kwargs else False + if not os.path.isdir(self.res_dir): + os.makedirs(self.res_dir) + with open(self.result_file, 'w') as f_stdout: + self.__logger.info("Calling %s", cmd) + process = subprocess.Popen( + cmd, shell=True, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + for line in iter(process.stdout.readline, ''): + if console: + sys.stdout.write(line) + f_stdout.write(line) + process.wait() + with open(self.result_file, 'r') as f_stdin: + self.__logger.debug("$ %s\n%s", cmd, f_stdin.read().rstrip()) + return process.returncode except KeyError: self.__logger.error("Please give cmd as arg. kwargs: %s", kwargs) - except subprocess.CalledProcessError: - self.__logger.error("Execute command: %s failed", cmd) return -1 |