2015-01-21 21:32:33 +00:00
|
|
|
import pytest
|
2020-06-16 08:57:16 +00:00
|
|
|
import asyncio
|
2022-08-24 19:03:16 +00:00
|
|
|
import pytest_asyncio
|
2015-01-23 16:39:17 +00:00
|
|
|
import tempfile
|
|
|
|
import shutil
|
2020-10-02 06:37:50 +00:00
|
|
|
import sys
|
|
|
|
import os
|
2021-04-05 04:51:41 +00:00
|
|
|
import uuid
|
2021-04-12 10:07:59 +00:00
|
|
|
import configparser
|
2021-11-18 08:07:10 +00:00
|
|
|
import base64
|
2023-08-11 07:32:05 +00:00
|
|
|
import stat
|
2020-06-16 04:29:03 +00:00
|
|
|
|
2020-12-02 08:09:08 +00:00
|
|
|
from fastapi import FastAPI
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
|
|
|
from httpx import AsyncClient
|
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
|
|
|
|
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
|
2021-04-05 04:51:41 +00:00
|
|
|
from gns3server.db.models import Base, User, Compute
|
2020-12-07 06:22:36 +00:00
|
|
|
from gns3server.db.repositories.users import UsersRepository
|
2021-04-05 04:51:41 +00:00
|
|
|
from gns3server.db.repositories.computes import ComputesRepository
|
2020-12-07 06:22:36 +00:00
|
|
|
from gns3server.api.routes.controller.dependencies.database import get_db_session
|
2021-04-05 04:51:41 +00:00
|
|
|
from gns3server import schemas
|
2021-04-15 09:00:22 +00:00
|
|
|
from gns3server.schemas.controller.computes import Protocol
|
2020-12-07 06:22:36 +00:00
|
|
|
from gns3server.services import auth_service
|
2021-04-12 10:07:59 +00:00
|
|
|
from gns3server.services.authentication import DEFAULT_JWT_SECRET_KEY
|
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-12-02 08:09:08 +00:00
|
|
|
|
2020-12-07 06:22:36 +00:00
|
|
|
# https://github.com/pytest-dev/pytest-asyncio/issues/68
|
|
|
|
# this event_loop is used by pytest-asyncio, and redefining it
|
|
|
|
# is currently the only way of changing the scope of this fixture
|
2021-04-05 04:51:41 +00:00
|
|
|
@pytest.fixture(scope="class")
|
2020-12-07 06:22:36 +00:00
|
|
|
def event_loop(request):
|
|
|
|
|
|
|
|
loop = asyncio.get_event_loop_policy().new_event_loop()
|
|
|
|
yield loop
|
|
|
|
loop.close()
|
|
|
|
|
|
|
|
|
2022-08-24 19:03:16 +00:00
|
|
|
@pytest_asyncio.fixture(scope="class")
|
2020-12-07 08:23:40 +00:00
|
|
|
async def app() -> FastAPI:
|
2020-12-07 06:22:36 +00:00
|
|
|
|
|
|
|
from gns3server.api.server import app as gns3app
|
|
|
|
yield gns3app
|
|
|
|
|
|
|
|
|
2022-08-24 19:03:16 +00:00
|
|
|
@pytest_asyncio.fixture(scope="class")
|
2021-03-30 23:28:52 +00:00
|
|
|
async def db_engine():
|
2015-01-21 21:32:33 +00:00
|
|
|
|
2021-03-28 10:47:29 +00:00
|
|
|
db_url = os.getenv("GNS3_TEST_DATABASE_URI", "sqlite+aiosqlite:///:memory:") # "sqlite:///./sql_test_app.db"
|
2020-12-07 06:22:36 +00:00
|
|
|
engine = create_async_engine(db_url, connect_args={"check_same_thread": False}, future=True)
|
|
|
|
yield engine
|
2021-03-30 23:28:52 +00:00
|
|
|
#await engine.sync_engine.dispose()
|
2020-12-02 08:09:08 +00:00
|
|
|
|
|
|
|
|
2022-08-24 19:03:16 +00:00
|
|
|
@pytest_asyncio.fixture(scope="class")
|
2020-12-07 08:23:40 +00:00
|
|
|
async def db_session(db_engine):
|
2020-06-16 04:29:03 +00:00
|
|
|
|
2020-12-07 06:22:36 +00:00
|
|
|
# recreate database tables for each class
|
|
|
|
# preferred and faster way would be to rollback the session/transaction
|
|
|
|
# but it doesn't work for some reason
|
2021-05-08 06:07:51 +00:00
|
|
|
async with db_engine.connect() as conn:
|
2021-04-18 08:09:47 +00:00
|
|
|
# Speed up tests by avoiding to hash the 'admin' password everytime the default super admin is added
|
|
|
|
# to the database using the "after_create" sqlalchemy event
|
|
|
|
hashed_password = "$2b$12$jPsNU9IS7.EWEqXahtDfo.26w6VLOLCuFEHKNvDpOjxs5e0WpqJfa"
|
|
|
|
with patch("gns3server.services.authentication.AuthService.hash_password", return_value=hashed_password):
|
|
|
|
await conn.run_sync(Base.metadata.drop_all)
|
|
|
|
await conn.run_sync(Base.metadata.create_all)
|
2018-10-17 10:32:10 +00:00
|
|
|
|
2021-05-08 06:07:51 +00:00
|
|
|
session = AsyncSession(db_engine, expire_on_commit=False)
|
2020-12-07 06:22:36 +00:00
|
|
|
try:
|
|
|
|
yield session
|
|
|
|
finally:
|
|
|
|
await session.close()
|
2016-03-16 15:34:16 +00:00
|
|
|
|
2020-10-19 08:55:32 +00:00
|
|
|
|
2022-08-24 19:03:16 +00:00
|
|
|
@pytest_asyncio.fixture
|
2021-04-19 00:10:04 +00:00
|
|
|
async def base_client(app: FastAPI, db_session: AsyncSession) -> AsyncClient:
|
2020-12-07 06:22:36 +00:00
|
|
|
|
|
|
|
async def _get_test_db():
|
|
|
|
try:
|
|
|
|
yield db_session
|
|
|
|
finally:
|
|
|
|
pass
|
|
|
|
|
|
|
|
app.dependency_overrides[get_db_session] = _get_test_db
|
2020-12-02 08:09:08 +00:00
|
|
|
|
|
|
|
async with AsyncClient(
|
|
|
|
app=app,
|
|
|
|
base_url="http://test-api",
|
|
|
|
headers={"Content-Type": "application/json"}
|
2021-04-19 00:10:04 +00:00
|
|
|
) as async_client:
|
|
|
|
yield async_client
|
2020-10-19 08:55:32 +00:00
|
|
|
|
|
|
|
|
2022-08-24 19:03:16 +00:00
|
|
|
@pytest_asyncio.fixture
|
2020-12-07 06:22:36 +00:00
|
|
|
async def test_user(db_session: AsyncSession) -> User:
|
|
|
|
|
2021-04-05 04:51:41 +00:00
|
|
|
new_user = schemas.UserCreate(
|
2020-12-07 06:22:36 +00:00
|
|
|
username="user1",
|
|
|
|
email="user1@email.com",
|
|
|
|
password="user1_password",
|
|
|
|
)
|
|
|
|
user_repo = UsersRepository(db_session)
|
|
|
|
existing_user = await user_repo.get_user_by_username(new_user.username)
|
|
|
|
if existing_user:
|
|
|
|
return existing_user
|
2021-05-27 07:58:44 +00:00
|
|
|
user = await user_repo.create_user(new_user)
|
|
|
|
|
2023-08-27 08:20:42 +00:00
|
|
|
# add new user to the "Users" group
|
2021-05-27 07:58:44 +00:00
|
|
|
group = await user_repo.get_user_group_by_name("Users")
|
|
|
|
await user_repo.add_member_to_user_group(group.user_group_id, user)
|
|
|
|
return user
|
2020-12-07 06:22:36 +00:00
|
|
|
|
|
|
|
|
2022-08-24 19:03:16 +00:00
|
|
|
@pytest_asyncio.fixture
|
2021-04-05 04:51:41 +00:00
|
|
|
async def test_compute(db_session: AsyncSession) -> Compute:
|
|
|
|
|
|
|
|
new_compute = schemas.ComputeCreate(
|
|
|
|
compute_id=uuid.uuid4(),
|
|
|
|
protocol=Protocol.http,
|
|
|
|
host="localhost",
|
|
|
|
port=4242,
|
|
|
|
user="julien",
|
|
|
|
password="secure"
|
|
|
|
)
|
|
|
|
|
|
|
|
compute_repo = ComputesRepository(db_session)
|
|
|
|
existing_compute = await compute_repo.get_compute(new_compute.compute_id)
|
|
|
|
if existing_compute:
|
|
|
|
return existing_compute
|
|
|
|
return await compute_repo.create_compute(new_compute)
|
|
|
|
|
|
|
|
|
2020-12-07 06:22:36 +00:00
|
|
|
@pytest.fixture
|
2021-04-19 00:10:04 +00:00
|
|
|
def unauthorized_client(base_client: AsyncClient, test_user: User) -> AsyncClient:
|
|
|
|
return base_client
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def authorized_client(base_client: AsyncClient, test_user: User) -> AsyncClient:
|
2020-12-07 06:22:36 +00:00
|
|
|
|
|
|
|
access_token = auth_service.create_access_token(test_user.username)
|
2021-04-19 00:10:04 +00:00
|
|
|
base_client.headers = {
|
|
|
|
**base_client.headers,
|
2020-12-07 06:22:36 +00:00
|
|
|
"Authorization": f"Bearer {access_token}",
|
|
|
|
}
|
2021-04-19 00:10:04 +00:00
|
|
|
return base_client
|
|
|
|
|
2020-12-07 06:22:36 +00:00
|
|
|
|
2022-08-24 19:03:16 +00:00
|
|
|
@pytest_asyncio.fixture
|
2021-04-19 00:10:04 +00:00
|
|
|
async def client(base_client: AsyncClient) -> AsyncClient:
|
2021-04-18 08:09:47 +00:00
|
|
|
|
2021-04-19 00:10:04 +00:00
|
|
|
# The super admin is automatically created when the users table is created
|
|
|
|
# this account that can access all endpoints without restrictions.
|
2021-04-18 08:09:47 +00:00
|
|
|
access_token = auth_service.create_access_token("admin")
|
2021-04-19 00:10:04 +00:00
|
|
|
base_client.headers = {
|
|
|
|
**base_client.headers,
|
2021-04-18 08:09:47 +00:00
|
|
|
"Authorization": f"Bearer {access_token}",
|
|
|
|
}
|
2021-04-19 00:10:04 +00:00
|
|
|
return base_client
|
|
|
|
|
2020-12-07 06:22:36 +00:00
|
|
|
|
2022-08-24 19:03:16 +00:00
|
|
|
@pytest_asyncio.fixture
|
2021-11-18 08:07:10 +00:00
|
|
|
async def compute_client(base_client: AsyncClient) -> AsyncClient:
|
|
|
|
|
2023-11-12 08:11:41 +00:00
|
|
|
# default compute username is 'gns3'
|
|
|
|
base64_credentials = base64.b64encode(b"gns3:").decode("ascii")
|
2021-11-18 08:07:10 +00:00
|
|
|
base_client.headers = {
|
|
|
|
**base_client.headers,
|
|
|
|
"Authorization": f"Basic {base64_credentials}",
|
|
|
|
}
|
|
|
|
return base_client
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2022-08-24 19:03:16 +00:00
|
|
|
@pytest_asyncio.fixture
|
2020-10-02 06:37:50 +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
|
|
|
|
|
|
|
|
2016-04-19 08:47:53 +00:00
|
|
|
@pytest.fixture
|
2021-04-12 10:07:59 +00:00
|
|
|
def config(tmpdir):
|
|
|
|
|
|
|
|
path = str(tmpdir / "server.conf")
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
with open(path, "w+") as f:
|
|
|
|
config.write(f)
|
|
|
|
Config.reset()
|
|
|
|
config = Config.instance(files=[path])
|
2016-04-19 08:47:53 +00:00
|
|
|
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
|
|
|
|
2021-04-12 07:32:23 +00:00
|
|
|
path = config.settings.Server.images_path
|
2016-06-07 13:34:04 +00:00
|
|
|
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
|
|
|
|
2021-04-12 07:32:23 +00:00
|
|
|
path = config.settings.Server.symbols_path
|
2016-06-28 17:58:57 +00:00
|
|
|
os.makedirs(path, exist_ok=True)
|
|
|
|
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
|
|
|
|
2021-04-12 07:32:23 +00:00
|
|
|
path = config.settings.Server.projects_path
|
2016-06-17 15:50:06 +00:00
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
2021-04-12 07:32:23 +00:00
|
|
|
path = config.settings.Server.ubridge_path
|
2020-06-16 04:29:03 +00:00
|
|
|
os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
|
|
open(path, 'w+').close()
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
2022-03-30 10:38:34 +00:00
|
|
|
def run_around_tests(monkeypatch, config, port_manager):
|
2020-06-16 04:29:03 +00:00
|
|
|
"""
|
|
|
|
This setup a temporary project file environment around tests
|
|
|
|
"""
|
|
|
|
|
|
|
|
tmppath = tempfile.mkdtemp()
|
|
|
|
|
|
|
|
for module in MODULES:
|
|
|
|
module._instance = None
|
|
|
|
|
2021-04-12 10:07:59 +00:00
|
|
|
config.settings.Controller.jwt_secret_key = DEFAULT_JWT_SECRET_KEY
|
|
|
|
|
2021-04-15 08:42:08 +00:00
|
|
|
secrets_dir = os.path.join(tmppath, 'secrets')
|
|
|
|
os.makedirs(secrets_dir)
|
|
|
|
config.settings.Server.secrets_dir = secrets_dir
|
|
|
|
|
|
|
|
projects_dir = os.path.join(tmppath, 'projects')
|
|
|
|
os.makedirs(projects_dir)
|
|
|
|
config.settings.Server.projects_path = projects_dir
|
|
|
|
|
|
|
|
symbols_dir = os.path.join(tmppath, 'symbols')
|
|
|
|
os.makedirs(symbols_dir)
|
|
|
|
config.settings.Server.symbols_path = symbols_dir
|
|
|
|
|
|
|
|
images_dir = os.path.join(tmppath, 'images')
|
|
|
|
os.makedirs(images_dir)
|
|
|
|
config.settings.Server.images_path = images_dir
|
|
|
|
|
|
|
|
appliances_dir = os.path.join(tmppath, 'appliances')
|
|
|
|
os.makedirs(appliances_dir)
|
|
|
|
config.settings.Server.appliances_path = appliances_dir
|
|
|
|
|
2021-04-12 07:32:23 +00:00
|
|
|
config.settings.Server.ubridge_path = os.path.join(tmppath, 'bin', 'ubridge')
|
2020-06-16 04:29:03 +00:00
|
|
|
|
|
|
|
# Prevent executions of the VM if we forgot to mock something
|
2021-04-12 07:32:23 +00:00
|
|
|
config.settings.VirtualBox.vboxmanage_path = tmppath
|
|
|
|
config.settings.VPCS.vpcs_path = tmppath
|
|
|
|
config.settings.VMware.vmrun_path = tmppath
|
|
|
|
config.settings.Dynamips.dynamips_path = tmppath
|
|
|
|
|
2020-06-16 04:29:03 +00:00
|
|
|
|
|
|
|
# Force turn off KVM because it's not available on CI
|
2021-04-12 07:32:23 +00:00
|
|
|
config.settings.Qemu.enable_hardware_acceleration = False
|
2020-06-16 04:29:03 +00:00
|
|
|
|
|
|
|
monkeypatch.setattr("gns3server.utils.path.get_default_project_directory", lambda *args: os.path.join(tmppath, 'projects'))
|
|
|
|
|
2023-08-11 07:32:05 +00:00
|
|
|
# Force sys.platform to the original value. Because it seems not be restored correctly after each test
|
2020-06-16 04:29:03 +00:00
|
|
|
sys.platform = sys.original_platform
|
|
|
|
|
|
|
|
yield
|
|
|
|
|
2023-08-11 07:32:05 +00:00
|
|
|
# A helper should not raise Exception
|
2020-06-16 04:29:03 +00:00
|
|
|
try:
|
|
|
|
shutil.rmtree(tmppath)
|
|
|
|
except BaseException:
|
|
|
|
pass
|
2023-08-11 07:32:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fake_executable(monkeypatch, tmpdir) -> str:
|
|
|
|
|
|
|
|
monkeypatch.setenv("PATH", str(tmpdir))
|
|
|
|
executable_path = os.path.join(os.environ["PATH"], "fake_executable")
|
|
|
|
with open(executable_path, "w+") as f:
|
|
|
|
f.write("1")
|
|
|
|
os.chmod(executable_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
|
|
|
|
return executable_path
|