mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 05:51:15 +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
@@ -37,19 +37,20 @@ async def manager(port_manager):
|
||||
return m
|
||||
|
||||
|
||||
def test_vm_invalid_dynamips_path(manager):
|
||||
def test_vm_invalid_dynamips_path(manager, config):
|
||||
|
||||
with patch("gns3server.config.Config.get_section_config", return_value={"dynamips_path": "/bin/test_fake"}):
|
||||
with pytest.raises(DynamipsError):
|
||||
manager.find_dynamips()
|
||||
config.settings.Dynamips.dynamips_path = "/bin/test_fake"
|
||||
with pytest.raises(DynamipsError):
|
||||
manager.find_dynamips()
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported by Windows")
|
||||
def test_vm_non_executable_dynamips_path(manager):
|
||||
def test_vm_non_executable_dynamips_path(manager, config):
|
||||
|
||||
tmpfile = tempfile.NamedTemporaryFile()
|
||||
with patch("gns3server.config.Config.get_section_config", return_value={"dynamips_path": tmpfile.name}):
|
||||
with pytest.raises(DynamipsError):
|
||||
manager.find_dynamips()
|
||||
config.settings.Dynamips.dynamips_path = tmpfile.name
|
||||
with pytest.raises(DynamipsError):
|
||||
manager.find_dynamips()
|
||||
|
||||
|
||||
def test_get_dynamips_id(manager):
|
||||
|
||||
@@ -66,11 +66,11 @@ def test_convert_project_before_2_0_0_b3(compute_project, manager):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_router_invalid_dynamips_path(compute_project, manager):
|
||||
async def test_router_invalid_dynamips_path(compute_project, config, manager):
|
||||
|
||||
config = Config.instance()
|
||||
config.set("Dynamips", "dynamips_path", "/bin/test_fake")
|
||||
config.set("Dynamips", "allocate_aux_console_ports", False)
|
||||
config.settings.Dynamips.dynamips_path = "/bin/test_fake"
|
||||
config.settings.Dynamips.allocate_aux_console_ports = False
|
||||
|
||||
with pytest.raises(DynamipsError):
|
||||
router = Router("test", "00010203-0405-0607-0809-0a0b0c0d0e0e", compute_project, manager)
|
||||
|
||||
@@ -47,12 +47,10 @@ async def manager(port_manager):
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
@pytest.mark.asyncio
|
||||
async def vm(compute_project, manager, tmpdir, fake_iou_bin, iourc_file):
|
||||
async def vm(compute_project, manager, config, tmpdir, fake_iou_bin, iourc_file):
|
||||
|
||||
vm = IOUVM("test", str(uuid.uuid4()), compute_project, manager, application_id=1)
|
||||
config = manager.config.get_section_config("IOU")
|
||||
config["iourc_path"] = iourc_file
|
||||
manager.config.set_section_config("IOU", config)
|
||||
config.settings.IOU.iourc_path = iourc_file
|
||||
vm.path = "iou.bin"
|
||||
return vm
|
||||
|
||||
@@ -118,7 +116,7 @@ async def test_start(vm):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_start_with_iourc(vm, tmpdir):
|
||||
async def test_start_with_iourc(vm, tmpdir, config):
|
||||
|
||||
fake_file = str(tmpdir / "iourc")
|
||||
with open(fake_file, "w+") as f:
|
||||
@@ -131,13 +129,13 @@ async def test_start_with_iourc(vm, tmpdir):
|
||||
vm._start_ubridge = AsyncioMagicMock(return_value=True)
|
||||
vm._ubridge_send = AsyncioMagicMock()
|
||||
|
||||
with patch("gns3server.config.Config.get_section_config", return_value={"iourc_path": fake_file}):
|
||||
with asyncio_patch("asyncio.create_subprocess_exec", return_value=mock_process) as exec_mock:
|
||||
mock_process.returncode = None
|
||||
await vm.start()
|
||||
assert vm.is_running()
|
||||
arsgs, kwargs = exec_mock.call_args
|
||||
assert kwargs["env"]["IOURC"] == fake_file
|
||||
config.settings.IOU.iourc_path = fake_file
|
||||
with asyncio_patch("asyncio.create_subprocess_exec", return_value=mock_process) as exec_mock:
|
||||
mock_process.returncode = None
|
||||
await vm.start()
|
||||
assert vm.is_running()
|
||||
arsgs, kwargs = exec_mock.call_args
|
||||
assert kwargs["env"]["IOURC"] == fake_file
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -224,7 +222,7 @@ async def test_close(vm, port_manager):
|
||||
|
||||
def test_path(vm, fake_iou_bin, config):
|
||||
|
||||
config.set_section_config("Server", {"local": True})
|
||||
config.settings.Server.local = True
|
||||
vm.path = fake_iou_bin
|
||||
assert vm.path == fake_iou_bin
|
||||
|
||||
@@ -237,7 +235,7 @@ def test_path_relative(vm, fake_iou_bin):
|
||||
|
||||
def test_path_invalid_bin(vm, tmpdir, config):
|
||||
|
||||
config.set_section_config("Server", {"local": True})
|
||||
config.settings.Server.local = True
|
||||
path = str(tmpdir / "test.bin")
|
||||
|
||||
with open(path, "w+") as f:
|
||||
|
||||
@@ -77,7 +77,7 @@ async def vm(compute_project, manager, fake_qemu_binary, fake_qemu_img_binary):
|
||||
vm._start_ubridge = AsyncioMagicMock()
|
||||
vm._ubridge_hypervisor = MagicMock()
|
||||
vm._ubridge_hypervisor.is_running.return_value = True
|
||||
vm.manager.config.set("Qemu", "enable_hardware_acceleration", False)
|
||||
vm.manager.config.settings.Qemu.enable_hardware_acceleration = False
|
||||
return vm
|
||||
|
||||
|
||||
@@ -894,14 +894,14 @@ def test_get_qemu_img(vm, tmpdir):
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_with_hardware_acceleration_darwin(darwin_platform, vm):
|
||||
|
||||
vm.manager.config.set("Qemu", "enable_hardware_acceleration", False)
|
||||
vm.manager.config.settings.Qemu.enable_hardware_acceleration = False
|
||||
assert await vm._run_with_hardware_acceleration("qemu-system-x86_64", "") is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_run_with_hardware_acceleration_windows(windows_platform, vm):
|
||||
|
||||
vm.manager.config.set("Qemu", "enable_hardware_acceleration", False)
|
||||
vm.manager.config.settings.Qemu.enable_hardware_acceleration = False
|
||||
assert await vm._run_with_hardware_acceleration("qemu-system-x86_64", "") is False
|
||||
|
||||
|
||||
@@ -909,7 +909,7 @@ async def test_run_with_hardware_acceleration_windows(windows_platform, vm):
|
||||
async def test_run_with_kvm_linux(linux_platform, vm):
|
||||
|
||||
with patch("os.path.exists", return_value=True) as os_path:
|
||||
vm.manager.config.set("Qemu", "enable_kvm", True)
|
||||
vm.manager.config.settings.Qemu.enable_hardware_acceleration = True
|
||||
assert await vm._run_with_hardware_acceleration("qemu-system-x86_64", "") is True
|
||||
os_path.assert_called_with("/dev/kvm")
|
||||
|
||||
@@ -918,7 +918,7 @@ async def test_run_with_kvm_linux(linux_platform, vm):
|
||||
async def test_run_with_kvm_linux_options_no_kvm(linux_platform, vm):
|
||||
|
||||
with patch("os.path.exists", return_value=True) as os_path:
|
||||
vm.manager.config.set("Qemu", "enable_kvm", True)
|
||||
vm.manager.config.settings.Qemu.enable_hardware_acceleration = True
|
||||
assert await vm._run_with_hardware_acceleration("qemu-system-x86_64", "-no-kvm") is False
|
||||
|
||||
|
||||
@@ -926,7 +926,8 @@ async def test_run_with_kvm_linux_options_no_kvm(linux_platform, vm):
|
||||
async def test_run_with_kvm_not_x86(linux_platform, vm):
|
||||
|
||||
with patch("os.path.exists", return_value=True):
|
||||
vm.manager.config.set("Qemu", "enable_kvm", True)
|
||||
vm.manager.config.settings.Qemu.enable_hardware_acceleration = True
|
||||
vm.manager.config.settings.Qemu.require_hardware_acceleration = True
|
||||
with pytest.raises(QemuError):
|
||||
await vm._run_with_hardware_acceleration("qemu-system-arm", "")
|
||||
|
||||
@@ -935,6 +936,7 @@ async def test_run_with_kvm_not_x86(linux_platform, vm):
|
||||
async def test_run_with_kvm_linux_dev_kvm_missing(linux_platform, vm):
|
||||
|
||||
with patch("os.path.exists", return_value=False):
|
||||
vm.manager.config.set("Qemu", "enable_kvm", True)
|
||||
vm.manager.config.settings.Qemu.enable_hardware_acceleration = True
|
||||
vm.manager.config.settings.Qemu.require_hardware_acceleration = True
|
||||
with pytest.raises(QemuError):
|
||||
await vm._run_with_hardware_acceleration("qemu-system-x86_64", "")
|
||||
@@ -86,7 +86,7 @@ def test_get_abs_image_path(qemu, tmpdir, config):
|
||||
path2 = force_unix_path(str(tmpdir / "QEMU" / "test2.bin"))
|
||||
open(path2, 'w+').close()
|
||||
|
||||
config.set_section_config("Server", {"images_path": str(tmpdir)})
|
||||
config.settings.Server.images_path = str(tmpdir)
|
||||
assert qemu.get_abs_image_path(path1) == path1
|
||||
assert qemu.get_abs_image_path("test1.bin") == path1
|
||||
assert qemu.get_abs_image_path(path2) == path2
|
||||
@@ -105,14 +105,16 @@ def test_get_abs_image_path_non_local(qemu, tmpdir, config):
|
||||
path2 = force_unix_path(str(path2))
|
||||
|
||||
# If non local we can't use path outside images directory
|
||||
config.set_section_config("Server", {"images_path": str(tmpdir / "images"), "local": False})
|
||||
config.settings.Server.images_path = str(tmpdir / "images")
|
||||
config.settings.Server.local = False
|
||||
assert qemu.get_abs_image_path(path1) == path1
|
||||
with pytest.raises(NodeError):
|
||||
qemu.get_abs_image_path(path2)
|
||||
with pytest.raises(NodeError):
|
||||
qemu.get_abs_image_path("C:\\test2.bin")
|
||||
|
||||
config.set_section_config("Server", {"images_path": str(tmpdir / "images"), "local": True})
|
||||
config.settings.Server.images_path = str(tmpdir / "images")
|
||||
config.settings.Server.local = True
|
||||
assert qemu.get_abs_image_path(path2) == path2
|
||||
|
||||
|
||||
@@ -126,10 +128,9 @@ def test_get_abs_image_additional_image_paths(qemu, tmpdir, config):
|
||||
path2.write("1", ensure=True)
|
||||
path2 = force_unix_path(str(path2))
|
||||
|
||||
config.set_section_config("Server", {
|
||||
"images_path": str(tmpdir / "images1"),
|
||||
"additional_images_path": "/tmp/null24564;{}".format(str(tmpdir / "images2")),
|
||||
"local": False})
|
||||
config.settings.Server.images_path = str(tmpdir / "images1")
|
||||
config.settings.Server.additional_images_paths = "/tmp/null24564;" + str(tmpdir / "images2")
|
||||
config.settings.Server.local = False
|
||||
|
||||
assert qemu.get_abs_image_path("test1.bin") == path1
|
||||
assert qemu.get_abs_image_path("test2.bin") == path2
|
||||
@@ -150,9 +151,9 @@ def test_get_abs_image_recursive(qemu, tmpdir, config):
|
||||
path2.write("1", ensure=True)
|
||||
path2 = force_unix_path(str(path2))
|
||||
|
||||
config.set_section_config("Server", {
|
||||
"images_path": str(tmpdir / "images1"),
|
||||
"local": False})
|
||||
config.settings.Server.images_path = str(tmpdir / "images1")
|
||||
config.settings.Server.local = False
|
||||
|
||||
assert qemu.get_abs_image_path("test1.bin") == path1
|
||||
assert qemu.get_abs_image_path("test2.bin") == path2
|
||||
# Absolute path
|
||||
@@ -169,9 +170,9 @@ def test_get_abs_image_recursive_ova(qemu, tmpdir, config):
|
||||
path2.write("1", ensure=True)
|
||||
path2 = force_unix_path(str(path2))
|
||||
|
||||
config.set_section_config("Server", {
|
||||
"images_path": str(tmpdir / "images1"),
|
||||
"local": False})
|
||||
config.settings.Server.images_path = str(tmpdir / "images1")
|
||||
config.settings.Server.local = False
|
||||
|
||||
assert qemu.get_abs_image_path("test.ova/test1.bin") == path1
|
||||
assert qemu.get_abs_image_path("test.ova/test2.bin") == path2
|
||||
# Absolute path
|
||||
@@ -199,11 +200,10 @@ def test_get_relative_image_path(qemu, tmpdir, config):
|
||||
path5 = force_unix_path(str(tmpdir / "images1" / "VBOX" / "test5.bin"))
|
||||
open(path5, 'w+').close()
|
||||
|
||||
config.set_section_config("Server", {
|
||||
"images_path": str(tmpdir / "images1"),
|
||||
"additional_images_path": str(tmpdir / "images2"),
|
||||
"local": True
|
||||
})
|
||||
config.settings.Server.images_path = str(tmpdir / "images1")
|
||||
config.settings.Server.additional_images_paths = str(tmpdir / "images2")
|
||||
config.settings.Server.local = True
|
||||
|
||||
assert qemu.get_relative_image_path(path1) == "test1.bin"
|
||||
assert qemu.get_relative_image_path("test1.bin") == "test1.bin"
|
||||
assert qemu.get_relative_image_path(path2) == "test2.bin"
|
||||
|
||||
@@ -135,10 +135,10 @@ def test_set_console_host(config):
|
||||
"""
|
||||
|
||||
p = PortManager()
|
||||
config.set_section_config("Server", {"allow_remote_console": False})
|
||||
config.settings.Server.allow_remote_console = False
|
||||
p.console_host = "10.42.1.42"
|
||||
assert p.console_host == "10.42.1.42"
|
||||
p = PortManager()
|
||||
config.set_section_config("Server", {"allow_remote_console": True})
|
||||
config.settings.Server.allow_remote_console = True
|
||||
p.console_host = "10.42.1.42"
|
||||
assert p.console_host == "0.0.0.0"
|
||||
@@ -194,30 +194,30 @@ async def test_project_close(node, compute_project):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_files(tmpdir):
|
||||
async def test_list_files(tmpdir, config):
|
||||
|
||||
with patch("gns3server.config.Config.get_section_config", return_value={"projects_path": str(tmpdir)}):
|
||||
project = Project(project_id=str(uuid4()))
|
||||
path = project.path
|
||||
os.makedirs(os.path.join(path, "vm-1", "dynamips"))
|
||||
with open(os.path.join(path, "vm-1", "dynamips", "test.bin"), "w+") as f:
|
||||
f.write("test")
|
||||
open(os.path.join(path, "vm-1", "dynamips", "test.ghost"), "w+").close()
|
||||
with open(os.path.join(path, "test.txt"), "w+") as f:
|
||||
f.write("test2")
|
||||
config.settings.Server.projects_path = str(tmpdir)
|
||||
project = Project(project_id=str(uuid4()))
|
||||
path = project.path
|
||||
os.makedirs(os.path.join(path, "vm-1", "dynamips"))
|
||||
with open(os.path.join(path, "vm-1", "dynamips", "test.bin"), "w+") as f:
|
||||
f.write("test")
|
||||
open(os.path.join(path, "vm-1", "dynamips", "test.ghost"), "w+").close()
|
||||
with open(os.path.join(path, "test.txt"), "w+") as f:
|
||||
f.write("test2")
|
||||
|
||||
files = await project.list_files()
|
||||
files = await project.list_files()
|
||||
|
||||
assert files == [
|
||||
{
|
||||
"path": "test.txt",
|
||||
"md5sum": "ad0234829205b9033196ba818f7a872b"
|
||||
},
|
||||
{
|
||||
"path": os.path.join("vm-1", "dynamips", "test.bin"),
|
||||
"md5sum": "098f6bcd4621d373cade4e832627b4f6"
|
||||
}
|
||||
]
|
||||
assert files == [
|
||||
{
|
||||
"path": "test.txt",
|
||||
"md5sum": "ad0234829205b9033196ba818f7a872b"
|
||||
},
|
||||
{
|
||||
"path": os.path.join("vm-1", "dynamips", "test.bin"),
|
||||
"md5sum": "098f6bcd4621d373cade4e832627b4f6"
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
@@ -36,40 +36,40 @@ async def manager(port_manager):
|
||||
return m
|
||||
|
||||
|
||||
def test_vm_invalid_vboxmanage_path(manager):
|
||||
def test_vm_invalid_vboxmanage_path(manager, config):
|
||||
|
||||
with patch("gns3server.config.Config.get_section_config", return_value={"vboxmanage_path": "/bin/test_fake"}):
|
||||
with pytest.raises(VirtualBoxError):
|
||||
manager.find_vboxmanage()
|
||||
config.settings.VirtualBox.vboxmanage_path = "/bin/test_fake"
|
||||
with pytest.raises(VirtualBoxError):
|
||||
manager.find_vboxmanage()
|
||||
|
||||
|
||||
def test_vm_non_executable_vboxmanage_path(manager):
|
||||
def test_vm_non_executable_vboxmanage_path(manager, config):
|
||||
|
||||
tmpfile = tempfile.NamedTemporaryFile()
|
||||
with patch("gns3server.config.Config.get_section_config", return_value={"vboxmanage_path": tmpfile.name}):
|
||||
with pytest.raises(VirtualBoxError):
|
||||
manager.find_vboxmanage()
|
||||
config.settings.VirtualBox.vboxmanage_path = tmpfile.name
|
||||
with pytest.raises(VirtualBoxError):
|
||||
manager.find_vboxmanage()
|
||||
|
||||
|
||||
def test_vm_invalid_executable_name_vboxmanage_path(manager, tmpdir):
|
||||
def test_vm_invalid_executable_name_vboxmanage_path(manager, config, tmpdir):
|
||||
|
||||
path = str(tmpdir / "vpcs")
|
||||
with open(path, "w+") as f:
|
||||
f.write(path)
|
||||
os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
|
||||
with patch("gns3server.config.Config.get_section_config", return_value={"vboxmanage_path": path}):
|
||||
with pytest.raises(VirtualBoxError):
|
||||
manager.find_vboxmanage()
|
||||
config.settings.VirtualBox.vboxmanage_path = path
|
||||
with pytest.raises(VirtualBoxError):
|
||||
manager.find_vboxmanage()
|
||||
|
||||
|
||||
def test_vboxmanage_path(manager, tmpdir):
|
||||
def test_vboxmanage_path(manager, config, tmpdir):
|
||||
|
||||
path = str(tmpdir / "VBoxManage")
|
||||
with open(path, "w+") as f:
|
||||
f.write(path)
|
||||
os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
|
||||
with patch("gns3server.config.Config.get_section_config", return_value={"vboxmanage_path": path}):
|
||||
assert manager.find_vboxmanage() == path
|
||||
config.settings.VirtualBox.vboxmanage_path = path
|
||||
assert manager.find_vboxmanage() == path
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
Reference in new issue
Block a user