Merge "When updating jobs the response body may be empty"

This commit is contained in:
Zuul 2019-07-23 15:12:05 +00:00 committed by Gerrit Code Review
commit 35f660a8b9
5 changed files with 11 additions and 23 deletions

View File

@ -368,6 +368,8 @@ class Jenkins(object):
try:
response = self.jenkins_open(requests.Request(
'GET', self._build_url(CRUMB_URL)), add_crumb=False)
if not response:
raise EmptyResponseException("Empty response for crumb")
except (NotFoundException, EmptyResponseException):
self.crumb = False
else:
@ -532,16 +534,6 @@ class Jenkins(object):
# raise exceptions if occurred
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
# when accessing .text property
return response

View File

@ -60,7 +60,7 @@ class JenkinsBuildJobTest(JenkinsJobsTestBase):
self.assertEqual(
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('')))
@patch('jenkins.requests.Session.send', autospec=True)

View File

@ -108,7 +108,7 @@ class JenkinsMaybeAddCrumbTest(JenkinsTestBase):
@patch.object(jenkins.Jenkins, 'jenkins_open')
def test_return_empty_response(self, jenkins_mock):
"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')
self.j.maybe_add_crumb(request)
@ -254,11 +254,9 @@ class JenkinsOpenTest(JenkinsTestBase):
j = jenkins.Jenkins('http://example.com', 'test', 'test')
self.assertTrue(j.wait_for_normal_op(0))
@patch.object(jenkins.Jenkins, 'jenkins_open',
side_effect=jenkins.EmptyResponseException())
@patch.object(jenkins.Jenkins, 'get_version',
side_effect=jenkins.EmptyResponseException())
def test_wait_for_normal_op__empty_response(self, version_mock, jenkins_mock):
side_effect=jenkins.BadHTTPException())
def test_wait_for_normal_op__failed_response(self, version_mock):
j = jenkins.Jenkins('http://example.com', 'test', 'test')
self.assertFalse(j.wait_for_normal_op(0))

View File

@ -266,11 +266,10 @@ class JenkinsCreateNodeTest(JenkinsNodesTestBase):
@requests_mock.Mocker()
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(
self.make_url('computer/test_node/api/json?depth=0'),
status_code=200, json=self.node_info,
headers={'content-length': '20'}
status_code=200, json=self.node_info
)
with self.assertRaises(jenkins.JenkinsException) as context_manager:

View File

@ -36,11 +36,10 @@ class JenkinsVersionTest(JenkinsTestBase):
'Error communicating with server[{0}/]'.format(self.base_url))
@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)
with self.assertRaises(jenkins.EmptyResponseException) as context_manager:
with self.assertRaises(KeyError) as context_manager:
self.j.get_version()
self.assertEqual(
str(context_manager.exception),
'Error communicating with server[{0}/]:'
' empty response'.format(self.base_url))
"'x-jenkins'")