[Kimchi-devel] [PATCH v2][Wok 8/8] Add Plugins Manager test cases

Aline Manera alinefm at linux.vnet.ibm.com
Wed Jun 8 19:16:23 UTC 2016


Unless the idea to only have sample plugin enabled on test mode, this 
patch looks good for me.

On 06/06/2016 04:13 PM, Rodrigo Trujillo wrote:
> Test cases for new API
>
> Signed-off-by: Rodrigo Trujillo <rodrigo.trujillo at linux.vnet.ibm.com>
> ---
>   tests/test_plugin.py | 103 +++++++++++++++++++++++++++++++++++++++++++++++++--
>   1 file changed, 99 insertions(+), 4 deletions(-)
>
> diff --git a/tests/test_plugin.py b/tests/test_plugin.py
> index d785cfa..01a3be3 100644
> --- a/tests/test_plugin.py
> +++ b/tests/test_plugin.py
> @@ -20,11 +20,13 @@
>   # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
>
>   import json
> +import os
>   import unittest
>   from functools import partial
>
> -#from wok.pluginsmanager import get_enabled_plugins
> +from wok.config import paths
>   from wok.pluginsmanager import Plugins
> +from wok.utils import wok_log
>
>   import utils
>
> @@ -50,9 +52,9 @@ def tearDownModule():
>       test_server.stop()
>
>
> - at unittest.skipUnless(
> -    'sample' in Plugins().get_enabled_plugins(),
> -    'sample plugin is not enabled, skip this test!')
> +#
> +# In test mode, the plugin Sample should always be enabled
> +#
>   class PluginTests(unittest.TestCase):
>
>       def setUp(self):
> @@ -121,3 +123,96 @@ class PluginTests(unittest.TestCase):
>           req = json.dumps({'name': 'nowidth', 'length': 40})
>           resp = self.request('/plugins/sample/rectangles', req, 'POST')
>           self.assertEquals(400, resp.status)
> +
> +
> +def _plugins_number():
> +    """ Find number of plugins in <wokpath>/wok/plugins"""
> +    plugin_dir = paths.plugins_dir
> +    try:
> +        return len([d for d in os.listdir(plugin_dir)
> +                    if os.path.isdir(os.path.join(plugin_dir, d))])
> +    except OSError as e:
> +        wok_log.error("Failed reading plugins directory '%s': %s" %
> +                      (plugin_dir, e.message))
> +        return 0
> +
> +
> +class PluginsManagerTests(unittest.TestCase):
> +    """
> +    This class test functionalities of Plugins Manager and plugins API, using
> +    the Sample plugin, which should be enabled by default during tests
> +    """
> +
> +    def setUp(self):
> +        self.request = partial(utils.request, host, ssl_port)
> +        self.info = Plugins().get_plugin_info('sample')
> +        with open(self.info['conf_file']) as f:
> +            self.enable_sample_plugin = ('enable = True' in f.read())
> +
> +    def test_api(self):
> +        # Only SAMPLE should be enabled
> +        out = [{'name': 'sample', 'enabled': True}]
> +
> +        # List of plugins
> +        resp = self.request('/pluginsmanager', {}, 'GET')
> +        plugins = json.loads(resp.read())
> +        self.assertEquals(_plugins_number(), len(plugins))
> +        self.assertIn(out[0], plugins)
> +
> +        # Wrong plugin name
> +        resp = self.request('/pluginsmanager/samplewrong', {}, 'GET')
> +        self.assertEquals(404, resp.status)
> +        self.assertIn('WOKPLUG0001E', json.loads(resp.read())['reason'])
> +
> +        # Lookup plugin name
> +        resp = self.request('/pluginsmanager/sample', {}, 'GET')
> +        self.assertEquals(200, resp.status)
> +        self.assertDictEqual(out[0], json.loads(resp.read()))
> +
> +        # Enabling wrong plugin
> +        resp = self.request('/pluginsmanager/samplewrong/enable', {}, 'POST')
> +        self.assertEquals(404, resp.status)
> +        self.assertIn('WOKPLUG0001E', json.loads(resp.read())['reason'])
> +
> +        # Enabling Sample (already enabled, no problem)
> +        resp = self.request('/pluginsmanager/sample/enable', {}, 'POST')
> +        self.assertEquals(200, resp.status)
> +        self.assertDictEqual(out[0], json.loads(resp.read()))
> +
> +        # Disabling Sample
> +        resp = self.request('/pluginsmanager/sample/disable', {}, 'POST')
> +        self.assertEquals(200, resp.status)
> +        out[0]['enabled'] = False
> +        self.assertDictEqual(out[0], json.loads(resp.read()))
> +
> +        # Disabling Sample (already disabled, no problem)
> +        resp = self.request('/pluginsmanager/sample/disable', {}, 'POST')
> +        self.assertEquals(200, resp.status)
> +        self.assertDictEqual(out[0], json.loads(resp.read()))
> +
> +        # Check config file
> +        with open(self.info['conf_file']) as f:
> +            self.assertIn('enable = False', f.read())
> +
> +        # Backing configuration file to initial state
> +        if self.enable_sample_plugin:
> +            Plugins()._enable_plugin_conf_file(self.info['conf_file'])
> +
> +    def test_not_allowed_methods(self):
> +        # Only GET allowed in Collection
> +        resp = self.request('/pluginsmanager', {}, 'POST')
> +        self.assertEquals(405, resp.status)
> +        resp = self.request('/pluginsmanager', {}, 'PUT')
> +        self.assertEquals(405, resp.status)
> +        resp = self.request('/pluginsmanager', {}, 'DELETE')
> +        self.assertEquals(405, resp.status)
> +
> +        # Only GET/POST allowed in resource
> +        resp = self.request('/pluginsmanager/sample', {}, 'PUT')
> +        self.assertEquals(405, resp.status)
> +        resp = self.request('/pluginsmanager/sample', {}, 'DELETE')
> +        self.assertEquals(405, resp.status)
> +        resp = self.request('/pluginsmanager/sample/enable', {}, 'PUT')
> +        self.assertEquals(405, resp.status)
> +        resp = self.request('/pluginsmanager/sample/disable', {}, 'DELETE')
> +        self.assertEquals(405, resp.status)




More information about the Kimchi-devel mailing list