diff options
author | 2018-12-16 17:50:38 +0100 | |
---|---|---|
committer | 2018-12-27 15:50:59 +0100 | |
commit | 2265d0ccb133ae39644d096df25cb92b530f7545 (patch) | |
tree | 32dc2fe620af03ae56319ca331074d09c8fd3f7b /xtesting/core/feature.py | |
parent | dc424b09f6a5b7d912b037a59d201e86d4fcec3a (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>
(cherry picked from commit ebe4291c4457d84cb8425999cfa12371c1c7ce40)
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 |