File: test_info.py

package info (click to toggle)
python-jenkins 1.8.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 556 kB
  • sloc: python: 6,099; makefile: 150
file content (89 lines) | stat: -rw-r--r-- 3,447 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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)