Merge "Adds assert_node_exists()"

This commit is contained in:
Jenkins
2014-07-02 16:37:56 +00:00
committed by Gerrit Code Review
2 changed files with 31 additions and 2 deletions

View File

@@ -516,6 +516,17 @@ class Jenkins(object):
except JenkinsException:
return False
def assert_node_exists(self, name,
exception_message='node[%s] does not exist'):
'''
:param name: Name of Jenkins node, ``str``
:param exception_message: Message to use for the exception. Formatted
with ``name``
:throws: :class:`JenkinsException` whenever the node does not exist
'''
if not self.node_exists(name):
raise JenkinsException(exception_message % name)
def delete_node(self, name):
'''Delete Jenkins node permanently.
@@ -599,8 +610,7 @@ class Jenkins(object):
self.jenkins_open(Request(
self.server + CREATE_NODE % urlencode(params)))
if not self.node_exists(name):
raise JenkinsException('create[%s] failed' % (name))
self.assert_node_exists(name, 'create[%s] failed')
def get_build_console_output(self, name, number):
'''Get build console text.

View File

@@ -824,6 +824,25 @@ class JenkinsTest(unittest.TestCase):
str(context_manager.exception),
'node[test_node] does not exist')
@patch.object(jenkins.Jenkins, 'jenkins_open')
def test_assert_node_exists__node_missing(self, jenkins_mock):
jenkins_mock.side_effect = [None]
j = jenkins.Jenkins('http://example.com/', 'test', 'test')
with self.assertRaises(jenkins.JenkinsException) as context_manager:
j.assert_node_exists('NonExistentNode')
self.assertEqual(
str(context_manager.exception),
'node[NonExistentNode] does not exist')
@patch.object(jenkins.Jenkins, 'jenkins_open')
def test_assert_node_exists__node_exists(self, jenkins_mock):
jenkins_mock.side_effect = [
json.dumps({'name': 'ExistingNode'})
]
j = jenkins.Jenkins('http://example.com/', 'test', 'test')
j.assert_node_exists('ExistingNode')
@patch.object(jenkins.Jenkins, 'jenkins_open')
def test_delete_node(self, jenkins_mock):
node_info = {