Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
assert response.service_uuid.uuid == SERVICE_DEV1_DEV2_UUID
CONTEXT_WITH_SERVICE = copy.deepcopy(CONTEXT)
CONTEXT_WITH_SERVICE['service_ids'].append(SERVICE_DEV1_DEV2_ID)
response = context_client_grpc.SetContext(Context(**CONTEXT_WITH_SERVICE))
assert response.context_uuid.uuid == DEFAULT_CONTEXT_UUID
response = context_client_grpc.SetService(Service(**SERVICE_DEV2_DEV3))
assert response.context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID
assert response.service_uuid.uuid == SERVICE_DEV2_DEV3_UUID
CONTEXT_WITH_SERVICE = copy.deepcopy(CONTEXT)
CONTEXT_WITH_SERVICE['service_ids'].append(SERVICE_DEV2_DEV3_ID)
response = context_client_grpc.SetContext(Context(**CONTEXT_WITH_SERVICE))
assert response.context_uuid.uuid == DEFAULT_CONTEXT_UUID
response = context_client_grpc.SetService(Service(**SERVICE_DEV1_DEV3))
assert response.context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID
assert response.service_uuid.uuid == SERVICE_DEV1_DEV3_UUID
CONTEXT_WITH_SERVICE = copy.deepcopy(CONTEXT)
CONTEXT_WITH_SERVICE['service_ids'].append(SERVICE_DEV1_DEV3_ID)
response = context_client_grpc.SetContext(Context(**CONTEXT_WITH_SERVICE))
assert response.context_uuid.uuid == DEFAULT_CONTEXT_UUID
events = events_collector.get_events(block=True, count=11)
assert isinstance(events[0], ContextEvent)
assert events[0].event.event_type == EventTypeEnum.EVENTTYPE_CREATE
assert events[0].context_id.context_uuid.uuid == DEFAULT_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 == DEFAULT_CONTEXT_UUID
assert events[1].topology_id.topology_uuid.uuid == DEFAULT_TOPOLOGY_UUID
assert isinstance(events[2], DeviceEvent)
assert events[2].event.event_type == EventTypeEnum.EVENTTYPE_CREATE
assert events[2].device_id.device_uuid.uuid == DEVICE1_UUID
assert isinstance(events[3], DeviceEvent)
assert events[3].event.event_type == EventTypeEnum.EVENTTYPE_CREATE
assert events[3].device_id.device_uuid.uuid == DEVICE2_UUID
assert isinstance(events[4], DeviceEvent)
assert events[4].event.event_type == EventTypeEnum.EVENTTYPE_CREATE
assert events[4].device_id.device_uuid.uuid == DEVICE3_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 == DEFAULT_CONTEXT_UUID
assert events[5].service_id.service_uuid.uuid == SERVICE_DEV1_DEV2_UUID
assert isinstance(events[6], ContextEvent)
assert events[6].event.event_type == EventTypeEnum.EVENTTYPE_UPDATE
assert events[6].context_id.context_uuid.uuid == DEFAULT_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 == DEFAULT_CONTEXT_UUID
assert events[7].service_id.service_uuid.uuid == SERVICE_DEV2_DEV3_UUID
assert isinstance(events[8], ContextEvent)
assert events[8].event.event_type == EventTypeEnum.EVENTTYPE_UPDATE
assert events[8].context_id.context_uuid.uuid == DEFAULT_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 == DEFAULT_CONTEXT_UUID
assert events[9].service_id.service_uuid.uuid == SERVICE_DEV1_DEV3_UUID
assert isinstance(events[10], ContextEvent)
assert events[10].event.event_type == EventTypeEnum.EVENTTYPE_UPDATE
assert events[10].context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID
# ----- Get when the object does not exist -------------------------------------------------------------------------
with pytest.raises(grpc.RpcError) as e:
context_client_grpc.GetConnection(ConnectionId(**CONNECTION_DEV1_DEV3_ID))
assert e.value.code() == grpc.StatusCode.NOT_FOUND
assert e.value.details() == 'Connection({:s}) not found'.format(CONNECTION_DEV1_DEV3_UUID)
# ----- List when the object does not exist ------------------------------------------------------------------------
response = context_client_grpc.ListConnectionIds(ServiceId(**SERVICE_DEV1_DEV3_ID))
assert len(response.connection_ids) == 0
response = context_client_grpc.ListConnections(ServiceId(**SERVICE_DEV1_DEV3_ID))
assert len(response.connections) == 0
# ----- Dump state of database before create the object ------------------------------------------------------------
db_entries = context_database.dump()
LOGGER.info('----- Database Dump [{:3d} entries] -------------------------'.format(len(db_entries)))
for db_entry in db_entries:
LOGGER.info(' [{:>4s}] {:40s} :: {:s}'.format(*db_entry)) # pragma: no cover
LOGGER.info('-----------------------------------------------------------')
assert len(db_entries) == 140
# ----- Create the object ------------------------------------------------------------------------------------------
with pytest.raises(grpc.RpcError) as e:
WRONG_CONNECTION = copy.deepcopy(CONNECTION_DEV1_DEV3)
WRONG_CONNECTION['path_hops_endpoint_ids'][0]\
['topology_id']['context_id']['context_uuid']['uuid'] = 'wrong-context-uuid'
context_client_grpc.SetConnection(Connection(**WRONG_CONNECTION))
assert e.value.code() == grpc.StatusCode.NOT_FOUND
# TODO: should we check that all endpoints belong to same topology?
# TODO: should we check that endpoints form links over the topology?
msg = 'EndPoint(DEV1/EP100:wrong-context-uuid/admin) not found'
assert e.value.details() == msg
response = context_client_grpc.SetConnection(Connection(**CONNECTION_DEV1_DEV3))
assert response.connection_uuid.uuid == CONNECTION_DEV1_DEV3_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_DEV1_DEV3_UUID
# ----- Update the object ------------------------------------------------------------------------------------------
response = context_client_grpc.SetConnection(Connection(**CONNECTION_DEV1_DEV3))
assert response.connection_uuid.uuid == CONNECTION_DEV1_DEV3_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_DEV1_DEV3_UUID
# ----- Dump state of database after create/update the object ------------------------------------------------------
db_entries = context_database.dump()
LOGGER.info('----- Database Dump [{:3d} entries] -------------------------'.format(len(db_entries)))
for db_entry in db_entries:
LOGGER.info(' [{:>4s}] {:40s} :: {:s}'.format(*db_entry)) # pragma: no cover
LOGGER.info('-----------------------------------------------------------')
assert len(db_entries) == 156
# ----- Get when the object exists ---------------------------------------------------------------------------------
response = context_client_grpc.GetConnection(ConnectionId(**CONNECTION_DEV1_DEV3_ID))
assert response.connection_id.connection_uuid.uuid == CONNECTION_DEV1_DEV3_UUID
assert response.service_id.context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID
assert response.service_id.service_uuid.uuid == SERVICE_DEV1_DEV3_UUID
assert len(response.path_hops_endpoint_ids) == 6
assert len(response.sub_service_ids) == 2
# ----- List when the object exists --------------------------------------------------------------------------------
response = context_client_grpc.ListConnectionIds(ServiceId(**SERVICE_DEV1_DEV3_ID))
assert len(response.connection_ids) == 1
assert response.connection_ids[0].connection_uuid.uuid == CONNECTION_DEV1_DEV3_UUID
response = context_client_grpc.ListConnections(ServiceId(**SERVICE_DEV1_DEV3_ID))
assert len(response.connections) == 1
assert response.connections[0].connection_id.connection_uuid.uuid == CONNECTION_DEV1_DEV3_UUID
assert len(response.connections[0].path_hops_endpoint_ids) == 6
assert len(response.connections[0].sub_service_ids) == 2
# ----- Remove the object ------------------------------------------------------------------------------------------
context_client_grpc.RemoveConnection(ConnectionId(**CONNECTION_DEV1_DEV3_ID))
context_client_grpc.RemoveService(ServiceId(**SERVICE_DEV1_DEV3_ID))
context_client_grpc.RemoveService(ServiceId(**SERVICE_DEV2_DEV3_ID))
context_client_grpc.RemoveService(ServiceId(**SERVICE_DEV1_DEV2_ID))
context_client_grpc.RemoveDevice(DeviceId(**DEVICE1_ID))
context_client_grpc.RemoveDevice(DeviceId(**DEVICE2_ID))
context_client_grpc.RemoveDevice(DeviceId(**DEVICE3_ID))
context_client_grpc.RemoveTopology(TopologyId(**TOPOLOGY_ID))
context_client_grpc.RemoveContext(ContextId(**CONTEXT_ID))
# ----- Check remove event -----------------------------------------------------------------------------------------
events = events_collector.get_events(block=True, count=9)
assert isinstance(events[0], ConnectionEvent)
assert events[0].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE
assert events[0].connection_id.connection_uuid.uuid == CONNECTION_DEV1_DEV3_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 == DEFAULT_CONTEXT_UUID
assert events[1].service_id.service_uuid.uuid == SERVICE_DEV1_DEV3_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 == DEFAULT_CONTEXT_UUID
assert events[2].service_id.service_uuid.uuid == SERVICE_DEV2_DEV3_UUID
assert isinstance(events[3], ServiceEvent)
assert events[3].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE
assert events[3].service_id.context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID
assert events[3].service_id.service_uuid.uuid == SERVICE_DEV1_DEV2_UUID
assert isinstance(events[4], DeviceEvent)
assert events[4].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE
assert events[4].device_id.device_uuid.uuid == DEVICE1_UUID
assert isinstance(events[5], DeviceEvent)
assert events[5].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE
assert events[5].device_id.device_uuid.uuid == DEVICE2_UUID
assert isinstance(events[6], DeviceEvent)
assert events[6].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE
assert events[6].device_id.device_uuid.uuid == DEVICE3_UUID
assert isinstance(events[7], TopologyEvent)
assert events[7].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE
assert events[7].topology_id.context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID
assert events[7].topology_id.topology_uuid.uuid == DEFAULT_TOPOLOGY_UUID
assert isinstance(events[8], ContextEvent)
assert events[8].event.event_type == EventTypeEnum.EVENTTYPE_REMOVE
assert events[8].context_id.context_uuid.uuid == DEFAULT_CONTEXT_UUID
# ----- Stop the EventsCollector -----------------------------------------------------------------------------------
events_collector.stop()
# ----- Dump state of database after remove the object -------------------------------------------------------------
db_entries = context_database.dump()
LOGGER.info('----- Database Dump [{:3d} entries] -------------------------'.format(len(db_entries)))
for db_entry in db_entries:
LOGGER.info(' [{:>4s}] {:40s} :: {:s}'.format(*db_entry)) # pragma: no cover
LOGGER.info('-----------------------------------------------------------')
assert len(db_entries) == 0
# ----- Test REST API methods ------------------------------------------------------------------------------------------
def test_rest_populate_database(
context_db_mb : Tuple[Database, MessageBroker], # pylint: disable=redefined-outer-name
context_service_grpc : ContextService # pylint: disable=redefined-outer-name
):
database = context_db_mb[0]
database.clear_all()
populate('127.0.0.1', GRPC_PORT)
def test_rest_get_context_ids(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
reply = do_rest_request('/context_ids')
validate_context_ids(reply)
def test_rest_get_contexts(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
reply = do_rest_request('/contexts')
validate_contexts(reply)
def test_rest_get_context(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
context_uuid = urllib.parse.quote(DEFAULT_CONTEXT_UUID)
reply = do_rest_request('/context/{:s}'.format(context_uuid))
validate_context(reply)
def test_rest_get_topology_ids(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
context_uuid = urllib.parse.quote(DEFAULT_CONTEXT_UUID)
reply = do_rest_request('/context/{:s}/topology_ids'.format(context_uuid))
validate_topology_ids(reply)
def test_rest_get_topologies(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
context_uuid = urllib.parse.quote(DEFAULT_CONTEXT_UUID)
reply = do_rest_request('/context/{:s}/topologies'.format(context_uuid))
validate_topologies(reply)
def test_rest_get_topology(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
context_uuid = urllib.parse.quote(DEFAULT_CONTEXT_UUID)
topology_uuid = urllib.parse.quote(DEFAULT_TOPOLOGY_UUID)
reply = do_rest_request('/context/{:s}/topology/{:s}'.format(context_uuid, topology_uuid))
validate_topology(reply, num_devices=3, num_links=3)
def test_rest_get_service_ids(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
context_uuid = urllib.parse.quote(DEFAULT_CONTEXT_UUID)
reply = do_rest_request('/context/{:s}/service_ids'.format(context_uuid))
validate_service_ids(reply)
def test_rest_get_services(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
context_uuid = urllib.parse.quote(DEFAULT_CONTEXT_UUID)
reply = do_rest_request('/context/{:s}/services'.format(context_uuid))
validate_services(reply)
def test_rest_get_service(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
context_uuid = urllib.parse.quote(DEFAULT_CONTEXT_UUID)
service_uuid = urllib.parse.quote(SERVICE_DEV1_DEV2_UUID, safe='')
reply = do_rest_request('/context/{:s}/service/{:s}'.format(context_uuid, service_uuid))
validate_service(reply)
def test_rest_get_device_ids(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
reply = do_rest_request('/device_ids')
validate_device_ids(reply)
def test_rest_get_devices(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
reply = do_rest_request('/devices')
validate_devices(reply)
def test_rest_get_device(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
device_uuid = urllib.parse.quote(DEVICE1_UUID, safe='')
reply = do_rest_request('/device/{:s}'.format(device_uuid))
validate_device(reply)
def test_rest_get_link_ids(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
reply = do_rest_request('/link_ids')
validate_link_ids(reply)
def test_rest_get_links(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
reply = do_rest_request('/links')
validate_links(reply)
def test_rest_get_link(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
link_uuid = urllib.parse.quote(LINK_DEV1_DEV2_UUID, safe='')
reply = do_rest_request('/link/{:s}'.format(link_uuid))
validate_link(reply)
def test_rest_get_connection_ids(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
context_uuid = urllib.parse.quote(DEFAULT_CONTEXT_UUID)
service_uuid = urllib.parse.quote(SERVICE_DEV1_DEV3_UUID, safe='')
reply = do_rest_request('/context/{:s}/service/{:s}/connection_ids'.format(context_uuid, service_uuid))
validate_connection_ids(reply)
def test_rest_get_connections(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
context_uuid = urllib.parse.quote(DEFAULT_CONTEXT_UUID)
service_uuid = urllib.parse.quote(SERVICE_DEV1_DEV3_UUID, safe='')
reply = do_rest_request('/context/{:s}/service/{:s}/connections'.format(context_uuid, service_uuid))
validate_connections(reply)
def test_rest_get_connection(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
connection_uuid = urllib.parse.quote(CONNECTION_DEV1_DEV3_UUID, safe='')
reply = do_rest_request('/connection/{:s}'.format(connection_uuid))
validate_connection(reply)
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
# ----- Test misc. Context internal tools ------------------------------------------------------------------------------
def test_tools_fast_string_hasher():
with pytest.raises(TypeError) as e:
fast_hasher(27)
assert str(e.value) == "data(27) must be " + FASTHASHER_DATA_ACCEPTED_FORMAT + ", found <class 'int'>"
with pytest.raises(TypeError) as e:
fast_hasher({27})
assert str(e.value) == "data({27}) must be " + FASTHASHER_DATA_ACCEPTED_FORMAT + ", found <class 'set'>"
with pytest.raises(TypeError) as e:
fast_hasher({'27'})
assert str(e.value) == "data({'27'}) must be " + FASTHASHER_DATA_ACCEPTED_FORMAT + ", found <class 'set'>"
with pytest.raises(TypeError) as e:
fast_hasher([27])
assert str(e.value) == "data[0](27) must be " + FASTHASHER_ITEM_ACCEPTED_FORMAT + ", found <class 'int'>"
fast_hasher('hello-world')
fast_hasher('hello-world'.encode('UTF-8'))
fast_hasher(['hello', 'world'])
fast_hasher(('hello', 'world'))
fast_hasher(['hello'.encode('UTF-8'), 'world'.encode('UTF-8')])
fast_hasher(('hello'.encode('UTF-8'), 'world'.encode('UTF-8')))