Resolve "(CTTC) NBI unitary tests not running and/or not working"

Closes #94 (closed)

Merge request reports

Loading
+25 −0
Changes for scripts/run_tests_locally-nbi-debug-api.sh: 25 added lines, 0 removed lines.
Original line number Diff line number Diff line
#!/bin/bash
# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


PROJECTDIR=`pwd`

cd $PROJECTDIR/src
RCFILE=$PROJECTDIR/coverage/.coveragerc

# Run unitary tests and analyze coverage of code at same time
# helpful pytest flags: --log-level=INFO -o log_cli=true --verbose --maxfail=1 --durations=0
coverage run --rcfile=$RCFILE --append -m pytest --log-level=INFO --verbose \
    nbi/tests/test_debug_api.py
+58 −7
Changes for src/common/tests/MockServicerImpl_Context.py: 58 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -25,12 +25,13 @@ from common.proto.context_pb2 import (
    Slice, SliceEvent, SliceFilter, SliceId, SliceIdList, SliceList,
    Topology, TopologyDetails, TopologyEvent, TopologyId, TopologyIdList, TopologyList)
from common.proto.context_pb2_grpc import ContextServiceServicer
from common.proto.policy_pb2 import PolicyRule, PolicyRuleId, PolicyRuleIdList, PolicyRuleList
from common.tools.grpc.Tools import grpc_message_to_json, grpc_message_to_json_string
from common.tools.object_factory.Device import json_device_id
from common.tools.object_factory.Link import json_link_id
from .InMemoryObjectDatabase import InMemoryObjectDatabase
from .MockMessageBroker import (
    TOPIC_CONNECTION, TOPIC_CONTEXT, TOPIC_DEVICE, TOPIC_LINK, TOPIC_SERVICE, TOPIC_SLICE, TOPIC_TOPOLOGY,
    TOPIC_CONNECTION, TOPIC_CONTEXT, TOPIC_DEVICE, TOPIC_LINK, TOPIC_SERVICE, TOPIC_SLICE, TOPIC_TOPOLOGY, TOPIC_POLICY,
    MockMessageBroker, notify_event)

LOGGER = logging.getLogger(__name__)
@@ -164,10 +165,12 @@ class MockServicerImpl_Context(ContextServiceServicer):
            rw_request = Topology()
            rw_request.CopyFrom(request)

            # pylint: disable=no-member
            del rw_request.device_ids[:]
            for device_uuid in sorted(device_uuids):
                rw_request.device_ids.append(DeviceId(**json_device_id(device_uuid)))

            # pylint: disable=no-member
            del rw_request.link_ids[:]
            for link_uuid in sorted(link_uuids):
                rw_request.link_ids.append(LinkId(**json_link_id(link_uuid)))
@@ -181,7 +184,9 @@ class MockServicerImpl_Context(ContextServiceServicer):
            if _topology_id.topology_uuid.uuid == topology_uuid: break
        else:
            # topology not found, add it
            context_.topology_ids.add().topology_uuid.uuid = topology_uuid
            topology_id = context_.topology_ids.add()
            topology_id.context_id.context_uuid.uuid = context_uuid
            topology_id.topology_uuid.uuid = topology_uuid

        LOGGER.debug('[SetTopology] reply={:s}'.format(grpc_message_to_json_string(reply)))
        return reply
@@ -195,7 +200,8 @@ class MockServicerImpl_Context(ContextServiceServicer):

        context_ = self.obj_db.get_entry('context', context_uuid, context)
        for _topology_id in context_.topology_ids:
            if _topology_id.topology_uuid.uuid == topology_uuid:
            if _topology_id.context_id.context_uuid.uuid != context_uuid: continue
            if _topology_id.topology_uuid.uuid != topology_uuid: continue
            context_.topology_ids.remove(_topology_id)
            break

@@ -423,7 +429,9 @@ class MockServicerImpl_Context(ContextServiceServicer):
            if _slice_id.slice_uuid.uuid == slice_uuid: break
        else:
            # slice not found, add it
            context_.slice_ids.add().slice_uuid.uuid = slice_uuid
            slice_id = context_.slice_ids.add()
            slice_id.context_id.context_uuid.uuid = context_uuid
            slice_id.slice_uuid.uuid = slice_uuid

        LOGGER.debug('[SetSlice] reply={:s}'.format(grpc_message_to_json_string(reply)))
        return reply
@@ -437,7 +445,8 @@ class MockServicerImpl_Context(ContextServiceServicer):

        context_ = self.obj_db.get_entry('context', context_uuid, context)
        for _slice_id in context_.slice_ids:
            if _slice_id.slice_uuid.uuid == slice_uuid:
            if _slice_id.context_id.context_uuid.uuid != context_uuid: continue
            if _slice_id.slice_uuid.uuid != slice_uuid: continue
            context_.slice_ids.remove(_slice_id)
            break

@@ -515,7 +524,9 @@ class MockServicerImpl_Context(ContextServiceServicer):
            if _service_id.service_uuid.uuid == service_uuid: break
        else:
            # service not found, add it
            context_.service_ids.add().service_uuid.uuid = service_uuid
            service_id = context_.service_ids.add()
            service_id.context_id.context_uuid.uuid = context_uuid
            service_id.service_uuid.uuid = service_uuid

        LOGGER.debug('[SetService] reply={:s}'.format(grpc_message_to_json_string(reply)))
        return reply
@@ -529,7 +540,8 @@ class MockServicerImpl_Context(ContextServiceServicer):

        context_ = self.obj_db.get_entry('context', context_uuid, context)
        for _service_id in context_.service_ids:
            if _service_id.service_uuid.uuid == service_uuid:
            if _service_id.context_id.context_uuid.uuid != context_uuid: continue
            if _service_id.service_uuid.uuid != service_uuid: continue
            context_.service_ids.remove(_service_id)
            break

