2015-04-28 08:43:27 +00:00
|
|
|
#!/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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import locale
|
|
|
|
|
|
|
|
from gns3server import run
|
|
|
|
from gns3server.config import Config
|
|
|
|
from gns3server.version import __version__
|
|
|
|
|
|
|
|
|
|
|
|
def test_locale_check():
|
|
|
|
|
|
|
|
try:
|
2020-06-16 08:57:16 +00:00
|
|
|
locale.setlocale(locale.LC_ALL, ("fr_FR", "UTF-8"))
|
2015-04-28 08:43:27 +00:00
|
|
|
except: # Locale is not available on the server
|
|
|
|
return
|
|
|
|
run.locale_check()
|
|
|
|
assert locale.getlocale() == ('fr_FR', 'UTF-8')
|
|
|
|
|
|
|
|
|
2021-04-12 07:32:23 +00:00
|
|
|
def test_parse_arguments(capsys, config, tmpdir):
|
2015-04-28 08:43:27 +00:00
|
|
|
|
2021-04-12 07:32:23 +00:00
|
|
|
server_config = config.settings.Server
|
2015-04-28 08:43:27 +00:00
|
|
|
with pytest.raises(SystemExit):
|
2015-07-05 19:14:05 +00:00
|
|
|
run.parse_arguments(["--fail"])
|
2015-04-28 08:43:27 +00:00
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert "usage" in err
|
|
|
|
assert "fail" in err
|
|
|
|
assert "unrecognized arguments" in err
|
|
|
|
|
2020-06-16 11:57:49 +00:00
|
|
|
# with pytest.raises(SystemExit):
|
|
|
|
# run.parse_arguments(["-v"])
|
|
|
|
# out, _ = capsys.readouterr()
|
|
|
|
# assert __version__ in out
|
|
|
|
# with pytest.raises(SystemExit):
|
|
|
|
# run.parse_arguments(["--version"])
|
|
|
|
# out, _ = capsys.readouterr()
|
|
|
|
# assert __version__ in out
|
|
|
|
#
|
|
|
|
# with pytest.raises(SystemExit):
|
|
|
|
# run.parse_arguments(["-h"])
|
|
|
|
# out, _ = capsys.readouterr()
|
|
|
|
# assert __version__ in out
|
|
|
|
# assert "optional arguments" in out
|
|
|
|
#
|
|
|
|
# with pytest.raises(SystemExit):
|
|
|
|
# run.parse_arguments(["--help"])
|
|
|
|
# out, _ = capsys.readouterr()
|
|
|
|
# assert __version__ in out
|
|
|
|
# assert "optional arguments" in out
|
2015-04-28 08:43:27 +00:00
|
|
|
|
2015-07-05 19:14:05 +00:00
|
|
|
assert run.parse_arguments(["--host", "192.168.1.1"]).host == "192.168.1.1"
|
2021-04-12 07:32:23 +00:00
|
|
|
assert run.parse_arguments([]).host == "localhost"
|
|
|
|
server_config.host = "192.168.1.2"
|
2015-07-05 19:14:05 +00:00
|
|
|
assert run.parse_arguments(["--host", "192.168.1.1"]).host == "192.168.1.1"
|
|
|
|
assert run.parse_arguments([]).host == "192.168.1.2"
|
2015-04-28 08:43:27 +00:00
|
|
|
|
2015-07-05 19:14:05 +00:00
|
|
|
assert run.parse_arguments(["--port", "8002"]).port == 8002
|
2016-03-25 14:31:17 +00:00
|
|
|
assert run.parse_arguments([]).port == 3080
|
2021-04-12 07:32:23 +00:00
|
|
|
server_config.port = 8003
|
2015-07-05 19:14:05 +00:00
|
|
|
assert run.parse_arguments([]).port == 8003
|
2015-04-28 08:43:27 +00:00
|
|
|
|
2015-07-05 19:14:05 +00:00
|
|
|
assert run.parse_arguments(["--ssl"]).ssl
|
|
|
|
assert run.parse_arguments([]).ssl is False
|
2021-04-12 07:32:23 +00:00
|
|
|
server_config.ssl = True
|
2015-07-05 19:14:05 +00:00
|
|
|
assert run.parse_arguments([]).ssl
|
2015-04-28 08:43:27 +00:00
|
|
|
|
2015-07-05 19:14:05 +00:00
|
|
|
assert run.parse_arguments(["--certfile", "bla"]).certfile == "bla"
|
2021-04-12 07:32:23 +00:00
|
|
|
assert run.parse_arguments([]).certfile is None
|
2015-04-28 08:43:27 +00:00
|
|
|
|
2015-07-05 19:14:05 +00:00
|
|
|
assert run.parse_arguments(["--certkey", "blu"]).certkey == "blu"
|
2021-04-12 07:32:23 +00:00
|
|
|
assert run.parse_arguments([]).certkey is None
|
2015-04-28 08:43:27 +00:00
|
|
|
|
2015-07-05 19:14:05 +00:00
|
|
|
assert run.parse_arguments(["-L"]).local
|
|
|
|
assert run.parse_arguments(["--local"]).local
|
2021-04-12 07:32:23 +00:00
|
|
|
server_config.local = False
|
2015-07-05 19:14:05 +00:00
|
|
|
assert run.parse_arguments([]).local is False
|
2021-04-12 07:32:23 +00:00
|
|
|
server_config.local = True
|
2015-07-05 19:14:05 +00:00
|
|
|
assert run.parse_arguments([]).local
|
2015-04-28 08:43:27 +00:00
|
|
|
|
2015-07-05 19:14:05 +00:00
|
|
|
assert run.parse_arguments(["-A"]).allow
|
|
|
|
assert run.parse_arguments(["--allow"]).allow
|
|
|
|
assert run.parse_arguments([]).allow is False
|
2021-04-12 07:32:23 +00:00
|
|
|
server_config.allow_remote_console = True
|
2015-07-05 19:14:05 +00:00
|
|
|
assert run.parse_arguments([]).allow
|
2015-04-28 08:43:27 +00:00
|
|
|
|
2015-07-05 19:14:05 +00:00
|
|
|
assert run.parse_arguments(["-q"]).quiet
|
|
|
|
assert run.parse_arguments(["--quiet"]).quiet
|
|
|
|
assert run.parse_arguments([]).quiet is False
|
2015-04-28 08:43:27 +00:00
|
|
|
|
2015-07-05 19:14:05 +00:00
|
|
|
assert run.parse_arguments(["-d"]).debug
|
|
|
|
assert run.parse_arguments([]).debug is False
|
2021-04-12 07:32:23 +00:00
|
|
|
server_config.debug = True
|
2015-07-05 19:14:05 +00:00
|
|
|
assert run.parse_arguments([]).debug
|
2015-04-28 08:43:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_set_config_with_args():
|
|
|
|
|
|
|
|
config = Config.instance()
|
|
|
|
args = run.parse_arguments(["--host",
|
|
|
|
"192.168.1.1",
|
|
|
|
"--local",
|
|
|
|
"--allow",
|
|
|
|
"--port",
|
|
|
|
"8001",
|
|
|
|
"--ssl",
|
|
|
|
"--certfile",
|
|
|
|
"bla",
|
|
|
|
"--certkey",
|
|
|
|
"blu",
|
2015-07-05 19:14:05 +00:00
|
|
|
"--debug"])
|
2015-04-28 08:43:27 +00:00
|
|
|
run.set_config(args)
|
2021-04-12 07:32:23 +00:00
|
|
|
server_config = config.settings.Server
|
|
|
|
|
|
|
|
assert server_config.local
|
|
|
|
assert server_config.allow_remote_console
|
|
|
|
assert server_config.host
|
|
|
|
assert server_config.port
|
|
|
|
assert server_config.ssl
|
|
|
|
assert server_config.certfile
|
|
|
|
assert server_config.certkey
|
|
|
|
assert server_config.debug
|