提交 b5b914c0 编写于 作者: L Lucas Meneghel Rodrigues 提交者: Lucas Meneghel Rodrigues

Merge pull request #161 from ruda/refactory_plugins

Refactory plugins
......@@ -50,5 +50,6 @@ class PluginsList(plugin.Plugin):
status = bcolors.healthy_str("Enabled")
else:
status = bcolors.fail_header_str("Disabled")
pipe.write(format_str % (bcolors.header_str(plug.name), plug.__doc__.strip(),
pipe.write(format_str % (bcolors.header_str(plug.name),
plug.description,
status))
......@@ -91,18 +91,26 @@ class ExternalPluginManager(PluginManager):
import imp
if path:
candidates = glob(os.path.join(path, pattern))
candidates = [(os.path.splitext(os.path.basename(x))[0], path) for x in candidates]
candidates = [(x[0], imp.find_module(x[0], [path])) for x in candidates]
candidates = [(os.path.splitext(os.path.basename(x))[0], path)
for x in candidates]
candidates = [(x[0], imp.find_module(x[0], [path]))
for x in candidates]
for candidate in candidates:
try:
mod = imp.load_module(candidate[0], *candidate[1])
except Exception as err:
log.error("Could not load plugin '%s': %s", candidate[0], err)
log.error("Could not load module plugin '%s': %s",
candidate[0], err)
else:
any_plugin = False
for name in mod.__dict__:
x = getattr(mod, name)
if isinstance(x, type) and issubclass(x, Plugin):
self.add_plugin(x())
any_plugin = True
if not any_plugin:
log.error("Could not find any plugin in module '%s'",
candidate[0])
def add_plugins(self, plugins):
for plugin in plugins:
......
......@@ -23,15 +23,28 @@ class Plugin(object):
You'll inherit from this to write you own plugins.
"""
name = 'plugin'
enabled = True
name = 'noname'
enabled = False
def __init__(self):
"""Creates a new plugin instance."""
self.name = self.__class__.name
self.enabled = self.__class__.enabled
def __init__(self, name=None, enabled=None):
"""Creates a new plugin instance.
:param name: plugin short name.
:param enabled: plugin status: enabled or not.
"""
if name is not None:
self.name = name
if enabled is not None:
self.enabled = enabled
if self.__doc__ is not None:
self.description = self.__doc__.strip()
else:
self.description = 'There is no description for this plugin'
self.configured = False
def __repr__(self):
return "%s(name='%s')" % (self.__class__.__name__, self.name)
def configure(self, app_parser, cmd_parser):
"""Configuration and argument parsing.
......
......@@ -50,6 +50,19 @@ from avocado.plugins.plugin import Plugin
class VoidPlugin(Plugin)
"""
HELLO_PLUGIN_CONTENTS = """#!/usr/bin/env python
from avocado.plugins.plugin import Plugin
class HelloWorld(Plugin):
name = 'hello'
enabled = True
def configure(self, app_parser, cmd_parser):
parser = cmd_parser.add_parser('hello')
parser.set_defaults(func=self.hello)
self.configured = True
def hello(self, args):
print 'Hello World!'
"""
class RunnerOperationTest(unittest.TestCase):
......@@ -247,33 +260,54 @@ class RunnerDropinTest(unittest.TestCase):
shutil.rmtree(self.base_logdir, ignore_errors=True)
class PluginsTest(unittest.TestCase):
class ExternalPluginsTest(unittest.TestCase):
def setUp(self):
self.base_outputdir = tempfile.mkdtemp(prefix='avocado_plugins')
self.base_sourcedir = tempfile.mkdtemp(prefix='avocado_source_plugins')
def test_void_plugin(self):
self.void_plugin = os.path.join(self.base_sourcedir, 'avocado_void.py')
with open(self.void_plugin, 'w') as void:
void.write(VOID_PLUGIN_CONTENTS)
os.chmod(self.void_plugin, 0775)
os.chdir(basedir)
cmd_line = './scripts/avocado --plugins %s plugins' % self.base_sourcedir
result = process.run(cmd_line, ignore_status=True)
expected_output = 'noname'
self.assertIn(expected_output, result.stdout)
def test_syntax_error_plugin(self):
self.syntax_err_plugin = os.path.join(self.base_sourcedir, 'avocado_syntax_err.py')
with open(self.syntax_err_plugin, 'w') as synerr:
synerr.write(SYNTAX_ERROR_PLUGIN_CONTENTS)
os.chmod(self.syntax_err_plugin, 0775)
def test_void_plugin(self):
os.chdir(basedir)
cmd_line = './scripts/avocado --plugins %s' % self.base_sourcedir
result = process.run(cmd_line, ignore_status=True)
expected_output = 'Plugins must implement the method configure'
expected_output = 'invalid syntax'
self.assertIn(expected_output, result.stderr)
def test_syntax_error_plugin(self):
def test_hello_plugin(self):
self.hello_plugin = os.path.join(self.base_sourcedir, 'avocado_hello.py')
with open(self.hello_plugin, 'w') as hello:
hello.write(HELLO_PLUGIN_CONTENTS)
os.chmod(self.hello_plugin, 0775)
os.chdir(basedir)
cmd_line = './scripts/avocado --plugins %s' % self.base_sourcedir
cmd_line = './scripts/avocado --plugins %s hello' % self.base_sourcedir
result = process.run(cmd_line, ignore_status=True)
expected_output = 'invalid syntax'
self.assertIn(expected_output, result.stderr)
expected_output = 'Hello World!'
self.assertIn(expected_output, result.stdout)
def tearDown(self):
if os.path.isdir(self.base_sourcedir):
shutil.rmtree(self.base_sourcedir, ignore_errors=True)
class PluginsTest(unittest.TestCase):
def setUp(self):
self.base_outputdir = tempfile.mkdtemp(prefix='avocado_plugins')
def test_sysinfo_plugin(self):
os.chdir(basedir)
......@@ -319,12 +353,6 @@ class PluginsTest(unittest.TestCase):
(expected_rc, result))
self.assertNotIn('Disabled', output)
def tearDown(self):
if os.path.isdir(self.base_outputdir):
shutil.rmtree(self.base_outputdir, ignore_errors=True)
if os.path.isdir(self.base_sourcedir):
shutil.rmtree(self.base_sourcedir, ignore_errors=True)
class ParseXMLError(Exception):
pass
......
#!/usr/bin/env python
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See LICENSE for more details.
#
# Copyright: Red Hat Inc. 2014
# Author: Ruda Moura <rmoura@redhat.com>
import sys
import os
import unittest
# simple magic for using scripts within a source tree
basedir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
basedir = os.path.dirname(basedir)
if os.path.isdir(os.path.join(basedir, 'avocado')):
sys.path.append(basedir)
from avocado.plugins import plugin
class NullPlugin(plugin.Plugin):
pass
class FakePlugin(plugin.Plugin):
"""
Fake plugin
"""
name = 'fake'
enabled = True
def configure(self, app_parser, cmd_parser):
self.configured = True
def activate(self, app_args):
self.activated = True
class PluginsBasicTest(unittest.TestCase):
def setUp(self):
self.p = plugin.Plugin()
self.null = NullPlugin()
self.fake = FakePlugin()
self.disabled_fake = FakePlugin(enabled=False)
def testCreate(self):
self.assertTrue(self.p)
self.assertFalse(self.p.enabled)
self.assertEqual(self.p.name, 'noname')
self.assertTrue(self.p.description)
self.assertTrue(self.null.name, 'noname')
self.assertFalse(self.null.enabled)
self.assertTrue(self.fake)
self.assertEqual(self.fake.name, 'fake')
self.assertTrue(self.fake.enabled)
self.assertEqual(self.fake.description, 'Fake plugin')
self.assertFalse(self.disabled_fake.enabled)
def testConfigure(self):
self.assertRaises(NotImplementedError, self.p.configure, None, None)
self.assertRaises(NotImplementedError, self.null.configure, None, None)
self.fake.configure(None, None)
self.assertTrue(self.fake.configured, True)
def testActivate(self):
self.p.activate(None)
self.null.activate(None)
self.fake.activate(None)
self.assertTrue(self.fake.activated)
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册