2008-07-27 23:34:25 -05:00
|
|
|
# Authors:
|
|
|
|
# Jason Gerard DeRose <jderose@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2008 Red Hat
|
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
2010-12-09 06:59:11 -06:00
|
|
|
# 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 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2008-07-27 23:34:25 -05:00
|
|
|
#
|
|
|
|
# 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 the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2010-12-09 06:59:11 -06:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-07-27 23:34:25 -05:00
|
|
|
|
|
|
|
"""
|
2008-10-07 22:25:23 -05:00
|
|
|
Test the `ipalib.plugable` module.
|
2008-07-27 23:34:25 -05:00
|
|
|
"""
|
|
|
|
|
2014-12-16 07:45:37 -06:00
|
|
|
# FIXME: Pylint errors
|
|
|
|
# pylint: disable=no-member
|
|
|
|
|
2015-12-16 09:06:03 -06:00
|
|
|
from ipatests.util import raises, read_only
|
2013-05-21 06:40:27 -05:00
|
|
|
from ipatests.util import ClassChecker, create_test_api
|
2011-02-15 14:20:51 -06:00
|
|
|
from ipalib import plugable, errors, text
|
2008-07-27 23:34:25 -05:00
|
|
|
|
2015-04-24 07:39:48 -05:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
pytestmark = pytest.mark.tier0
|
2008-07-27 23:34:25 -05:00
|
|
|
|
2008-08-14 02:10:07 -05:00
|
|
|
class test_Plugin(ClassChecker):
|
2008-08-08 17:45:09 -05:00
|
|
|
"""
|
2008-10-08 00:20:00 -05:00
|
|
|
Test the `ipalib.plugable.Plugin` class.
|
2008-08-08 17:45:09 -05:00
|
|
|
"""
|
2008-08-14 02:10:07 -05:00
|
|
|
_cls = plugable.Plugin
|
2008-08-08 17:13:49 -05:00
|
|
|
|
2008-08-08 17:45:09 -05:00
|
|
|
def test_class(self):
|
2008-10-08 00:20:00 -05:00
|
|
|
"""
|
|
|
|
Test the `ipalib.plugable.Plugin` class.
|
|
|
|
"""
|
2008-08-08 17:45:09 -05:00
|
|
|
assert self.cls.__bases__ == (plugable.ReadOnly,)
|
2008-08-14 02:10:07 -05:00
|
|
|
assert type(self.cls.api) is property
|
2008-08-05 01:33:09 -05:00
|
|
|
|
2008-12-17 22:47:43 -06:00
|
|
|
def test_init(self):
|
2008-08-08 18:26:17 -05:00
|
|
|
"""
|
2008-12-17 22:47:43 -06:00
|
|
|
Test the `ipalib.plugable.Plugin.__init__` method.
|
2008-08-08 18:26:17 -05:00
|
|
|
"""
|
2015-06-22 05:58:43 -05:00
|
|
|
api = 'the api instance'
|
|
|
|
o = self.cls(api)
|
2008-12-17 22:47:43 -06:00
|
|
|
assert o.name == 'Plugin'
|
2011-02-15 14:20:51 -06:00
|
|
|
assert isinstance(o.doc, text.Gettext)
|
2008-08-08 18:26:17 -05:00
|
|
|
class some_subclass(self.cls):
|
2008-12-17 22:57:58 -06:00
|
|
|
"""
|
|
|
|
Do sub-classy things.
|
|
|
|
|
|
|
|
Although it doesn't know how to comport itself and is not for mixed
|
|
|
|
company, this class *is* useful as we all need a little sub-class
|
|
|
|
now and then.
|
2008-12-18 00:08:52 -06:00
|
|
|
|
|
|
|
One more paragraph.
|
2008-12-17 22:57:58 -06:00
|
|
|
"""
|
2015-06-22 05:58:43 -05:00
|
|
|
o = some_subclass(api)
|
2008-12-17 22:47:43 -06:00
|
|
|
assert o.name == 'some_subclass'
|
2008-12-18 00:08:52 -06:00
|
|
|
assert o.summary == 'Do sub-classy things.'
|
2011-02-15 14:20:51 -06:00
|
|
|
assert isinstance(o.doc, text.Gettext)
|
2008-12-18 00:08:52 -06:00
|
|
|
class another_subclass(self.cls):
|
|
|
|
pass
|
2015-06-22 05:58:43 -05:00
|
|
|
o = another_subclass(api)
|
2008-12-18 00:08:52 -06:00
|
|
|
assert o.summary == '<%s>' % o.fullname
|
2008-08-12 21:34:36 -05:00
|
|
|
|
2008-12-21 20:34:32 -06:00
|
|
|
# Test that Plugin makes sure the subclass hasn't defined attributes
|
|
|
|
# whose names conflict with the logger methods set in Plugin.__init__():
|
|
|
|
class check(self.cls):
|
|
|
|
info = 'whatever'
|
2015-08-24 05:40:33 -05:00
|
|
|
e = raises(Exception, check, api)
|
2008-12-21 20:34:32 -06:00
|
|
|
assert str(e) == \
|
2013-05-21 06:40:27 -05:00
|
|
|
"info is already bound to ipatests.test_ipalib.test_plugable.check()"
|
2008-12-21 20:34:32 -06:00
|
|
|
|
2008-08-09 14:09:10 -05:00
|
|
|
def test_finalize(self):
|
|
|
|
"""
|
2008-10-08 00:20:00 -05:00
|
|
|
Test the `ipalib.plugable.Plugin.finalize` method.
|
2008-08-09 14:09:10 -05:00
|
|
|
"""
|
2015-06-22 05:59:35 -05:00
|
|
|
class api(object):
|
|
|
|
@staticmethod
|
|
|
|
def is_production_mode():
|
|
|
|
return False
|
2015-06-22 05:58:43 -05:00
|
|
|
o = self.cls(api)
|
2008-09-21 16:50:56 -05:00
|
|
|
assert not o.__islocked__()
|
|
|
|
o.finalize()
|
|
|
|
assert o.__islocked__()
|
2008-07-31 17:36:15 -05:00
|
|
|
|
2008-12-21 18:12:00 -06:00
|
|
|
|
2016-03-07 01:47:27 -06:00
|
|
|
def test_Registry():
|
2008-10-08 00:20:00 -05:00
|
|
|
"""
|
2016-03-07 01:47:27 -06:00
|
|
|
Test the `ipalib.plugable.Registry` class
|
2008-10-08 00:20:00 -05:00
|
|
|
"""
|
2008-08-08 12:11:29 -05:00
|
|
|
class Base1(object):
|
2008-08-08 16:40:03 -05:00
|
|
|
pass
|
2008-08-08 12:11:29 -05:00
|
|
|
class Base2(object):
|
2008-08-08 16:40:03 -05:00
|
|
|
pass
|
2008-08-08 12:11:29 -05:00
|
|
|
class Base3(object):
|
2008-08-08 16:40:03 -05:00
|
|
|
pass
|
2008-08-08 12:11:29 -05:00
|
|
|
class plugin1(Base1):
|
2008-08-08 16:40:03 -05:00
|
|
|
pass
|
2008-08-08 12:11:29 -05:00
|
|
|
class plugin2(Base2):
|
2008-08-08 16:40:03 -05:00
|
|
|
pass
|
2008-08-08 12:11:29 -05:00
|
|
|
class plugin3(Base3):
|
2008-08-08 16:40:03 -05:00
|
|
|
pass
|
2008-08-08 12:11:29 -05:00
|
|
|
|
2016-03-07 01:47:27 -06:00
|
|
|
# Test creation of Registry:
|
|
|
|
r = plugable.Registry()
|
2008-08-08 12:11:29 -05:00
|
|
|
|
|
|
|
# Check that TypeError is raised trying to register something that isn't
|
|
|
|
# a class:
|
2009-01-03 18:27:53 -06:00
|
|
|
p = plugin1()
|
2016-03-07 01:47:27 -06:00
|
|
|
e = raises(TypeError, r(), p)
|
2009-01-03 18:27:53 -06:00
|
|
|
assert str(e) == 'plugin must be a class; got %r' % p
|
2008-08-08 12:11:29 -05:00
|
|
|
|
|
|
|
# Check that registration works
|
2016-03-07 01:47:27 -06:00
|
|
|
r()(plugin1)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
|
|
|
# Check that DuplicateError is raised trying to register exact class
|
|
|
|
# again:
|
2016-03-07 01:47:27 -06:00
|
|
|
e = raises(errors.PluginDuplicateError, r(), plugin1)
|
2009-01-03 18:27:53 -06:00
|
|
|
assert e.plugin is plugin1
|
2008-08-08 12:11:29 -05:00
|
|
|
|
2015-06-24 10:14:54 -05:00
|
|
|
# Check that overriding works
|
2008-08-08 12:11:29 -05:00
|
|
|
class base1_extended(Base1):
|
2008-08-08 16:40:03 -05:00
|
|
|
pass
|
2014-12-16 07:45:37 -06:00
|
|
|
class plugin1(base1_extended): # pylint: disable=function-redefined
|
2008-08-08 16:40:03 -05:00
|
|
|
pass
|
2016-03-07 01:47:27 -06:00
|
|
|
r(override=True)(plugin1)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
2008-08-14 13:50:21 -05:00
|
|
|
# Test that another plugin can be registered:
|
2016-03-07 01:47:27 -06:00
|
|
|
r()(plugin2)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
2008-08-15 00:07:17 -05:00
|
|
|
# Setup to test more registration:
|
2008-08-08 12:11:29 -05:00
|
|
|
class plugin1a(Base1):
|
2008-08-08 16:40:03 -05:00
|
|
|
pass
|
2016-03-07 01:47:27 -06:00
|
|
|
r()(plugin1a)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
|
|
|
class plugin1b(Base1):
|
2008-08-08 16:40:03 -05:00
|
|
|
pass
|
2016-03-07 01:47:27 -06:00
|
|
|
r()(plugin1b)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
|
|
|
class plugin2a(Base2):
|
2008-08-08 16:40:03 -05:00
|
|
|
pass
|
2016-03-07 01:47:27 -06:00
|
|
|
r()(plugin2a)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
|
|
|
class plugin2b(Base2):
|
2008-08-08 16:40:03 -05:00
|
|
|
pass
|
2016-03-07 01:47:27 -06:00
|
|
|
r()(plugin2b)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
2008-08-01 15:42:35 -05:00
|
|
|
|
2008-10-27 00:28:06 -05:00
|
|
|
class test_API(ClassChecker):
|
2008-10-08 00:20:00 -05:00
|
|
|
"""
|
|
|
|
Test the `ipalib.plugable.API` class.
|
|
|
|
"""
|
2008-08-08 12:11:29 -05:00
|
|
|
|
2008-10-27 00:28:06 -05:00
|
|
|
_cls = plugable.API
|
2008-08-08 12:11:29 -05:00
|
|
|
|
2008-10-27 00:28:06 -05:00
|
|
|
def test_API(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.plugable.API` class.
|
|
|
|
"""
|
|
|
|
assert issubclass(plugable.API, plugable.ReadOnly)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
2008-10-27 00:28:06 -05:00
|
|
|
# Setup the test bases, create the API:
|
|
|
|
class base0(plugable.Plugin):
|
|
|
|
def method(self, n):
|
|
|
|
return n
|
2008-08-08 12:11:29 -05:00
|
|
|
|
2008-10-27 00:28:06 -05:00
|
|
|
class base1(plugable.Plugin):
|
|
|
|
def method(self, n):
|
|
|
|
return n + 1
|
2008-08-08 12:11:29 -05:00
|
|
|
|
2015-06-15 08:36:26 -05:00
|
|
|
class API(plugable.API):
|
|
|
|
bases = (base0, base1)
|
|
|
|
modules = ()
|
|
|
|
|
|
|
|
api = API()
|
2008-10-30 02:34:46 -05:00
|
|
|
api.env.mode = 'unit_test'
|
|
|
|
api.env.in_tree = True
|
2015-06-22 05:16:34 -05:00
|
|
|
r = api.add_plugin
|
2008-08-08 12:11:29 -05:00
|
|
|
|
2008-10-27 00:28:06 -05:00
|
|
|
class base0_plugin0(base0):
|
|
|
|
pass
|
2015-06-24 10:14:54 -05:00
|
|
|
r(base0_plugin0)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
2008-10-27 00:28:06 -05:00
|
|
|
class base0_plugin1(base0):
|
|
|
|
pass
|
2015-06-24 10:14:54 -05:00
|
|
|
r(base0_plugin1)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
2008-10-27 00:28:06 -05:00
|
|
|
class base0_plugin2(base0):
|
|
|
|
pass
|
2015-06-24 10:14:54 -05:00
|
|
|
r(base0_plugin2)
|
2008-08-08 12:11:29 -05:00
|
|
|
|
2008-10-27 00:28:06 -05:00
|
|
|
class base1_plugin0(base1):
|
|
|
|
pass
|
2015-06-24 10:14:54 -05:00
|
|
|
r(base1_plugin0)
|
2008-10-27 00:28:06 -05:00
|
|
|
|
|
|
|
class base1_plugin1(base1):
|
|
|
|
pass
|
2015-06-24 10:14:54 -05:00
|
|
|
r(base1_plugin1)
|
2008-10-27 00:28:06 -05:00
|
|
|
|
|
|
|
class base1_plugin2(base1):
|
|
|
|
pass
|
2015-06-24 10:14:54 -05:00
|
|
|
r(base1_plugin2)
|
2008-10-27 00:28:06 -05:00
|
|
|
|
|
|
|
# Test API instance:
|
|
|
|
assert api.isdone('bootstrap') is False
|
|
|
|
assert api.isdone('finalize') is False
|
|
|
|
api.finalize()
|
|
|
|
assert api.isdone('bootstrap') is True
|
|
|
|
assert api.isdone('finalize') is True
|
|
|
|
|
2009-08-04 03:41:11 -05:00
|
|
|
def get_base_name(b):
|
2008-10-27 00:28:06 -05:00
|
|
|
return 'base%d' % b
|
|
|
|
|
2009-08-04 03:41:11 -05:00
|
|
|
|
|
|
|
def get_plugin_name(b, p):
|
2008-10-27 00:28:06 -05:00
|
|
|
return 'base%d_plugin%d' % (b, p)
|
|
|
|
|
2015-08-12 08:23:56 -05:00
|
|
|
for b in range(2):
|
2009-08-04 03:41:11 -05:00
|
|
|
base_name = get_base_name(b)
|
|
|
|
base = locals()[base_name]
|
2008-10-27 00:28:06 -05:00
|
|
|
ns = getattr(api, base_name)
|
|
|
|
assert isinstance(ns, plugable.NameSpace)
|
|
|
|
assert read_only(api, base_name) is ns
|
|
|
|
assert len(ns) == 3
|
2015-08-12 08:23:56 -05:00
|
|
|
for p in range(3):
|
2009-08-04 03:41:11 -05:00
|
|
|
plugin_name = get_plugin_name(b, p)
|
|
|
|
plugin = locals()[plugin_name]
|
|
|
|
inst = ns[plugin_name]
|
|
|
|
assert isinstance(inst, base)
|
|
|
|
assert isinstance(inst, plugin)
|
|
|
|
assert inst.name == plugin_name
|
|
|
|
assert read_only(ns, plugin_name) is inst
|
|
|
|
assert inst.method(7) == 7 + b
|
2008-10-27 00:28:06 -05:00
|
|
|
|
|
|
|
# Test that calling finilize again raises AssertionError:
|
2015-08-24 05:40:33 -05:00
|
|
|
e = raises(Exception, api.finalize)
|
2008-10-27 00:28:06 -05:00
|
|
|
assert str(e) == 'API.finalize() already called', str(e)
|
|
|
|
|
|
|
|
def test_bootstrap(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.plugable.API.bootstrap` method.
|
|
|
|
"""
|
2008-12-08 17:56:24 -06:00
|
|
|
(o, home) = create_test_api()
|
2008-10-27 16:36:41 -05:00
|
|
|
assert o.env._isdone('_bootstrap') is False
|
|
|
|
assert o.env._isdone('_finalize_core') is False
|
2008-10-27 00:28:06 -05:00
|
|
|
assert o.isdone('bootstrap') is False
|
2008-10-27 17:08:17 -05:00
|
|
|
o.bootstrap(my_test_override='Hello, world!')
|
2008-10-27 00:28:06 -05:00
|
|
|
assert o.isdone('bootstrap') is True
|
2008-10-27 16:36:41 -05:00
|
|
|
assert o.env._isdone('_bootstrap') is True
|
|
|
|
assert o.env._isdone('_finalize_core') is True
|
2008-10-27 17:08:17 -05:00
|
|
|
assert o.env.my_test_override == 'Hello, world!'
|
2015-08-24 05:40:33 -05:00
|
|
|
e = raises(Exception, o.bootstrap)
|
2008-10-27 00:28:06 -05:00
|
|
|
assert str(e) == 'API.bootstrap() already called'
|
2008-10-27 00:53:44 -05:00
|
|
|
|
|
|
|
def test_load_plugins(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.plugable.API.load_plugins` method.
|
|
|
|
"""
|
2008-12-08 17:56:24 -06:00
|
|
|
(o, home) = create_test_api()
|
2008-10-27 00:53:44 -05:00
|
|
|
assert o.isdone('bootstrap') is False
|
|
|
|
assert o.isdone('load_plugins') is False
|
2008-10-28 00:39:43 -05:00
|
|
|
o.load_plugins()
|
2008-10-27 00:53:44 -05:00
|
|
|
assert o.isdone('bootstrap') is True
|
|
|
|
assert o.isdone('load_plugins') is True
|
2015-08-24 05:40:33 -05:00
|
|
|
e = raises(Exception, o.load_plugins)
|
2008-10-27 00:53:44 -05:00
|
|
|
assert str(e) == 'API.load_plugins() already called'
|