When updating jobs the response body may be empty
Other specific requests may need a body. Thus remove the general check for an empty body and add a more specific one where needed. I noticed this because when updating a Jenkins job with Jenkins Job Builder I got an EmptyResponseException even though updating worked correctly. Change-Id: Ie144e6fb0b922a681e6c65ffbe5120da72a495f0
This commit is contained in:
parent
5c287258eb
commit
a2c6f6a8ff
@ -368,6 +368,8 @@ class Jenkins(object):
|
|||||||
try:
|
try:
|
||||||
response = self.jenkins_open(requests.Request(
|
response = self.jenkins_open(requests.Request(
|
||||||
'GET', self._build_url(CRUMB_URL)), add_crumb=False)
|
'GET', self._build_url(CRUMB_URL)), add_crumb=False)
|
||||||
|
if not response:
|
||||||
|
raise EmptyResponseException("Empty response for crumb")
|
||||||
except (NotFoundException, EmptyResponseException):
|
except (NotFoundException, EmptyResponseException):
|
||||||
self.crumb = False
|
self.crumb = False
|
||||||
else:
|
else:
|
||||||
@ -532,16 +534,6 @@ class Jenkins(object):
|
|||||||
# raise exceptions if occurred
|
# raise exceptions if occurred
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
headers = response.headers
|
|
||||||
if (headers.get('content-length') is None and
|
|
||||||
headers.get('transfer-encoding') is None and
|
|
||||||
headers.get('location') is None and
|
|
||||||
(response.content is None or len(response.content) <= 0)):
|
|
||||||
# response body should only exist if one of these is provided
|
|
||||||
raise EmptyResponseException(
|
|
||||||
"Error communicating with server[%s]: "
|
|
||||||
"empty response" % self.server)
|
|
||||||
|
|
||||||
# Response objects will automatically return unicode encoded
|
# Response objects will automatically return unicode encoded
|
||||||
# when accessing .text property
|
# when accessing .text property
|
||||||
return response
|
return response
|
||||||
|
@ -60,7 +60,7 @@ class JenkinsBuildJobTest(JenkinsJobsTestBase):
|
|||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
str(context_mgr.exception),
|
str(context_mgr.exception),
|
||||||
'Error communicating with server[{0}]: empty response'.format(
|
"Header 'Location' not found in response from server[{0}]".format(
|
||||||
self.make_url('')))
|
self.make_url('')))
|
||||||
|
|
||||||
@patch('jenkins.requests.Session.send', autospec=True)
|
@patch('jenkins.requests.Session.send', autospec=True)
|
||||||
|
@ -108,7 +108,7 @@ class JenkinsMaybeAddCrumbTest(JenkinsTestBase):
|
|||||||
@patch.object(jenkins.Jenkins, 'jenkins_open')
|
@patch.object(jenkins.Jenkins, 'jenkins_open')
|
||||||
def test_return_empty_response(self, jenkins_mock):
|
def test_return_empty_response(self, jenkins_mock):
|
||||||
"Don't try to create crumb header from an empty response"
|
"Don't try to create crumb header from an empty response"
|
||||||
jenkins_mock.side_effect = jenkins.EmptyResponseException("empty response")
|
jenkins_mock.return_value = ''
|
||||||
request = jenkins.requests.Request('GET', 'http://example.com/job/TestJob')
|
request = jenkins.requests.Request('GET', 'http://example.com/job/TestJob')
|
||||||
|
|
||||||
self.j.maybe_add_crumb(request)
|
self.j.maybe_add_crumb(request)
|
||||||
@ -254,11 +254,9 @@ class JenkinsOpenTest(JenkinsTestBase):
|
|||||||
j = jenkins.Jenkins('http://example.com', 'test', 'test')
|
j = jenkins.Jenkins('http://example.com', 'test', 'test')
|
||||||
self.assertTrue(j.wait_for_normal_op(0))
|
self.assertTrue(j.wait_for_normal_op(0))
|
||||||
|
|
||||||
@patch.object(jenkins.Jenkins, 'jenkins_open',
|
|
||||||
side_effect=jenkins.EmptyResponseException())
|
|
||||||
@patch.object(jenkins.Jenkins, 'get_version',
|
@patch.object(jenkins.Jenkins, 'get_version',
|
||||||
side_effect=jenkins.EmptyResponseException())
|
side_effect=jenkins.BadHTTPException())
|
||||||
def test_wait_for_normal_op__empty_response(self, version_mock, jenkins_mock):
|
def test_wait_for_normal_op__failed_response(self, version_mock):
|
||||||
j = jenkins.Jenkins('http://example.com', 'test', 'test')
|
j = jenkins.Jenkins('http://example.com', 'test', 'test')
|
||||||
self.assertFalse(j.wait_for_normal_op(0))
|
self.assertFalse(j.wait_for_normal_op(0))
|
||||||
|
|
||||||
|
@ -266,11 +266,10 @@ class JenkinsCreateNodeTest(JenkinsNodesTestBase):
|
|||||||
|
|
||||||
@requests_mock.Mocker()
|
@requests_mock.Mocker()
|
||||||
def test_already_exists(self, req_mock):
|
def test_already_exists(self, req_mock):
|
||||||
req_mock.get(self.make_url(jenkins.CRUMB_URL))
|
req_mock.get(self.make_url(jenkins.CRUMB_URL), content=None)
|
||||||
req_mock.get(
|
req_mock.get(
|
||||||
self.make_url('computer/test_node/api/json?depth=0'),
|
self.make_url('computer/test_node/api/json?depth=0'),
|
||||||
status_code=200, json=self.node_info,
|
status_code=200, json=self.node_info
|
||||||
headers={'content-length': '20'}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
with self.assertRaises(jenkins.JenkinsException) as context_manager:
|
with self.assertRaises(jenkins.JenkinsException) as context_manager:
|
||||||
|
@ -36,11 +36,10 @@ class JenkinsVersionTest(JenkinsTestBase):
|
|||||||
'Error communicating with server[{0}/]'.format(self.base_url))
|
'Error communicating with server[{0}/]'.format(self.base_url))
|
||||||
|
|
||||||
@patch('jenkins.requests.Session.send', autospec=True)
|
@patch('jenkins.requests.Session.send', autospec=True)
|
||||||
def test_return_empty_response(self, session_send_mock):
|
def test_return_without_version_header(self, session_send_mock):
|
||||||
session_send_mock.return_value = build_response_mock(0)
|
session_send_mock.return_value = build_response_mock(0)
|
||||||
with self.assertRaises(jenkins.EmptyResponseException) as context_manager:
|
with self.assertRaises(KeyError) as context_manager:
|
||||||
self.j.get_version()
|
self.j.get_version()
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
str(context_manager.exception),
|
str(context_manager.exception),
|
||||||
'Error communicating with server[{0}/]:'
|
"'x-jenkins'")
|
||||||
' empty response'.format(self.base_url))
|
|
||||||
|
Loading…
Reference in New Issue
Block a user