Skip to content

Commit

Permalink
PESDLC-1319 Use exceptions and add timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
savex committed May 8, 2024
1 parent 3b595c1 commit 771abda
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions tests/rptest/clients/kubectl.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,17 @@ def _install(self):
def logger(self) -> Logger:
return self._redpanda.logger

def _local_cmd(self, cmd: list[str]):
def _local_cmd(self, cmd: list[str], timeout=900):
"""Run the given command locally and return the stdout as bytes.
Logs stdout and stderr on failure."""
Logs stdout and stderr on failure.
cmd: list[str]: command to run
throws CalledProcessError on non-zero exit code
thwows
returns: stdout as string if not empty, else stderr
"""
def _prepare_output(sout: str, serr: str) -> str:
return f"\n--------- stdout -----------\n{sout}" \
f"\n--------- stderr -----------\n{serr}\n"
Expand All @@ -155,20 +163,34 @@ def _prepare_output(sout: str, serr: str) -> str:
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True)
s_out, s_err = process.communicate()
try:
s_out, s_err = process.communicate()
# safety check for process termination
if process.poll() is None:
# Should never happen (c)
process.kill()
except subprocess.TimeoutExpired:
process.kill()
s_out, s_err = process.communicate()
raise subprocess.TimeoutExpired(cmd, timeout, s_out, s_err)

# Log all collected strings, including JSONs collected
self.logger.debug(_prepare_output(s_out, s_err))

# TODO: Handle s_err output
# It will be hard to detect errors as a lot of apps and utils
# just uses stderr as normal output. For example, tsh

process.kill()
# just uses stderr as normal output. For example, tsh (teleport tunnel)

if process.returncode != 0:
self.logger.info(f'Command failed (rc={process.returncode}).\n'
f'{_prepare_output(s_out, s_err)}')
raise RuntimeError("Last command returned non-zero exit code")

self.logger.info(f"Command failed (rc={process.returncode}): "
f"'{' '.join(cmd)}'\n"
f"{_prepare_output(s_out, s_err)}")
raise subprocess.CalledProcessError(process.returncode, cmd, s_out,
s_err)

# Most of the time stdout will hold valuable data
# In rare occasions when return code is 0, and and stderr is not empty
# return that instead.
return s_out if len(s_out) > 0 else s_err

def _ssh_cmd(self, cmd: list[str]):
Expand Down

0 comments on commit 771abda

Please sign in to comment.