mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-23 14:24:34 +02:00
Use Pydantic to validate the server config file.
This commit is contained in:
1 parent
478119b40d
commit
30ebae207f
61 files changed
+871
-695
No files matched your search
+81
-34
@@ -17,8 +17,11 @@
|
||||
|
||||
|
||||
import configparser
|
||||
import pytest
|
||||
|
||||
from gns3server.config import Config
|
||||
from gns3server.config import ServerConfig
|
||||
from pydantic import ValidationError
|
||||
|
||||
|
||||
def load_config(tmpdir, settings):
|
||||
@@ -45,7 +48,6 @@ def write_config(tmpdir, settings):
|
||||
"""
|
||||
|
||||
path = str(tmpdir / "server.conf")
|
||||
|
||||
config = configparser.ConfigParser()
|
||||
config.read_dict(settings)
|
||||
with open(path, "w+") as f:
|
||||
@@ -53,41 +55,26 @@ def write_config(tmpdir, settings):
|
||||
return path
|
||||
|
||||
|
||||
def test_get_section_config(tmpdir):
|
||||
@pytest.mark.parametrize(
|
||||
"setting, value, result",
|
||||
(
|
||||
("allowed_interfaces", "", []),
|
||||
("allowed_interfaces", "eth0", ["eth0"]),
|
||||
("allowed_interfaces", "eth1,eth2", ["eth1", "eth2"]),
|
||||
("additional_images_paths", "", []),
|
||||
("additional_images_paths", "/path/to/dir1", ["/path/to/dir1"]),
|
||||
("additional_images_paths", "/path/to/dir1;/path/to/dir2", ["/path/to/dir1", "/path/to/dir2"])
|
||||
)
|
||||
)
|
||||
def test_server_settings_to_list(tmpdir, setting: str, value: str, result: str):
|
||||
|
||||
config = load_config(tmpdir, {
|
||||
"Server": {
|
||||
"host": "127.0.0.1",
|
||||
}
|
||||
})
|
||||
assert dict(config.get_section_config("Server")) == {"host": "127.0.0.1"}
|
||||
|
||||
|
||||
def test_set_section_config(tmpdir):
|
||||
|
||||
config = load_config(tmpdir, {
|
||||
"Server": {
|
||||
"host": "127.0.0.1",
|
||||
"local": "false"
|
||||
setting: value
|
||||
}
|
||||
})
|
||||
|
||||
assert dict(config.get_section_config("Server")) == {"host": "127.0.0.1", "local": "false"}
|
||||
config.set_section_config("Server", {"host": "192.168.1.1", "local": True})
|
||||
assert dict(config.get_section_config("Server")) == {"host": "192.168.1.1", "local": "true"}
|
||||
|
||||
|
||||
def test_set(tmpdir):
|
||||
|
||||
config = load_config(tmpdir, {
|
||||
"Server": {
|
||||
"host": "127.0.0.1"
|
||||
}
|
||||
})
|
||||
|
||||
assert dict(config.get_section_config("Server")) == {"host": "127.0.0.1"}
|
||||
config.set("Server", "host", "192.168.1.1")
|
||||
assert dict(config.get_section_config("Server")) == {"host": "192.168.1.1"}
|
||||
assert config.settings.dict(exclude_unset=True)["Server"][setting] == result
|
||||
|
||||
|
||||
def test_reload(tmpdir):
|
||||
@@ -98,9 +85,7 @@ def test_reload(tmpdir):
|
||||
}
|
||||
})
|
||||
|
||||
assert dict(config.get_section_config("Server")) == {"host": "127.0.0.1"}
|
||||
config.set_section_config("Server", {"host": "192.168.1.1"})
|
||||
assert dict(config.get_section_config("Server")) == {"host": "192.168.1.1"}
|
||||
assert config.settings.Server.host == "127.0.0.1"
|
||||
|
||||
write_config(tmpdir, {
|
||||
"Server": {
|
||||
@@ -109,4 +94,66 @@ def test_reload(tmpdir):
|
||||
})
|
||||
|
||||
config.reload()
|
||||
assert dict(config.get_section_config("Server")) == {"host": "192.168.1.1"}
|
||||
assert config.settings.Server.host == "192.168.1.2"
|
||||
|
||||
|
||||
def test_server_password_hidden():
|
||||
|
||||
server_settings = {"Server": {"password": "password123"}}
|
||||
config = ServerConfig(**server_settings)
|
||||
assert str(config.Server.password) == "**********"
|
||||
assert config.Server.password.get_secret_value() == "password123"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"settings, exception_expected",
|
||||
(
|
||||
({"protocol": "https1"}, True),
|
||||
({"console_start_port_range": 15000}, False),
|
||||
({"console_start_port_range": 0}, True),
|
||||
({"console_start_port_range": 68000}, True),
|
||||
({"console_end_port_range": 15000}, False),
|
||||
({"console_end_port_range": 0}, True),
|
||||
({"console_end_port_range": 68000}, True),
|
||||
({"console_start_port_range": 10000, "console_end_port_range": 5000}, True),
|
||||
({"vnc_console_start_port_range": 6000}, False),
|
||||
({"vnc_console_start_port_range": 1000}, True),
|
||||
({"vnc_console_end_port_range": 6000}, False),
|
||||
({"vnc_console_end_port_range": 1000}, True),
|
||||
({"vnc_console_start_port_range": 7000, "vnc_console_end_port_range": 6000}, True),
|
||||
({"auth": True, "user": "user1"}, False),
|
||||
({"auth": True, "user": ""}, True),
|
||||
({"auth": True}, True),
|
||||
)
|
||||
)
|
||||
def test_server_settings(settings: dict, exception_expected: bool):
|
||||
|
||||
server_settings = {"Server": settings}
|
||||
|
||||
if exception_expected:
|
||||
with pytest.raises(ValidationError):
|
||||
ServerConfig(**server_settings)
|
||||
else:
|
||||
ServerConfig(**server_settings)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"settings, exception_expected",
|
||||
(
|
||||
({"vmnet_start_range": 0}, True),
|
||||
({"vmnet_start_range": 256}, True),
|
||||
({"vmnet_end_range": 0}, True),
|
||||
({"vmnet_end_range": 256}, True),
|
||||
({"vmnet_start_range": 2, "vmnet_end_range": 10}, False),
|
||||
({"vmnet_start_range": 5, "vmnet_end_range": 3}, True)
|
||||
)
|
||||
)
|
||||
def test_vmware_settings(settings: dict, exception_expected: bool):
|
||||
|
||||
vmware_settings = {"VMware": settings}
|
||||
|
||||
if exception_expected:
|
||||
with pytest.raises(ValidationError):
|
||||
ServerConfig(**vmware_settings)
|
||||
else:
|
||||
ServerConfig(**vmware_settings)
|
||||
Reference in new issue
Block a user