diff --git a/src/context/service/ContextServiceServicerImpl.py b/src/context/service/ContextServiceServicerImpl.py index 34608d61944bd19b349fae44b18a83d9f4220e84..f5b2662b37865f1bdb39d4a053dc3b297fd56e82 100644 --- a/src/context/service/ContextServiceServicerImpl.py +++ b/src/context/service/ContextServiceServicerImpl.py @@ -278,29 +278,29 @@ class ContextServiceServicerImpl(ContextServiceServicer, ContextPolicyServiceSer @safe_and_metered_rpc_method(METRICS_POOL, LOGGER) def ListConnectionIds(self, request : ServiceId, context : grpc.ServicerContext) -> ConnectionIdList: - return connection_list_ids(self.db_engine, request) + return ConnectionIdList(connection_ids=connection_list_ids(self.db_engine, request)) @safe_and_metered_rpc_method(METRICS_POOL, LOGGER) def ListConnections(self, request : ContextId, context : grpc.ServicerContext) -> ConnectionList: - return connection_list_objs(self.db_engine, request) + return ConnectionList(connections=connection_list_objs(self.db_engine, request)) @safe_and_metered_rpc_method(METRICS_POOL, LOGGER) def GetConnection(self, request : ConnectionId, context : grpc.ServicerContext) -> Connection: - return connection_get(self.db_engine, request) + return Connection(**connection_get(self.db_engine, request)) @safe_and_metered_rpc_method(METRICS_POOL, LOGGER) def SetConnection(self, request : Connection, context : grpc.ServicerContext) -> ConnectionId: - connection_id,updated = connection_set(self.db_engine, request) # pylint: disable=unused-variable - #event_type = EventTypeEnum.EVENTTYPE_UPDATE if updated else EventTypeEnum.EVENTTYPE_CREATE - #notify_event(self.messagebroker, TOPIC_CONNECTION, event_type, {'connection_id': connection_id}) - return connection_id + connection_id,updated = connection_set(self.db_engine, request) + event_type = EventTypeEnum.EVENTTYPE_UPDATE if updated else EventTypeEnum.EVENTTYPE_CREATE + notify_event(self.messagebroker, TOPIC_CONNECTION, event_type, {'connection_id': connection_id}) + return ConnectionId(**connection_id) @safe_and_metered_rpc_method(METRICS_POOL, LOGGER) def RemoveConnection(self, request : ConnectionId, context : grpc.ServicerContext) -> Empty: - deleted = connection_delete(self.db_engine, request) # pylint: disable=unused-variable - #if deleted: - # event_type = EventTypeEnum.EVENTTYPE_REMOVE - # notify_event(self.messagebroker, TOPIC_CONNECTION, event_type, {'connection_id': request}) + connection_id,deleted = connection_delete(self.db_engine, request) + if deleted: + event_type = EventTypeEnum.EVENTTYPE_REMOVE + notify_event(self.messagebroker, TOPIC_CONNECTION, event_type, {'connection_id': connection_id}) return Empty() @safe_and_metered_rpc_method(METRICS_POOL, LOGGER) diff --git a/src/context/service/database/Connection.py b/src/context/service/database/Connection.py index 2f6fb84337b085b29e3fd275e96d9bf4c4495e70..f1616e96e715b7f63722d8e939b7851c029d04a0 100644 --- a/src/context/service/database/Connection.py +++ b/src/context/service/database/Connection.py @@ -19,7 +19,7 @@ from sqlalchemy.exc import IntegrityError from sqlalchemy.orm import Session, sessionmaker from sqlalchemy_cockroachdb import run_transaction from typing import Dict, List, Optional, Tuple -from common.proto.context_pb2 import Connection, ConnectionId, ConnectionIdList, ConnectionList, ServiceId +from common.proto.context_pb2 import Connection, ConnectionId, ServiceId from common.method_wrappers.ServiceExceptions import NotFoundException from common.tools.grpc.Tools import grpc_message_to_json_string from common.tools.object_factory.Connection import json_connection_id @@ -28,23 +28,23 @@ from .uuids.Connection import connection_get_uuid from .uuids.EndPoint import endpoint_get_uuid from .uuids.Service import service_get_uuid -def connection_list_ids(db_engine : Engine, request : ServiceId) -> ConnectionIdList: +LOGGER = logging.getLogger(__name__) + +def connection_list_ids(db_engine : Engine, request : ServiceId) -> List[Dict]: _,service_uuid = service_get_uuid(request, allow_random=False) def callback(session : Session) -> List[Dict]: obj_list : List[ConnectionModel] = session.query(ConnectionModel).filter_by(service_uuid=service_uuid).all() - #.options(selectinload(ContextModel.connection)).filter_by(context_uuid=context_uuid).one_or_none() return [obj.dump_id() for obj in obj_list] - return ConnectionIdList(connection_ids=run_transaction(sessionmaker(bind=db_engine), callback)) + return run_transaction(sessionmaker(bind=db_engine), callback) -def connection_list_objs(db_engine : Engine, request : ServiceId) -> ConnectionList: +def connection_list_objs(db_engine : Engine, request : ServiceId) -> List[Dict]: _,service_uuid = service_get_uuid(request, allow_random=False) def callback(session : Session) -> List[Dict]: obj_list : List[ConnectionModel] = session.query(ConnectionModel).filter_by(service_uuid=service_uuid).all() - #.options(selectinload(ContextModel.connection)).filter_by(context_uuid=context_uuid).one_or_none() return [obj.dump() for obj in obj_list] - return ConnectionList(connections=run_transaction(sessionmaker(bind=db_engine), callback)) + return run_transaction(sessionmaker(bind=db_engine), callback) -def connection_get(db_engine : Engine, request : ConnectionId) -> Connection: +def connection_get(db_engine : Engine, request : ConnectionId) -> Dict: connection_uuid = connection_get_uuid(request, allow_random=False) def callback(session : Session) -> Optional[Dict]: obj : Optional[ConnectionModel] = session.query(ConnectionModel)\ @@ -55,17 +55,21 @@ def connection_get(db_engine : Engine, request : ConnectionId) -> Connection: raise NotFoundException('Connection', request.connection_uuid.uuid, extra_details=[ 'connection_uuid generated was: {:s}'.format(connection_uuid), ]) - return Connection(**obj) + return obj -def connection_set(db_engine : Engine, request : Connection) -> Tuple[ConnectionId, bool]: +def connection_set(db_engine : Engine, request : Connection) -> Tuple[Dict, bool]: connection_uuid = connection_get_uuid(request.connection_id, allow_random=True) _,service_uuid = service_get_uuid(request.service_id, allow_random=False) settings = grpc_message_to_json_string(request.settings), + now = datetime.datetime.utcnow() + connection_data = [{ 'connection_uuid': connection_uuid, 'service_uuid' : service_uuid, 'settings' : settings, + 'created_at' : now, + 'updated_at' : now, }] connection_endpoints_data : List[Dict] = list() @@ -78,21 +82,27 @@ def connection_set(db_engine : Engine, request : Connection) -> Tuple[Connection }) connection_subservices_data : List[Dict] = list() - for i,service_id in enumerate(request.sub_service_ids): + for service_id in request.sub_service_ids: _, service_uuid = service_get_uuid(service_id, allow_random=False) connection_subservices_data.append({ 'connection_uuid': connection_uuid, 'subservice_uuid': service_uuid, }) - def callback(session : Session) -> None: + def callback(session : Session) -> bool: stmt = insert(ConnectionModel).values(connection_data) stmt = stmt.on_conflict_do_update( index_elements=[ConnectionModel.connection_uuid], - set_=dict(settings = stmt.excluded.settings) + set_=dict( + settings = stmt.excluded.settings, + updated_at = stmt.excluded.updated_at, + ) ) - session.execute(stmt) + stmt = stmt.returning(ConnectionModel.created_at, ConnectionModel.updated_at) + created_at,updated_at = session.execute(stmt).fetchone() + updated = updated_at > created_at + # TODO: manage update connection endpoints if len(connection_endpoints_data) > 0: stmt = insert(ConnectionEndPointModel).values(connection_endpoints_data) stmt = stmt.on_conflict_do_nothing( @@ -115,6 +125,7 @@ def connection_set(db_engine : Engine, request : Connection) -> Tuple[Connection else: raise + # TODO: manage update connection subservices if len(connection_subservices_data) > 0: stmt = insert(ConnectionSubServiceModel).values(connection_subservices_data) stmt = stmt.on_conflict_do_nothing( @@ -122,13 +133,15 @@ def connection_set(db_engine : Engine, request : Connection) -> Tuple[Connection ) session.execute(stmt) - run_transaction(sessionmaker(bind=db_engine), callback) - updated = False # TODO: improve and check if created/updated + return updated + + updated = run_transaction(sessionmaker(bind=db_engine), callback) return ConnectionId(**json_connection_id(connection_uuid)),updated -def connection_delete(db_engine : Engine, request : ConnectionId) -> bool: +def connection_delete(db_engine : Engine, request : ConnectionId) -> Tuple[Dict, bool]: connection_uuid = connection_get_uuid(request, allow_random=False) def callback(session : Session) -> bool: num_deleted = session.query(ConnectionModel).filter_by(connection_uuid=connection_uuid).delete() return num_deleted > 0 - return run_transaction(sessionmaker(bind=db_engine), callback) + deleted = run_transaction(sessionmaker(bind=db_engine), callback) + return ConnectionId(**json_connection_id(connection_uuid)),deleted diff --git a/src/context/service/database/models/ConnectionModel.py b/src/context/service/database/models/ConnectionModel.py index a1d45a9344bbdcf0000b5f8cad28347aa6f25d51..966dcab4da419e9a0d0b24cd0340b37c5485cd91 100644 --- a/src/context/service/database/models/ConnectionModel.py +++ b/src/context/service/database/models/ConnectionModel.py @@ -13,7 +13,7 @@ # limitations under the License. import json, logging, operator -from sqlalchemy import Column, ForeignKey, Integer, CheckConstraint, String +from sqlalchemy import Column, DateTime, ForeignKey, Integer, CheckConstraint, String from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.orm import relationship from typing import Dict @@ -27,6 +27,8 @@ class ConnectionModel(_Base): connection_uuid = Column(UUID(as_uuid=False), primary_key=True) service_uuid = Column(ForeignKey('service.service_uuid', ondelete='RESTRICT'), nullable=False) settings = Column(String, nullable=False) + created_at = Column(DateTime, nullable=False) + updated_at = Column(DateTime, nullable=False) connection_service = relationship('ServiceModel') # back_populates='connections' connection_endpoints = relationship('ConnectionEndPointModel') # lazy='joined', back_populates='connection' diff --git a/src/context/tests/test_connection.py b/src/context/tests/test_connection.py index 4cc5407b4bafb55013bfbc46dc9850a1b33f8864..909ddb6efe2c4613b40050162fc9a4ba27dce304 100644 --- a/src/context/tests/test_connection.py +++ b/src/context/tests/test_connection.py @@ -12,28 +12,31 @@ # See the License for the specific language governing permissions and # limitations under the License. -import copy, grpc, pytest +import copy, grpc, pytest, time from common.proto.context_pb2 import ( - Connection, ConnectionId, Context, ContextId, Device, DeviceId, EndPointId, Service, ServiceId, Topology, TopologyId) + Connection, ConnectionEvent, ConnectionId, Context, ContextEvent, ContextId, Device, DeviceEvent, DeviceId, EndPointId, EventTypeEnum, Service, ServiceEvent, ServiceId, Topology, TopologyEvent, TopologyId) from context.client.ContextClient import ContextClient from context.service.database.uuids.Connection import connection_get_uuid from context.service.database.uuids.EndPoint import endpoint_get_uuid -#from context.client.EventsCollector import EventsCollector +from context.client.EventsCollector import EventsCollector from .Objects import ( CONNECTION_R1_R3, CONNECTION_R1_R3_ID, CONNECTION_R1_R3_NAME, CONTEXT, CONTEXT_ID, DEVICE_R1, DEVICE_R1_ID, DEVICE_R2, DEVICE_R2_ID, DEVICE_R3, DEVICE_R3_ID, SERVICE_R1_R2, SERVICE_R1_R2_ID, SERVICE_R1_R3, SERVICE_R1_R3_ID, SERVICE_R2_R3, SERVICE_R2_R3_ID, TOPOLOGY, TOPOLOGY_ID) +GET_EVENTS_TIMEOUT = 10.0 + @pytest.mark.depends(on=['context/tests/test_service.py::test_service', 'context/tests/test_slice.py::test_slice']) def test_connection(context_client : ContextClient) -> None: # ----- Initialize the EventsCollector ----------------------------------------------------------------------------- - #events_collector = EventsCollector( - # context_client, log_events_received=True, - # activate_context_collector = False, activate_topology_collector = False, activate_device_collector = False, - # activate_link_collector = False, activate_service_collector = False, activate_slice_collector = False, - # activate_connection_collector = True) - #events_collector.start() + events_collector = EventsCollector( + context_client, log_events_received=True, + activate_context_collector = True, activate_topology_collector = True, activate_device_collector = True, + activate_link_collector = True, activate_service_collector = True, activate_slice_collector = True, + activate_connection_collector = True) + events_collector.start() + time.sleep(3) # ----- Prepare dependencies for the test and capture related events ----------------------------------------------- response = context_client.SetContext(Context(**CONTEXT)) @@ -47,61 +50,52 @@ def test_connection(context_client : ContextClient) -> None: device_r1_uuid = response.device_uuid.uuid response = context_client.SetDevice(Device(**DEVICE_R2)) - device_r2_uuid = response.device_uuid.uuid # pylint: disable=unused-variable + device_r2_uuid = response.device_uuid.uuid response = context_client.SetDevice(Device(**DEVICE_R3)) - device_r3_uuid = response.device_uuid.uuid # pylint: disable=unused-variable + device_r3_uuid = response.device_uuid.uuid response = context_client.SetService(Service(**SERVICE_R1_R2)) assert response.context_id.context_uuid.uuid == context_uuid - service_r1_r2_uuid = response.service_uuid.uuid # pylint: disable=unused-variable + service_r1_r2_uuid = response.service_uuid.uuid response = context_client.SetService(Service(**SERVICE_R2_R3)) assert response.context_id.context_uuid.uuid == context_uuid - service_r2_r3_uuid = response.service_uuid.uuid # pylint: disable=unused-variable + service_r2_r3_uuid = response.service_uuid.uuid response = context_client.SetService(Service(**SERVICE_R1_R3)) assert response.context_id.context_uuid.uuid == context_uuid service_r1_r3_uuid = response.service_uuid.uuid - #events = events_collector.get_events(block=True, count=8) - #assert isinstance(events[0], ContextEvent) - #assert events[0].event.event_type == EventTypeEnum.EVENTTYPE_CREATE - #assert events[0].context_id.context_uuid.uuid == context_uuid - #assert isinstance(events[1], TopologyEvent) - #assert events[1].event.event_type == EventTypeEnum.EVENTTYPE_CREATE - #assert events[1].topology_id.context_id.context_uuid.uuid == context_uuid - #assert events[1].topology_id.topology_uuid.uuid == topology_uuid - #assert isinstance(events[2], DeviceEvent) - #assert events[2].event.event_type == EventTypeEnum.EVENTTYPE_CREATE - #assert events[2].device_id.device_uuid.uuid == device_r1_uuid - #assert isinstance(events[3], DeviceEvent) - #assert events[3].event.event_type == EventTypeEnum.EVENTTYPE_CREATE - #assert events[3].device_id.device_uuid.uuid == device_r2_uuid - #assert isinstance(events[4], DeviceEvent) - #assert events[4].event.event_type == EventTypeEnum.EVENTTYPE_CREATE - #assert events[4].device_id.device_uuid.uuid == device_r3_uuid - #assert isinstance(events[5], ServiceEvent) - #assert events[5].event.event_type == EventTypeEnum.EVENTTYPE_CREATE - #assert events[5].service_id.context_id.context_uuid.uuid == context_uuid - #assert events[5].service_id.service_uuid.uuid == service_r1_r2_uuid - #assert isinstance(events[6], ContextEvent) - #assert events[6].event.event_type == EventTypeEnum.EVENTTYPE_UPDATE - #assert events[6].context_id.context_uuid.uuid == context_uuid - #assert isinstance(events[7], ServiceEvent) - #assert events[7].event.event_type == EventTypeEnum.EVENTTYPE_CREATE - #assert events[7].service_id.context_id.context_uuid.uuid == context_uuid - #assert events[7].service_id.service_uuid.uuid == service_r2_r3_uuid - #assert isinstance(events[8], ContextEvent) - #assert events[8].event.event_type == EventTypeEnum.EVENTTYPE_UPDATE - #assert events[8].context_id.context_uuid.uuid == context_uuid - #assert isinstance(events[9], ServiceEvent) - #assert events[9].event.event_type == EventTypeEnum.EVENTTYPE_CREATE - #assert events[9].service_id.context_id.context_uuid.uuid == context_uuid - #assert events[9].service_id.service_uuid.uuid == service_r1_r3_uuid - #assert isinstance(events[10], ContextEvent) - #assert events[10].event.event_type == EventTypeEnum.EVENTTYPE_UPDATE - #assert events[10].context_id.context_uuid.uuid == context_uuid + events = events_collector.get_events(block=True, count=8, timeout=GET_EVENTS_TIMEOUT) + assert isinstance(events[0], ContextEvent) + assert events[0].event.event_type == EventTypeEnum.EVENTTYPE_CREATE + assert events[0].context_id.context_uuid.uuid == context_uuid + assert isinstance(events[1], TopologyEvent) + assert events[1].event.event_type == EventTypeEnum.EVENTTYPE_CREATE + assert events[1].topology_id.context_id.context_uuid.uuid == context_uuid + assert events[1].topology_id.topology_uuid.uuid == topology_uuid + assert isinstance(events[2], DeviceEvent) + assert events[2].event.event_type == EventTypeEnum.EVENTTYPE_CREATE + assert events[2].device_id.device_uuid.uuid == device_r1_uuid + assert isinstance(events[3], DeviceEvent) + assert events[3].event.event_type == EventTypeEnum.EVENTTYPE_CREATE + assert events[3].device_id.device_uuid.uuid == device_r2_uuid + assert isinstance(events[4], DeviceEvent) + assert events[4].event.event_type == EventTypeEnum.EVENTTYPE_CREATE + assert events[4].device_id.device_uuid.uuid == device_r3_uuid + assert isinstance(events[5], ServiceEvent) + assert events[5].event.event_type == EventTypeEnum.EVENTTYPE_CREATE + assert events[5].service_id.context_id.context_uuid.uuid == context_uuid + assert events[5].service_id.service_uuid.uuid == service_r1_r2_uuid + assert isinstance(events[6], ServiceEvent) + assert events[6].event.event_type == EventTypeEnum.EVENTTYPE_CREATE + assert events[6].service_id.context_id.context_uuid.uuid == context_uuid + assert events[6].service_id.service_uuid.uuid == service_r2_r3_uuid + assert isinstance(events[7], ServiceEvent) + assert events[7].event.event_type == EventTypeEnum.EVENTTYPE_CREATE + assert events[7].service_id.context_id.context_uuid.uuid == context_uuid + assert events[7].service_id.service_uuid.uuid == service_r1_r3_uuid # ----- Get when the object does not exist ------------------------------------------------------------------------- connection_id = ConnectionId(**CONNECTION_R1_R3_ID) @@ -137,10 +131,10 @@ def test_connection(context_client : ContextClient) -> None: connection_r1_r3_uuid = response.connection_uuid.uuid # ----- Check create event ----------------------------------------------------------------------------------------- - #event = events_collector.get_event(block=True) - #assert isinstance(event, ConnectionEvent) - #assert event.event.event_type == EventTypeEnum.EVENTTYPE_CREATE - #assert event.connection_id.connection_uuid.uuid == connection_r1_r3_uuid + event = events_collector.get_event(block=True, timeout=GET_EVENTS_TIMEOUT) + assert isinstance(event, ConnectionEvent) + assert event.event.event_type == EventTypeEnum.EVENTTYPE_CREATE + assert event.connection_id.connection_uuid.uuid == connection_r1_r3_uuid # ----- Get when the object exists --------------------------------------------------------------------------------- response = context_client.GetConnection(ConnectionId(**CONNECTION_R1_R3_ID)) @@ -167,10 +161,10 @@ def test_connection(context_client : ContextClient) -> None: assert response.connection_uuid.uuid == connection_r1_r3_uuid # ----- Check update event ----------------------------------------------------------------------------------------- - #event = events_collector.get_event(block=True) - #assert isinstance(event, ConnectionEvent) - #assert event.event.event_type == EventTypeEnum.EVENTTYPE_UPDATE - #assert event.connection_id.connection_uuid.uuid == connection_r1_r3_uuid + event = events_collector.get_event(block=True, timeout=GET_EVENTS_TIMEOUT) + assert isinstance(event, ConnectionEvent) + assert event.event.event_type == EventTypeEnum.EVENTTYPE_UPDATE + assert event.connection_id.connection_uuid.uuid == connection_r1_r3_uuid # ----- Get when the object is modified ---------------------------------------------------------------------------- response = context_client.GetConnection(ConnectionId(**CONNECTION_R1_R3_ID)) @@ -195,10 +189,10 @@ def test_connection(context_client : ContextClient) -> None: context_client.RemoveConnection(ConnectionId(**CONNECTION_R1_R3_ID)) # ----- Check remove event ----------------------------------------------------------------------------------------- - #event = events_collector.get_event(block=True) - #assert isinstance(event, ConnectionEvent) - #assert event.event.event_type == EventTypeEnum.EVENTTYPE_REMOVE - #assert event.connection_id.connection_uuid.uuid == connection_r1_r3_uuid + event = events_collector.get_event(block=True, timeout=GET_EVENTS_TIMEOUT) + assert isinstance(event, ConnectionEvent) + assert event.event.event_type == EventTypeEnum.EVENTTYPE_REMOVE + assert event.connection_id.connection_uuid.uuid == connection_r1_r3_uuid # ----- List after deleting the object ----------------------------------------------------------------------------- response = context_client.ListConnectionIds(ServiceId(**SERVICE_R1_R3_ID)) @@ -217,35 +211,35 @@ def test_connection(context_client : ContextClient) -> None: context_client.RemoveTopology(TopologyId(**TOPOLOGY_ID)) context_client.RemoveContext(ContextId(**CONTEXT_ID)) - #events = events_collector.get_events(block=True, count=8) - #assert isinstance(events[0], ServiceEvent) - #assert events[0].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE - #assert events[0].service_id.context_id.context_uuid.uuid == context_uuid - #assert events[0].service_id.service_uuid.uuid == service_r1_r3_uuid - #assert isinstance(events[1], ServiceEvent) - #assert events[1].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE - #assert events[1].service_id.context_id.context_uuid.uuid == context_uuid - #assert events[1].service_id.service_uuid.uuid == service_r2_r3_uuid - #assert isinstance(events[2], ServiceEvent) - #assert events[2].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE - #assert events[2].service_id.context_id.context_uuid.uuid == context_uuid - #assert events[2].service_id.service_uuid.uuid == service_r1_r2_uuid - #assert isinstance(events[3], DeviceEvent) - #assert events[3].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE - #assert events[3].device_id.device_uuid.uuid == device_r1_uuid - #assert isinstance(events[4], DeviceEvent) - #assert events[4].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE - #assert events[4].device_id.device_uuid.uuid == device_r2_uuid - #assert isinstance(events[5], DeviceEvent) - #assert events[5].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE - #assert events[5].device_id.device_uuid.uuid == device_r3_uuid - #assert isinstance(events[6], TopologyEvent) - #assert events[6].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE - #assert events[6].topology_id.context_id.context_uuid.uuid == context_uuid - #assert events[6].topology_id.topology_uuid.uuid == topology_uuid - #assert isinstance(events[7], ContextEvent) - #assert events[7].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE - #assert events[7].context_id.context_uuid.uuid == context_uuid + events = events_collector.get_events(block=True, count=8, timeout=GET_EVENTS_TIMEOUT) + assert isinstance(events[0], ServiceEvent) + assert events[0].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE + assert events[0].service_id.context_id.context_uuid.uuid == context_uuid + assert events[0].service_id.service_uuid.uuid == service_r1_r3_uuid + assert isinstance(events[1], ServiceEvent) + assert events[1].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE + assert events[1].service_id.context_id.context_uuid.uuid == context_uuid + assert events[1].service_id.service_uuid.uuid == service_r2_r3_uuid + assert isinstance(events[2], ServiceEvent) + assert events[2].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE + assert events[2].service_id.context_id.context_uuid.uuid == context_uuid + assert events[2].service_id.service_uuid.uuid == service_r1_r2_uuid + assert isinstance(events[3], DeviceEvent) + assert events[3].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE + assert events[3].device_id.device_uuid.uuid == device_r1_uuid + assert isinstance(events[4], DeviceEvent) + assert events[4].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE + assert events[4].device_id.device_uuid.uuid == device_r2_uuid + assert isinstance(events[5], DeviceEvent) + assert events[5].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE + assert events[5].device_id.device_uuid.uuid == device_r3_uuid + assert isinstance(events[6], TopologyEvent) + assert events[6].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE + assert events[6].topology_id.context_id.context_uuid.uuid == context_uuid + assert events[6].topology_id.topology_uuid.uuid == topology_uuid + assert isinstance(events[7], ContextEvent) + assert events[7].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE + assert events[7].context_id.context_uuid.uuid == context_uuid # ----- Stop the EventsCollector ----------------------------------------------------------------------------------- - #events_collector.stop() + events_collector.stop() diff --git a/src/context/tests/test_context.py b/src/context/tests/test_context.py index 4337db239a5319fa0a877a2f0f0f6918616920ab..77f1dc3805a4ad585b54a191a93390f62bc40425 100644 --- a/src/context/tests/test_context.py +++ b/src/context/tests/test_context.py @@ -19,6 +19,8 @@ from context.service.database.uuids.Context import context_get_uuid from context.client.EventsCollector import EventsCollector from .Objects import CONTEXT, CONTEXT_ID, CONTEXT_NAME +GET_EVENTS_TIMEOUT = 10.0 + def test_context(context_client : ContextClient) -> None: # ----- Initialize the EventsCollector ----------------------------------------------------------------------------- @@ -51,7 +53,7 @@ def test_context(context_client : ContextClient) -> None: assert response.context_uuid.uuid == context_uuid # ----- Check create event ----------------------------------------------------------------------------------------- - event = events_collector.get_event(block=True, timeout=1.0) + event = events_collector.get_event(block=True, timeout=GET_EVENTS_TIMEOUT) assert isinstance(event, ContextEvent) assert event.event.event_type == EventTypeEnum.EVENTTYPE_CREATE assert event.context_id.context_uuid.uuid == context_uuid @@ -85,7 +87,7 @@ def test_context(context_client : ContextClient) -> None: assert response.context_uuid.uuid == context_uuid # ----- Check update event ----------------------------------------------------------------------------------------- - event = events_collector.get_event(block=True, timeout=1.0) + event = events_collector.get_event(block=True, timeout=GET_EVENTS_TIMEOUT) assert isinstance(event, ContextEvent) assert event.event.event_type == EventTypeEnum.EVENTTYPE_UPDATE assert event.context_id.context_uuid.uuid == context_uuid @@ -115,7 +117,7 @@ def test_context(context_client : ContextClient) -> None: context_client.RemoveContext(ContextId(**CONTEXT_ID)) # ----- Check remove event ----------------------------------------------------------------------------------------- - event = events_collector.get_event(block=True, timeout=1.0) + event = events_collector.get_event(block=True, timeout=GET_EVENTS_TIMEOUT) assert isinstance(event, ContextEvent) assert event.event.event_type == EventTypeEnum.EVENTTYPE_REMOVE assert event.context_id.context_uuid.uuid == context_uuid diff --git a/src/context/tests/test_device.py b/src/context/tests/test_device.py index 6e2fdd52dd0dbafe647b923d9f2a8e8a27ca6b51..bcbe4cc3b4319609fbb364dff1171c39e8be4989 100644 --- a/src/context/tests/test_device.py +++ b/src/context/tests/test_device.py @@ -21,6 +21,8 @@ from context.service.database.uuids.Device import device_get_uuid from context.client.EventsCollector import EventsCollector from .Objects import CONTEXT, CONTEXT_ID, DEVICE_R1, DEVICE_R1_ID, DEVICE_R1_NAME, TOPOLOGY, TOPOLOGY_ID +GET_EVENTS_TIMEOUT = 10.0 + @pytest.mark.depends(on=['context/tests/test_topology.py::test_topology']) def test_device(context_client : ContextClient) -> None: @@ -41,7 +43,7 @@ def test_device(context_client : ContextClient) -> None: assert response.context_id.context_uuid.uuid == context_uuid topology_uuid = response.topology_uuid.uuid - events = events_collector.get_events(block=True, count=2, timeout=1.0) + events = events_collector.get_events(block=True, count=2, timeout=GET_EVENTS_TIMEOUT) assert isinstance(events[0], ContextEvent) assert events[0].event.event_type == EventTypeEnum.EVENTTYPE_CREATE assert events[0].context_id.context_uuid.uuid == context_uuid @@ -81,7 +83,7 @@ def test_device(context_client : ContextClient) -> None: assert response.device_uuid.uuid == device_uuid # ----- Check create event ----------------------------------------------------------------------------------------- - event = events_collector.get_event(block=True, timeout=1.0) + event = events_collector.get_event(block=True, timeout=GET_EVENTS_TIMEOUT) assert isinstance(event, DeviceEvent) assert event.event.event_type == EventTypeEnum.EVENTTYPE_CREATE assert event.device_id.device_uuid.uuid == device_uuid @@ -124,7 +126,7 @@ def test_device(context_client : ContextClient) -> None: assert response.device_uuid.uuid == device_uuid # ----- Check update event ----------------------------------------------------------------------------------------- - event = events_collector.get_event(block=True, timeout=1.0) + event = events_collector.get_event(block=True, timeout=GET_EVENTS_TIMEOUT) assert isinstance(event, DeviceEvent) assert event.event.event_type == EventTypeEnum.EVENTTYPE_UPDATE assert event.device_id.device_uuid.uuid == device_uuid @@ -170,7 +172,7 @@ def test_device(context_client : ContextClient) -> None: context_client.RemoveDevice(DeviceId(**DEVICE_R1_ID)) # ----- Check remove event ----------------------------------------------------------------------------------------- - event = events_collector.get_event(block=True, timeout=1.0) + event = events_collector.get_event(block=True, timeout=GET_EVENTS_TIMEOUT) assert isinstance(event, DeviceEvent) assert event.event.event_type == EventTypeEnum.EVENTTYPE_REMOVE assert event.device_id.device_uuid.uuid == device_uuid @@ -192,7 +194,7 @@ def test_device(context_client : ContextClient) -> None: context_client.RemoveTopology(TopologyId(**TOPOLOGY_ID)) context_client.RemoveContext(ContextId(**CONTEXT_ID)) - events = events_collector.get_events(block=True, count=2, timeout=1.0) + events = events_collector.get_events(block=True, count=2, timeout=GET_EVENTS_TIMEOUT) assert isinstance(events[0], TopologyEvent) assert events[0].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE assert events[0].topology_id.context_id.context_uuid.uuid == context_uuid diff --git a/src/context/tests/test_link.py b/src/context/tests/test_link.py index 59fed4870955e431ff4b1b42b3353130c162bbd8..c8ed1d486ba37ead3c79bb8c25506669c656e764 100644 --- a/src/context/tests/test_link.py +++ b/src/context/tests/test_link.py @@ -23,6 +23,8 @@ from .Objects import ( CONTEXT, CONTEXT_ID, DEVICE_R1, DEVICE_R1_ID, DEVICE_R2, DEVICE_R2_ID, LINK_R1_R2, LINK_R1_R2_ID, LINK_R1_R2_NAME, TOPOLOGY, TOPOLOGY_ID) +GET_EVENTS_TIMEOUT = 10.0 + @pytest.mark.depends(on=['context/tests/test_device.py::test_device']) def test_link(context_client : ContextClient) -> None: @@ -49,7 +51,7 @@ def test_link(context_client : ContextClient) -> None: response = context_client.SetDevice(Device(**DEVICE_R2)) device_r2_uuid = response.device_uuid.uuid - events = events_collector.get_events(block=True, count=4, timeout=1.0) + events = events_collector.get_events(block=True, count=4, timeout=GET_EVENTS_TIMEOUT) assert isinstance(events[0], ContextEvent) assert events[0].event.event_type == EventTypeEnum.EVENTTYPE_CREATE assert events[0].context_id.context_uuid.uuid == context_uuid @@ -85,7 +87,7 @@ def test_link(context_client : ContextClient) -> None: assert response.link_uuid.uuid == link_uuid # ----- Check create event ----------------------------------------------------------------------------------------- - event = events_collector.get_event(block=True, timeout=1.0) + event = events_collector.get_event(block=True, timeout=GET_EVENTS_TIMEOUT) assert isinstance(event, LinkEvent) assert event.event.event_type == EventTypeEnum.EVENTTYPE_CREATE assert event.link_id.link_uuid.uuid == link_uuid @@ -115,7 +117,7 @@ def test_link(context_client : ContextClient) -> None: assert response.link_uuid.uuid == link_uuid # ----- Check update event ----------------------------------------------------------------------------------------- - event = events_collector.get_event(block=True, timeout=1.0) + event = events_collector.get_event(block=True, timeout=GET_EVENTS_TIMEOUT) assert isinstance(event, LinkEvent) assert event.event.event_type == EventTypeEnum.EVENTTYPE_UPDATE assert event.link_id.link_uuid.uuid == link_uuid @@ -151,7 +153,7 @@ def test_link(context_client : ContextClient) -> None: context_client.RemoveLink(LinkId(**LINK_R1_R2_ID)) # ----- Check remove event ----------------------------------------------------------------------------------------- - event = events_collector.get_event(block=True, timeout=1.0) + event = events_collector.get_event(block=True, timeout=GET_EVENTS_TIMEOUT) assert isinstance(event, LinkEvent) assert event.event.event_type == EventTypeEnum.EVENTTYPE_REMOVE assert event.link_id.link_uuid.uuid == link_uuid @@ -177,7 +179,7 @@ def test_link(context_client : ContextClient) -> None: context_client.RemoveTopology(TopologyId(**TOPOLOGY_ID)) context_client.RemoveContext(ContextId(**CONTEXT_ID)) - events = events_collector.get_events(block=True, count=4, timeout=1.0) + events = events_collector.get_events(block=True, count=4, timeout=GET_EVENTS_TIMEOUT) assert isinstance(events[0], DeviceEvent) assert events[0].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE assert events[0].device_id.device_uuid.uuid == device_r1_uuid diff --git a/src/context/tests/test_service.py b/src/context/tests/test_service.py index e80437dbb9d623bd6a021ad3d0c779a16326e36c..4e46c24ad90329aac580b9ea92e21a33dd0263be 100644 --- a/src/context/tests/test_service.py +++ b/src/context/tests/test_service.py @@ -23,6 +23,8 @@ from .Objects import ( CONTEXT, CONTEXT_ID, CONTEXT_NAME, DEVICE_R1, DEVICE_R1_ID, SERVICE_R1_R2_NAME, DEVICE_R2, DEVICE_R2_ID, SERVICE_R1_R2, SERVICE_R1_R2_ID, TOPOLOGY, TOPOLOGY_ID) +GET_EVENTS_TIMEOUT = 10.0 + @pytest.mark.depends(on=['context/tests/test_link.py::test_link']) def test_service(context_client : ContextClient) -> None: @@ -49,7 +51,7 @@ def test_service(context_client : ContextClient) -> None: response = context_client.SetDevice(Device(**DEVICE_R2)) device_r2_uuid = response.device_uuid.uuid - events = events_collector.get_events(block=True, count=4, timeout=1.0) + events = events_collector.get_events(block=True, count=4, timeout=GET_EVENTS_TIMEOUT) assert isinstance(events[0], ContextEvent) assert events[0].event.event_type == EventTypeEnum.EVENTTYPE_CREATE assert events[0].context_id.context_uuid.uuid == context_uuid @@ -102,7 +104,7 @@ def test_service(context_client : ContextClient) -> None: assert response.service_uuid.uuid == service_uuid # ----- Check create event ----------------------------------------------------------------------------------------- - event = events_collector.get_event(block=True, timeout=1.0) + event = events_collector.get_event(block=True, timeout=GET_EVENTS_TIMEOUT) assert isinstance(event, ServiceEvent) assert event.event.event_type == EventTypeEnum.EVENTTYPE_CREATE assert event.service_id.context_id.context_uuid.uuid == context_uuid @@ -155,7 +157,7 @@ def test_service(context_client : ContextClient) -> None: assert response.service_uuid.uuid == service_uuid # ----- Check update event ----------------------------------------------------------------------------------------- - event = events_collector.get_event(block=True, timeout=1.0) + event = events_collector.get_event(block=True, timeout=GET_EVENTS_TIMEOUT) assert isinstance(event, ServiceEvent) assert event.event.event_type == EventTypeEnum.EVENTTYPE_UPDATE assert event.service_id.context_id.context_uuid.uuid == context_uuid @@ -193,7 +195,7 @@ def test_service(context_client : ContextClient) -> None: context_client.RemoveService(ServiceId(**SERVICE_R1_R2_ID)) # ----- Check remove event ----------------------------------------------------------------------------------------- - event = events_collector.get_event(block=True, timeout=1.0) + event = events_collector.get_event(block=True, timeout=GET_EVENTS_TIMEOUT) assert isinstance(event, ServiceEvent) assert event.event.event_type == EventTypeEnum.EVENTTYPE_REMOVE assert event.service_id.context_id.context_uuid.uuid == context_uuid @@ -217,7 +219,7 @@ def test_service(context_client : ContextClient) -> None: context_client.RemoveTopology(TopologyId(**TOPOLOGY_ID)) context_client.RemoveContext(ContextId(**CONTEXT_ID)) - events = events_collector.get_events(block=True, count=4, timeout=1.0) + events = events_collector.get_events(block=True, count=4, timeout=GET_EVENTS_TIMEOUT) assert isinstance(events[0], DeviceEvent) assert events[0].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE assert events[0].device_id.device_uuid.uuid == device_r1_uuid diff --git a/src/context/tests/test_slice.py b/src/context/tests/test_slice.py index cb7eb773776e5d38050c09485fd88b90c8d67601..6996bb39e74aeccdaec261850925cff217dcb842 100644 --- a/src/context/tests/test_slice.py +++ b/src/context/tests/test_slice.py @@ -14,8 +14,8 @@ import copy, grpc, pytest, time from common.proto.context_pb2 import ( - Context, ContextEvent, ContextId, Device, DeviceEvent, DeviceId, EventTypeEnum, Link, LinkEvent, LinkId, Service, ServiceEvent, ServiceId, Slice, SliceEvent, SliceId, SliceStatusEnum, Topology, TopologyEvent, - TopologyId) + Context, ContextEvent, ContextId, Device, DeviceEvent, DeviceId, EventTypeEnum, Link, LinkEvent, LinkId, Service, + ServiceEvent, ServiceId, Slice, SliceEvent, SliceId, SliceStatusEnum, Topology, TopologyEvent, TopologyId) from context.client.ContextClient import ContextClient from context.service.database.uuids.Slice import slice_get_uuid from context.client.EventsCollector import EventsCollector @@ -24,6 +24,8 @@ from .Objects import ( LINK_R1_R2, LINK_R1_R2_ID, LINK_R1_R3, LINK_R1_R3_ID, LINK_R2_R3, LINK_R2_R3_ID, SERVICE_R1_R2, SERVICE_R1_R2_ID, SERVICE_R2_R3, SERVICE_R2_R3_ID, SLICE_R1_R3, SLICE_R1_R3_ID, SLICE_R1_R3_NAME, TOPOLOGY, TOPOLOGY_ID) +GET_EVENTS_TIMEOUT = 10.0 + @pytest.mark.depends(on=['context/tests/test_service.py::test_service']) def test_slice(context_client : ContextClient) -> None: @@ -70,7 +72,7 @@ def test_slice(context_client : ContextClient) -> None: assert response.context_id.context_uuid.uuid == context_uuid service_r2_r3_uuid = response.service_uuid.uuid - events = events_collector.get_events(block=True, count=10, timeout=1.0) + events = events_collector.get_events(block=True, count=10, timeout=GET_EVENTS_TIMEOUT) assert isinstance(events[0], ContextEvent) assert events[0].event.event_type == EventTypeEnum.EVENTTYPE_CREATE assert events[0].context_id.context_uuid.uuid == context_uuid @@ -143,7 +145,7 @@ def test_slice(context_client : ContextClient) -> None: assert response.slice_uuid.uuid == slice_uuid # ----- Check create event ----------------------------------------------------------------------------------------- - event = events_collector.get_event(block=True, timeout=1.0) + event = events_collector.get_event(block=True, timeout=GET_EVENTS_TIMEOUT) assert isinstance(event, SliceEvent) assert event.event.event_type == EventTypeEnum.EVENTTYPE_CREATE assert event.slice_id.context_id.context_uuid.uuid == context_uuid @@ -194,7 +196,7 @@ def test_slice(context_client : ContextClient) -> None: assert response.slice_uuid.uuid == slice_uuid # ----- Check update event ----------------------------------------------------------------------------------------- - event = events_collector.get_event(block=True, timeout=1.0) + event = events_collector.get_event(block=True, timeout=GET_EVENTS_TIMEOUT) assert isinstance(event, SliceEvent) assert event.event.event_type == EventTypeEnum.EVENTTYPE_UPDATE assert event.slice_id.context_id.context_uuid.uuid == context_uuid @@ -230,7 +232,7 @@ def test_slice(context_client : ContextClient) -> None: context_client.RemoveSlice(SliceId(**SLICE_R1_R3_ID)) # ----- Check remove event ----------------------------------------------------------------------------------------- - event = events_collector.get_event(block=True, timeout=1.0) + event = events_collector.get_event(block=True, timeout=GET_EVENTS_TIMEOUT) assert isinstance(event, SliceEvent) assert event.event.event_type == EventTypeEnum.EVENTTYPE_REMOVE assert event.slice_id.context_id.context_uuid.uuid == context_uuid diff --git a/src/context/tests/test_topology.py b/src/context/tests/test_topology.py index 311e0f87407784951630935117bda07d1cf00c15..6a3367d49ae004a4c9670d04256f2f726f125a52 100644 --- a/src/context/tests/test_topology.py +++ b/src/context/tests/test_topology.py @@ -20,6 +20,8 @@ from context.service.database.uuids.Topology import topology_get_uuid from context.client.EventsCollector import EventsCollector from .Objects import CONTEXT, CONTEXT_ID, CONTEXT_NAME, TOPOLOGY, TOPOLOGY_ID, TOPOLOGY_NAME +GET_EVENTS_TIMEOUT = 10.0 + @pytest.mark.depends(on=['context/tests/test_context.py::test_context']) def test_topology(context_client : ContextClient) -> None: @@ -36,7 +38,7 @@ def test_topology(context_client : ContextClient) -> None: response = context_client.SetContext(Context(**CONTEXT)) context_uuid = response.context_uuid.uuid - event = events_collector.get_event(block=True, timeout=1.0) + event = events_collector.get_event(block=True, timeout=GET_EVENTS_TIMEOUT) assert isinstance(event, ContextEvent) assert event.event.event_type == EventTypeEnum.EVENTTYPE_CREATE assert event.context_id.context_uuid.uuid == context_uuid @@ -68,7 +70,7 @@ def test_topology(context_client : ContextClient) -> None: assert response.topology_uuid.uuid == topology_uuid # ----- Check create event ----------------------------------------------------------------------------------------- - event = events_collector.get_event(block=True, timeout=1.0) + event = events_collector.get_event(block=True, timeout=GET_EVENTS_TIMEOUT) assert isinstance(event, TopologyEvent) assert event.event.event_type == EventTypeEnum.EVENTTYPE_CREATE assert event.topology_id.context_id.context_uuid.uuid == context_uuid @@ -114,7 +116,7 @@ def test_topology(context_client : ContextClient) -> None: assert response.topology_uuid.uuid == topology_uuid # ----- Check update event ----------------------------------------------------------------------------------------- - event = events_collector.get_event(block=True, timeout=1.0) + event = events_collector.get_event(block=True, timeout=GET_EVENTS_TIMEOUT) assert isinstance(event, TopologyEvent) assert event.event.event_type == EventTypeEnum.EVENTTYPE_UPDATE assert event.topology_id.context_id.context_uuid.uuid == context_uuid @@ -146,7 +148,7 @@ def test_topology(context_client : ContextClient) -> None: context_client.RemoveTopology(TopologyId(**TOPOLOGY_ID)) # ----- Check remove event ----------------------------------------------------------------------------------------- - event = events_collector.get_event(block=True, timeout=1.0) + event = events_collector.get_event(block=True, timeout=GET_EVENTS_TIMEOUT) assert isinstance(event, TopologyEvent) assert event.event.event_type == EventTypeEnum.EVENTTYPE_REMOVE assert event.topology_id.context_id.context_uuid.uuid == context_uuid @@ -167,7 +169,7 @@ def test_topology(context_client : ContextClient) -> None: # ----- Clean dependencies used in the test and capture related events --------------------------------------------- context_client.RemoveContext(ContextId(**CONTEXT_ID)) - event = events_collector.get_event(block=True, timeout=1.0) + event = events_collector.get_event(block=True, timeout=GET_EVENTS_TIMEOUT) assert isinstance(event, ContextEvent) assert event.event.event_type == EventTypeEnum.EVENTTYPE_REMOVE assert event.context_id.context_uuid.uuid == context_uuid