From b88218a5f7450fd5b640877753a861942abc260c Mon Sep 17 00:00:00 2001 From: Peter Ondrejka Date: Thu, 9 Feb 2023 11:16:07 +0100 Subject: [PATCH] poll for task intended to fail (#888) --- 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 f2437c2a..1f0d90bf 100644 --- a/nailgun/entities.py +++ b/nailgun/entities.py @@ -3095,7 +3095,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 @@ -3109,6 +3109,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". @@ -3120,7 +3122,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 )