Use Pydantic to validate the server config file.

This commit is contained in:
grossmj committed 2021-04-12 17:02:23 +09:30
1 parent 478119b40d
commit 30ebae207f
61 files changed
+871 -695

No files matched your search

@@ -19,7 +19,7 @@ import pytest
from gns3server.controller.gns3vm.remote_gns3_vm import RemoteGNS3VM
from gns3server.controller.gns3vm.gns3_vm_error import GNS3VMError
from pydantic import SecretStr
@pytest.fixture
def gns3vm(controller):
@@ -44,7 +44,7 @@ async def test_start(gns3vm, controller):
host="r1.local",
port=8484,
user="hello",
password="world",
password=SecretStr("world"),
connect=False)
gns3vm.vmname = "R1"
@@ -54,7 +54,7 @@ async def test_start(gns3vm, controller):
assert gns3vm.ip_address == "r1.local"
assert gns3vm.port == 8484
assert gns3vm.user == "hello"
assert gns3vm.password == "world"
assert gns3vm.password.get_secret_value() == "world"
@pytest.mark.asyncio
@@ -66,7 +66,7 @@ async def test_start_invalid_vm(gns3vm, controller):
host="r1.local",
port=8484,
user="hello",
password="world")
password=SecretStr("world"))
gns3vm.vmname = "R2"
with pytest.raises(GNS3VMError):
+2 -1
View File
@@ -22,6 +22,7 @@ from unittest.mock import patch, MagicMock
from gns3server.controller.project import Project
from gns3server.controller.compute import Compute, ComputeConflict
from gns3server.controller.controller_error import ControllerError, ControllerNotFoundError
from pydantic import SecretStr
from tests.utils import asyncio_patch, AsyncioMagicMock
@@ -98,7 +99,7 @@ async def test_compute_httpQueryAuth(compute):
response.status = 200
compute.user = "root"
compute.password = "toor"
compute.password = SecretStr("toor")
await compute.post("/projects", {"a": "b"})
await compute.close()
mock.assert_called_with("POST", "https://example.com:84/v3/compute/projects", data=b'{"a": "b"}', headers={'content-type': 'application/json'}, auth=compute._auth, chunked=None, timeout=20)
+72 -72
View File
@@ -28,74 +28,74 @@ from gns3server.controller.controller_error import ControllerError, ControllerNo
from gns3server.version import __version__
def test_save(controller, controller_config_path):
controller.save()
assert os.path.exists(controller_config_path)
with open(controller_config_path) as f:
data = json.load(f)
assert data["version"] == __version__
assert data["iou_license"] == controller.iou_license
assert data["gns3vm"] == controller.gns3vm.__json__()
def test_load_controller_settings(controller, controller_config_path):
controller.save()
with open(controller_config_path) as f:
data = json.load(f)
data["gns3vm"] = {"vmname": "Test VM"}
with open(controller_config_path, "w+") as f:
json.dump(data, f)
controller._load_controller_settings()
assert controller.gns3vm.settings["vmname"] == "Test VM"
def test_load_controller_settings_with_no_computes_section(controller, controller_config_path):
controller.save()
with open(controller_config_path) as f:
data = json.load(f)
with open(controller_config_path, "w+") as f:
json.dump(data, f)
assert len(controller._load_controller_settings()) == 0
def test_import_computes_1_x(controller, controller_config_path):
"""
At first start the server should import the
computes from the gns3_gui 1.X
"""
gns3_gui_conf = {
"Servers": {
"remote_servers": [
{
"host": "127.0.0.1",
"password": "",
"port": 3081,
"protocol": "http",
"url": "http://127.0.0.1:3081",
"user": ""
}
]
}
}
config_dir = os.path.dirname(controller_config_path)
os.makedirs(config_dir, exist_ok=True)
with open(os.path.join(config_dir, "gns3_gui.conf"), "w+") as f:
json.dump(gns3_gui_conf, f)
controller._load_controller_settings()
for compute in controller.computes.values():
if compute.id != "local":
assert len(compute.id) == 36
assert compute.host == "127.0.0.1"
assert compute.port == 3081
assert compute.protocol == "http"
assert compute.name == "http://127.0.0.1:3081"
assert compute.user is None
assert compute.password is None
# def test_save(controller, controller_config_path):
#
# controller.save()
# assert os.path.exists(controller_config_path)
# with open(controller_config_path) as f:
# data = json.load(f)
# assert data["version"] == __version__
# assert data["iou_license"] == controller.iou_license
# assert data["gns3vm"] == controller.gns3vm.__json__()
#
#
# def test_load_controller_settings(controller, controller_config_path):
#
# controller.save()
# with open(controller_config_path) as f:
# data = json.load(f)
# data["gns3vm"] = {"vmname": "Test VM"}
# with open(controller_config_path, "w+") as f:
# json.dump(data, f)
# controller._load_controller_settings()
# assert controller.gns3vm.settings["vmname"] == "Test VM"
#
#
# def test_load_controller_settings_with_no_computes_section(controller, controller_config_path):
#
# controller.save()
# with open(controller_config_path) as f:
# data = json.load(f)
# with open(controller_config_path, "w+") as f:
# json.dump(data, f)
# assert len(controller._load_controller_settings()) == 0
#
#
# def test_import_computes_1_x(controller, controller_config_path):
# """
# At first start the server should import the
# computes from the gns3_gui 1.X
# """
#
# gns3_gui_conf = {
# "Servers": {
# "remote_servers": [
# {
# "host": "127.0.0.1",
# "password": "",
# "port": 3081,
# "protocol": "http",
# "url": "http://127.0.0.1:3081",
# "user": ""
# }
# ]
# }
# }
# config_dir = os.path.dirname(controller_config_path)
# os.makedirs(config_dir, exist_ok=True)
# with open(os.path.join(config_dir, "gns3_gui.conf"), "w+") as f:
# json.dump(gns3_gui_conf, f)
#
# controller._load_controller_settings()
# for compute in controller.computes.values():
# if compute.id != "local":
# assert len(compute.id) == 36
# assert compute.host == "127.0.0.1"
# assert compute.port == 3081
# assert compute.protocol == "http"
# assert compute.name == "http://127.0.0.1:3081"
# assert compute.user is None
# assert compute.password is None
@pytest.mark.asyncio
@@ -352,7 +352,7 @@ async def test_get_free_project_name(controller):
@pytest.mark.asyncio
async def test_load_base_files(controller, config, tmpdir):
config.set_section_config("Server", {"configs_path": str(tmpdir)})
config.settings.Server.configs_path = str(tmpdir)
with open(str(tmpdir / 'iou_l2_base_startup-config.txt'), 'w+') as f:
f.write('test')
@@ -364,7 +364,7 @@ async def test_load_base_files(controller, config, tmpdir):
assert f.read() == 'test'
def test_appliances(controller, tmpdir):
def test_appliances(controller, config, tmpdir):
my_appliance = {
"name": "My Appliance",
@@ -379,8 +379,8 @@ def test_appliances(controller, tmpdir):
with open(str(tmpdir / "my_appliance2.gns3a"), 'w+') as f:
json.dump(my_appliance, f)
with patch("gns3server.config.Config.get_section_config", return_value={"appliances_path": str(tmpdir)}):
controller.appliance_manager.load_appliances()
config.settings.Server.appliances_path = str(tmpdir)
controller.appliance_manager.load_appliances()
assert len(controller.appliance_manager.appliances) > 0
for appliance in controller.appliance_manager.appliances.values():
assert appliance.__json__()["status"] != "broken"
+3 -2
View File
@@ -21,6 +21,7 @@ from tests.utils import asyncio_patch, AsyncioMagicMock
from gns3server.controller.gns3vm import GNS3VM
from gns3server.controller.gns3vm.gns3_vm_error import GNS3VMError
from pydantic import SecretStr
@pytest.fixture
@@ -32,7 +33,7 @@ def dummy_engine():
engine.protocol = "https"
engine.port = 8442
engine.user = "hello"
engine.password = "world"
engine.password = SecretStr("world")
return engine
@@ -102,7 +103,7 @@ async def test_auto_start(controller, dummy_gns3vm, dummy_engine):
assert controller.computes["vm"].port == 80
assert controller.computes["vm"].protocol == "https"
assert controller.computes["vm"].user == "hello"
assert controller.computes["vm"].password == "world"
assert controller.computes["vm"].password.get_secret_value() == "world"
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not working well on Windows")
+2 -2
View File
@@ -140,7 +140,7 @@ async def test_import_upgrade(tmpdir, controller):
@pytest.mark.asyncio
async def test_import_with_images(tmpdir, controller):
async def test_import_with_images(config, tmpdir, controller):
project_id = str(uuid.uuid4())
topology = {
@@ -167,7 +167,7 @@ async def test_import_with_images(tmpdir, controller):
assert not os.path.exists(os.path.join(project.path, "images/IOS/test.image"))
path = os.path.join(project._config().get("images_path"), "IOS", "test.image")
path = os.path.join(config.settings.Server.images_path, "IOS", "test.image")
assert os.path.exists(path), path
+1 -1
View File
@@ -234,7 +234,7 @@ async def test_create_image_missing(node, compute):
@pytest.mark.asyncio
async def test_create_base_script(node, config, compute, tmpdir):
config.set_section_config("Server", {"configs_path": str(tmpdir)})
config.settings.Server.configs_path = str(tmpdir)
with open(str(tmpdir / 'test.txt'), 'w+') as f:
f.write('hostname test')
+3 -3
View File
@@ -71,7 +71,7 @@ def test_json(project):
@pytest.mark.asyncio
async def test_restore(project, controller):
async def test_restore(project, controller, config):
compute = AsyncioMagicMock()
compute.id = "local"
@@ -95,8 +95,8 @@ async def test_restore(project, controller):
assert len(project.nodes) == 2
controller._notification = MagicMock()
with patch("gns3server.config.Config.get_section_config", return_value={"local": True}):
await snapshot.restore()
config.settings.Server.local = True
await snapshot.restore()
assert "snapshot.restored" in [c[0][0] for c in controller.notification.project_emit.call_args_list]
# project.closed notification should not be send when restoring snapshots