From 608d2ab58e01b37700b76888b6f58d086ba84bf0 Mon Sep 17 00:00:00 2001 From: Peter Ondrejka Date: Tue, 31 Jan 2023 14:29:26 +0100 Subject: [PATCH] poll for task intended to fail --- nailgun/entities.py | 6 ++++-- nailgun/entity_mixins.py | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/nailgun/entities.py b/nailgun/entities.py index 4bc47550..df8b102b 100644 --- a/nailgun/entities.py +++ b/nailgun/entities.py @@ -3100,7 +3100,7 @@ def path(self, which=None): return f'{super().path(which="base")}/{which}' return super().path(which) - def poll(self, poll_rate=None, timeout=None): + def poll(self, poll_rate=None, timeout=None, must_succeed=True): """Return the status of a task or timeout. There are several API calls that trigger asynchronous tasks, such as @@ -3114,6 +3114,8 @@ def poll(self, poll_rate=None, timeout=None): ``nailgun.entity_mixins.TASK_POLL_RATE``. :param timeout: Maximum number of seconds to wait until timing out. Defaults to ``nailgun.entity_mixins.TASK_TIMEOUT``. + :param must_succeed: Raise error when task finishes with other then success + result. :returns: Information about the asynchronous task. :raises: ``nailgun.entity_mixins.TaskTimedOutError`` if the task completes with any result other than "success". @@ -3125,7 +3127,7 @@ def poll(self, poll_rate=None, timeout=None): """ # See nailgun.entity_mixins._poll_task for an explanation of why a # private method is called. - return _poll_task(self.id, self._server_config, poll_rate, timeout) + return _poll_task(self.id, self._server_config, poll_rate, timeout, must_succeed) def summary(self, synchronous=True, timeout=None, **kwargs): """Helper to view a summary of tasks. diff --git a/nailgun/entity_mixins.py b/nailgun/entity_mixins.py index ddb0b712..f7f97be0 100644 --- a/nailgun/entity_mixins.py +++ b/nailgun/entity_mixins.py @@ -82,7 +82,7 @@ def __init__(self, message, task_id): self.task_id = task_id -def _poll_task(task_id, server_config, poll_rate=None, timeout=None): +def _poll_task(task_id, server_config, poll_rate=None, timeout=None, must_succeed=True): """Implement :meth:`nailgun.entities.ForemanTask.poll`. See :meth:`nailgun.entities.ForemanTask.poll` for a full description of how @@ -130,7 +130,7 @@ def raise_task_timeout(): # pragma: no cover timer.cancel() # Check for task success or failure. - if task_info['result'] != 'success': + if must_succeed and task_info['result'] != 'success': raise TaskFailedError( f"Task {task_id} did not succeed. Task information: {task_info}", task_id )