python-jenkins/tests/test_info.py
Darragh Bailey 34cca0c4d9 Migration to using requests
Convert to the requests library to allow for more sophisticated response
handling to be added.

Want to allow for applications to override the response handling in
certain cases where the default is incorrect. Handling of urlopen
responses results in version specific handling to ensure correct
behaviour across multiple versions of python.

Changing to use the requests package, provides a higher level interface
and removes some of the version specific handling for exceptions.

Change-Id: I5369a0d35be4bf8b3b197a51e60aba21b5742cc7
Depends-On: Iabd70aa457ceb4dbc147d7cbaeec913148cb3b56
2018-02-12 11:16:54 +00:00

90 lines
3.4 KiB
Python

import json
from mock import patch
import jenkins
from tests.base import JenkinsTestBase
from tests.helper import build_response_mock
class JenkinsInfoTest(JenkinsTestBase):
@patch.object(jenkins.Jenkins, 'jenkins_open')
def test_simple(self, jenkins_mock):
job_info_to_return = {
u'jobs': {
u'url': u'http://your_url_here/job/my_job/',
u'color': u'blue',
u'name': u'my_job',
}
}
jenkins_mock.return_value = json.dumps(job_info_to_return)
job_info = self.j.get_info()
self.assertEqual(job_info, job_info_to_return)
self.assertEqual(
jenkins_mock.call_args[0][0].url,
self.make_url('api/json'))
self._check_requests(jenkins_mock.call_args_list)
@patch('jenkins.requests.Session.send', autospec=True)
def test_raise_HTTPError(self, session_send_mock):
session_send_mock.side_effect = iter([
build_response_mock(404, reason="Not Found"), # crumb
build_response_mock(499, reason="Unhandled Error"), # request
])
with self.assertRaises(jenkins.BadHTTPException) as context_manager:
self.j.get_info()
self.assertEqual(
session_send_mock.call_args_list[1][0][1].url,
self.make_url('api/json'))
self.assertEqual(
str(context_manager.exception),
'Error communicating with server[{0}]'.format(self.make_url('')))
@patch.object(jenkins.Jenkins, 'jenkins_open')
def test_raise_BadStatusLine(self, jenkins_mock):
jenkins_mock.side_effect = jenkins.BadStatusLine('not a valid status line')
with self.assertRaises(jenkins.BadHTTPException) as context_manager:
self.j.get_info()
self.assertEqual(
jenkins_mock.call_args[0][0].url,
self.make_url('api/json'))
self.assertEqual(
str(context_manager.exception),
'Error communicating with server[{0}]'.format(self.make_url('')))
self._check_requests(jenkins_mock.call_args_list)
@patch.object(jenkins.Jenkins, 'jenkins_open')
def test_return_invalid_json(self, jenkins_mock):
jenkins_mock.return_value = 'not valid JSON'
with self.assertRaises(jenkins.JenkinsException) as context_manager:
self.j.get_info()
self.assertEqual(
jenkins_mock.call_args[0][0].url,
self.make_url('api/json'))
self.assertEqual(
str(context_manager.exception),
'Could not parse JSON info for server[{0}]'.format(self.make_url('')))
self._check_requests(jenkins_mock.call_args_list)
@patch.object(jenkins.Jenkins, 'jenkins_open')
def test_return_empty_response(self, jenkins_mock):
jenkins_mock.side_effect = jenkins.JenkinsException(
"Error communicating with server[{0}]: empty response".
format(self.make_url('')))
with self.assertRaises(jenkins.JenkinsException) as context_manager:
self.j.get_info()
self.assertEqual(
jenkins_mock.call_args[0][0].url,
self.make_url('api/json'))
self.assertEqual(
str(context_manager.exception),
'Error communicating with server[{0}]: '
'empty response'.format(self.make_url('')))
self._check_requests(jenkins_mock.call_args_list)