From 106048dd87478a08e7238c4fe99660137fa30186 Mon Sep 17 00:00:00 2001 From: grahamlyons Date: Thu, 9 Feb 2017 16:30:52 +0000 Subject: [PATCH] Fix error thrown in presence of placeholder tasks When there are placeholder tasks in an executor for a node then there is no build number yet assigned and attempting to access that key in the dictionary will fail. This fix filters out any tasks which are instances of that class before proceeding. Change-Id: Ie5b237c3e6023c1822f91e5982ec84f1a363130c Closes-Bug: #1659787 --- jenkins/__init__.py | 2 +- tests/test_build.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/jenkins/__init__.py b/jenkins/__init__.py index 5977039..26672b9 100755 --- a/jenkins/__init__.py +++ b/jenkins/__init__.py @@ -1146,7 +1146,7 @@ class Jenkins(object): raise for executor in info['executors']: executable = executor['currentExecutable'] - if executable: + if executable and 'PlaceholderTask' not in executable.get('_class', ''): executor_number = executor['number'] build_number = executable['number'] url = executable['url'] diff --git a/tests/test_build.py b/tests/test_build.py index 497ae30..c6d3728 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -417,3 +417,40 @@ class JenkinsListRunningBuildsTest(JenkinsTestBase): builds = self.j.get_running_builds() # Should treat the slave as not running any builds self.assertEqual([], builds) + + @patch.object(jenkins.Jenkins, 'get_node_info') + @patch.object(jenkins.Jenkins, 'get_nodes') + def test_placeholder_task_in_queue(self, nodes_mock, node_info_mock): + nodes_to_return = [{ + 'name': "foo-slave", 'offline': False + }] + nodes_mock.return_value = nodes_to_return + node_info_to_return = { + "executors": [ + { + "currentExecutable": None, + "currentWorkUnit": None, + "idle": True, + "likelyStuck": False, + "number": 1, + "progress": -1 + }, + { + 'currentExecutable': { + '_class': ( + 'org.jenkinsci.plugins.workflow.support.steps.' + 'ExecutorStepExecution$PlaceholderTask$' + 'PlaceholderExecutable' + ) + }, + 'currentWorkUnit': {}, + 'idle': False, + 'likelyStuck': False, + 'number': 1, + 'progress': 0 + } + ], + } + node_info_mock.return_value = node_info_to_return + builds = self.j.get_running_builds() + self.assertEqual([], builds)