@@ -615,3 +627,42 @@ class MockServicerImpl_Context(ContextServiceServicer):
    def GetConnectionEvents(self, request : Empty, context : grpc.ServicerContext) -> Iterator[ConnectionEvent]:
        LOGGER.debug('[GetConnectionEvents] request={:s}'.format(grpc_message_to_json_string(request)))
        for message in self.msg_broker.consume({TOPIC_CONNECTION}): yield ConnectionEvent(**json.loads(message.content))

    def ListPolicyRuleIds(self, request : Empty, context : grpc.ServicerContext):   # pylint: disable=unused-argument
        LOGGER.debug('[ListPolicyRuleIds] request={:s}'.format(grpc_message_to_json_string(request)))
        reply = PolicyRuleIdList(policyRuleIdList=[
            getattr(policy_rule, policy_rule.WhichOneof('policy_rule')).policyRuleBasic.policyRuleId
            for policy_rule in self.obj_db.get_entries('policy')
        ])
        LOGGER.debug('[ListPolicyRuleIds] reply={:s}'.format(grpc_message_to_json_string(reply)))
        return reply

    def ListPolicyRules(self, request : Empty, context : grpc.ServicerContext):     # pylint: disable=unused-argument
        LOGGER.debug('[ListPolicyRules] request={:s}'.format(grpc_message_to_json_string(request)))
        reply = PolicyRuleList(policyRules=self.obj_db.get_entries('policy'))
        LOGGER.debug('[ListPolicyRules] reply={:s}'.format(grpc_message_to_json_string(reply)))
        return reply

    def GetPolicyRule(self, request : PolicyRuleId, context : grpc.ServicerContext):
        LOGGER.debug('[GetPolicyRule] request={:s}'.format(grpc_message_to_json_string(request)))
        reply = self.obj_db.get_entry('policy_rule', request.uuid.uuid, context)
        LOGGER.debug('[GetPolicyRule] reply={:s}'.format(grpc_message_to_json_string(reply)))
        return reply

    def SetPolicyRule(self, request : PolicyRule, context : grpc.ServicerContext):  # pylint: disable=unused-argument
        LOGGER.debug('[SetPolicyRule] request={:s}'.format(grpc_message_to_json_string(request)))
        policy_type = request.WhichOneof('policy_rule')
        policy_uuid = getattr(request, policy_type).policyRuleBasic.policyRuleId.uuid.uuid
        rule_id_field = '{:s}.policyRuleBasic.policyRuleId'.format(policy_type)
        reply, _ = self._set(request, 'policy', policy_uuid, rule_id_field, TOPIC_POLICY)
        LOGGER.debug('[SetPolicyRule] reply={:s}'.format(grpc_message_to_json_string(reply)))
        return reply

    def RemovePolicyRule(self, request : PolicyRuleId, context : grpc.ServicerContext):
        LOGGER.debug('[RemovePolicyRule] request={:s}'.format(grpc_message_to_json_string(request)))
        policy_type = request.WhichOneof('policy_rule')
        policy_uuid = getattr(request, policy_type).policyRuleBasic.policyRuleId.uuid.uuid
        rule_id_field = '{:s}.policyRuleBasic.policyRuleId'.format(policy_type)
        reply = self._del(request, 'policy', policy_uuid, rule_id_field, TOPIC_CONTEXT, context)
        LOGGER.debug('[RemovePolicyRule] reply={:s}'.format(grpc_message_to_json_string(reply)))
        return reply
+241 −30
Changes for src/common/type_checkers/Assertions.py: 241 added lines, 30 removed lines.
Original line number Diff line number Diff line
@@ -12,7 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Dict
import logging
from typing import Callable, Dict

LOGGER = logging.getLogger(__name__)

# ----- Enumerations ---------------------------------------------------------------------------------------------------
def validate_config_action_enum(message):
@@ -23,6 +26,14 @@ def validate_config_action_enum(message):
        'CONFIGACTION_DELETE',
    ]

def validate_constraint_action_enum(message):
    assert isinstance(message, str)
    assert message in [
        'CONSTRAINTACTION_UNDEFINED',
        'CONSTRAINTACTION_SET',
        'CONSTRAINTACTION_DELETE',
    ]

def validate_device_driver_enum(message):
    assert isinstance(message, str)
    assert message in [
@@ -35,6 +46,7 @@ def validate_device_driver_enum(message):
        'DEVICEDRIVER_XR',
        'DEVICEDRIVER_IETF_L2VPN',
        'DEVICEDRIVER_GNMI_OPENCONFIG',
        'DEVICEDRIVER_FLEXSCALE',
    ]

def validate_device_operational_status_enum(message):
@@ -64,6 +76,8 @@ def validate_service_type_enum(message):
        'SERVICETYPE_L3NM',
        'SERVICETYPE_L2NM',
        'SERVICETYPE_TAPI_CONNECTIVITY_SERVICE',
        'SERVICETYPE_TE',
        'SERVICETYPE_E2E',
    ]

def validate_service_state_enum(message):
@@ -77,6 +91,17 @@ def validate_service_state_enum(message):
        'SERVICESTATUS_SLA_VIOLATED',
    ]

def validate_slice_status_enum(message):
    assert isinstance(message, str)
    assert message in [
        'SLICESTATUS_UNDEFINED',
        'SLICESTATUS_PLANNED',
        'SLICESTATUS_INIT',
        'SLICESTATUS_ACTIVE',
        'SLICESTATUS_DEINIT',
        'SLICESTATUS_SLA_VIOLATED',
    ]


# ----- Common ---------------------------------------------------------------------------------------------------------
def validate_uuid(message, allow_empty=False):
@@ -114,28 +139,61 @@ def validate_config_rules(message):
    assert 'config_rules' in message
    for config_rule in message['config_rules']: validate_config_rule(config_rule)

