2008-09-23 19:12:35 -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-09-23 19:12:35 -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-09-23 19:12:35 -05:00
|
|
|
|
|
|
|
"""
|
2008-10-07 22:25:23 -05:00
|
|
|
Test the `ipalib.backend` module.
|
2008-09-23 19:12:35 -05:00
|
|
|
"""
|
2015-08-12 06:44:11 -05:00
|
|
|
from __future__ import print_function
|
2008-09-23 19:12:35 -05:00
|
|
|
|
2014-12-16 07:45:37 -06:00
|
|
|
# FIXME: Pylint errors
|
|
|
|
# pylint: disable=no-member
|
|
|
|
# pylint: disable=maybe-no-member
|
|
|
|
|
2009-01-23 16:49:16 -06:00
|
|
|
import threading
|
2013-05-21 06:40:27 -05:00
|
|
|
from ipatests.util import ClassChecker, raises, create_test_api
|
|
|
|
from ipatests.data import unicode_str
|
2009-01-23 16:49:16 -06:00
|
|
|
from ipalib.request import context, Connection
|
|
|
|
from ipalib.frontend import Command
|
2015-12-16 09:06:03 -06:00
|
|
|
from ipalib import backend, plugable, errors
|
2012-06-21 07:20:26 -05:00
|
|
|
from ipapython.version import API_VERSION
|
2009-01-23 16:49:16 -06:00
|
|
|
|
2015-04-24 07:39:48 -05:00
|
|
|
import pytest
|
2008-09-23 19:12:35 -05:00
|
|
|
|
2015-04-24 07:39:48 -05:00
|
|
|
pytestmark = pytest.mark.tier0
|
2008-09-23 19:12:35 -05:00
|
|
|
|
|
|
|
class test_Backend(ClassChecker):
|
|
|
|
"""
|
2008-10-30 02:11:33 -05:00
|
|
|
Test the `ipalib.backend.Backend` class.
|
2008-09-23 19:12:35 -05:00
|
|
|
"""
|
|
|
|
|
|
|
|
_cls = backend.Backend
|
|
|
|
|
|
|
|
def test_class(self):
|
|
|
|
assert self.cls.__bases__ == (plugable.Plugin,)
|
2008-10-30 02:11:33 -05:00
|
|
|
|
|
|
|
|
2018-09-26 04:59:50 -05:00
|
|
|
class Disconnect:
|
2009-01-23 19:02:32 -06:00
|
|
|
called = False
|
2009-01-23 16:49:16 -06:00
|
|
|
|
2010-01-13 10:38:35 -06:00
|
|
|
def __init__(self, id=None):
|
|
|
|
self.id = id
|
|
|
|
|
2009-01-23 19:02:32 -06:00
|
|
|
def __call__(self):
|
|
|
|
assert self.called is False
|
|
|
|
self.called = True
|
2010-01-13 10:38:35 -06:00
|
|
|
if self.id is not None:
|
|
|
|
delattr(context, self.id)
|
2009-01-23 16:49:16 -06:00
|
|
|
|
|
|
|
|
|
|
|
class test_Connectible(ClassChecker):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.backend.Connectible` class.
|
|
|
|
"""
|
|
|
|
|
|
|
|
_cls = backend.Connectible
|
|
|
|
|
|
|
|
def test_connect(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.backend.Connectible.connect` method.
|
|
|
|
"""
|
|
|
|
# Test that connection is created:
|
2015-06-22 05:58:43 -05:00
|
|
|
api = 'the api instance'
|
2009-01-23 16:49:16 -06:00
|
|
|
class example(self.cls):
|
2009-01-23 19:02:32 -06:00
|
|
|
def create_connection(self, *args, **kw):
|
|
|
|
object.__setattr__(self, 'args', args)
|
|
|
|
object.__setattr__(self, 'kw', kw)
|
|
|
|
return 'The connection.'
|
2015-06-22 05:58:43 -05:00
|
|
|
o = example(api, shared_instance=True)
|
2009-01-23 16:49:16 -06:00
|
|
|
args = ('Arg1', 'Arg2', 'Arg3')
|
|
|
|
kw = dict(key1='Val1', key2='Val2', key3='Val3')
|
|
|
|
assert not hasattr(context, 'example')
|
|
|
|
assert o.connect(*args, **kw) is None
|
|
|
|
conn = context.example
|
2009-01-23 19:02:32 -06:00
|
|
|
assert type(conn) is Connection
|
|
|
|
assert o.args == args
|
|
|
|
assert o.kw == kw
|
|
|
|
assert conn.conn == 'The connection.'
|
|
|
|
assert conn.disconnect == o.disconnect
|
2009-01-23 16:49:16 -06:00
|
|
|
|
2015-08-24 05:40:33 -05:00
|
|
|
# Test that Exception is raised if already connected:
|
2015-11-20 06:47:34 -06:00
|
|
|
m = "{0} is already connected ({1} in {2})"
|
2015-08-24 05:40:33 -05:00
|
|
|
e = raises(Exception, o.connect, *args, **kw)
|
2015-11-20 06:47:34 -06:00
|
|
|
assert str(e) == m.format(
|
|
|
|
'example', o.id, threading.currentThread().getName())
|
2009-01-23 16:49:16 -06:00
|
|
|
|
|
|
|
# Double check that it works after deleting context.example:
|
|
|
|
del context.example
|
|
|
|
assert o.connect(*args, **kw) is None
|
|
|
|
|
2009-01-23 19:02:32 -06:00
|
|
|
def test_create_connection(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.backend.Connectible.create_connection` method.
|
|
|
|
"""
|
2015-06-22 05:58:43 -05:00
|
|
|
api = 'the api instance'
|
2009-01-23 19:02:32 -06:00
|
|
|
class example(self.cls):
|
|
|
|
pass
|
|
|
|
for klass in (self.cls, example):
|
2015-06-22 05:58:43 -05:00
|
|
|
o = klass(api, shared_instance=True)
|
2009-01-23 19:02:32 -06:00
|
|
|
e = raises(NotImplementedError, o.create_connection)
|
|
|
|
assert str(e) == '%s.create_connection()' % klass.__name__
|
|
|
|
|
|
|
|
def test_disconnect(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.backend.Connectible.disconnect` method.
|
|
|
|
"""
|
2015-06-22 05:58:43 -05:00
|
|
|
api = 'the api instance'
|
2009-01-23 19:02:32 -06:00
|
|
|
class example(self.cls):
|
|
|
|
destroy_connection = Disconnect()
|
2015-06-22 05:58:43 -05:00
|
|
|
o = example(api, shared_instance=True)
|
2009-01-23 19:02:32 -06:00
|
|
|
|
2015-11-20 06:47:34 -06:00
|
|
|
m = "{0} is not connected ({1} in {2})"
|
2015-08-24 05:40:33 -05:00
|
|
|
e = raises(Exception, o.disconnect)
|
2015-11-20 06:47:34 -06:00
|
|
|
assert str(e) == m.format(
|
|
|
|
'example', o.id, threading.currentThread().getName())
|
2009-01-23 19:02:32 -06:00
|
|
|
|
|
|
|
context.example = 'The connection.'
|
|
|
|
assert o.disconnect() is None
|
|
|
|
assert example.destroy_connection.called is True
|
|
|
|
|
|
|
|
def test_destroy_connection(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.backend.Connectible.destroy_connection` method.
|
|
|
|
"""
|
2015-06-22 05:58:43 -05:00
|
|
|
api = 'the api instance'
|
2009-01-23 19:02:32 -06:00
|
|
|
class example(self.cls):
|
|
|
|
pass
|
|
|
|
for klass in (self.cls, example):
|
2015-06-22 05:58:43 -05:00
|
|
|
o = klass(api, shared_instance=True)
|
2009-01-23 19:02:32 -06:00
|
|
|
e = raises(NotImplementedError, o.destroy_connection)
|
|
|
|
assert str(e) == '%s.destroy_connection()' % klass.__name__
|
|
|
|
|
2009-01-23 16:49:16 -06:00
|
|
|
def test_isconnected(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.backend.Connectible.isconnected` method.
|
|
|
|
"""
|
2015-06-22 05:58:43 -05:00
|
|
|
api = 'the api instance'
|
2009-01-23 16:49:16 -06:00
|
|
|
class example(self.cls):
|
|
|
|
pass
|
|
|
|
for klass in (self.cls, example):
|
2015-06-22 05:58:43 -05:00
|
|
|
o = klass(api, shared_instance=True)
|
2009-01-23 16:49:16 -06:00
|
|
|
assert o.isconnected() is False
|
2009-01-23 19:02:32 -06:00
|
|
|
conn = 'whatever'
|
2009-01-23 16:49:16 -06:00
|
|
|
setattr(context, klass.__name__, conn)
|
|
|
|
assert o.isconnected() is True
|
|
|
|
delattr(context, klass.__name__)
|
|
|
|
|
|
|
|
def test_conn(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.backend.Connectible.conn` property.
|
|
|
|
"""
|
2015-06-22 05:58:43 -05:00
|
|
|
api = 'the api instance'
|
2015-11-20 06:47:34 -06:00
|
|
|
msg = '{0} is not connected ({1} in {2})'
|
2009-01-23 16:49:16 -06:00
|
|
|
class example(self.cls):
|
|
|
|
pass
|
|
|
|
for klass in (self.cls, example):
|
2015-06-22 05:58:43 -05:00
|
|
|
o = klass(api, shared_instance=True)
|
2009-01-23 16:49:16 -06:00
|
|
|
e = raises(AttributeError, getattr, o, 'conn')
|
2015-11-20 06:47:34 -06:00
|
|
|
assert str(e) == msg.format(
|
|
|
|
klass.__name__, o.id, threading.currentThread().getName()
|
2009-01-23 16:49:16 -06:00
|
|
|
)
|
2009-01-23 19:02:32 -06:00
|
|
|
conn = Connection('The connection.', Disconnect())
|
2009-01-23 16:49:16 -06:00
|
|
|
setattr(context, klass.__name__, conn)
|
|
|
|
assert o.conn is conn.conn
|
|
|
|
delattr(context, klass.__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class test_Executioner(ClassChecker):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.backend.Executioner` class.
|
|
|
|
"""
|
|
|
|
_cls = backend.Executioner
|
|
|
|
|
|
|
|
def test_execute(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.backend.Executioner.execute` method.
|
|
|
|
"""
|
2016-09-26 11:22:22 -05:00
|
|
|
api, _home = create_test_api(in_server=True)
|
2009-01-23 16:49:16 -06:00
|
|
|
|
|
|
|
class echo(Command):
|
2009-05-20 16:19:09 -05:00
|
|
|
takes_args = ('arg1', 'arg2+')
|
|
|
|
takes_options = ('option1?', 'option2?')
|
2009-01-23 16:49:16 -06:00
|
|
|
def execute(self, *args, **options):
|
|
|
|
assert type(args[1]) is tuple
|
2009-12-09 10:09:53 -06:00
|
|
|
return dict(result=args + (options,))
|
2015-06-22 05:16:34 -05:00
|
|
|
api.add_plugin(echo)
|
2009-01-23 16:49:16 -06:00
|
|
|
|
|
|
|
class good(Command):
|
2012-06-21 07:20:26 -05:00
|
|
|
def execute(self, **options):
|
2009-04-23 07:51:59 -05:00
|
|
|
raise errors.ValidationError(
|
2009-01-23 16:49:16 -06:00
|
|
|
name='nurse',
|
|
|
|
error=u'Not naughty!',
|
|
|
|
)
|
2015-06-22 05:16:34 -05:00
|
|
|
api.add_plugin(good)
|
2009-01-23 16:49:16 -06:00
|
|
|
|
|
|
|
class bad(Command):
|
2012-06-21 07:20:26 -05:00
|
|
|
def execute(self, **options):
|
2009-01-23 16:49:16 -06:00
|
|
|
raise ValueError('This is private.')
|
2015-06-22 05:16:34 -05:00
|
|
|
api.add_plugin(bad)
|
2009-01-23 16:49:16 -06:00
|
|
|
|
2009-03-13 01:53:05 -05:00
|
|
|
class with_name(Command):
|
|
|
|
"""
|
|
|
|
Test that a kwarg named 'name' can be used.
|
|
|
|
"""
|
2009-05-20 16:19:09 -05:00
|
|
|
takes_options = 'name'
|
2009-03-13 01:53:05 -05:00
|
|
|
def execute(self, **options):
|
2009-12-09 10:09:53 -06:00
|
|
|
return dict(result=options['name'].upper())
|
2015-06-22 05:16:34 -05:00
|
|
|
api.add_plugin(with_name)
|
2009-03-13 01:53:05 -05:00
|
|
|
|
2009-01-23 16:49:16 -06:00
|
|
|
api.finalize()
|
2015-06-22 05:58:43 -05:00
|
|
|
o = self.cls(api)
|
2009-01-23 16:49:16 -06:00
|
|
|
o.finalize()
|
|
|
|
|
|
|
|
# Test that CommandError is raised:
|
2010-01-13 10:38:35 -06:00
|
|
|
conn = Connection('The connection.', Disconnect('someconn'))
|
2009-01-23 16:49:16 -06:00
|
|
|
context.someconn = conn
|
2015-08-12 06:44:11 -05:00
|
|
|
print(str(list(context.__dict__)))
|
2009-04-23 07:51:59 -05:00
|
|
|
e = raises(errors.CommandError, o.execute, 'nope')
|
2009-01-23 16:49:16 -06:00
|
|
|
assert e.name == 'nope'
|
2009-01-23 19:02:32 -06:00
|
|
|
assert conn.disconnect.called is True # Make sure destroy_context() was called
|
2015-08-12 06:44:11 -05:00
|
|
|
print(str(list(context.__dict__)))
|
Use Python3-compatible dict method names
Python 2 has keys()/values()/items(), which return lists,
iterkeys()/itervalues()/iteritems(), which return iterators,
and viewkeys()/viewvalues()/viewitems() which return views.
Python 3 has only keys()/values()/items(), which return views.
To get iterators, one can use iter() or a for loop/comprehension;
for lists there's the list() constructor.
When iterating through the entire dict, without modifying the dict,
the difference between Python 2's items() and iteritems() is
negligible, especially on small dicts (the main overhead is
extra memory, not CPU time). In the interest of simpler code,
this patch changes many instances of iteritems() to items(),
iterkeys() to keys() etc.
In other cases, helpers like six.itervalues are used.
Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-08-11 06:51:14 -05:00
|
|
|
assert list(context.__dict__) == []
|
2009-01-23 16:49:16 -06:00
|
|
|
|
|
|
|
# Test with echo command:
|
|
|
|
arg1 = unicode_str
|
|
|
|
arg2 = (u'Hello', unicode_str, u'world!')
|
|
|
|
args = (arg1,) + arg2
|
2012-06-21 07:20:26 -05:00
|
|
|
options = dict(option1=u'How are you?', option2=unicode_str,
|
|
|
|
version=API_VERSION)
|
2009-01-23 16:49:16 -06:00
|
|
|
|
2010-01-13 10:38:35 -06:00
|
|
|
conn = Connection('The connection.', Disconnect('someconn'))
|
2009-01-23 16:49:16 -06:00
|
|
|
context.someconn = conn
|
2015-08-12 06:44:11 -05:00
|
|
|
print(o.execute('echo', arg1, arg2, **options))
|
|
|
|
print(dict(
|
2012-06-21 07:20:26 -05:00
|
|
|
result=(arg1, arg2, options)
|
2015-08-12 06:44:11 -05:00
|
|
|
))
|
2009-12-09 10:09:53 -06:00
|
|
|
assert o.execute('echo', arg1, arg2, **options) == dict(
|
|
|
|
result=(arg1, arg2, options)
|
|
|
|
)
|
2009-01-23 19:02:32 -06:00
|
|
|
assert conn.disconnect.called is True # Make sure destroy_context() was called
|
Use Python3-compatible dict method names
Python 2 has keys()/values()/items(), which return lists,
iterkeys()/itervalues()/iteritems(), which return iterators,
and viewkeys()/viewvalues()/viewitems() which return views.
Python 3 has only keys()/values()/items(), which return views.
To get iterators, one can use iter() or a for loop/comprehension;
for lists there's the list() constructor.
When iterating through the entire dict, without modifying the dict,
the difference between Python 2's items() and iteritems() is
negligible, especially on small dicts (the main overhead is
extra memory, not CPU time). In the interest of simpler code,
this patch changes many instances of iteritems() to items(),
iterkeys() to keys() etc.
In other cases, helpers like six.itervalues are used.
Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-08-11 06:51:14 -05:00
|
|
|
assert list(context.__dict__) == []
|
2009-01-23 16:49:16 -06:00
|
|
|
|
2010-01-13 10:38:35 -06:00
|
|
|
conn = Connection('The connection.', Disconnect('someconn'))
|
2009-01-23 16:49:16 -06:00
|
|
|
context.someconn = conn
|
2009-12-09 10:09:53 -06:00
|
|
|
assert o.execute('echo', *args, **options) == dict(
|
|
|
|
result=(arg1, arg2, options)
|
|
|
|
)
|
2009-01-23 19:02:32 -06:00
|
|
|
assert conn.disconnect.called is True # Make sure destroy_context() was called
|
Use Python3-compatible dict method names
Python 2 has keys()/values()/items(), which return lists,
iterkeys()/itervalues()/iteritems(), which return iterators,
and viewkeys()/viewvalues()/viewitems() which return views.
Python 3 has only keys()/values()/items(), which return views.
To get iterators, one can use iter() or a for loop/comprehension;
for lists there's the list() constructor.
When iterating through the entire dict, without modifying the dict,
the difference between Python 2's items() and iteritems() is
negligible, especially on small dicts (the main overhead is
extra memory, not CPU time). In the interest of simpler code,
this patch changes many instances of iteritems() to items(),
iterkeys() to keys() etc.
In other cases, helpers like six.itervalues are used.
Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-08-11 06:51:14 -05:00
|
|
|
assert list(context.__dict__) == []
|
2009-01-23 16:49:16 -06:00
|
|
|
|
|
|
|
# Test with good command:
|
2010-01-13 10:38:35 -06:00
|
|
|
conn = Connection('The connection.', Disconnect('someconn'))
|
2009-01-23 16:49:16 -06:00
|
|
|
context.someconn = conn
|
2009-04-23 07:51:59 -05:00
|
|
|
e = raises(errors.ValidationError, o.execute, 'good')
|
2009-01-23 16:49:16 -06:00
|
|
|
assert e.name == 'nurse'
|
|
|
|
assert e.error == u'Not naughty!'
|
2009-01-23 19:02:32 -06:00
|
|
|
assert conn.disconnect.called is True # Make sure destroy_context() was called
|
Use Python3-compatible dict method names
Python 2 has keys()/values()/items(), which return lists,
iterkeys()/itervalues()/iteritems(), which return iterators,
and viewkeys()/viewvalues()/viewitems() which return views.
Python 3 has only keys()/values()/items(), which return views.
To get iterators, one can use iter() or a for loop/comprehension;
for lists there's the list() constructor.
When iterating through the entire dict, without modifying the dict,
the difference between Python 2's items() and iteritems() is
negligible, especially on small dicts (the main overhead is
extra memory, not CPU time). In the interest of simpler code,
this patch changes many instances of iteritems() to items(),
iterkeys() to keys() etc.
In other cases, helpers like six.itervalues are used.
Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-08-11 06:51:14 -05:00
|
|
|
assert list(context.__dict__) == []
|
2009-01-23 16:49:16 -06:00
|
|
|
|
|
|
|
# Test with bad command:
|
2010-01-13 10:38:35 -06:00
|
|
|
conn = Connection('The connection.', Disconnect('someconn'))
|
2009-01-23 16:49:16 -06:00
|
|
|
context.someconn = conn
|
2009-04-23 07:51:59 -05:00
|
|
|
e = raises(errors.InternalError, o.execute, 'bad')
|
2009-01-23 19:02:32 -06:00
|
|
|
assert conn.disconnect.called is True # Make sure destroy_context() was called
|
Use Python3-compatible dict method names
Python 2 has keys()/values()/items(), which return lists,
iterkeys()/itervalues()/iteritems(), which return iterators,
and viewkeys()/viewvalues()/viewitems() which return views.
Python 3 has only keys()/values()/items(), which return views.
To get iterators, one can use iter() or a for loop/comprehension;
for lists there's the list() constructor.
When iterating through the entire dict, without modifying the dict,
the difference between Python 2's items() and iteritems() is
negligible, especially on small dicts (the main overhead is
extra memory, not CPU time). In the interest of simpler code,
this patch changes many instances of iteritems() to items(),
iterkeys() to keys() etc.
In other cases, helpers like six.itervalues are used.
Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-08-11 06:51:14 -05:00
|
|
|
assert list(context.__dict__) == []
|
2009-03-13 01:53:05 -05:00
|
|
|
|
|
|
|
# Test with option 'name':
|
2010-01-13 10:38:35 -06:00
|
|
|
conn = Connection('The connection.', Disconnect('someconn'))
|
2009-03-13 01:53:05 -05:00
|
|
|
context.someconn = conn
|
2012-06-21 07:20:26 -05:00
|
|
|
expected = dict(result=u'TEST')
|
|
|
|
assert expected == o.execute('with_name', name=u'test',
|
|
|
|
version=API_VERSION)
|