2017-04-27 21:34:23 +01:00
|
|
|
"""
|
|
|
|
Unit test fixture module.
|
|
|
|
"""
|
2017-08-02 20:25:57 +01:00
|
|
|
|
2019-03-22 06:29:26 +00:00
|
|
|
import threading
|
2019-03-22 20:45:53 +00:00
|
|
|
import time
|
2017-08-02 20:25:57 +01:00
|
|
|
|
2019-10-24 07:15:19 +01:00
|
|
|
import mock
|
2017-04-27 21:34:23 +01:00
|
|
|
import pytest
|
2019-09-11 05:33:35 +01:00
|
|
|
|
2019-06-20 01:34:33 +01:00
|
|
|
from core.api.grpc.client import InterfaceHelper
|
|
|
|
from core.api.grpc.server import CoreGrpcServer
|
2018-05-01 18:40:25 +01:00
|
|
|
from core.emulator.coreemu import CoreEmu
|
2020-06-16 20:50:24 +01:00
|
|
|
from core.emulator.data import IpPrefixes
|
2019-10-24 19:52:25 +01:00
|
|
|
from core.emulator.distributed import DistributedServer
|
|
|
|
from core.emulator.enumerations import EventTypes
|
2019-10-24 17:06:14 +01:00
|
|
|
from core.emulator.session import Session
|
2019-10-24 07:15:19 +01:00
|
|
|
from core.nodes.base import CoreNode
|
2020-02-14 21:18:05 +00:00
|
|
|
from core.nodes.netclient import LinuxNetClient
|
2017-07-26 16:48:59 +01:00
|
|
|
|
2017-07-12 17:46:57 +01:00
|
|
|
EMANE_SERVICES = "zebra|OSPFv3MDR|IPForward"
|
|
|
|
|
2017-04-27 21:34:23 +01:00
|
|
|
|
2019-10-24 07:15:19 +01:00
|
|
|
class PatchManager:
|
|
|
|
def __init__(self):
|
|
|
|
self.patches = []
|
|
|
|
|
2020-02-14 21:18:05 +00:00
|
|
|
def patch_obj(self, _cls, attribute, return_value=None):
|
|
|
|
p = mock.patch.object(_cls, attribute, return_value=return_value)
|
2019-10-24 07:15:19 +01:00
|
|
|
p.start()
|
|
|
|
self.patches.append(p)
|
|
|
|
|
|
|
|
def patch(self, func):
|
|
|
|
p = mock.patch(func)
|
|
|
|
p.start()
|
|
|
|
self.patches.append(p)
|
|
|
|
|
|
|
|
def shutdown(self):
|
|
|
|
for p in self.patches:
|
|
|
|
p.stop()
|
|
|
|
|
|
|
|
|
2019-10-24 18:58:26 +01:00
|
|
|
class MockServer:
|
2020-06-05 22:34:19 +01:00
|
|
|
def __init__(self, coreemu):
|
|
|
|
self.config = {}
|
2019-10-24 18:58:26 +01:00
|
|
|
self.coreemu = coreemu
|
|
|
|
|
|
|
|
|
2019-10-24 17:06:14 +01:00
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def patcher(request):
|
|
|
|
patch_manager = PatchManager()
|
2020-02-14 21:18:05 +00:00
|
|
|
patch_manager.patch_obj(DistributedServer, "remote_cmd", return_value="1")
|
2019-10-24 17:06:14 +01:00
|
|
|
if request.config.getoption("mock"):
|
|
|
|
patch_manager.patch("os.mkdir")
|
|
|
|
patch_manager.patch("core.utils.cmd")
|
2020-07-07 05:47:46 +01:00
|
|
|
patch_manager.patch("core.utils.which")
|
2019-10-24 17:06:14 +01:00
|
|
|
patch_manager.patch("core.nodes.netclient.get_net_client")
|
2020-02-14 21:18:05 +00:00
|
|
|
patch_manager.patch_obj(
|
|
|
|
LinuxNetClient, "get_mac", return_value="00:00:00:00:00:00"
|
|
|
|
)
|
2021-09-17 22:34:37 +01:00
|
|
|
patch_manager.patch_obj(CoreNode, "create_file")
|
2019-10-24 17:06:14 +01:00
|
|
|
yield patch_manager
|
|
|
|
patch_manager.shutdown()
|
2017-07-10 18:44:10 +01:00
|
|
|
|
2018-06-11 20:26:12 +01:00
|
|
|
|
2019-10-24 07:15:19 +01:00
|
|
|
@pytest.fixture(scope="session")
|
2019-10-24 17:06:14 +01:00
|
|
|
def global_coreemu(patcher):
|
2019-10-24 07:15:19 +01:00
|
|
|
coreemu = CoreEmu(config={"emane_prefix": "/usr"})
|
|
|
|
yield coreemu
|
2018-04-26 00:33:58 +01:00
|
|
|
coreemu.shutdown()
|
2017-07-10 18:44:10 +01:00
|
|
|
|
2018-06-15 22:03:27 +01:00
|
|
|
|
2019-10-24 07:15:19 +01:00
|
|
|
@pytest.fixture(scope="session")
|
2019-10-24 17:06:14 +01:00
|
|
|
def global_session(request, patcher, global_coreemu):
|
|
|
|
mkdir = not request.config.getoption("mock")
|
|
|
|
session = Session(1000, {"emane_prefix": "/usr"}, mkdir)
|
2021-05-07 21:10:05 +01:00
|
|
|
session.service_manager = global_coreemu.service_manager
|
2019-10-24 07:15:19 +01:00
|
|
|
yield session
|
|
|
|
session.shutdown()
|
2017-07-10 18:44:10 +01:00
|
|
|
|
2019-10-24 07:15:19 +01:00
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
2018-04-26 00:33:58 +01:00
|
|
|
def ip_prefixes():
|
|
|
|
return IpPrefixes(ip4_prefix="10.83.0.0/16")
|
2017-07-26 16:48:59 +01:00
|
|
|
|
|
|
|
|
2019-10-24 07:15:19 +01:00
|
|
|
@pytest.fixture(scope="session")
|
2020-06-16 17:30:16 +01:00
|
|
|
def iface_helper():
|
2019-03-30 05:12:34 +00:00
|
|
|
return InterfaceHelper(ip4_prefix="10.83.0.0/16")
|
|
|
|
|
|
|
|
|
2019-10-24 07:15:19 +01:00
|
|
|
@pytest.fixture(scope="module")
|
2019-10-24 17:06:14 +01:00
|
|
|
def module_grpc(global_coreemu):
|
|
|
|
grpc_server = CoreGrpcServer(global_coreemu)
|
|
|
|
thread = threading.Thread(target=grpc_server.listen, args=("localhost:50051",))
|
|
|
|
thread.daemon = True
|
|
|
|
thread.start()
|
|
|
|
time.sleep(0.1)
|
|
|
|
yield grpc_server
|
|
|
|
grpc_server.server.stop(None)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def grpc_server(module_grpc):
|
|
|
|
yield module_grpc
|
2020-09-15 21:07:08 +01:00
|
|
|
for session in module_grpc.coreemu.sessions.values():
|
|
|
|
session.set_state(EventTypes.CONFIGURATION_STATE)
|
2019-10-24 17:06:14 +01:00
|
|
|
module_grpc.coreemu.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def session(global_session):
|
|
|
|
global_session.set_state(EventTypes.CONFIGURATION_STATE)
|
|
|
|
yield global_session
|
|
|
|
global_session.clear()
|
|
|
|
|
|
|
|
|
2017-08-02 20:25:57 +01:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption("--distributed", help="distributed server address")
|
2019-10-24 07:15:19 +01:00
|
|
|
parser.addoption("--mock", action="store_true", help="run without mocking")
|
2017-08-02 20:25:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
distributed_param = "distributed_address"
|
|
|
|
if distributed_param in metafunc.fixturenames:
|
|
|
|
distributed_address = metafunc.config.getoption("distributed")
|
|
|
|
metafunc.parametrize(distributed_param, [distributed_address])
|