2015-01-21 21:32:33 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2015 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
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2015-02-27 17:30:22 +00:00
|
|
|
|
2015-01-21 21:32:33 +00:00
|
|
|
import pytest
|
|
|
|
import socket
|
|
|
|
import asyncio
|
2015-01-23 16:39:17 +00:00
|
|
|
import tempfile
|
|
|
|
import shutil
|
2015-02-09 09:18:37 +00:00
|
|
|
import os
|
2015-02-27 17:30:22 +00:00
|
|
|
import sys
|
2015-01-21 21:32:33 +00:00
|
|
|
from aiohttp import web
|
2015-05-14 10:03:17 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
2015-01-23 16:39:17 +00:00
|
|
|
|
2015-02-27 17:30:22 +00:00
|
|
|
sys._called_from_test = True
|
2015-08-25 08:13:49 +00:00
|
|
|
sys.original_platform = sys.platform
|
|
|
|
|
2015-02-27 17:30:22 +00:00
|
|
|
# Prevent execution of external binaries
|
|
|
|
os.environ["PATH"] = tempfile.mkdtemp()
|
|
|
|
|
2015-01-23 16:39:17 +00:00
|
|
|
from gns3server.config import Config
|
2015-01-21 21:32:33 +00:00
|
|
|
from gns3server.web.route import Route
|
|
|
|
# TODO: get rid of *
|
|
|
|
from gns3server.handlers import *
|
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
|
2016-03-03 15:02:27 +00:00
|
|
|
from gns3server.controller import Controller
|
2015-02-23 10:27:07 +00:00
|
|
|
from tests.handlers.api.base import Query
|
2015-01-21 21:32:33 +00:00
|
|
|
|
|
|
|
|
2015-02-16 16:20:07 +00:00
|
|
|
@pytest.yield_fixture
|
|
|
|
def restore_original_path():
|
|
|
|
"""
|
|
|
|
Temporary restore a standard path environnement. This allow
|
|
|
|
to run external binaries.
|
|
|
|
"""
|
|
|
|
os.environ["PATH"] = "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
|
|
|
|
yield
|
|
|
|
os.environ["PATH"] = tempfile.mkdtemp()
|
|
|
|
|
|
|
|
|
2016-09-02 12:09:01 +00:00
|
|
|
@pytest.yield_fixture(scope="session")
|
2015-01-21 21:32:33 +00:00
|
|
|
def loop(request):
|
|
|
|
"""Return an event loop and destroy it at the end of test"""
|
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
asyncio.set_event_loop(loop) # Replace main loop to avoid conflict between tests
|
2016-09-02 12:09:01 +00:00
|
|
|
yield loop
|
|
|
|
#loop.close()
|
|
|
|
asyncio.set_event_loop(None)
|
2015-01-21 21:32:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _get_unused_port():
|
|
|
|
""" Return an unused port on localhost. In rare occasion it can return
|
|
|
|
an already used port (race condition)"""
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
s.bind(('localhost', 0))
|
|
|
|
addr, port = s.getsockname()
|
|
|
|
s.close()
|
|
|
|
return port
|
|
|
|
|
|
|
|
|
2016-09-02 12:09:01 +00:00
|
|
|
@pytest.yield_fixture
|
|
|
|
def http_server(request, loop, port_manager, monkeypatch, controller):
|
2015-01-23 20:01:23 +00:00
|
|
|
"""A GNS3 server"""
|
2015-01-22 15:12:21 +00:00
|
|
|
|
2015-01-21 21:32:33 +00:00
|
|
|
app = web.Application()
|
|
|
|
for method, route, handler in Route.get_routes():
|
|
|
|
app.router.add_route(method, route, handler)
|
2016-03-16 15:34:16 +00:00
|
|
|
|
|
|
|
host = "localhost"
|
|
|
|
|
|
|
|
# We try multiple time. Because on Travis test can fail when because the port is taken by someone else
|
|
|
|
for i in range(0, 5):
|
|
|
|
port = _get_unused_port()
|
|
|
|
try:
|
|
|
|
srv = loop.create_server(app.make_handler(), host, port)
|
|
|
|
srv = loop.run_until_complete(srv)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
break
|
2015-01-21 21:32:33 +00:00
|
|
|
|
2016-09-02 12:09:01 +00:00
|
|
|
yield (host, port)
|
|
|
|
|
|
|
|
loop.run_until_complete(controller.stop())
|
|
|
|
for module in MODULES:
|
|
|
|
instance = module.instance()
|
|
|
|
monkeypatch.setattr('gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.close', lambda self: True)
|
|
|
|
loop.run_until_complete(instance.unload())
|
|
|
|
srv.close()
|
|
|
|
srv.wait_closed()
|
2016-03-07 14:01:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def http_root(loop, http_server):
|
|
|
|
"""
|
|
|
|
Return an helper allowing you to call the server without any prefix
|
|
|
|
"""
|
|
|
|
host, port = http_server
|
2015-01-21 21:32:33 +00:00
|
|
|
return Query(loop, host=host, port=port)
|
|
|
|
|
|
|
|
|
2016-03-07 14:01:35 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def http_controller(loop, http_server):
|
|
|
|
"""
|
|
|
|
Return an helper allowing you to call the server API without any prefix
|
|
|
|
"""
|
|
|
|
host, port = http_server
|
2016-03-11 16:16:09 +00:00
|
|
|
return Query(loop, host=host, port=port, api_version=2)
|
2016-03-07 14:01:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2016-04-15 15:57:06 +00:00
|
|
|
def http_compute(loop, http_server):
|
2016-03-07 14:01:35 +00:00
|
|
|
"""
|
|
|
|
Return an helper allowing you to call the hypervisor API via HTTP
|
|
|
|
"""
|
|
|
|
host, port = http_server
|
2016-04-15 15:57:06 +00:00
|
|
|
return Query(loop, host=host, port=port, prefix="/compute", api_version=2)
|
2016-03-07 14:01:35 +00:00
|
|
|
|
|
|
|
|
2015-02-26 09:45:37 +00:00
|
|
|
@pytest.fixture(scope="function")
|
2015-05-14 10:03:17 +00:00
|
|
|
def project(tmpdir):
|
2015-01-22 15:12:21 +00:00
|
|
|
"""A GNS3 lab"""
|
|
|
|
|
2015-05-14 10:03:17 +00:00
|
|
|
p = ProjectManager.instance().create_project(project_id="a1e920ca-338a-4e9f-b363-aa607b09dd80")
|
|
|
|
return p
|
2015-01-21 21:32:33 +00:00
|
|
|
|
|
|
|
|
2016-10-24 19:39:35 +00:00
|
|
|
@pytest.fixture(scope="function")
|
2015-01-21 21:32:33 +00:00
|
|
|
def port_manager():
|
2015-01-22 15:12:21 +00:00
|
|
|
"""An instance of port manager"""
|
2016-10-26 12:43:47 +00:00
|
|
|
PortManager._instance = None
|
|
|
|
p = PortManager.instance()
|
|
|
|
p.console_host = "127.0.0.1"
|
|
|
|
return p
|
2015-01-21 21:32:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
2015-03-21 23:19:12 +00:00
|
|
|
def free_console_port(request, port_manager, project):
|
2015-01-22 15:12:21 +00:00
|
|
|
"""Get a free TCP port"""
|
|
|
|
|
2015-01-21 21:32:33 +00:00
|
|
|
# In case of already use ports we will raise an exception
|
2015-03-21 23:19:12 +00:00
|
|
|
port = port_manager.get_free_tcp_port(project)
|
2015-01-21 21:32:33 +00:00
|
|
|
# We release the port immediately in order to allow
|
|
|
|
# the test do whatever the test want
|
2015-03-21 23:19:12 +00:00
|
|
|
port_manager.release_tcp_port(port, project)
|
2015-01-21 21:32:33 +00:00
|
|
|
return port
|
2015-01-23 16:39:17 +00:00
|
|
|
|
|
|
|
|
2015-06-09 14:35:21 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def ethernet_device():
|
2015-11-08 20:34:27 +00:00
|
|
|
import psutil
|
|
|
|
return sorted(psutil.net_if_addrs().keys())[0]
|
2015-06-09 14:35:21 +00:00
|
|
|
|
|
|
|
|
2016-03-03 15:02:27 +00:00
|
|
|
@pytest.fixture
|
2016-04-19 13:35:50 +00:00
|
|
|
def controller_config_path(tmpdir):
|
|
|
|
return str(tmpdir / "config" / "gns3_controller.conf")
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def controller(tmpdir, controller_config_path):
|
2016-03-03 15:02:27 +00:00
|
|
|
Controller._instance = None
|
2016-04-19 13:35:50 +00:00
|
|
|
controller = Controller.instance()
|
|
|
|
controller._config_file = controller_config_path
|
|
|
|
return controller
|
2016-03-03 15:02:27 +00:00
|
|
|
|
|
|
|
|
2016-04-19 08:47:53 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def config():
|
|
|
|
config = Config.instance()
|
|
|
|
config.clear()
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
2015-01-23 16:39:17 +00:00
|
|
|
@pytest.yield_fixture(autouse=True)
|
2016-04-19 08:47:53 +00:00
|
|
|
def run_around_tests(monkeypatch, port_manager, controller, config):
|
2015-02-06 10:14:56 +00:00
|
|
|
"""
|
|
|
|
This setup a temporay project file environnement around tests
|
|
|
|
"""
|
|
|
|
|
2015-01-23 16:39:17 +00:00
|
|
|
tmppath = tempfile.mkdtemp()
|
|
|
|
|
2016-10-24 19:39:35 +00:00
|
|
|
for module in MODULES:
|
|
|
|
module._instance = None
|
|
|
|
|
2016-01-26 14:32:40 +00:00
|
|
|
os.makedirs(os.path.join(tmppath, 'projects'))
|
2016-05-11 16:42:55 +00:00
|
|
|
config.set("Server", "projects_path", os.path.join(tmppath, 'projects'))
|
2016-06-28 17:58:57 +00:00
|
|
|
config.set("Server", "symbols_path", os.path.join(tmppath, 'symbols'))
|
2016-01-26 14:32:40 +00:00
|
|
|
config.set("Server", "images_path", os.path.join(tmppath, 'images'))
|
2016-06-23 10:10:18 +00:00
|
|
|
config.set("Server", "ubridge_path", os.path.join(tmppath, 'bin', 'ubridge'))
|
2015-06-11 07:18:02 +00:00
|
|
|
config.set("Server", "auth", False)
|
2015-01-23 16:39:17 +00:00
|
|
|
|
2015-06-26 15:13:32 +00:00
|
|
|
# Prevent executions of the VM if we forgot to mock something
|
2015-03-16 14:03:41 +00:00
|
|
|
config.set("VirtualBox", "vboxmanage_path", tmppath)
|
|
|
|
config.set("VPCS", "vpcs_path", tmppath)
|
2015-06-26 15:13:32 +00:00
|
|
|
config.set("VMware", "vmrun_path", tmppath)
|
|
|
|
|
|
|
|
# Force turn off KVM because it's not available on CI
|
|
|
|
config.set("Qemu", "enable_kvm", False)
|
|
|
|
|
2016-05-11 16:42:55 +00:00
|
|
|
monkeypatch.setattr("gns3server.utils.path.get_default_project_directory", lambda *args: os.path.join(tmppath, 'projects'))
|
2015-02-06 10:14:56 +00:00
|
|
|
|
2015-08-25 08:13:49 +00:00
|
|
|
# Force sys.platform to the original value. Because it seem not be restore correctly at each tests
|
|
|
|
sys.platform = sys.original_platform
|
|
|
|
|
2015-01-23 16:39:17 +00:00
|
|
|
yield
|
|
|
|
|
2015-01-26 12:54:44 +00:00
|
|
|
# An helper should not raise Exception
|
|
|
|
try:
|
|
|
|
shutil.rmtree(tmppath)
|
|
|
|
except:
|
|
|
|
pass
|
2015-08-07 14:49:45 +00:00
|
|
|
|
2015-09-03 14:46:34 +00:00
|
|
|
|
2016-06-07 13:34:04 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def images_dir(config):
|
|
|
|
"""
|
|
|
|
Get the location of images
|
|
|
|
"""
|
|
|
|
path = config.get_section_config("Server").get("images_path")
|
|
|
|
os.makedirs(path, exist_ok=True)
|
|
|
|
os.makedirs(os.path.join(path, "QEMU"))
|
|
|
|
os.makedirs(os.path.join(path, "IOU"))
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
2016-06-28 17:58:57 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def symbols_dir(config):
|
|
|
|
"""
|
|
|
|
Get the location of symbols
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
path = config.get_section_config("Server").get("projects_path")
|
|
|
|
os.makedirs(path, exist_ok=True)
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
2016-06-23 10:10:18 +00:00
|
|
|
@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
|
|
|
|
|
|
|
|
|
2015-08-07 14:49:45 +00:00
|
|
|
@pytest.yield_fixture
|
|
|
|
def darwin_platform():
|
|
|
|
"""
|
|
|
|
Change sys.plaform to Darwin
|
|
|
|
"""
|
|
|
|
old_platform = sys.platform
|
|
|
|
sys.platform = "darwin10.10"
|
|
|
|
yield
|
|
|
|
sys.plaform = old_platform
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.yield_fixture
|
|
|
|
def windows_platform():
|
|
|
|
"""
|
|
|
|
Change sys.plaform to Windows
|
|
|
|
"""
|
|
|
|
old_platform = sys.platform
|
|
|
|
sys.platform = "win10"
|
|
|
|
yield
|
|
|
|
sys.plaform = old_platform
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.yield_fixture
|
|
|
|
def linux_platform():
|
|
|
|
"""
|
|
|
|
Change sys.plaform to Linux
|
|
|
|
"""
|
|
|
|
old_platform = sys.platform
|
|
|
|
sys.platform = "linuxdebian"
|
|
|
|
yield
|
|
|
|
sys.plaform = old_platform
|
2016-03-10 09:32:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def async_run(loop):
|
|
|
|
"""
|
|
|
|
Shortcut for running in asyncio loop
|
|
|
|
"""
|
|
|
|
return lambda x: loop.run_until_complete(asyncio.async(x))
|
2016-08-19 17:02:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.yield_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
|
|
|
|
"""
|
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
|