CONSTRAINT_TYPES = {
    'custom',
    'schedule',
    'endpoint_location',
    'sla_capacity',
    'sla_latency',
    'sla_availability',
    'sla_isolation',
def validate_constraint_custom(message):
    assert isinstance(message, dict)
    assert len(message.keys()) == 2
    assert 'constraint_type' in message
    assert isinstance(message['constraint_type'], str)
    assert 'constraint_value' in message
    assert isinstance(message['constraint_value'], str)

def validate_constraint_sla_capacity(message):
    assert isinstance(message, dict)
    assert len(message.keys()) == 1
    assert 'capacity_gbps' in message
    assert isinstance(message['capacity_gbps'], (int, float))

def validate_constraint_sla_latency(message):
    assert isinstance(message, dict)
    assert len(message.keys()) == 1
    assert 'e2e_latency_ms' in message
    assert isinstance(message['e2e_latency_ms'], (int, float))

def validate_constraint_sla_availability(message):
    assert isinstance(message, dict)
    assert len(message.keys()) == 3
    assert 'num_disjoint_paths' in message
    assert isinstance(message['num_disjoint_paths'], int)
    assert message['num_disjoint_paths'] >= 0
    assert 'all_active' in message
    assert isinstance(message['all_active'], bool)
    assert 'availability' in message
    assert isinstance(message['availability'], (int, float))
    assert message['availability'] >= 0 and message['availability'] <= 100

CONSTRAINT_TYPE_TO_VALIDATOR = {
    'custom'            : validate_constraint_custom,
    #'schedule'          : validate_constraint_schedule,
    #'endpoint_location' : validate_constraint_endpoint_location,
    #'endpoint_priority' : validate_constraint_endpoint_priority,
    'sla_capacity'      : validate_constraint_sla_capacity,
    'sla_latency'       : validate_constraint_sla_latency,
    'sla_availability'  : validate_constraint_sla_availability,
    #'sla_isolation'     : validate_constraint_sla_isolation,
    #'exclusions'        : validate_constraint_exclusions,
}

def validate_constraint(message):
    assert isinstance(message, dict)
    assert len(message.keys()) == 1
    other_keys = list(message.keys())
    constraint_type = other_keys[0]
    assert constraint_type in CONSTRAINT_TYPES
    assert constraint_type == 'custom', 'Constraint Type Validator for {:s} not implemented'.format(constraint_type)
    custom : Dict = message['custom']
    assert len(custom.keys()) == 2
    assert 'constraint_type' in custom
    assert isinstance(custom['constraint_type'], str)
    assert 'constraint_value' in custom
    assert isinstance(custom['constraint_value'], str)
    assert len(message.keys()) == 2
    assert 'action' in message
    validate_constraint_action_enum(message['action'])
    other_keys = set(list(message.keys()))
    other_keys.discard('action')
    constraint_type = other_keys.pop()
    validator : Callable = CONSTRAINT_TYPE_TO_VALIDATOR.get(constraint_type)
    assert validator is not None, 'Constraint Type Validator for {:s} not implemented'.format(constraint_type)
    validator(message[constraint_type])


# ----- Identifiers ----------------------------------------------------------------------------------------------------
@@ -192,6 +250,15 @@ def validate_connection_id(message):
    assert 'connection_uuid' in message
    validate_uuid(message['connection_uuid'])

def validate_slice_id(message, context_uuid = None):
    assert isinstance(message, dict)
    assert len(message.keys()) == 2
    assert 'context_id' in message
    validate_context_id(message['context_id'])
    if context_uuid is not None: assert message['context_id']['context_uuid']['uuid'] == context_uuid
    assert 'slice_uuid' in message
    validate_uuid(message['slice_uuid'])


# ----- Lists of Identifiers -------------------------------------------------------------------------------------------

@@ -209,6 +276,13 @@ def validate_service_ids(message, context_uuid=None):
    assert isinstance(message['service_ids'], list)
    for service_id in message['service_ids']: validate_service_id(service_id, context_uuid=context_uuid)

def validate_slice_ids(message, context_uuid=None):
    assert isinstance(message, dict)
    assert len(message.keys()) == 1
    assert 'slice_ids' in message
    assert isinstance(message['slice_ids'], list)
    for slice_id in message['slice_ids']: validate_slice_id(slice_id, context_uuid=context_uuid)

def validate_topology_ids(message, context_uuid=None):
    assert isinstance(message, dict)
    assert len(message.keys()) == 1
@@ -242,16 +316,21 @@ def validate_connection_ids(message):

def validate_context(message):
    assert isinstance(message, dict)
    assert len(message.keys()) == 3
    assert len(message.keys()) == 5
    assert 'context_id' in message
    validate_context_id(message['context_id'])
    context_uuid = message['context_id']['context_uuid']['uuid']
    assert 'service_ids' in message
    assert isinstance(message['service_ids'], list)
    for service_id in message['service_ids']: validate_service_id(service_id, context_uuid=context_uuid)
    assert 'name' in message
    assert isinstance(message['name'], str)
    assert 'topology_ids' in message
    assert isinstance(message['topology_ids'], list)
    for topology_id in message['topology_ids']: validate_topology_id(topology_id, context_uuid=context_uuid)
    assert 'service_ids' in message
    assert isinstance(message['service_ids'], list)
    for service_id in message['service_ids']: validate_service_id(service_id, context_uuid=context_uuid)
    assert 'slice_ids' in message
    assert isinstance(message['slice_ids'], list)
    for slice_id in message['slice_ids']: validate_slice_id(slice_id, context_uuid=context_uuid)

def validate_service_state(message):
    assert isinstance(message, dict)
@@ -259,11 +338,19 @@ def validate_service_state(message):
    assert 'service_status' in message
    validate_service_state_enum(message['service_status'])

def validate_slice_status(message):
    assert isinstance(message, dict)
    assert len(message.keys()) == 1
    assert 'slice_status' in message
    validate_slice_status_enum(message['slice_status'])

def validate_service(message):
    assert isinstance(message, dict)
    assert len(message.keys()) == 6
    assert len(message.keys()) == 7
    assert 'service_id' in message
    validate_service_id(message['service_id'])
    assert 'name' in message
    assert isinstance(message['name'], str)
    assert 'service_type' in message
    validate_service_type_enum(message['service_type'])
    assert 'service_endpoint_ids' in message
@@ -277,11 +364,44 @@ def validate_service(message):
    assert 'service_config' in message
    validate_config_rules(message['service_config'])

def validate_slice(message):
    assert isinstance(message, dict)
    assert len(message.keys()) in {8, 9}
    assert 'slice_id' in message
    validate_slice_id(message['slice_id'])
    assert 'name' in message
    assert isinstance(message['name'], str)
    assert 'slice_endpoint_ids' in message
    assert isinstance(message['slice_endpoint_ids'], list)
    for endpoint_id in message['slice_endpoint_ids']: validate_endpoint_id(endpoint_id)
    assert 'slice_constraints' in message
    assert isinstance(message['slice_constraints'], list)
    for constraint in message['slice_constraints']: validate_constraint(constraint)
    assert 'slice_service_ids' in message
    assert isinstance(message['slice_service_ids'], list)
    for service_id in message['slice_service_ids']: validate_service_id(service_id)
    assert 'slice_subslice_ids' in message
    assert isinstance(message['slice_subslice_ids'], list)
    for slice_id in message['slice_subslice_ids']: validate_slice_id(slice_id)
    assert 'slice_status' in message
    validate_slice_status(message['slice_status'])
    assert 'slice_config' in message
    validate_config_rules(message['slice_config'])
    if len(message.keys()) == 9:
        assert 'slice_owner' in message
        assert isinstance(message['slice_owner'], dict)
        assert 'owner_uuid' in message['slice_owner']
        validate_uuid(message['slice_owner']['owner_uuid'])
        assert 'owner_string' in message['slice_owner']
        assert isinstance(message['slice_owner']['owner_string'], str)

def validate_topology(message, num_devices=None, num_links=None):
    assert isinstance(message, dict)
    assert len(message.keys()) == 3
    assert len(message.keys()) == 4
    assert 'topology_id' in message
    validate_topology_id(message['topology_id'])
    assert 'name' in message
    assert isinstance(message['name'], str)
    assert 'device_ids' in message
    assert isinstance(message['device_ids'], list)
    if num_devices is not None: assert len(message['device_ids']) == num_devices
@@ -293,20 +413,49 @@ def validate_topology(message, num_devices=None, num_links=None):

def validate_endpoint(message):
    assert isinstance(message, dict)
    assert len(message.keys()) == 3
    assert len(message.keys()) == 4
    assert 'endpoint_id' in message
    validate_endpoint_id(message['endpoint_id'])
    assert 'name' in message
    assert isinstance(message['name'], str)
    assert 'endpoint_type' in message
    assert isinstance(message['endpoint_type'], str)
    assert 'kpi_sample_types' in message
    assert isinstance(message['kpi_sample_types'], list)
    for kpi_sample_type in message['kpi_sample_types']: validate_kpi_sample_types_enum(kpi_sample_type)

def validate_component(component):
    assert isinstance(component, dict)
    assert len(component.keys()) == 5
    assert 'component_uuid' in component
    validate_uuid(component['component_uuid'])
    assert 'name' in component
    assert isinstance(component['name'], str)
    assert 'type' in component
    assert isinstance(component['type'], str)
    assert 'attributes' in component
    assert isinstance(component['attributes'], dict)
    for k,v in component['attributes'].items():
        assert isinstance(k, str)
        assert isinstance(v, str)
    assert 'parent' in component
    assert isinstance(component['parent'], str)

def validate_link_attributes(link_attributes):
    assert isinstance(link_attributes, dict)
    assert len(link_attributes.keys()) == 2
    assert 'total_capacity_gbps' in link_attributes
    assert isinstance(link_attributes['total_capacity_gbps'], (int, float))
    assert 'used_capacity_gbps' in link_attributes
    assert isinstance(link_attributes['used_capacity_gbps'], (int, float))

def validate_device(message):
    assert isinstance(message, dict)
    assert len(message.keys()) == 6
    assert len(message.keys()) in {8, 9}
    assert 'device_id' in message
    validate_device_id(message['device_id'])
    assert 'name' in message
    assert isinstance(message['name'], str)
    assert 'device_type' in message
    assert isinstance(message['device_type'], str)
    assert 'device_config' in message
@@ -319,19 +468,30 @@ def validate_device(message):
    assert 'device_endpoints' in message
    assert isinstance(message['device_endpoints'], list)
    for endpoint in message['device_endpoints']: validate_endpoint(endpoint)
    assert 'components' in message
    assert isinstance(message['components'], list)
    for component in message['components']: validate_component(component)
    if len(message.keys()) == 9:
        assert 'controller_id' in message
        if len(message['controller_id']) > 0:
            validate_device_id(message['controller_id'])

def validate_link(message):
    assert isinstance(message, dict)
    assert len(message.keys()) == 2
    assert len(message.keys()) == 4
    assert 'link_id' in message
    validate_link_id(message['link_id'])
    assert 'name' in message
    assert isinstance(message['name'], str)
    assert 'link_endpoint_ids' in message
    assert isinstance(message['link_endpoint_ids'], list)
    for endpoint_id in message['link_endpoint_ids']: validate_endpoint_id(endpoint_id)
    assert 'attributes' in message
    validate_link_attributes(message['attributes'])

def validate_connection(message):
    assert isinstance(message, dict)
    assert len(message.keys()) == 4
    assert len(message.keys()) in {4, 5}
    assert 'connection_id' in message
    validate_connection_id(message['connection_id'])
    assert 'service_id' in message
@@ -342,6 +502,50 @@ def validate_connection(message):
    assert 'sub_service_ids' in message
    assert isinstance(message['sub_service_ids'], list)
    for sub_service_id in message['sub_service_ids']: validate_service_id(sub_service_id)
    if len(message.keys()) == 5:
        assert 'settings' in message
        assert isinstance(message['settings'], dict)
        # TODO: improve validation of data types, especially for uint values, IP/MAC addresses, TCP/UDP ports, etc.
        if 'l0' in message['settings']:
            assert isinstance(message['settings']['l0'], dict)
            if 'lsp_symbolic_name' in message['settings']['l0']:
                assert isinstance(message['settings']['l0']['lsp_symbolic_name'], str)
        if 'l2' in message['settings']:
            assert isinstance(message['settings']['l2'], dict)
            if 'src_mac_address' in message['settings']['l2']:
                assert isinstance(message['settings']['l2']['src_mac_address'], str)
            if 'dst_mac_address' in message['settings']['l2']:
                assert isinstance(message['settings']['l2']['dst_mac_address'], str)
            if 'ether_type' in message['settings']['l2']:
                assert isinstance(message['settings']['l2']['ether_type'], int)
            if 'vlan_id' in message['settings']['l2']:
                assert isinstance(message['settings']['l2']['vlan_id'], int)
            if 'mpls_label' in message['settings']['l2']:
                assert isinstance(message['settings']['l2']['mpls_label'], int)
            if 'mpls_traffic_class' in message['settings']['l2']:
                assert isinstance(message['settings']['l2']['mpls_traffic_class'], int)
        if 'l3' in message['settings']:
            assert isinstance(message['settings']['l3'], dict)
            if 'src_ip_address' in message['settings']['l3']:
                assert isinstance(message['settings']['l3']['src_ip_address'], str)
            if 'dst_ip_address' in message['settings']['l3']:
                assert isinstance(message['settings']['l3']['dst_ip_address'], str)
            if 'dscp' in message['settings']['l3']:
                assert isinstance(message['settings']['l3']['dscp'], int)
            if 'protocol' in message['settings']['l3']:
                assert isinstance(message['settings']['l3']['protocol'], int)
            if 'ttl' in message['settings']['l3']:
                assert isinstance(message['settings']['l3']['ttl'], int)
        if 'l4' in message['settings']:
            assert isinstance(message['settings']['l4'], dict)
            if 'src_port' in message['settings']['l4']:
                assert isinstance(message['settings']['l4']['src_port'], int)
            if 'dst_port' in message['settings']['l4']:
                assert isinstance(message['settings']['l4']['dst_port'], int)
            if 'tcp_flags' in message['settings']['l4']:
                assert isinstance(message['settings']['l4']['tcp_flags'], int)
            if 'ttl' in message['settings']['l4']:
                assert isinstance(message['settings']['l4']['ttl'], int)


# ----- Lists of Objects -----------------------------------------------------------------------------------------------
@@ -360,6 +564,13 @@ def validate_services(message):
    assert isinstance(message['services'], list)
    for service in message['services']: validate_service(service)

def validate_slices(message):
    assert isinstance(message, dict)
    assert len(message.keys()) == 1
    assert 'slices' in message
    assert isinstance(message['slices'], list)
    for slice_ in message['slices']: validate_slice(slice_)

def validate_topologies(message):
    assert isinstance(message, dict)
    assert len(message.keys()) == 1
+442 −0

File added.

Preview size limit exceeded, changes collapsed.

+147 −150
Changes for src/nbi/tests/test_debug_api.py: 147 added lines, 150 removed lines.
Original line number Diff line number Diff line
@@ -12,209 +12,206 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging, os, pytest, time, urllib
from common.Constants import DEFAULT_CONTEXT_NAME, DEFAULT_TOPOLOGY_NAME, ServiceNameEnum
from common.proto.context_pb2 import Connection, Context, Device, Link, Service, Slice, Topology
from common.proto.policy_pb2 import PolicyRuleIdList, PolicyRuleId, PolicyRuleList, PolicyRule
from common.Settings import (
    ENVVAR_SUFIX_SERVICE_HOST, ENVVAR_SUFIX_SERVICE_PORT_GRPC, ENVVAR_SUFIX_SERVICE_PORT_HTTP, get_env_var_name,
    get_service_port_grpc, get_service_port_http
import logging, urllib
from common.Constants import DEFAULT_CONTEXT_NAME, DEFAULT_TOPOLOGY_NAME
from common.proto.context_pb2 import ContextId
from common.tools.descriptor.Loader import (
    DescriptorLoader, check_descriptor_load_results, validate_empty_scenario
)
from common.tools.object_factory.Context import json_context_id
from common.type_checkers.Assertions import (
    validate_connection, validate_connection_ids, validate_connections, validate_context, validate_context_ids,
    validate_contexts, validate_device, validate_device_ids, validate_devices, validate_link, validate_link_ids,
    validate_links, validate_service, validate_service_ids, validate_services, validate_topologies, validate_topology,
    validate_topology_ids)
    validate_connection, validate_connection_ids, validate_connections,
    validate_context, validate_context_ids, validate_contexts,
    validate_device, validate_device_ids, validate_devices,
    validate_link, validate_link_ids, validate_links,
    validate_service, validate_service_ids, validate_services,
    validate_slice, validate_slice_ids, validate_slices,
    validate_topologies, validate_topology, validate_topology_ids
)
from context.client.ContextClient import ContextClient
from nbi.tests.PrepareTestScenario import do_rest_get_request
from .MockService_Dependencies import MockService_Dependencies
from .Objects import (
    CONNECTION_R1_R3, CONNECTION_R1_R3_ID, CONNECTION_R1_R3_UUID, CONTEXT, CONTEXT_ID, DEVICE_R1, DEVICE_R1_ID,
    DEVICE_R1_UUID, DEVICE_R2, DEVICE_R2_ID, DEVICE_R2_UUID, DEVICE_R3, DEVICE_R3_ID, DEVICE_R3_UUID, LINK_R1_R2,
    LINK_R1_R2_ID, LINK_R1_R2_UUID, SERVICE_R1_R2, SERVICE_R1_R2_ID, SERVICE_R1_R2_UUID, SERVICE_R1_R3,
    SERVICE_R1_R3_ID, SERVICE_R1_R3_UUID, SERVICE_R2_R3, SERVICE_R2_R3_ID, SERVICE_R2_R3_UUID, SLICE_R1_R3, TOPOLOGY,
    TOPOLOGY_ID, POLICY_RULE, POLICY_RULE_ID, POLICY_RULE_UUID
from nbi.service.rest_server.RestServer import RestServer
from .PrepareTestScenario import ( # pylint: disable=unused-import
    # be careful, order of symbols is important here!
    mock_service, nbi_service_rest, context_client,
    do_rest_get_request
)

LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.DEBUG)

DESCRIPTOR_FILE = 'nbi/tests/data/debug_api_dummy.json'

@pytest.fixture(scope='session')
def mock_service():
    _service = MockService_Dependencies(MOCKSERVICE_PORT)
    _service.configure_env_vars()
    _service.start()
    yield _service
    _service.stop()
JSON_ADMIN_CONTEXT_ID = json_context_id(DEFAULT_CONTEXT_NAME)
ADMIN_CONTEXT_ID = ContextId(**JSON_ADMIN_CONTEXT_ID)


# ----- Prepare Environment --------------------------------------------------------------------------------------------

LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.DEBUG)
def test_prepare_environment(context_client : ContextClient) -> None: # pylint: disable=redefined-outer-name
    validate_empty_scenario(context_client)
    descriptor_loader = DescriptorLoader(descriptors_file=DESCRIPTOR_FILE, context_client=context_client)
    results = descriptor_loader.process()
    check_descriptor_load_results(results, descriptor_loader)
    descriptor_loader.validate()

