2008-08-08 16:46:23 -05:00
|
|
|
# Authors:
|
|
|
|
# Jason Gerard DeRose <jderose@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2008 Red Hat
|
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
|
|
|
# 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; version 2 only
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
"""
|
2008-10-07 22:25:23 -05:00
|
|
|
Test the `ipalib.cli` module.
|
2008-08-08 16:46:23 -05:00
|
|
|
"""
|
|
|
|
|
2008-10-07 23:30:53 -05:00
|
|
|
from tests.util import raises, getitem, no_set, no_del, read_only, ClassChecker
|
2008-08-14 00:46:20 -05:00
|
|
|
from ipalib import cli, plugable
|
2008-08-08 16:46:23 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_to_cli():
|
2008-08-14 03:28:48 -05:00
|
|
|
"""
|
2008-10-08 00:29:42 -05:00
|
|
|
Test the `ipalib.cli.to_cli` function.
|
2008-08-14 03:28:48 -05:00
|
|
|
"""
|
2008-08-08 16:46:23 -05:00
|
|
|
f = cli.to_cli
|
|
|
|
assert f('initialize') == 'initialize'
|
|
|
|
assert f('user_add') == 'user-add'
|
|
|
|
|
|
|
|
|
|
|
|
def test_from_cli():
|
2008-08-14 03:28:48 -05:00
|
|
|
"""
|
2008-10-08 00:29:42 -05:00
|
|
|
Test the `ipalib.cli.from_cli` function.
|
2008-08-14 03:28:48 -05:00
|
|
|
"""
|
2008-08-08 16:46:23 -05:00
|
|
|
f = cli.from_cli
|
|
|
|
assert f('initialize') == 'initialize'
|
|
|
|
assert f('user-add') == 'user_add'
|
2008-08-11 14:35:57 -05:00
|
|
|
|
|
|
|
|
2008-08-12 20:52:17 -05:00
|
|
|
def get_cmd_name(i):
|
|
|
|
return 'cmd_%d' % i
|
|
|
|
|
2008-10-08 00:29:42 -05:00
|
|
|
|
2008-08-15 14:49:04 -05:00
|
|
|
class DummyCommand(object):
|
2008-08-12 20:52:17 -05:00
|
|
|
def __init__(self, name):
|
|
|
|
self.__name = name
|
|
|
|
|
|
|
|
def __get_name(self):
|
|
|
|
return self.__name
|
|
|
|
name = property(__get_name)
|
|
|
|
|
2008-10-08 00:29:42 -05:00
|
|
|
|
2008-08-12 20:52:17 -05:00
|
|
|
class DummyAPI(object):
|
|
|
|
def __init__(self, cnt):
|
2008-08-14 00:46:20 -05:00
|
|
|
self.__cmd = plugable.NameSpace(self.__cmd_iter(cnt))
|
2008-08-12 20:52:17 -05:00
|
|
|
|
|
|
|
def __get_cmd(self):
|
|
|
|
return self.__cmd
|
2008-08-15 14:49:04 -05:00
|
|
|
Command = property(__get_cmd)
|
2008-08-12 20:52:17 -05:00
|
|
|
|
|
|
|
def __cmd_iter(self, cnt):
|
|
|
|
for i in xrange(cnt):
|
2008-08-15 14:49:04 -05:00
|
|
|
yield DummyCommand(get_cmd_name(i))
|
2008-08-12 20:52:17 -05:00
|
|
|
|
|
|
|
def finalize(self):
|
|
|
|
pass
|
|
|
|
|
2008-08-12 23:10:23 -05:00
|
|
|
def register(self, *args, **kw):
|
|
|
|
pass
|
|
|
|
|
2008-08-12 20:52:17 -05:00
|
|
|
|
2008-08-11 14:35:57 -05:00
|
|
|
class test_CLI(ClassChecker):
|
|
|
|
"""
|
2008-10-08 00:29:42 -05:00
|
|
|
Test the `ipalib.cli.CLI` class.
|
2008-08-11 14:35:57 -05:00
|
|
|
"""
|
|
|
|
_cls = cli.CLI
|
|
|
|
|
|
|
|
def test_class(self):
|
2008-10-08 00:29:42 -05:00
|
|
|
"""
|
|
|
|
Test the `ipalib.cli.CLI` class.
|
|
|
|
"""
|
2008-08-11 14:35:57 -05:00
|
|
|
assert type(self.cls.api) is property
|
|
|
|
|
|
|
|
def test_api(self):
|
|
|
|
"""
|
2008-10-08 00:29:42 -05:00
|
|
|
Test the `ipalib.cli.CLI.api` property.
|
2008-08-11 14:35:57 -05:00
|
|
|
"""
|
|
|
|
api = 'the plugable.API instance'
|
|
|
|
o = self.cls(api)
|
|
|
|
assert read_only(o, 'api') is api
|
2008-08-12 11:49:23 -05:00
|
|
|
|
2008-09-04 01:33:57 -05:00
|
|
|
def dont_parse(self):
|
2008-08-13 01:25:42 -05:00
|
|
|
"""
|
2008-10-08 00:29:42 -05:00
|
|
|
Test the `ipalib.cli.CLI.parse` method.
|
2008-08-13 01:25:42 -05:00
|
|
|
"""
|
|
|
|
o = self.cls(None)
|
|
|
|
args = ['hello', 'naughty', 'nurse']
|
|
|
|
kw = dict(
|
|
|
|
first_name='Naughty',
|
|
|
|
last_name='Nurse',
|
|
|
|
)
|
|
|
|
opts = ['--%s=%s' % (k.replace('_', '-'), v) for (k, v) in kw.items()]
|
|
|
|
assert o.parse(args + []) == (args, {})
|
|
|
|
assert o.parse(opts + []) == ([], kw)
|
|
|
|
assert o.parse(args + opts) == (args, kw)
|
|
|
|
assert o.parse(opts + args) == (args, kw)
|
|
|
|
|
2008-08-12 20:52:17 -05:00
|
|
|
def test_mcl(self):
|
|
|
|
"""
|
2008-10-08 00:29:42 -05:00
|
|
|
Test the `ipalib.cli.CLI.mcl` property .
|
2008-08-12 20:52:17 -05:00
|
|
|
"""
|
|
|
|
cnt = 100
|
|
|
|
api = DummyAPI(cnt)
|
2008-08-15 14:49:04 -05:00
|
|
|
len(api.Command) == cnt
|
2008-08-12 20:52:17 -05:00
|
|
|
o = self.cls(api)
|
|
|
|
assert o.mcl is None
|
2008-09-03 23:39:01 -05:00
|
|
|
o.build_map()
|
2008-08-12 20:52:17 -05:00
|
|
|
assert o.mcl == 6 # len('cmd_99')
|
2008-08-12 21:00:31 -05:00
|
|
|
|
|
|
|
def test_dict(self):
|
|
|
|
"""
|
2008-10-08 00:29:42 -05:00
|
|
|
Test container emulation of `ipalib.cli.CLI` class.
|
2008-08-12 21:00:31 -05:00
|
|
|
"""
|
|
|
|
cnt = 25
|
|
|
|
api = DummyAPI(cnt)
|
2008-08-15 14:49:04 -05:00
|
|
|
assert len(api.Command) == cnt
|
2008-08-12 21:00:31 -05:00
|
|
|
o = self.cls(api)
|
2008-09-03 23:39:01 -05:00
|
|
|
o.build_map()
|
2008-08-15 14:49:04 -05:00
|
|
|
for cmd in api.Command():
|
2008-08-12 21:00:31 -05:00
|
|
|
key = cli.to_cli(cmd.name)
|
|
|
|
assert key in o
|
|
|
|
assert o[key] is cmd
|
|
|
|
assert cmd.name not in o
|
|
|
|
raises(KeyError, getitem, o, cmd.name)
|