grpc added initial tests and fixture to support them

This commit is contained in:
bharnden 2019-03-21 23:29:26 -07:00
parent 0c0ff95fb6
commit 3498a59ed5
4 changed files with 74 additions and 9 deletions

View file

@ -216,6 +216,7 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
super(CoreGrpcServer, self).__init__()
self.coreemu = coreemu
self.running = True
self.server = None
atexit.register(self._exit_handler)
def _exit_handler(self):
@ -230,16 +231,16 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
def listen(self, address="[::]:50051"):
logging.info("starting grpc api: %s", address)
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
core_pb2_grpc.add_CoreApiServicer_to_server(self, server)
server.add_insecure_port(address)
server.start()
self.server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
core_pb2_grpc.add_CoreApiServicer_to_server(self, self.server)
self.server.add_insecure_port(address)
self.server.start()
try:
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(0)
self.server.stop(None)
def get_session(self, _id, context):
session = self.coreemu.sessions.get(_id)

View file

@ -56,8 +56,8 @@ def cored(cfg):
# initialize grpc api
if cfg["grpc"] == "True":
api_server = CoreGrpcServer(server.coreemu)
grpc_thread = threading.Thread(target=api_server.listen)
grpc_server = CoreGrpcServer(server.coreemu)
grpc_thread = threading.Thread(target=grpc_server.listen)
grpc_thread.daemon = True
grpc_thread.start()

View file

@ -3,6 +3,7 @@ Unit test fixture module.
"""
import os
import threading
import pytest
from mock.mock import MagicMock
@ -26,6 +27,8 @@ from core.enumerations import LinkTypes
from core.enumerations import MessageFlags
from core.enumerations import NodeTlvs
from core.enumerations import NodeTypes
from core.grpc.client import CoreGrpcClient
from core.grpc.server import CoreGrpcServer
from core.misc import ipaddress
from core.misc.ipaddress import MacAddress
from core.service import ServiceManager
@ -204,6 +207,18 @@ class CoreServerTest(object):
self.server.server_close()
@pytest.fixture
def grpc_server():
coremu = CoreEmu()
grpc_server = CoreGrpcServer(coremu)
thread = threading.Thread(target=grpc_server.listen)
thread.daemon = True
thread.start()
yield grpc_server
coremu.shutdown()
grpc_server.server.stop(None)
@pytest.fixture
def session():
# use coreemu and create a session
@ -242,8 +257,6 @@ def cored():
# cleanup
server.shutdown()
#
# cleanup services
ServiceManager.services.clear()

51
daemon/tests/test_grpc.py Normal file
View file

@ -0,0 +1,51 @@
import os
import time
import pytest
from core.grpc import core_pb2
from core.enumerations import NodeTypes
from core.grpc.client import CoreGrpcClient
MODELS = [
"router",
"host",
"PC",
"mdr",
]
NET_TYPES = [
NodeTypes.SWITCH,
NodeTypes.HUB,
NodeTypes.WIRELESS_LAN
]
class TestGrpc:
def test_create_session(self, grpc_server):
# given
client = CoreGrpcClient()
# when
with client.context_connect():
response = client.create_session()
# then
assert isinstance(response.id, int)
assert isinstance(response.state, int)
session = grpc_server.coreemu.sessions.get(response.id)
assert session is not None
assert session.state == response.state
def test_delete_session(self, grpc_server):
# given
client = CoreGrpcClient()
session = grpc_server.coreemu.create_session()
# then
with client.context_connect():
response = client.delete_session(session.session_id)
# then
assert response.result is True
assert grpc_server.coreemu.sessions.get(session.session_id) is None