Skip to content
Snippets Groups Projects
test_context.py 6.73 KiB
Newer Older
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
#
# 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.

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
import copy, grpc, pytest
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from common.proto.context_pb2 import Context, ContextId, Empty
from context.client.ContextClient import ContextClient
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from context.service.database.methods.uuids.Context import context_get_uuid
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#from context.client.EventsCollector import EventsCollector
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
from .Objects import CONTEXT, CONTEXT_ID, CONTEXT_NAME
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
def test_context(context_client : ContextClient) -> None:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

    # ----- Initialize the EventsCollector -----------------------------------------------------------------------------
    #events_collector = EventsCollector(
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    #    context_client, log_events_received=True,
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    #    activate_context_collector = True, activate_topology_collector = False, activate_device_collector = False,
    #    activate_link_collector = False, activate_service_collector = False, activate_slice_collector = False,
    #    activate_connection_collector = False)
    #events_collector.start()

    # ----- Get when the object does not exist -------------------------------------------------------------------------
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    context_id = ContextId(**CONTEXT_ID)
    context_uuid = context_get_uuid(context_id, allow_random=False)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    with pytest.raises(grpc.RpcError) as e:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        context_client.GetContext(context_id)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    assert e.value.code() == grpc.StatusCode.NOT_FOUND
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    MSG = 'Context({:s}) not found; context_uuid generated was: {:s}'
    assert e.value.details() == MSG.format(CONTEXT_NAME, context_uuid)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

    # ----- List when the object does not exist ------------------------------------------------------------------------
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    response = context_client.ListContextIds(Empty())
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    assert len(response.context_ids) == 0

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    response = context_client.ListContexts(Empty())
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    assert len(response.contexts) == 0

    # ----- Create the object ------------------------------------------------------------------------------------------
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    response = context_client.SetContext(Context(**CONTEXT))
    assert response.context_uuid.uuid == context_uuid
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

    # ----- Check create event -----------------------------------------------------------------------------------------
    #event = events_collector.get_event(block=True, timeout=10.0)
    #assert isinstance(event, ContextEvent)
    #assert event.event.event_type == EventTypeEnum.EVENTTYPE_CREATE
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    #assert event.context_id.context_uuid.uuid == context_uuid
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

    # ----- Get when the object exists ---------------------------------------------------------------------------------
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    response = context_client.GetContext(ContextId(**CONTEXT_ID))
    assert response.context_id.context_uuid.uuid == context_uuid
    assert response.name == CONTEXT_NAME
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    assert len(response.topology_ids) == 0
    assert len(response.service_ids) == 0
    assert len(response.slice_ids) == 0

    # ----- List when the object exists --------------------------------------------------------------------------------
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    response = context_client.ListContextIds(Empty())
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    assert len(response.context_ids) == 1
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    assert response.context_ids[0].context_uuid.uuid == context_uuid
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    response = context_client.ListContexts(Empty())
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    assert len(response.contexts) == 1
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    assert response.contexts[0].context_id.context_uuid.uuid == context_uuid
    assert response.contexts[0].name == CONTEXT_NAME
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    assert len(response.contexts[0].topology_ids) == 0
    assert len(response.contexts[0].service_ids) == 0
    assert len(response.contexts[0].slice_ids) == 0

    # ----- Update the object ------------------------------------------------------------------------------------------
    new_context_name = 'new'
    CONTEXT_WITH_NAME = copy.deepcopy(CONTEXT)
    CONTEXT_WITH_NAME['name'] = new_context_name
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    response = context_client.SetContext(Context(**CONTEXT_WITH_NAME))
    assert response.context_uuid.uuid == context_uuid
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

    # ----- Check update event -----------------------------------------------------------------------------------------
    #event = events_collector.get_event(block=True, timeout=10.0)
    #assert isinstance(event, ContextEvent)
    #assert event.event.event_type == EventTypeEnum.EVENTTYPE_UPDATE
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    #assert event.context_id.context_uuid.uuid == context_uuid
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

    # ----- Get when the object is modified ----------------------------------------------------------------------------
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    response = context_client.GetContext(ContextId(**CONTEXT_ID))
    assert response.context_id.context_uuid.uuid == context_uuid
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    assert response.name == new_context_name
    assert len(response.topology_ids) == 0
    assert len(response.service_ids) == 0
    assert len(response.slice_ids) == 0

    # ----- List when the object is modified ---------------------------------------------------------------------------
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    response = context_client.ListContextIds(Empty())
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    assert len(response.context_ids) == 1
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    assert response.context_ids[0].context_uuid.uuid == context_uuid
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    response = context_client.ListContexts(Empty())
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    assert len(response.contexts) == 1
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    assert response.contexts[0].context_id.context_uuid.uuid == context_uuid
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    assert response.contexts[0].name == new_context_name
    assert len(response.contexts[0].topology_ids) == 0
    assert len(response.contexts[0].service_ids) == 0
    assert len(response.contexts[0].slice_ids) == 0

    # ----- Remove the object ------------------------------------------------------------------------------------------
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    context_client.RemoveContext(ContextId(**CONTEXT_ID))
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

    # ----- Check remove event -----------------------------------------------------------------------------------------
    #event = events_collector.get_event(block=True, timeout=10.0)
    #assert isinstance(event, ContextEvent)
    #assert event.event.event_type == EventTypeEnum.EVENTTYPE_REMOVE
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    #assert event.context_id.context_uuid.uuid == context_uuid
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

    # ----- List after deleting the object -----------------------------------------------------------------------------
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    response = context_client.ListContextIds(Empty())
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    assert len(response.context_ids) == 0

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    response = context_client.ListContexts(Empty())
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    assert len(response.contexts) == 0

    # ----- Stop the EventsCollector -----------------------------------------------------------------------------------
    #events_collector.stop()