2015-01-21 23:32:33 +02: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 19:30:22 +02:00
|
|
|
|
2015-01-21 23:32:33 +02:00
|
|
|
import pytest
|
|
|
|
import socket
|
|
|
|
import asyncio
|
2015-01-23 18:39:17 +02:00
|
|
|
import tempfile
|
|
|
|
import shutil
|
2015-02-09 11:18:37 +02:00
|
|
|
import os
|
2015-02-27 19:30:22 +02:00
|
|
|
import sys
|
2015-01-21 23:32:33 +02:00
|
|
|
from aiohttp import web
|
2015-05-14 13:03:17 +03:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
2015-01-23 18:39:17 +02:00
|
|
|
|
2015-02-27 19:30:22 +02:00
|
|
|
sys._called_from_test = True
|
2015-08-25 11:13:49 +03:00
|
|
|
sys.original_platform = sys.platform
|
|
|
|
|
2015-02-27 19:30:22 +02:00
|
|
|
# Prevent execution of external binaries
|
|
|
|
os.environ["PATH"] = tempfile.mkdtemp()
|
|
|
|
|
2015-01-23 18:39:17 +02:00
|
|
|
from gns3server.config import Config
|
2015-01-21 23:32:33 +02:00
|
|
|
from gns3server.web.route import Route
|
|
|
|
# TODO: get rid of *
|
|
|
|
from gns3server.handlers import *
|
|
|
|
from gns3server.modules import MODULES
|
|
|
|
from gns3server.modules.port_manager import PortManager
|
|
|
|
from gns3server.modules.project_manager import ProjectManager
|
2015-02-23 12:27:07 +02:00
|
|
|
from tests.handlers.api.base import Query
|
2015-01-21 23:32:33 +02:00
|
|
|
|
|
|
|
|
2015-02-16 18:20:07 +02: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()
|
|
|
|
|
|
|
|
|
2015-01-21 23:32:33 +02:00
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
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
|
|
|
|
|
|
|
|
def tear_down():
|
|
|
|
loop.close()
|
|
|
|
asyncio.set_event_loop(None)
|
|
|
|
request.addfinalizer(tear_down)
|
|
|
|
return loop
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2015-11-02 17:34:34 +02:00
|
|
|
@pytest.fixture
|
2015-02-09 11:59:09 +02:00
|
|
|
def server(request, loop, port_manager, monkeypatch):
|
2015-01-23 22:01:23 +02:00
|
|
|
"""A GNS3 server"""
|
2015-01-22 17:12:21 +02:00
|
|
|
|
2015-01-21 23:32:33 +02:00
|
|
|
port = _get_unused_port()
|
|
|
|
host = "localhost"
|
|
|
|
app = web.Application()
|
|
|
|
for method, route, handler in Route.get_routes():
|
|
|
|
app.router.add_route(method, route, handler)
|
|
|
|
for module in MODULES:
|
|
|
|
instance = module.instance()
|
|
|
|
instance.port_manager = port_manager
|
|
|
|
srv = loop.create_server(app.make_handler(), host, port)
|
|
|
|
srv = loop.run_until_complete(srv)
|
|
|
|
|
|
|
|
def tear_down():
|
|
|
|
for module in MODULES:
|
2015-01-23 08:40:51 +02:00
|
|
|
instance = module.instance()
|
2015-02-09 11:59:09 +02:00
|
|
|
monkeypatch.setattr('gns3server.modules.virtualbox.virtualbox_vm.VirtualBoxVM.close', lambda self: True)
|
2015-01-23 08:40:51 +02:00
|
|
|
loop.run_until_complete(instance.unload())
|
2015-01-21 23:32:33 +02:00
|
|
|
srv.close()
|
|
|
|
srv.wait_closed()
|
|
|
|
request.addfinalizer(tear_down)
|
|
|
|
return Query(loop, host=host, port=port)
|
|
|
|
|
|
|
|
|
2015-02-26 11:45:37 +02:00
|
|
|
@pytest.fixture(scope="function")
|
2015-05-14 13:03:17 +03:00
|
|
|
def project(tmpdir):
|
2015-01-22 17:12:21 +02:00
|
|
|
"""A GNS3 lab"""
|
|
|
|
|
2015-05-14 13:03:17 +03:00
|
|
|
p = ProjectManager.instance().create_project(project_id="a1e920ca-338a-4e9f-b363-aa607b09dd80")
|
|
|
|
return p
|
2015-01-21 23:32:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def port_manager():
|
2015-01-22 17:12:21 +02:00
|
|
|
"""An instance of port manager"""
|
|
|
|
|
2015-01-24 01:38:59 +02:00
|
|
|
return PortManager("127.0.0.1")
|
2015-01-21 23:32:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
2015-03-22 01:19:12 +02:00
|
|
|
def free_console_port(request, port_manager, project):
|
2015-01-22 17:12:21 +02:00
|
|
|
"""Get a free TCP port"""
|
|
|
|
|
2015-01-21 23:32:33 +02:00
|
|
|
# In case of already use ports we will raise an exception
|
2015-03-22 01:19:12 +02:00
|
|
|
port = port_manager.get_free_tcp_port(project)
|
2015-01-21 23:32:33 +02:00
|
|
|
# We release the port immediately in order to allow
|
|
|
|
# the test do whatever the test want
|
2015-03-22 01:19:12 +02:00
|
|
|
port_manager.release_tcp_port(port, project)
|
2015-01-21 23:32:33 +02:00
|
|
|
return port
|
2015-01-23 18:39:17 +02:00
|
|
|
|
|
|
|
|
2015-06-09 17:35:21 +03:00
|
|
|
@pytest.fixture
|
|
|
|
def ethernet_device():
|
2015-11-08 22:34:27 +02:00
|
|
|
import psutil
|
|
|
|
return sorted(psutil.net_if_addrs().keys())[0]
|
2015-06-09 17:35:21 +03:00
|
|
|
|
|
|
|
|
2015-01-23 18:39:17 +02:00
|
|
|
@pytest.yield_fixture(autouse=True)
|
2015-09-03 17:46:34 +03:00
|
|
|
def run_around_tests(monkeypatch, port_manager):
|
2015-02-06 12:14:56 +02:00
|
|
|
"""
|
|
|
|
This setup a temporay project file environnement around tests
|
|
|
|
"""
|
|
|
|
|
2015-01-23 18:39:17 +02:00
|
|
|
tmppath = tempfile.mkdtemp()
|
|
|
|
|
2015-09-03 17:46:34 +03:00
|
|
|
port_manager._instance = port_manager
|
2015-01-23 18:39:17 +02:00
|
|
|
config = Config.instance()
|
2015-03-17 16:40:58 +02:00
|
|
|
config.clear()
|
2015-03-16 16:03:41 +02:00
|
|
|
config.set("Server", "project_directory", tmppath)
|
2015-06-11 10:18:02 +03:00
|
|
|
config.set("Server", "auth", False)
|
2015-01-23 18:39:17 +02:00
|
|
|
|
2015-06-26 18:13:32 +03:00
|
|
|
# Prevent executions of the VM if we forgot to mock something
|
2015-03-16 16:03:41 +02:00
|
|
|
config.set("VirtualBox", "vboxmanage_path", tmppath)
|
|
|
|
config.set("VPCS", "vpcs_path", tmppath)
|
2015-06-26 18:13:32 +03:00
|
|
|
config.set("VMware", "vmrun_path", tmppath)
|
|
|
|
|
|
|
|
# Force turn off KVM because it's not available on CI
|
|
|
|
config.set("Qemu", "enable_kvm", False)
|
|
|
|
|
2015-02-06 12:14:56 +02:00
|
|
|
monkeypatch.setattr("gns3server.modules.project.Project._get_default_project_directory", lambda *args: tmppath)
|
|
|
|
|
2015-08-25 11:13:49 +03: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 18:39:17 +02:00
|
|
|
yield
|
|
|
|
|
2015-01-26 14:54:44 +02:00
|
|
|
# An helper should not raise Exception
|
|
|
|
try:
|
|
|
|
shutil.rmtree(tmppath)
|
|
|
|
except:
|
|
|
|
pass
|
2015-08-07 17:49:45 +03:00
|
|
|
|
2015-09-03 17:46:34 +03:00
|
|
|
|
2015-08-07 17:49:45 +03: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
|