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-10-27 16:19:49 -05:00
|
|
|
from tests.util import TempHome
|
2008-10-27 15:48:02 -05:00
|
|
|
from ipalib import cli, plugable, frontend, backend
|
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
|
|
|
|
|
2008-10-27 16:19:49 -05:00
|
|
|
def new(self, argv=tuple()):
|
|
|
|
home = TempHome()
|
2008-10-27 15:48:02 -05:00
|
|
|
api = plugable.API(
|
|
|
|
frontend.Command,
|
|
|
|
frontend.Object,
|
|
|
|
frontend.Method,
|
|
|
|
frontend.Property,
|
|
|
|
frontend.Application,
|
|
|
|
backend.Backend,
|
2008-08-13 01:25:42 -05:00
|
|
|
)
|
2008-10-27 16:19:49 -05:00
|
|
|
api.env.in_tree = True
|
2008-10-27 15:48:02 -05:00
|
|
|
o = self.cls(api, argv)
|
|
|
|
assert o.api is api
|
2008-10-27 16:19:49 -05:00
|
|
|
return (o, api, home)
|
2008-08-13 01:25:42 -05:00
|
|
|
|
2008-10-27 15:48:02 -05:00
|
|
|
def test_init(self):
|
2008-08-12 20:52:17 -05:00
|
|
|
"""
|
2008-10-27 15:48:02 -05:00
|
|
|
Test the `ipalib.cli.CLI.__init__` method.
|
2008-08-12 20:52:17 -05:00
|
|
|
"""
|
2008-10-27 15:48:02 -05:00
|
|
|
argv = ['-v', 'user-add', '--first=Jonh', '--last=Doe']
|
2008-10-27 16:19:49 -05:00
|
|
|
(o, api, home) = self.new(argv)
|
|
|
|
assert o.api is api
|
2008-10-27 15:48:02 -05:00
|
|
|
assert o.argv == tuple(argv)
|
|
|
|
|
|
|
|
def test_parse_globals(self):
|
2008-08-12 21:00:31 -05:00
|
|
|
"""
|
2008-10-27 15:48:02 -05:00
|
|
|
Test the `ipalib.cli.CLI.parse_globals` method.
|
2008-08-12 21:00:31 -05:00
|
|
|
"""
|
2008-10-27 15:48:02 -05:00
|
|
|
# Test with empty argv
|
2008-10-27 16:19:49 -05:00
|
|
|
(o, api, home) = self.new()
|
2008-10-27 15:48:02 -05:00
|
|
|
assert not hasattr(o, 'options')
|
|
|
|
assert not hasattr(o, 'cmd_argv')
|
|
|
|
assert o.isdone('parse_globals') is False
|
|
|
|
o.parse_globals()
|
|
|
|
assert o.isdone('parse_globals') is True
|
|
|
|
assert o.options.interactive is True
|
|
|
|
assert o.options.verbose is False
|
|
|
|
assert o.options.config_file is None
|
|
|
|
assert o.options.environment is None
|
|
|
|
assert o.cmd_argv == tuple()
|
|
|
|
e = raises(StandardError, o.parse_globals)
|
|
|
|
assert str(e) == 'CLI.parse_globals() already called'
|
|
|
|
|
|
|
|
# Test with a populated argv
|
|
|
|
argv = ('-a', '-n', '-v', '-c', '/my/config.conf', '-e', 'my_key=my_val')
|
|
|
|
cmd_argv = ('user-add', '--first', 'John', '--last', 'Doe')
|
2008-10-27 16:19:49 -05:00
|
|
|
(o, api, home) = self.new(argv + cmd_argv)
|
2008-10-27 15:48:02 -05:00
|
|
|
assert not hasattr(o, 'options')
|
|
|
|
assert not hasattr(o, 'cmd_argv')
|
|
|
|
assert o.isdone('parse_globals') is False
|
|
|
|
o.parse_globals()
|
|
|
|
assert o.isdone('parse_globals') is True
|
|
|
|
assert o.options.prompt_all is True
|
|
|
|
assert o.options.interactive is False
|
|
|
|
assert o.options.verbose is True
|
|
|
|
assert o.options.config_file == '/my/config.conf'
|
|
|
|
assert o.options.environment == 'my_key=my_val'
|
|
|
|
assert o.cmd_argv == cmd_argv
|
|
|
|
e = raises(StandardError, o.parse_globals)
|
|
|
|
assert str(e) == 'CLI.parse_globals() already called'
|
2008-10-27 15:49:34 -05:00
|
|
|
|
|
|
|
def test_bootstrap(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.cli.CLI.bootstrap` method.
|
|
|
|
"""
|
2008-10-27 16:19:49 -05:00
|
|
|
(o, api, home) = self.new()
|
|
|
|
keys = tuple(api.env)
|
2008-10-27 16:36:41 -05:00
|
|
|
assert api.isdone('bootstrap') is False
|
2008-10-27 16:19:49 -05:00
|
|
|
assert o.isdone('parse_globals') is False
|
|
|
|
assert o.isdone('bootstrap') is False
|
|
|
|
o.bootstrap()
|
2008-10-27 16:36:41 -05:00
|
|
|
assert api.isdone('bootstrap') is True
|
2008-10-27 16:19:49 -05:00
|
|
|
assert o.isdone('parse_globals') is True
|
|
|
|
assert o.isdone('bootstrap') is True
|
|
|
|
e = raises(StandardError, o.bootstrap)
|
|
|
|
assert str(e) == 'CLI.bootstrap() already called'
|
|
|
|
assert api.env.verbose is False
|
|
|
|
assert api.env.context == 'cli'
|