From a97e9f687c9a213e0bfb5c116db54cff92bf86cc Mon Sep 17 00:00:00 2001 From: Justin Riley Date: Fri, 19 Aug 2016 09:19:50 -0400 Subject: [PATCH] add tests for cli --- onedns/cli.py | 21 ++++++++++++--------- onedns/tests/test_cli.py | 24 ++++++++++++++++++++++++ onedns/tests/test_shell.py | 1 + 3 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 onedns/tests/test_cli.py diff --git a/onedns/cli.py b/onedns/cli.py index 2a38100..0ba4aad 100644 --- a/onedns/cli.py +++ b/onedns/cli.py @@ -5,20 +5,22 @@ from onedns import server from onedns import logger -def daemon(args, one_args): +def daemon(args, one_args, **kwargs): + testing = kwargs.get('testing', False) + vms = kwargs.get('vms') srv = server.OneDNS(args.domain, one_kwargs=one_args) - srv.sync() - srv.daemon(dns_port=args.dns_port) + srv.sync(vms=vms) + srv.daemon(dns_port=args.dns_port, testing=testing) -def shell(args, one_args): +def shell(args, one_args, **kwargs): srv = server.OneDNS(args.domain, one_kwargs=one_args) oneclient = srv._one ns = dict(one_dns=srv, oneclient=oneclient, log=logger.log) utils.shell(local_ns=ns) -def main(args=None): +def get_parser(): parser = argparse.ArgumentParser( description='OneDNS - Dynamic DNS for OpenNebula') parser.add_argument('--debug', required=False, @@ -42,12 +44,13 @@ def main(args=None): shell_parser = subparsers.add_parser('shell') shell_parser.set_defaults(func=shell) + return parser - args = parser.parse_args(args=args) +def main(**kwargs): + parser = get_parser() + args = parser.parse_args(args=kwargs.pop('args', None)) logger.configure_onedns_logging(debug=args.debug) - args_dict = vars(args) one_args = utils.get_kwargs_from_dict(args_dict, 'one_') - - args.func(args, one_args) + args.func(args, one_args, **kwargs) diff --git a/onedns/tests/test_cli.py b/onedns/tests/test_cli.py new file mode 100644 index 0000000..95f7a8a --- /dev/null +++ b/onedns/tests/test_cli.py @@ -0,0 +1,24 @@ +import pytest + +import mock + +from onedns import cli +from onedns.tests import test_shell + + +def test_cli_help(): + with pytest.raises(SystemExit): + cli.main(args=['--help']) + + +@mock.patch.object(cli, 'logger', mock.MagicMock()) +def test_cli_subcmd_daemon(vms): + cli.main(args=['daemon'], testing=True, vms=vms) + + +@mock.patch.dict('sys.modules', test_shell.IPY_MODULES) +@mock.patch.object(cli, 'logger', mock.MagicMock()) +def test_cli_subcmd_shell(): + test_shell.IPY.embed.reset_mock() + cli.main(args=['shell'], testing=True) + test_shell.IPY.embed.assert_called_once() diff --git a/onedns/tests/test_shell.py b/onedns/tests/test_shell.py index a5e4e6f..a8caaa6 100644 --- a/onedns/tests/test_shell.py +++ b/onedns/tests/test_shell.py @@ -14,6 +14,7 @@ IPY_MODULES = { @mock.patch.dict('sys.modules', IPY_MODULES) def test_with_ipython(): + IPY.embed.reset_mock() from onedns import utils ns = dict(test=True) utils.shell(local_ns=ns)