Skip to content

Commit

Permalink
Container: support expected failures
Browse files Browse the repository at this point in the history
Sometimes we expect a failure when executing, allow this case
to be valid.
  • Loading branch information
DylanVanAssche committed Feb 22, 2024
1 parent 2aeca2d commit dbc2eec
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions bench_executor/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,16 @@ def run(self, command: str = '', detach=True) -> bool:
self._logger.error(f'Starting container "{self._name}" failed!')
return False

def exec(self, command: str) -> Tuple[bool, List[str]]:
def exec(self, command: str,
expect_failure: bool = False) -> Tuple[bool, List[str]]:
"""Execute a command in the container.
Parameters
----------
command : str
The command to execute in the container.
expect_failure : bool
True if the command is expected to fail.
Returns
-------
Expand All @@ -182,15 +185,27 @@ def exec(self, command: str) -> Tuple[bool, List[str]]:
if self._container is None:
self._logger.error('Container is not initialized yet')
return False, []
exit_code: int
exit_code, output = self._container.exec_run(command)
logs = output.decode()
for line in logs.split('\n'):
self._logger.debug(line.strip())
if exit_code == 0:
if expect_failure and exit_code != 0:
self._logger.debug('Expected failure found with exit code '
f'"{exit_code}"')
return True, logs
elif not expect_failure and exit_code == 0:
self._logger.debug('Command executed succesfully as expected')
return True, logs
except docker.errors.APIError as e:
self._logger.error(f'Failed to execute command: {e}')

if expect_failure:
self._logger.error(f'Command succeed with exit code "{exit_code}"'
'but expected failure')
else:
self._logger.error(f'Command failed with exit code "{exit_code}"')

return False, logs

def run_and_wait_for_log(self, log_line: str, command: str = '') -> bool:
Expand Down

0 comments on commit dbc2eec

Please sign in to comment.