From 338294b3f4d121292cc1ed5397d2344453cc1ceb Mon Sep 17 00:00:00 2001 From: Clark Boylan Date: Thu, 10 Sep 2015 15:04:51 -0700 Subject: [PATCH] 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 --- README.rst | 1 + jenkins/__init__.py | 13 +++++++++ tests/test_quiet_down.py | 59 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 tests/test_quiet_down.py diff --git a/README.rst b/README.rst index e244340..1794450 100644 --- a/README.rst +++ b/README.rst @@ -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:: diff --git a/jenkins/__init__.py b/jenkins/__init__.py index 9cd1a44..f110b9d 100644 --- a/jenkins/__init__.py +++ b/jenkins/__init__.py @@ -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 = ''' @@ -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') diff --git a/tests/test_quiet_down.py b/tests/test_quiet_down.py new file mode 100644 index 0000000..415e6b3 --- /dev/null +++ b/tests/test_quiet_down.py @@ -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)