From ef981239cc6ba0653f0c6e5df26103370f65b815 Mon Sep 17 00:00:00 2001 From: Jonathan Dowland Date: Thu, 28 Nov 2024 11:26:15 +0000 Subject: [PATCH 1/2] _execute: return tuple of success, logs Adjust _execute to return the success/failure status of the command and the commands output as two separate values, paired. Adjust s2i_inner to reflect the altered return type, and to assign the returned output to s2i_build_log in the success and failure cases. Fixes #72. Signed-off-by: Jonathan Dowland --- steps/s2i_steps.py | 7 +++---- steps/steps.py | 11 ++++------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/steps/s2i_steps.py b/steps/s2i_steps.py index ca13e6c..458d7fc 100644 --- a/steps/s2i_steps.py +++ b/steps/s2i_steps.py @@ -35,10 +35,9 @@ def s2i_inner(context, application, path='.', env="", incremental=False, tag="ma ) logger.info("Executing new S2I build with the command [%s]..." % command) - output = _execute(command) - if output: - context.config.userdata['s2i_build_log'] = output - return output + success, output = _execute(command) + context.config.userdata['s2i_build_log'] = output + return success @given(u's2i build {application} from {path} without running') diff --git a/steps/steps.py b/steps/steps.py index be96784..663572e 100644 --- a/steps/steps.py +++ b/steps/steps.py @@ -44,8 +44,8 @@ def _execute(command, log_output=True): fcntl.fcntl(proc.stdout.fileno(), fcntl.F_GETFL) | os.O_NONBLOCK, ) + out = "" if log_output: - out = "" while proc.poll() is None: readx = select.select([proc.stdout, proc.stderr], [], [])[0] @@ -63,16 +63,13 @@ def _execute(command, log_output=True): if retcode != 0: logger.error( "Command '%s' returned code was %s, check logs" % (command, retcode)) - return False + return False, out except subprocess.CalledProcessError: logger.error("Command '%s' failed, check logs" % command) - return False + return False, out - if log_output: - return out - else: - return True + return True, out @then(u'check that page is not served') From 972b8cea0421b1fff25ca6561703fa36a47d34a1 Mon Sep 17 00:00:00 2001 From: Jonathan Dowland Date: Thu, 28 Nov 2024 12:54:23 +0000 Subject: [PATCH 2/2] Add type hints for _execute Signed-off-by: Jonathan Dowland --- steps/steps.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/steps/steps.py b/steps/steps.py index 663572e..e44f58f 100644 --- a/steps/steps.py +++ b/steps/steps.py @@ -6,6 +6,7 @@ import select import socket import fcntl +from typing import Tuple from behave import then, given from container import ExecException @@ -15,7 +16,7 @@ TIMEOUT = int(os.getenv('BEHAVE_TIMEOUT', '30')) -def _execute(command, log_output=True): +def _execute(command, log_output=True) -> Tuple[bool, str]: """ Helper method to execute a shell command and redirect the logs to logger with proper log level.