diff --git a/gns3server/main.py b/gns3server/main.py index ce249dad..f8ca55d6 100644 --- a/gns3server/main.py +++ b/gns3server/main.py @@ -21,6 +21,7 @@ import datetime import sys import locale import argparse +import configparser from gns3server.server import Server from gns3server.web.logger import init_logger @@ -72,37 +73,54 @@ def locale_check(): log.info("Current locale is {}.{}".format(language, encoding)) -def parse_arguments(): +def parse_arguments(argv, config): + """ + Parse command line arguments and override local configuration + + :params args: Array of command line arguments + :params config: ConfigParser with default variable from configuration + """ + + defaults = { + "host": config.get("host", "127.0.0.1"), + "port": config.get("port", 8000), + "ssl": config.getboolean("ssl", False), + "certfile": config.get("certfile", ""), + "certkey": config.get("certkey", ""), + "local": config.getboolean("local", False), + "allow": config.getboolean("allow_remote_console", False), + "quiet": config.getboolean("quiet", False), + "debug": config.getboolean("debug", False), + } parser = argparse.ArgumentParser(description="GNS3 server version {}".format(__version__)) + parser.set_defaults(**defaults) parser.add_argument("-v", "--version", help="show the version", action="version", version=__version__) - parser.add_argument("--host", help="run on the given host/IP address", default="127.0.0.1") - parser.add_argument("--port", help="run on the given port", type=int, default=8000) - parser.add_argument("--config", help="use this config file", type=str, default=None) + parser.add_argument("--host", help="run on the given host/IP address") + parser.add_argument("--port", help="run on the given port", type=int) parser.add_argument("--ssl", action="store_true", help="run in SSL mode") - parser.add_argument("--certfile", help="SSL cert file", default="") - parser.add_argument("--certkey", help="SSL key file", default="") + parser.add_argument("--certfile", help="SSL cert file") + parser.add_argument("--certkey", help="SSL key file") parser.add_argument("-L", "--local", action="store_true", help="local mode (allows some insecure operations)") parser.add_argument("-A", "--allow", action="store_true", help="allow remote connections to local console ports") parser.add_argument("-q", "--quiet", action="store_true", help="do not show logs on stdout") parser.add_argument("-d", "--debug", action="store_true", help="show debug logs and enable code live reload") - args = parser.parse_args() - return args + return parser.parse_args(argv) def set_config(args): config = Config.instance() server_config = config.get_section_config("Server") - server_config["local"] = server_config.get("local", "true" if args.local else "false") - server_config["allow_remote_console"] = server_config.get("allow_remote_console", "true" if args.allow else "false") - server_config["host"] = server_config.get("host", args.host) - server_config["port"] = server_config.get("port", str(args.port)) - server_config["ssl"] = server_config.get("ssl", "true" if args.ssl else "false") - server_config["certfile"] = server_config.get("certfile", args.certfile) - server_config["certkey"] = server_config.get("certkey", args.certkey) - server_config["debug"] = server_config.get("debug", "true" if args.debug else "false") + server_config["local"] = str(args.local) + server_config["allow_remote_console"] = str(args.allow) + server_config["host"] = args.host + server_config["port"] = str(args.port) + server_config["ssl"] = str(args.ssl) + server_config["certfile"] = args.certfile + server_config["certkey"] = args.certkey + server_config["debug"] = str(args.debug) config.set_section_config("Server", server_config) @@ -112,7 +130,7 @@ def main(): """ level = logging.INFO - args = parse_arguments() + args = parse_arguments(sys.argv[1:], Config.instance().get_section_config("Server")) if args.debug: level = logging.DEBUG diff --git a/tests/test_main.py b/tests/test_main.py new file mode 100644 index 00000000..ed9006b3 --- /dev/null +++ b/tests/test_main.py @@ -0,0 +1,139 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# Copyright (C) 2015 GNS3 Technologies Inc. +# +# 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. +# +# 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, see . + + +import pytest +import locale + +from gns3server import main +from gns3server.config import Config +from gns3server.version import __version__ + + +def test_locale_check(): + + locale.setlocale(locale.LC_ALL, ("fr_FR")) + main.locale_check() + assert locale.getlocale() == ('fr_FR', 'UTF-8') + + +def test_parse_arguments(capsys): + + config = Config.instance() + server_config = config.get_section_config("Server") + + with pytest.raises(SystemExit): + main.parse_arguments(["--fail"], server_config) + out, err = capsys.readouterr() + assert "usage" in err + assert "fail" in err + assert "unrecognized arguments" in err + + with pytest.raises(SystemExit): + main.parse_arguments(["-v"], server_config) + out, err = capsys.readouterr() + assert __version__ in err + + with pytest.raises(SystemExit): + main.parse_arguments(["--version"], server_config) + out, err = capsys.readouterr() + assert __version__ in err + + with pytest.raises(SystemExit): + main.parse_arguments(["-h"], server_config) + out, err = capsys.readouterr() + assert __version__ in out + assert "optional arguments" in out + + with pytest.raises(SystemExit): + main.parse_arguments(["--help"], server_config) + out, err = capsys.readouterr() + assert __version__ in out + assert "optional arguments" in out + + assert main.parse_arguments(["--host", "192.168.1.1"], server_config).host == "192.168.1.1" + assert main.parse_arguments([], server_config).host == "127.0.0.1" + server_config["host"] = "192.168.1.2" + assert main.parse_arguments(["--host", "192.168.1.1"], server_config).host == "192.168.1.1" + assert main.parse_arguments([], server_config).host == "192.168.1.2" + + assert main.parse_arguments(["--port", "8002"], server_config).port == 8002 + assert main.parse_arguments([], server_config).port == 8000 + server_config["port"] = "8003" + assert main.parse_arguments([], server_config).port == 8003 + + assert main.parse_arguments(["--ssl"], server_config).ssl + assert main.parse_arguments([], server_config).ssl is False + server_config["ssl"] = "True" + assert main.parse_arguments([], server_config).ssl + + assert main.parse_arguments(["--certfile", "bla"], server_config).certfile == "bla" + assert main.parse_arguments([], server_config).certfile == "" + + assert main.parse_arguments(["--certkey", "blu"], server_config).certkey == "blu" + assert main.parse_arguments([], server_config).certkey == "" + + assert main.parse_arguments(["-L"], server_config).local + assert main.parse_arguments(["--local"], server_config).local + assert main.parse_arguments([], server_config).local is False + server_config["local"] = "True" + assert main.parse_arguments([], server_config).local + + assert main.parse_arguments(["-A"], server_config).allow + assert main.parse_arguments(["--allow"], server_config).allow + assert main.parse_arguments([], server_config).allow is False + server_config["allow_remote_console"] = "True" + assert main.parse_arguments([], server_config).allow + + assert main.parse_arguments(["-q"], server_config).quiet + assert main.parse_arguments(["--quiet"], server_config).quiet + assert main.parse_arguments([], server_config).quiet is False + + assert main.parse_arguments(["-d"], server_config).debug + assert main.parse_arguments([], server_config).debug is False + server_config["debug"] = "True" + assert main.parse_arguments([], server_config).debug + + +def test_set_config_with_args(): + + config = Config.instance() + args = main.parse_arguments(["--host", + "192.168.1.1", + "--local", + "--allow", + "--port", + "8001", + "--ssl", + "--certfile", + "bla", + "--certkey", + "blu", + "--debug"], + config.get_section_config("Server")) + main.set_config(args) + server_config = config.get_section_config("Server") + + assert server_config.getboolean("local") + assert server_config.getboolean("allow_remote_console") + assert server_config["host"] == "192.168.1.1" + assert server_config["port"] == "8001" + assert server_config.getboolean("ssl") + assert server_config["certfile"] == "bla" + assert server_config["certkey"] == "blu" + assert server_config.getboolean("debug")