better support for querying plugins
authorSalim Fadhley <sal@stodge.org>
Tue, 29 Jul 2014 23:56:36 +0000 (00:56 +0100)
committerSalim Fadhley <sal@stodge.org>
Tue, 29 Jul 2014 23:56:36 +0000 (00:56 +0100)
jenkinsapi/custom_exceptions.py
jenkinsapi/jenkins.py
jenkinsapi/plugin.py
jenkinsapi/plugins.py
jenkinsapi_tests/systests/base.py
jenkinsapi_tests/systests/test_jenkins.py
jenkinsapi_tests/unittests/test_plugins.py
release.sh

index 46c3b9dbca6f02f3d2f20aeca589fd4fafd3fef8..1b323b67b475279132e746d9844cc60cd073dbe4 100644 (file)
@@ -51,6 +51,11 @@ class UnknownQueueItem(KeyError, NotFound):
     """
     pass
 
+class UnknownPlugin(KeyError, NotFound):
+    """
+    Jenkins does not recognize the plugin requested.
+    """
+    pass
 
 class NoBuildData(NotFound):
     """
index 6d25c74a3810a0e9ff3a797db2918f6eab8b160d..5d50863ada86ce49ec7dadf96629c4af069d0625 100644 (file)
@@ -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):
index 5a69bdd91fe52970c19c9114cd85abe0a2967b4b..df4384f071a6e1e157aef4d4edb8ea74d30a3e0d 100644 (file)
@@ -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)
+        )
+        
index e1a05344ffee59334cc4413aa28c7461d60904b0..996e291e0704d2a8f002c8e68870908b585c6213 100644 (file)
@@ -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
index a373162a58945b13f54d3777104cfef054bf6317..8f7124528a7bf1d90469a0eb3e3e4dde29ccc327 100644 (file)
@@ -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()
index 7be315ea8cd67807c90f256126ed1a011bea550f..27e0114e2b5237d72bc602fad9c0aadbda1ce200 100644 (file)
@@ -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()
index 0113fc3e7e435537eba1508761da59a2eb42b319..33f4a997a9e97228a469437aceed365d17b35da7 100644 (file)
@@ -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), '<jenkinsapi.plugin.Plugin subversion>')
 
 if __name__ == '__main__':
     unittest.main()
index 97fec82ecd94c1a2fe151040d8de07a656a68d04..cb423a6ab1e23e4e8f37d06dde7a366a0b10c964 100755 (executable)
@@ -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