LOCAL_HOST = '127.0.0.1'
GRPC_PORT = 10000 + int(get_service_port_grpc(ServiceNameEnum.CONTEXT))   # avoid privileged ports
HTTP_PORT = 10000 + int(get_service_port_http(ServiceNameEnum.CONTEXT))   # avoid privileged ports

MOCKSERVICE_PORT = 10000
DEVICE_SERVICE_PORT = MOCKSERVICE_PORT + get_service_port_grpc(ServiceNameEnum.DEVICE) # avoid privileged ports

os.environ[get_env_var_name(ServiceNameEnum.CONTEXT, ENVVAR_SUFIX_SERVICE_HOST     )] = str(LOCAL_HOST)
os.environ[get_env_var_name(ServiceNameEnum.CONTEXT, ENVVAR_SUFIX_SERVICE_PORT_GRPC)] = str(GRPC_PORT)
os.environ[get_env_var_name(ServiceNameEnum.CONTEXT, ENVVAR_SUFIX_SERVICE_PORT_HTTP)] = str(HTTP_PORT)

@pytest.fixture(scope='session')
def context_service_grpc():
    _service = ContextService(context_s_mb[0], context_s_mb[1])
    _service.start()
    yield _service
    _service.stop()

@pytest.fixture(scope='session')
def context_service_rest():
    database = context_db_mb[0]
    _rest_server = RestServer()
    for endpoint_name, resource_class, resource_url in RESOURCES:
        _rest_server.add_resource(resource_class, resource_url, endpoint=endpoint_name, resource_class_args=(database,))
    _rest_server.start()
    time.sleep(1) # bring time for the server to start
    yield _rest_server
    _rest_server.shutdown()
    _rest_server.join()

