mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 05:51:15 +02:00
Move fixtures to conftest
This commit is contained in:
1 parent
f3e07d5ad9
commit
97cefa23fb
8 files changed
+99
-89
No files matched your search
@@ -21,19 +21,9 @@
|
||||
import json
|
||||
import re
|
||||
import asyncio
|
||||
import socket
|
||||
import pytest
|
||||
from aiohttp import web
|
||||
import aiohttp
|
||||
import os
|
||||
|
||||
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
|
||||
|
||||
|
||||
class Query:
|
||||
|
||||
@@ -125,53 +115,3 @@ class Query:
|
||||
def _example_file_path(self, method, path):
|
||||
path = re.sub('[^a-z0-9]', '', path)
|
||||
return "docs/api/examples/{}_{}.txt".format(method.lower(), path)
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
@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
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def server(request, loop, port_manager):
|
||||
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:
|
||||
loop.run_until_complete(module.destroy())
|
||||
srv.close()
|
||||
srv.wait_closed()
|
||||
request.addfinalizer(tear_down)
|
||||
return Query(loop, host=host, port=port)
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def project():
|
||||
return ProjectManager.instance().create_project(uuid="a1e920ca-338a-4e9f-b363-aa607b09dd80")
|
||||
@@ -20,8 +20,7 @@ This test suite check /project endpoint
|
||||
"""
|
||||
|
||||
|
||||
from tests.utils import asyncio_patch, port_manager
|
||||
from tests.api.base import server, loop
|
||||
from tests.utils import asyncio_patch
|
||||
from gns3server.version import __version__
|
||||
|
||||
|
||||
|
||||
@@ -20,8 +20,7 @@ This test suite check /version endpoint
|
||||
It's also used for unittest the HTTP implementation.
|
||||
"""
|
||||
|
||||
from tests.utils import asyncio_patch, port_manager
|
||||
from tests.api.base import server, loop
|
||||
from tests.utils import asyncio_patch
|
||||
from gns3server.version import __version__
|
||||
|
||||
|
||||
|
||||
@@ -15,8 +15,7 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from tests.api.base import server, loop, project
|
||||
from tests.utils import asyncio_patch, port_manager
|
||||
from tests.utils import asyncio_patch
|
||||
from gns3server.modules.virtualbox.virtualbox_vm import VirtualBoxVM
|
||||
|
||||
|
||||
|
||||
@@ -17,8 +17,7 @@
|
||||
|
||||
import pytest
|
||||
import os
|
||||
from tests.api.base import server, loop, project
|
||||
from tests.utils import asyncio_patch, free_console_port, port_manager
|
||||
from tests.utils import asyncio_patch
|
||||
from unittest.mock import patch, Mock
|
||||
from gns3server.modules.vpcs.vpcs_vm import VPCSVM
|
||||
|
||||
|
||||
Reference in new issue
Block a user