mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-06 07:11:13 +02:00
Refactor tests
* Use pytest-aiohttp * Use the async def / await syntax. * Fix tests to run with Python 3.8
This commit is contained in:
1 parent
f498ab06b4
commit
d3ea67da24
87 files changed
+3697
-3528
No files matched your search
@@ -1,6 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (C) 2015 GNS3 Technologies Inc.
|
||||
# Copyright (C) 2020 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
|
||||
@@ -16,7 +16,6 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import pytest
|
||||
import aiohttp
|
||||
import asyncio
|
||||
import os
|
||||
import stat
|
||||
@@ -24,10 +23,10 @@ import socket
|
||||
import sys
|
||||
import uuid
|
||||
import shutil
|
||||
|
||||
from tests.utils import asyncio_patch, AsyncioMagicMock
|
||||
|
||||
|
||||
from unittest.mock import patch, MagicMock, PropertyMock, call
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
||||
pytestmark = pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
|
||||
|
||||
@@ -36,29 +35,29 @@ if not sys.platform.startswith("win"):
|
||||
from gns3server.compute.iou.iou_error import IOUError
|
||||
from gns3server.compute.iou import IOU
|
||||
|
||||
from gns3server.config import Config
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def manager(port_manager):
|
||||
async def manager(loop, port_manager):
|
||||
|
||||
m = IOU.instance()
|
||||
m.port_manager = port_manager
|
||||
return m
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def vm(project, manager, tmpdir, fake_iou_bin, iourc_file):
|
||||
vm = IOUVM("test", str(uuid.uuid4()), project, manager, application_id=1)
|
||||
async def vm(loop, compute_project, manager, 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)
|
||||
|
||||
vm.path = "iou.bin"
|
||||
return vm
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def iourc_file(tmpdir):
|
||||
|
||||
path = str(tmpdir / "iourc")
|
||||
with open(path, "w+") as f:
|
||||
hostname = socket.gethostname()
|
||||
@@ -77,21 +76,23 @@ def fake_iou_bin(images_dir):
|
||||
return path
|
||||
|
||||
|
||||
def test_vm(project, manager):
|
||||
vm = IOUVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
|
||||
def test_vm(compute_project, manager):
|
||||
|
||||
vm = IOUVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", compute_project, manager)
|
||||
assert vm.name == "test"
|
||||
assert vm.id == "00010203-0405-0607-0809-0a0b0c0d0e0f"
|
||||
|
||||
|
||||
def test_vm_startup_config_content(project, manager):
|
||||
vm = IOUVM("test", "00010203-0405-0607-0808-0a0b0c0d0e0f", project, manager, application_id=1)
|
||||
def test_vm_startup_config_content(compute_project, manager):
|
||||
|
||||
vm = IOUVM("test", "00010203-0405-0607-0808-0a0b0c0d0e0f", compute_project, manager, application_id=1)
|
||||
vm.startup_config_content = "hostname %h"
|
||||
assert vm.name == "test"
|
||||
assert vm.startup_config_content == "hostname test"
|
||||
assert vm.id == "00010203-0405-0607-0808-0a0b0c0d0e0f"
|
||||
|
||||
|
||||
def test_start(loop, vm):
|
||||
async def test_start(vm):
|
||||
|
||||
mock_process = MagicMock()
|
||||
vm._check_requirements = AsyncioMagicMock(return_value=True)
|
||||
@@ -101,7 +102,7 @@ def test_start(loop, vm):
|
||||
|
||||
with asyncio_patch("asyncio.create_subprocess_exec", return_value=mock_process) as mock_exec:
|
||||
mock_process.returncode = None
|
||||
loop.run_until_complete(asyncio.ensure_future(vm.start()))
|
||||
await vm.start()
|
||||
assert vm.is_running()
|
||||
assert vm.command_line == ' '.join(mock_exec.call_args[0])
|
||||
|
||||
@@ -113,7 +114,7 @@ def test_start(loop, vm):
|
||||
vm._ubridge_send.assert_any_call("iol_bridge start IOL-BRIDGE-513")
|
||||
|
||||
|
||||
def test_start_with_iourc(loop, vm, tmpdir):
|
||||
async def test_start_with_iourc(vm, tmpdir):
|
||||
|
||||
fake_file = str(tmpdir / "iourc")
|
||||
with open(fake_file, "w+") as f:
|
||||
@@ -129,13 +130,13 @@ def test_start_with_iourc(loop, vm, tmpdir):
|
||||
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
|
||||
loop.run_until_complete(asyncio.ensure_future(vm.start()))
|
||||
await vm.start()
|
||||
assert vm.is_running()
|
||||
arsgs, kwargs = exec_mock.call_args
|
||||
assert kwargs["env"]["IOURC"] == fake_file
|
||||
|
||||
|
||||
def test_rename_nvram_file(loop, vm):
|
||||
async def test_rename_nvram_file(vm):
|
||||
"""
|
||||
It should rename the nvram file to the correct name before launching the VM
|
||||
"""
|
||||
@@ -151,9 +152,9 @@ def test_rename_nvram_file(loop, vm):
|
||||
assert os.path.exists(os.path.join(vm.working_dir, "vlan.dat-0000{}".format(vm.application_id)))
|
||||
|
||||
|
||||
def test_stop(loop, vm):
|
||||
process = MagicMock()
|
||||
async def test_stop(vm):
|
||||
|
||||
process = MagicMock()
|
||||
vm._check_requirements = AsyncioMagicMock(return_value=True)
|
||||
vm._check_iou_licence = AsyncioMagicMock(return_value=True)
|
||||
vm._start_ioucon = AsyncioMagicMock(return_value=True)
|
||||
@@ -167,17 +168,17 @@ def test_stop(loop, vm):
|
||||
|
||||
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
|
||||
with asyncio_patch("gns3server.utils.asyncio.wait_for_process_termination"):
|
||||
loop.run_until_complete(asyncio.ensure_future(vm.start()))
|
||||
await vm.start()
|
||||
process.returncode = None
|
||||
assert vm.is_running()
|
||||
loop.run_until_complete(asyncio.ensure_future(vm.stop()))
|
||||
await vm.stop()
|
||||
assert vm.is_running() is False
|
||||
process.terminate.assert_called_with()
|
||||
|
||||
|
||||
def test_reload(loop, vm, fake_iou_bin):
|
||||
process = MagicMock()
|
||||
async def test_reload(vm, fake_iou_bin):
|
||||
|
||||
process = MagicMock()
|
||||
vm._check_requirements = AsyncioMagicMock(return_value=True)
|
||||
vm._check_iou_licence = AsyncioMagicMock(return_value=True)
|
||||
vm._start_ioucon = AsyncioMagicMock(return_value=True)
|
||||
@@ -192,33 +193,35 @@ def test_reload(loop, vm, fake_iou_bin):
|
||||
|
||||
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
|
||||
with asyncio_patch("gns3server.utils.asyncio.wait_for_process_termination"):
|
||||
loop.run_until_complete(asyncio.ensure_future(vm.start()))
|
||||
await vm.start()
|
||||
assert vm.is_running()
|
||||
loop.run_until_complete(asyncio.ensure_future(vm.reload()))
|
||||
await vm.reload()
|
||||
assert vm.is_running() is True
|
||||
process.terminate.assert_called_with()
|
||||
|
||||
|
||||
def test_close(vm, port_manager, loop):
|
||||
async def test_close(vm, port_manager):
|
||||
|
||||
vm._start_ubridge = AsyncioMagicMock(return_value=True)
|
||||
vm._ubridge_send = AsyncioMagicMock()
|
||||
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM._check_requirements", return_value=True):
|
||||
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()):
|
||||
loop.run_until_complete(asyncio.ensure_future(vm.start()))
|
||||
await vm.start()
|
||||
port = vm.console
|
||||
loop.run_until_complete(asyncio.ensure_future(vm.close()))
|
||||
await vm.close()
|
||||
# Raise an exception if the port is not free
|
||||
port_manager.reserve_tcp_port(port, vm.project)
|
||||
assert vm.is_running() is False
|
||||
|
||||
|
||||
def test_path(vm, fake_iou_bin, config):
|
||||
|
||||
config.set_section_config("Server", {"local": True})
|
||||
vm.path = fake_iou_bin
|
||||
assert vm.path == fake_iou_bin
|
||||
|
||||
|
||||
def test_path_relative(vm, fake_iou_bin, tmpdir):
|
||||
def test_path_relative(vm, fake_iou_bin):
|
||||
|
||||
vm.path = "iou.bin"
|
||||
assert vm.path == fake_iou_bin
|
||||
@@ -249,9 +252,9 @@ def test_create_netmap_config(vm):
|
||||
assert "513:15/3 1:15/3" in content
|
||||
|
||||
|
||||
def test_build_command(vm, loop):
|
||||
async def test_build_command(vm):
|
||||
|
||||
assert loop.run_until_complete(asyncio.ensure_future(vm._build_command())) == [vm.path, str(vm.application_id)]
|
||||
assert await vm._build_command() == [vm.path, str(vm.application_id)]
|
||||
|
||||
|
||||
def test_get_startup_config(vm):
|
||||
@@ -262,6 +265,7 @@ def test_get_startup_config(vm):
|
||||
|
||||
|
||||
def test_update_startup_config(vm):
|
||||
|
||||
content = "service timestamps debug datetime msec\nservice timestamps log datetime msec\nno service password-encryption"
|
||||
vm.startup_config_content = content
|
||||
filepath = os.path.join(vm.working_dir, "startup-config.cfg")
|
||||
@@ -271,6 +275,7 @@ def test_update_startup_config(vm):
|
||||
|
||||
|
||||
def test_update_startup_config_empty(vm):
|
||||
|
||||
content = "service timestamps debug datetime msec\nservice timestamps log datetime msec\nno service password-encryption"
|
||||
vm.startup_config_content = content
|
||||
filepath = os.path.join(vm.working_dir, "startup-config.cfg")
|
||||
@@ -283,6 +288,7 @@ def test_update_startup_config_empty(vm):
|
||||
|
||||
|
||||
def test_update_startup_config_content_hostname(vm):
|
||||
|
||||
content = "hostname %h\n"
|
||||
vm.name = "pc1"
|
||||
vm.startup_config_content = content
|
||||
@@ -290,7 +296,8 @@ def test_update_startup_config_content_hostname(vm):
|
||||
assert f.read() == "hostname pc1\n"
|
||||
|
||||
|
||||
def test_change_name(vm, tmpdir):
|
||||
def test_change_name(vm):
|
||||
|
||||
path = os.path.join(vm.working_dir, "startup-config.cfg")
|
||||
vm.name = "world"
|
||||
with open(path, 'w+') as f:
|
||||
@@ -309,50 +316,48 @@ def test_change_name(vm, tmpdir):
|
||||
assert f.read() == "no service password-encryption\nhostname charlie\nno ip icmp rate-limit unreachable"
|
||||
|
||||
|
||||
def test_library_check(loop, vm):
|
||||
async def test_library_check(vm):
|
||||
|
||||
with asyncio_patch("gns3server.utils.asyncio.subprocess_check_output", return_value="") as mock:
|
||||
with asyncio_patch("gns3server.utils.asyncio.subprocess_check_output", return_value=""):
|
||||
await vm._library_check()
|
||||
|
||||
loop.run_until_complete(asyncio.ensure_future(vm._library_check()))
|
||||
|
||||
with asyncio_patch("gns3server.utils.asyncio.subprocess_check_output", return_value="libssl => not found") as mock:
|
||||
with asyncio_patch("gns3server.utils.asyncio.subprocess_check_output", return_value="libssl => not found"):
|
||||
with pytest.raises(IOUError):
|
||||
loop.run_until_complete(asyncio.ensure_future(vm._library_check()))
|
||||
await vm._library_check()
|
||||
|
||||
|
||||
def test_enable_l1_keepalives(loop, vm):
|
||||
|
||||
with asyncio_patch("gns3server.utils.asyncio.subprocess_check_output", return_value="***************************************************************\n\n-l Enable Layer 1 keepalive messages\n-u <n> UDP port base for distributed networks\n") as mock:
|
||||
async def test_enable_l1_keepalives(vm):
|
||||
|
||||
with asyncio_patch("gns3server.utils.asyncio.subprocess_check_output", return_value="***************************************************************\n\n-l Enable Layer 1 keepalive messages\n-u <n> UDP port base for distributed networks\n"):
|
||||
command = ["test"]
|
||||
loop.run_until_complete(asyncio.ensure_future(vm._enable_l1_keepalives(command)))
|
||||
await vm._enable_l1_keepalives(command)
|
||||
assert command == ["test", "-l"]
|
||||
|
||||
with asyncio_patch("gns3server.utils.asyncio.subprocess_check_output", return_value="***************************************************************\n\n-u <n> UDP port base for distributed networks\n") as mock:
|
||||
with asyncio_patch("gns3server.utils.asyncio.subprocess_check_output", return_value="***************************************************************\n\n-u <n> UDP port base for distributed networks\n"):
|
||||
|
||||
command = ["test"]
|
||||
with pytest.raises(IOUError):
|
||||
loop.run_until_complete(asyncio.ensure_future(vm._enable_l1_keepalives(command)))
|
||||
await vm._enable_l1_keepalives(command)
|
||||
assert command == ["test"]
|
||||
|
||||
|
||||
def test_start_capture(vm, tmpdir, manager, free_console_port, loop):
|
||||
async def test_start_capture(vm, tmpdir, manager, free_console_port):
|
||||
|
||||
output_file = str(tmpdir / "test.pcap")
|
||||
nio = manager.create_nio({"type": "nio_udp", "lport": free_console_port, "rport": free_console_port, "rhost": "127.0.0.1"})
|
||||
loop.run_until_complete(asyncio.ensure_future(vm.adapter_add_nio_binding(0, 0, nio)))
|
||||
loop.run_until_complete(asyncio.ensure_future(vm.start_capture(0, 0, output_file)))
|
||||
await vm.adapter_add_nio_binding(0, 0, nio)
|
||||
await vm.start_capture(0, 0, output_file)
|
||||
assert vm._adapters[0].get_nio(0).capturing
|
||||
|
||||
|
||||
def test_stop_capture(vm, tmpdir, manager, free_console_port, loop):
|
||||
async def test_stop_capture(vm, tmpdir, manager, free_console_port):
|
||||
|
||||
output_file = str(tmpdir / "test.pcap")
|
||||
nio = manager.create_nio({"type": "nio_udp", "lport": free_console_port, "rport": free_console_port, "rhost": "127.0.0.1"})
|
||||
loop.run_until_complete(asyncio.ensure_future(vm.adapter_add_nio_binding(0, 0, nio)))
|
||||
loop.run_until_complete(vm.start_capture(0, 0, output_file))
|
||||
await vm.adapter_add_nio_binding(0, 0, nio)
|
||||
await vm.start_capture(0, 0, output_file)
|
||||
assert vm._adapters[0].get_nio(0).capturing
|
||||
loop.run_until_complete(asyncio.ensure_future(vm.stop_capture(0, 0)))
|
||||
await vm.stop_capture(0, 0)
|
||||
assert vm._adapters[0].get_nio(0).capturing is False
|
||||
|
||||
|
||||
@@ -361,46 +366,45 @@ def test_get_legacy_vm_workdir():
|
||||
assert IOU.get_legacy_vm_workdir(42, "bla") == "iou/device-42"
|
||||
|
||||
|
||||
def test_invalid_iou_file(loop, vm, iourc_file):
|
||||
async def test_invalid_iou_file(vm, iourc_file):
|
||||
|
||||
hostname = socket.gethostname()
|
||||
|
||||
loop.run_until_complete(asyncio.ensure_future(vm._check_iou_licence()))
|
||||
await vm._check_iou_licence()
|
||||
|
||||
# Missing ;
|
||||
with pytest.raises(IOUError):
|
||||
with open(iourc_file, "w+") as f:
|
||||
f.write("[license]\n{} = aaaaaaaaaaaaaaaa".format(hostname))
|
||||
loop.run_until_complete(asyncio.ensure_future(vm._check_iou_licence()))
|
||||
await vm._check_iou_licence()
|
||||
|
||||
# Key too short
|
||||
with pytest.raises(IOUError):
|
||||
with open(iourc_file, "w+") as f:
|
||||
f.write("[license]\n{} = aaaaaaaaaaaaaa;".format(hostname))
|
||||
loop.run_until_complete(asyncio.ensure_future(vm._check_iou_licence()))
|
||||
await vm._check_iou_licence()
|
||||
|
||||
# Invalid hostname
|
||||
with pytest.raises(IOUError):
|
||||
with open(iourc_file, "w+") as f:
|
||||
f.write("[license]\nbla = aaaaaaaaaaaaaa;")
|
||||
loop.run_until_complete(asyncio.ensure_future(vm._check_iou_licence()))
|
||||
await vm._check_iou_licence()
|
||||
|
||||
# Missing licence section
|
||||
with pytest.raises(IOUError):
|
||||
with open(iourc_file, "w+") as f:
|
||||
f.write("[licensetest]\n{} = aaaaaaaaaaaaaaaa;")
|
||||
loop.run_until_complete(asyncio.ensure_future(vm._check_iou_licence()))
|
||||
await vm._check_iou_licence()
|
||||
|
||||
# Broken config file
|
||||
with pytest.raises(IOUError):
|
||||
with open(iourc_file, "w+") as f:
|
||||
f.write("[")
|
||||
loop.run_until_complete(asyncio.ensure_future(vm._check_iou_licence()))
|
||||
await vm._check_iou_licence()
|
||||
|
||||
# Missing file
|
||||
with pytest.raises(IOUError):
|
||||
os.remove(iourc_file)
|
||||
loop.run_until_complete(asyncio.ensure_future(vm._check_iou_licence()))
|
||||
await vm._check_iou_licence()
|
||||
|
||||
|
||||
def test_iourc_content(vm):
|
||||
@@ -437,11 +441,11 @@ def test_extract_configs(vm):
|
||||
assert len(private_config) == 0
|
||||
|
||||
|
||||
def test_application_id(project, manager):
|
||||
def test_application_id(compute_project, manager):
|
||||
"""
|
||||
Checks if uses local manager to get application_id when not set
|
||||
"""
|
||||
vm = IOUVM("test", str(uuid.uuid4()), project, manager, application_id=1)
|
||||
vm = IOUVM("test", str(uuid.uuid4()), compute_project, manager, application_id=1)
|
||||
assert vm.application_id == 1
|
||||
|
||||
vm.application_id = 3
|
||||
|
||||
Reference in new issue
Block a user