@pytest.fixture(scope='session')
def context_client_grpc(context_service_grpc : ContextService): # pylint: disable=redefined-outer-name
    _client = ContextClient()
    yield _client
    _client.close()

def test_populate_database():
    client = ContextClient(host=LOCAL_HOST, port=GRPC_PORT)
    client.SetContext(Context(**CONTEXT))
    client.SetTopology(Topology(**TOPOLOGY))
    client.SetDevice(Device(**DEVICE_R1))
    client.SetDevice(Device(**DEVICE_R2))
    client.SetDevice(Device(**DEVICE_R3))
    client.SetLink(Link(**LINK_R1_R2))
    client.SetLink(Link(**LINK_R1_R3))
    client.SetLink(Link(**LINK_R2_R3))
    client.SetService(Service(**SERVICE_R1_R2))
    client.SetService(Service(**SERVICE_R1_R3))
    client.SetService(Service(**SERVICE_R2_R3))
    client.SetSlice(Slice(**SLICE_R1_R3))
    client.SetConnection(Connection(**CONNECTION_R1_R3))

def test_rest_get_context_ids(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
    reply = do_rest_get_request('/context_ids')
    # Verify the scenario has no services/slices
    response = context_client.GetContext(ADMIN_CONTEXT_ID)
    assert len(response.topology_ids) == 1
    assert len(response.service_ids ) == 3
    assert len(response.slice_ids   ) == 1


# ----- Context --------------------------------------------------------------------------------------------------------

def test_rest_get_context_ids(nbi_service_rest: RestServer): # pylint: disable=redefined-outer-name, unused-argument
    reply = do_rest_get_request('/debug-api/context_ids')
    validate_context_ids(reply)

def test_rest_get_contexts(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
    reply = do_rest_get_request('/contexts')
def test_rest_get_contexts(nbi_service_rest : RestServer): # pylint: disable=redefined-outer-name, unused-argument
    reply = do_rest_get_request('/debug-api/contexts')
    validate_contexts(reply)

def test_rest_get_context(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
def test_rest_get_context(nbi_service_rest : RestServer): # pylint: disable=redefined-outer-name, unused-argument
    context_uuid = urllib.parse.quote(DEFAULT_CONTEXT_NAME)
    reply = do_rest_get_request('/context/{:s}'.format(context_uuid))
    reply = do_rest_get_request('/debug-api/context/{:s}'.format(context_uuid))
    validate_context(reply)

def test_rest_get_topology_ids(context_service_rest : RestServer): # pylint: disable=redefined-outer-name

# ----- Topology -------------------------------------------------------------------------------------------------------

def test_rest_get_topology_ids(nbi_service_rest : RestServer): # pylint: disable=redefined-outer-name, unused-argument
    context_uuid = urllib.parse.quote(DEFAULT_CONTEXT_NAME)
    reply = do_rest_get_request('/context/{:s}/topology_ids'.format(context_uuid))
    reply = do_rest_get_request('/debug-api/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
def test_rest_get_topologies(nbi_service_rest : RestServer): # pylint: disable=redefined-outer-name, unused-argument
    context_uuid = urllib.parse.quote(DEFAULT_CONTEXT_NAME)
    reply = do_rest_get_request('/context/{:s}/topologies'.format(context_uuid))
    reply = do_rest_get_request('/debug-api/context/{:s}/topologies'.format(context_uuid))
    validate_topologies(reply)

def test_rest_get_topology(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
def test_rest_get_topology(nbi_service_rest : RestServer): # pylint: disable=redefined-outer-name, unused-argument
    context_uuid = urllib.parse.quote(DEFAULT_CONTEXT_NAME)
    topology_uuid = urllib.parse.quote(DEFAULT_TOPOLOGY_NAME)
    reply = do_rest_get_request('/context/{:s}/topology/{:s}'.format(context_uuid, topology_uuid))
    validate_topology(reply, num_devices=3, num_links=3)
    reply = do_rest_get_request('/debug-api/context/{:s}/topology/{:s}'.format(context_uuid, topology_uuid))
    validate_topology(reply, num_devices=3, num_links=6)


# ----- Device ---------------------------------------------------------------------------------------------------------

def test_rest_get_device_ids(nbi_service_rest : RestServer): # pylint: disable=redefined-outer-name, unused-argument
    reply = do_rest_get_request('/debug-api/device_ids')
    validate_device_ids(reply)

def test_rest_get_devices(nbi_service_rest : RestServer): # pylint: disable=redefined-outer-name, unused-argument
    reply = do_rest_get_request('/debug-api/devices')
    validate_devices(reply)

def test_rest_get_device(nbi_service_rest : RestServer): # pylint: disable=redefined-outer-name, unused-argument
    device_uuid = urllib.parse.quote('R1', safe='')
    reply = do_rest_get_request('/debug-api/device/{:s}'.format(device_uuid))
    validate_device(reply)


# ----- Link -----------------------------------------------------------------------------------------------------------

def test_rest_get_link_ids(nbi_service_rest : RestServer): # pylint: disable=redefined-outer-name, unused-argument
    reply = do_rest_get_request('/debug-api/link_ids')
    validate_link_ids(reply)

def test_rest_get_service_ids(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
def test_rest_get_links(nbi_service_rest : RestServer): # pylint: disable=redefined-outer-name, unused-argument
    reply = do_rest_get_request('/debug-api/links')
    validate_links(reply)

def test_rest_get_link(nbi_service_rest : RestServer): # pylint: disable=redefined-outer-name, unused-argument
    link_uuid = urllib.parse.quote('R1/502==R2/501', safe='')
    reply = do_rest_get_request('/debug-api/link/{:s}'.format(link_uuid))
    validate_link(reply)


# ----- Service --------------------------------------------------------------------------------------------------------

def test_rest_get_service_ids(nbi_service_rest : RestServer): # pylint: disable=redefined-outer-name, unused-argument
    context_uuid = urllib.parse.quote(DEFAULT_CONTEXT_NAME)
    reply = do_rest_get_request('/context/{:s}/service_ids'.format(context_uuid))
    reply = do_rest_get_request('/debug-api/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
def test_rest_get_services(nbi_service_rest : RestServer): # pylint: disable=redefined-outer-name, unused-argument
    context_uuid = urllib.parse.quote(DEFAULT_CONTEXT_NAME)
    reply = do_rest_get_request('/context/{:s}/services'.format(context_uuid))
    reply = do_rest_get_request('/debug-api/context/{:s}/services'.format(context_uuid))
    validate_services(reply)

def test_rest_get_service(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
def test_rest_get_service(nbi_service_rest : RestServer): # pylint: disable=redefined-outer-name, unused-argument
    context_uuid = urllib.parse.quote(DEFAULT_CONTEXT_NAME)
    service_uuid = urllib.parse.quote(SERVICE_R1_R2_UUID, safe='')
    reply = do_rest_get_request('/context/{:s}/service/{:s}'.format(context_uuid, service_uuid))
    service_uuid = urllib.parse.quote('SVC:R1/200==R2/200', safe='')
    reply = do_rest_get_request('/debug-api/context/{:s}/service/{:s}'.format(context_uuid, service_uuid))
    validate_service(reply)

def test_rest_get_slice_ids(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
    context_uuid = urllib.parse.quote(DEFAULT_CONTEXT_NAME)
    reply = do_rest_get_request('/context/{:s}/slice_ids'.format(context_uuid))
    #validate_slice_ids(reply)

def test_rest_get_slices(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
    context_uuid = urllib.parse.quote(DEFAULT_CONTEXT_NAME)
    reply = do_rest_get_request('/context/{:s}/slices'.format(context_uuid))
    #validate_slices(reply)
# ----- Slice ----------------------------------------------------------------------------------------------------------

def test_rest_get_slice(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
def test_rest_get_slice_ids(nbi_service_rest : RestServer): # pylint: disable=redefined-outer-name, unused-argument
    context_uuid = urllib.parse.quote(DEFAULT_CONTEXT_NAME)
    slice_uuid = urllib.parse.quote(SLICE_R1_R3_UUID, safe='')
    reply = do_rest_get_request('/context/{:s}/slice/{:s}'.format(context_uuid, slice_uuid))
    #validate_slice(reply)

def test_rest_get_device_ids(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
    reply = do_rest_get_request('/device_ids')
    validate_device_ids(reply)

def test_rest_get_devices(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
    reply = do_rest_get_request('/devices')
    validate_devices(reply)
    reply = do_rest_get_request('/debug-api/context/{:s}/slice_ids'.format(context_uuid))
    validate_slice_ids(reply)

def test_rest_get_device(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
    device_uuid = urllib.parse.quote(DEVICE_R1_UUID, safe='')
    reply = do_rest_get_request('/device/{:s}'.format(device_uuid))
    validate_device(reply)
def test_rest_get_slices(nbi_service_rest : RestServer): # pylint: disable=redefined-outer-name, unused-argument
    context_uuid = urllib.parse.quote(DEFAULT_CONTEXT_NAME)
    reply = do_rest_get_request('/debug-api/context/{:s}/slices'.format(context_uuid))
    validate_slices(reply)

def test_rest_get_link_ids(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
    reply = do_rest_get_request('/link_ids')
    validate_link_ids(reply)
def test_rest_get_slice(nbi_service_rest : RestServer): # pylint: disable=redefined-outer-name, unused-argument
    context_uuid = urllib.parse.quote(DEFAULT_CONTEXT_NAME)
    slice_uuid = urllib.parse.quote('SLC:R1-R2-R3', safe='')
    reply = do_rest_get_request('/debug-api/context/{:s}/slice/{:s}'.format(context_uuid, slice_uuid))
    validate_slice(reply)

def test_rest_get_links(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
    reply = do_rest_get_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_R1_R2_UUID, safe='')
    reply = do_rest_get_request('/link/{:s}'.format(link_uuid))
    validate_link(reply)
# ----- Connection -----------------------------------------------------------------------------------------------------

def test_rest_get_connection_ids(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
def test_rest_get_connection_ids(nbi_service_rest : RestServer): # pylint: disable=redefined-outer-name, unused-argument
    context_uuid = urllib.parse.quote(DEFAULT_CONTEXT_NAME)
    service_uuid = urllib.parse.quote(SERVICE_R1_R3_UUID, safe='')
    reply = do_rest_get_request('/context/{:s}/service/{:s}/connection_ids'.format(context_uuid, service_uuid))
    service_uuid = urllib.parse.quote('SVC:R1/200==R2/200', safe='')
    reply = do_rest_get_request('/debug-api/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
def test_rest_get_connections(nbi_service_rest : RestServer): # pylint: disable=redefined-outer-name, unused-argument
    context_uuid = urllib.parse.quote(DEFAULT_CONTEXT_NAME)
    service_uuid = urllib.parse.quote(SERVICE_R1_R3_UUID, safe='')
    reply = do_rest_get_request('/context/{:s}/service/{:s}/connections'.format(context_uuid, service_uuid))
    service_uuid = urllib.parse.quote('SVC:R1/200==R2/200', safe='')
    reply = do_rest_get_request('/debug-api/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_R1_R3_UUID, safe='')
    reply = do_rest_get_request('/connection/{:s}'.format(connection_uuid))
def test_rest_get_connection(nbi_service_rest : RestServer): # pylint: disable=redefined-outer-name, unused-argument
    connection_uuid = urllib.parse.quote('CON:R1/200==R2/200:1', safe='')
    reply = do_rest_get_request('/debug-api/connection/{:s}'.format(connection_uuid))
    validate_connection(reply)

def test_rest_get_policyrule_ids(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
    reply = do_rest_get_request('/policyrule_ids')
# ----- Policy ---------------------------------------------------------------------------------------------------------

#def test_rest_get_policyrule_ids(nbi_service_rest : RestServer): # pylint: disable=redefined-outer-name, unused-argument
#    reply = do_rest_get_request('/debug-api/policyrule_ids')
#    validate_policyrule_ids(reply)

def test_rest_get_policyrules(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
    reply = do_rest_get_request('/policyrules')
#def test_rest_get_policyrules(nbi_service_rest : RestServer): # pylint: disable=redefined-outer-name, unused-argument
#    reply = do_rest_get_request('/debug-api/policyrules')
#    validate_policyrules(reply)

def test_rest_get_policyrule(context_service_rest : RestServer): # pylint: disable=redefined-outer-name
    policyrule_uuid = urllib.parse.quote(POLICYRULE_UUID, safe='')
    reply = do_rest_get_request('/policyrule/{:s}'.format(policyrule_uuid))
#def test_rest_get_policyrule(nbi_service_rest : RestServer): # pylint: disable=redefined-outer-name, unused-argument
#    policyrule_uuid_quoted = urllib.parse.quote(policyrule_uuid, safe='')
#    reply = do_rest_get_request('/debug-api/policyrule/{:s}'.format(policyrule_uuid_quoted))
#    validate_policyrule(reply)


# ----- Cleanup Environment --------------------------------------------------------------------------------------------

def test_cleanup_environment(context_client : ContextClient) -> None: # pylint: disable=redefined-outer-name
    # Verify the scenario has no services/slices
    response = context_client.GetContext(ADMIN_CONTEXT_ID)
    assert len(response.topology_ids) == 1
    assert len(response.service_ids ) == 3
    assert len(response.slice_ids   ) == 1

    # Load descriptors and validate the base scenario
    descriptor_loader = DescriptorLoader(descriptors_file=DESCRIPTOR_FILE, context_client=context_client)
    descriptor_loader.validate()
    descriptor_loader.unload()
    validate_empty_scenario(context_client)
Loading
Loading