From 404a5ccbe960c16904862ae74f3f87c9311987c8 Mon Sep 17 00:00:00 2001 From: Salim Fadhley Date: Wed, 30 Jul 2014 00:56:36 +0100 Subject: [PATCH] better support for querying plugins --- jenkinsapi/custom_exceptions.py | 5 +++++ jenkinsapi/jenkins.py | 8 ++++---- jenkinsapi/plugin.py | 11 +++++++++++ jenkinsapi/plugins.py | 13 +++++++++---- jenkinsapi_tests/systests/base.py | 10 +++++++++- jenkinsapi_tests/systests/test_jenkins.py | 18 ++++++++++++++++++ jenkinsapi_tests/unittests/test_plugins.py | 8 ++++++++ release.sh | 2 +- 8 files changed, 65 insertions(+), 10 deletions(-) diff --git a/jenkinsapi/custom_exceptions.py b/jenkinsapi/custom_exceptions.py index 46c3b9d..1b323b6 100644 --- a/jenkinsapi/custom_exceptions.py +++ b/jenkinsapi/custom_exceptions.py @@ -51,6 +51,11 @@ class UnknownQueueItem(KeyError, NotFound): """ pass +class UnknownPlugin(KeyError, NotFound): + """ + Jenkins does not recognize the plugin requested. + """ + pass class NoBuildData(NotFound): """ diff --git a/jenkinsapi/jenkins.py b/jenkinsapi/jenkins.py index 6d25c74..5d50863 100644 --- a/jenkinsapi/jenkins.py +++ b/jenkinsapi/jenkins.py @@ -332,12 +332,12 @@ class Jenkins(JenkinsBase): return Node(nodename=name, baseurl=self.get_node_url(nodename=name), jenkins_obj=self) - def get_plugins_url(self): + def get_plugins_url(self, depth): # This only ever needs to work on the base object - return '%s/pluginManager/api/python?depth=1' % self.baseurl + return '%s/pluginManager/api/python?depth=%i' % (self.baseurl, depth) - def get_plugins(self): - url = self.get_plugins_url() + def get_plugins(self, depth=1): + url = self.get_plugins_url(depth=depth) return Plugins(url, self) def has_plugin(self, plugin_name): diff --git a/jenkinsapi/plugin.py b/jenkinsapi/plugin.py index 5a69bdd..df4384f 100644 --- a/jenkinsapi/plugin.py +++ b/jenkinsapi/plugin.py @@ -14,3 +14,14 @@ class Plugin(object): def __eq__(self, other): return self.__dict__ == other.__dict__ + + def __str__(self): + return self.shortName + + def __repr__(self): + return "<%s.%s %s>" % ( + self.__class__.__module__, + self.__class__.__name__, + str(self) + ) + diff --git a/jenkinsapi/plugins.py b/jenkinsapi/plugins.py index e1a0534..996e291 100644 --- a/jenkinsapi/plugins.py +++ b/jenkinsapi/plugins.py @@ -4,9 +4,9 @@ jenkinsapi plugins from __future__ import print_function import logging - -from jenkinsapi.jenkinsbase import JenkinsBase from jenkinsapi.plugin import Plugin +from jenkinsapi.jenkinsbase import JenkinsBase +from jenkinsapi.custom_exceptions import UnknownPlugin log = logging.getLogger(__name__) @@ -30,6 +30,8 @@ class Plugins(JenkinsBase): def keys(self): return self.get_plugins_dict().keys() + __iter__ = keys + def iteritems(self): return self._get_plugins() @@ -48,8 +50,11 @@ class Plugins(JenkinsBase): return len(self.get_plugins_dict().keys()) def __getitem__(self, plugin_name): - return self.get_plugins_dict().get(plugin_name, None) - + try: + return self.get_plugins_dict()[plugin_name] + except KeyError: + raise UnknownPlugin(plugin_name) + def __contains__(self, plugin_name): """ True if plugin_name is the name of a defined plugin diff --git a/jenkinsapi_tests/systests/base.py b/jenkinsapi_tests/systests/base.py index a373162..8f71245 100644 --- a/jenkinsapi_tests/systests/base.py +++ b/jenkinsapi_tests/systests/base.py @@ -4,15 +4,23 @@ try: except ImportError: import unittest +import logging import jenkinsapi_tests.systests from jenkinsapi_tests.systests.job_configs import EMPTY_JOB from jenkinsapi.jenkins import Jenkins +log = logging.getLogger(__name__) + +DEFAULT_JENKINS_PORT = 8080 class BaseSystemTest(unittest.TestCase): def setUp(self): - port = jenkinsapi_tests.systests.state['launcher'].http_port + try: + port = jenkinsapi_tests.systests.state['launcher'].http_port + except KeyError: + log.warning("Jenkins was not launched from the test-framework, assuming port %i" % DEFAULT_JENKINS_PORT) + port = DEFAULT_JENKINS_PORT self.jenkins = Jenkins('http://localhost:%d' % port) self._delete_all_jobs() self._delete_all_views() diff --git a/jenkinsapi_tests/systests/test_jenkins.py b/jenkinsapi_tests/systests/test_jenkins.py index 7be315e..27e0114 100644 --- a/jenkinsapi_tests/systests/test_jenkins.py +++ b/jenkinsapi_tests/systests/test_jenkins.py @@ -7,6 +7,7 @@ try: except ImportError: import unittest from jenkinsapi.job import Job +from jenkinsapi.plugin import Plugin from jenkinsapi.invocation import Invocation from jenkinsapi_tests.systests.base import BaseSystemTest from jenkinsapi_tests.systests.job_configs import EMPTY_JOB @@ -97,6 +98,23 @@ class JobTests(BaseSystemTest): def test_get_master_data(self): master_data = self.jenkins.get_master_data() self.assertEquals(master_data['totalExecutors'], 2) + + def test_get_missing_plugin(self): + plugins = self.jenkins.get_plugins() + with self.assertRaises(KeyError): + plugins["lsdajdaslkjdlkasj"] # this plugin surely does not exist! + + def test_get_single_plugin(self): + plugins = self.jenkins.get_plugins() + plugin_name, plugin = next(plugins.iteritems()) + self.assertIsInstance(plugin_name, str) + self.assertIsInstance(plugin, Plugin) + self.assertFalse(plugin.dependencies[0]) + + def test_get_single_plugin_depth_2(self): + plugins = self.jenkins.get_plugins(depth=2) + _, plugin = next(plugins.iteritems()) + self.assertTrue(plugin.dependencies[0]) if __name__ == '__main__': unittest.main() diff --git a/jenkinsapi_tests/unittests/test_plugins.py b/jenkinsapi_tests/unittests/test_plugins.py index 0113fc3..33f4a99 100644 --- a/jenkinsapi_tests/unittests/test_plugins.py +++ b/jenkinsapi_tests/unittests/test_plugins.py @@ -167,6 +167,14 @@ class TestPlugins(unittest.TestCase): self.assertEquals('Jenkins Subversion Plug-in', plugin.longName) self.assertEquals('http://wiki.jenkins-ci.org/display/JENKINS/Subversion+Plugin', plugin.url) + + def test_plugin_repr(self): + p = Plugin( + { + 'shortName': 'subversion', + } + ) + self.assertEquals(repr(p), '') if __name__ == '__main__': unittest.main() diff --git a/release.sh b/release.sh index 97fec82..cb423a6 100755 --- a/release.sh +++ b/release.sh @@ -4,4 +4,4 @@ pip install coverage pip install sphinx ant release git tag v`jenkinsapi_version` -ant doc \ No newline at end of file +ant doc -- 2.34.1