2015-01-21 21:32:33 +00:00
|
|
|
import pytest
|
2020-06-16 08:57:16 +00:00
|
|
|
import asyncio
|
2015-01-23 16:39:17 +00:00
|
|
|
import tempfile
|
|
|
|
import shutil
|
2020-06-16 04:29:03 +00:00
|
|
|
import weakref
|
|
|
|
|
2015-01-21 21:32:33 +00:00
|
|
|
from aiohttp import web
|
2020-06-16 04:29:03 +00:00
|
|
|
from unittest.mock import MagicMock, patch
|
2020-01-07 22:03:31 +00:00
|
|
|
from pathlib import Path
|
2015-05-14 10:03:17 +00:00
|
|
|
|
2015-01-21 21:32:33 +00:00
|
|
|
from gns3server.web.route import Route
|
2020-06-16 04:29:03 +00:00
|
|
|
from gns3server.controller import Controller
|
|
|
|
from gns3server.config import Config
|
2016-04-15 15:57:06 +00:00
|
|
|
from gns3server.compute import MODULES
|
|
|
|
from gns3server.compute.port_manager import PortManager
|
|
|
|
from gns3server.compute.project_manager import ProjectManager
|
2020-06-16 04:29:03 +00:00
|
|
|
# this import will register all handlers
|
|
|
|
from gns3server.handlers import *
|
2015-01-21 21:32:33 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
from .handlers.api.base import Query
|
2015-01-21 21:32:33 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
sys._called_from_test = True
|
|
|
|
sys.original_platform = sys.platform
|
2015-01-21 21:32:33 +00:00
|
|
|
|
2020-06-16 09:15:11 +00:00
|
|
|
|
|
|
|
if sys.platform.startswith("win"):
|
2024-05-09 10:53:02 +00:00
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def loop(request):
|
2020-06-16 09:15:11 +00:00
|
|
|
"""Return an event loop and destroy it at the end of test"""
|
|
|
|
|
|
|
|
loop = asyncio.ProactorEventLoop()
|
|
|
|
asyncio.set_event_loop(loop) # Replace main loop to avoid conflict between tests
|
|
|
|
yield loop
|
|
|
|
asyncio.set_event_loop(None)
|
2020-06-16 04:38:17 +00:00
|
|
|
|
2015-01-21 21:32:33 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
async def http_client(aiohttp_client):
|
|
|
|
|
2018-10-17 10:32:10 +00:00
|
|
|
app = web.Application()
|
2020-06-16 04:29:03 +00:00
|
|
|
app['websockets'] = weakref.WeakSet()
|
2018-10-17 10:32:10 +00:00
|
|
|
for method, route, handler in Route.get_routes():
|
|
|
|
app.router.add_route(method, route, handler)
|
|
|
|
return await aiohttp_client(app)
|
|
|
|
|
2016-03-16 15:34:16 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def controller_config_path(tmpdir):
|
2018-10-17 10:32:10 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
return str(tmpdir / "config" / "gns3_controller.conf")
|
2016-03-16 15:34:16 +00:00
|
|
|
|
2018-10-17 10:32:10 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def controller(tmpdir, controller_config_path):
|
2015-01-21 21:32:33 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
Controller._instance = None
|
|
|
|
controller = Controller.instance()
|
|
|
|
os.makedirs(os.path.dirname(controller_config_path), exist_ok=True)
|
|
|
|
Path(controller_config_path).touch()
|
|
|
|
controller._config_file = controller_config_path
|
|
|
|
controller._config_loaded = True
|
|
|
|
return controller
|
2016-09-02 12:09:01 +00:00
|
|
|
|
2018-10-17 10:32:10 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def compute(controller):
|
2018-10-17 10:32:10 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
compute = MagicMock()
|
|
|
|
compute.id = "example.com"
|
|
|
|
controller._computes = {"example.com": compute}
|
|
|
|
return compute
|
2016-03-07 14:01:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2024-05-09 10:53:02 +00:00
|
|
|
async def project(tmpdir, controller):
|
2020-06-16 04:29:03 +00:00
|
|
|
|
|
|
|
return await controller.add_project(name="Test")
|
2015-01-21 21:32:33 +00:00
|
|
|
|
|
|
|
|
2016-03-07 14:01:35 +00:00
|
|
|
@pytest.fixture
|
2020-06-16 04:29:03 +00:00
|
|
|
def compute_project(tmpdir):
|
|
|
|
|
|
|
|
return ProjectManager.instance().create_project(project_id="a1e920ca-338a-4e9f-b363-aa607b09dd80")
|
2016-03-07 14:01:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2020-06-16 04:29:03 +00:00
|
|
|
def compute_api(http_client):
|
2016-03-07 14:01:35 +00:00
|
|
|
"""
|
|
|
|
Return an helper allowing you to call the hypervisor API via HTTP
|
|
|
|
"""
|
2015-01-21 21:32:33 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
return Query(http_client, prefix="/compute", api_version=2)
|
2015-01-23 16:39:17 +00:00
|
|
|
|
|
|
|
|
2015-06-09 14:35:21 +00:00
|
|
|
@pytest.fixture
|
2020-06-16 04:29:03 +00:00
|
|
|
def controller_api(http_client, controller):
|
|
|
|
"""
|
|
|
|
Return an helper allowing you to call the server API without any prefix
|
|
|
|
"""
|
2016-04-19 13:35:50 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
return Query(http_client, api_version=2)
|
2016-03-03 15:02:27 +00:00
|
|
|
|
|
|
|
|
2016-04-19 08:47:53 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def config():
|
2020-06-16 04:29:03 +00:00
|
|
|
|
2016-04-19 08:47:53 +00:00
|
|
|
config = Config.instance()
|
|
|
|
config.clear()
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
2016-06-07 13:34:04 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def images_dir(config):
|
|
|
|
"""
|
|
|
|
Get the location of images
|
|
|
|
"""
|
2020-06-16 04:29:03 +00:00
|
|
|
|
2016-06-07 13:34:04 +00:00
|
|
|
path = config.get_section_config("Server").get("images_path")
|
|
|
|
os.makedirs(path, exist_ok=True)
|
2020-08-13 07:48:45 +00:00
|
|
|
os.makedirs(os.path.join(path, "QEMU"), exist_ok=True)
|
|
|
|
os.makedirs(os.path.join(path, "IOU"), exist_ok=True)
|
2016-06-07 13:34:04 +00:00
|
|
|
return path
|
|
|
|
|
|
|
|
|
2016-06-28 17:58:57 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def symbols_dir(config):
|
|
|
|
"""
|
|
|
|
Get the location of symbols
|
|
|
|
"""
|
2020-06-16 04:29:03 +00:00
|
|
|
|
2016-06-28 17:58:57 +00:00
|
|
|
path = config.get_section_config("Server").get("symbols_path")
|
|
|
|
os.makedirs(path, exist_ok=True)
|
|
|
|
print(path)
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
2016-06-17 15:50:06 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def projects_dir(config):
|
|
|
|
"""
|
|
|
|
Get the location of images
|
|
|
|
"""
|
2020-06-16 04:29:03 +00:00
|
|
|
|
2016-06-17 15:50:06 +00:00
|
|
|
path = config.get_section_config("Server").get("projects_path")
|
|
|
|
os.makedirs(path, exist_ok=True)
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
def port_manager():
|
|
|
|
"""An instance of port manager"""
|
2016-06-23 10:10:18 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
PortManager._instance = None
|
|
|
|
p = PortManager.instance()
|
|
|
|
p.console_host = "127.0.0.1"
|
|
|
|
return p
|
2016-06-23 10:10:18 +00:00
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
def free_console_port(port_manager, compute_project):
|
|
|
|
"""Get a free TCP port"""
|
|
|
|
|
|
|
|
# In case of already use ports we will raise an exception
|
|
|
|
port = port_manager.get_free_tcp_port(compute_project)
|
|
|
|
# We release the port immediately in order to allow
|
|
|
|
# the test do whatever the test want
|
|
|
|
port_manager.release_tcp_port(port, compute_project)
|
|
|
|
return port
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2015-08-07 14:49:45 +00:00
|
|
|
def darwin_platform():
|
|
|
|
"""
|
|
|
|
Change sys.plaform to Darwin
|
|
|
|
"""
|
2020-06-16 04:29:03 +00:00
|
|
|
|
2015-08-07 14:49:45 +00:00
|
|
|
old_platform = sys.platform
|
|
|
|
sys.platform = "darwin10.10"
|
|
|
|
yield
|
|
|
|
sys.plaform = old_platform
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
@pytest.fixture
|
2015-08-07 14:49:45 +00:00
|
|
|
def windows_platform():
|
|
|
|
"""
|
2017-09-11 08:09:32 +00:00
|
|
|
Change sys.platform to Windows
|
2015-08-07 14:49:45 +00:00
|
|
|
"""
|
2020-06-16 04:29:03 +00:00
|
|
|
|
2015-08-07 14:49:45 +00:00
|
|
|
old_platform = sys.platform
|
|
|
|
sys.platform = "win10"
|
|
|
|
yield
|
|
|
|
sys.plaform = old_platform
|
|
|
|
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
@pytest.fixture
|
2015-08-07 14:49:45 +00:00
|
|
|
def linux_platform():
|
|
|
|
"""
|
2017-09-11 08:09:32 +00:00
|
|
|
Change sys.platform to Linux
|
2015-08-07 14:49:45 +00:00
|
|
|
"""
|
2020-06-16 04:29:03 +00:00
|
|
|
|
2015-08-07 14:49:45 +00:00
|
|
|
old_platform = sys.platform
|
|
|
|
sys.platform = "linuxdebian"
|
|
|
|
yield
|
|
|
|
sys.plaform = old_platform
|
2016-03-10 09:32:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2016-09-05 16:40:49 +00:00
|
|
|
def on_gns3vm(linux_platform):
|
2016-08-19 17:02:39 +00:00
|
|
|
"""
|
|
|
|
Mock the hostname to emulate the GNS3 VM
|
|
|
|
"""
|
2020-06-16 04:29:03 +00:00
|
|
|
|
2016-09-22 15:46:32 +00:00
|
|
|
with patch("gns3server.utils.interfaces.interfaces", return_value=[
|
|
|
|
{"name": "eth0", "special": False, "type": "ethernet"},
|
|
|
|
{"name": "eth1", "special": False, "type": "ethernet"},
|
|
|
|
{"name": "virbr0", "special": True, "type": "ethernet"}]):
|
2016-08-30 16:27:04 +00:00
|
|
|
with patch("socket.gethostname", return_value="gns3vm"):
|
|
|
|
yield
|
2020-06-16 04:29:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def ethernet_device():
|
|
|
|
|
|
|
|
import psutil
|
|
|
|
return sorted(psutil.net_if_addrs().keys())[0]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def ubridge_path(config):
|
|
|
|
"""
|
|
|
|
Get the location of a fake ubridge
|
|
|
|
"""
|
|
|
|
|
|
|
|
path = config.get_section_config("Server").get("ubridge_path")
|
|
|
|
os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
|
|
open(path, 'w+').close()
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def run_around_tests(monkeypatch, config, port_manager):#port_manager, controller, config):
|
|
|
|
"""
|
|
|
|
This setup a temporary project file environment around tests
|
|
|
|
"""
|
|
|
|
|
|
|
|
tmppath = tempfile.mkdtemp()
|
|
|
|
|
|
|
|
for module in MODULES:
|
|
|
|
module._instance = None
|
|
|
|
|
|
|
|
os.makedirs(os.path.join(tmppath, 'projects'))
|
|
|
|
config.set("Server", "projects_path", os.path.join(tmppath, 'projects'))
|
|
|
|
config.set("Server", "symbols_path", os.path.join(tmppath, 'symbols'))
|
|
|
|
config.set("Server", "images_path", os.path.join(tmppath, 'images'))
|
|
|
|
config.set("Server", "appliances_path", os.path.join(tmppath, 'appliances'))
|
|
|
|
config.set("Server", "ubridge_path", os.path.join(tmppath, 'bin', 'ubridge'))
|
|
|
|
config.set("Server", "auth", False)
|
|
|
|
|
|
|
|
# Prevent executions of the VM if we forgot to mock something
|
|
|
|
config.set("VirtualBox", "vboxmanage_path", tmppath)
|
|
|
|
config.set("VPCS", "vpcs_path", tmppath)
|
|
|
|
config.set("VMware", "vmrun_path", tmppath)
|
|
|
|
config.set("Dynamips", "dynamips_path", tmppath)
|
|
|
|
|
|
|
|
# Force turn off KVM because it's not available on CI
|
|
|
|
config.set("Qemu", "enable_kvm", False)
|
|
|
|
|
|
|
|
monkeypatch.setattr("gns3server.utils.path.get_default_project_directory", lambda *args: os.path.join(tmppath, 'projects'))
|
|
|
|
|
|
|
|
# Force sys.platform to the original value. Because it seem not be restore correctly at each tests
|
|
|
|
sys.platform = sys.original_platform
|
|
|
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
# An helper should not raise Exception
|
|
|
|
try:
|
|
|
|
shutil.rmtree(tmppath)
|
|
|
|
except BaseException:
|
|
|
|
pass
|