Add support for quiet down

This adds a Jenkins.quiet_down() method which can be used to quiet down
the server. Unit tests included.

Change-Id: I4e229aae6ad766bc6194a643fcd514e5538d6fa7
This commit is contained in:
Clark Boylan 2015-09-10 15:04:51 -07:00
parent 427753c3b1
commit 338294b3f4
3 changed files with 73 additions and 0 deletions

View File

@ -21,6 +21,7 @@ the things you can use it for:
* Enable/Disable nodes
* Get information on nodes
* Create/delete/reconfig views
* Put server in shutdown mode (quiet down)
* and many more..
To install::

View File

@ -102,6 +102,7 @@ CREATE_VIEW = 'createView?name=%(name)s'
CONFIG_VIEW = 'view/%(name)s/config.xml'
DELETE_VIEW = 'view/%(name)s/doDelete'
SCRIPT_TEXT = 'scriptText'
QUIET_DOWN = 'quietDown'
# for testing only
EMPTY_CONFIG_XML = '''<?xml version='1.0' encoding='UTF-8'?>
@ -1007,3 +1008,15 @@ class Jenkins(object):
'''
request = Request(self._build_url(CONFIG_VIEW, locals()))
return self.jenkins_open(request)
def quiet_down(self):
'''Prepare Jenkins for shutdown.
No new builds will be started allowing running builds to complete
prior to shutdown of the server.
'''
request = Request(self._build_url(QUIET_DOWN))
self.jenkins_open(request)
info = self.get_info()
if not info['quietingDown']:
raise JenkinsException('quiet down failed')

59
tests/test_quiet_down.py Normal file
View File

@ -0,0 +1,59 @@
import json
from mock import patch
import jenkins
from tests.base import JenkinsTestBase
class JenkinsQuietDownTest(JenkinsTestBase):
@patch.object(jenkins.Jenkins, 'jenkins_open')
def test_success(self, jenkins_mock):
job_info_to_return = {
"quietingDown": True,
}
jenkins_mock.return_value = json.dumps(job_info_to_return)
self.j.quiet_down()
self.assertEqual(
jenkins_mock.call_args_list[0][0][0].get_full_url(),
u'http://example.com/quietDown')
self.assertEqual(
jenkins_mock.call_args_list[1][0][0].get_full_url(),
u'http://example.com/api/json')
self._check_requests(jenkins_mock.call_args_list)
@patch.object(jenkins.Jenkins, 'jenkins_open')
def test_fail(self, jenkins_mock):
job_info_to_return = {
"quietingDown": False,
}
jenkins_mock.return_value = json.dumps(job_info_to_return)
with self.assertRaises(jenkins.JenkinsException) as context_manager:
self.j.quiet_down()
self.assertEqual(
jenkins_mock.call_args_list[0][0][0].get_full_url(),
u'http://example.com/quietDown')
self.assertEqual(
jenkins_mock.call_args_list[1][0][0].get_full_url(),
u'http://example.com/api/json')
self.assertEqual(
str(context_manager.exception),
'quiet down failed')
self._check_requests(jenkins_mock.call_args_list)
@patch.object(jenkins.Jenkins, 'jenkins_open')
def test_http_fail(self, jenkins_mock):
jenkins_mock.side_effect = jenkins.JenkinsException(
'Error in request. Possibly authentication failed [401]: '
'basic auth failed')
with self.assertRaises(jenkins.JenkinsException):
self.j.quiet_down()
self.assertEqual(
jenkins_mock.call_args[0][0].get_full_url(),
u'http://example.com/quietDown')
self._check_requests(jenkins_mock.call_args_list)