def test_get(self, reset_singleton):
        configuration_manager_mock = MagicMock(spec=ConfigurationManager)
        i_reg = InterestRegistry(configuration_manager_mock)

        # get when empty
        microservice_uuid = 'muuid'
        category_name = 'catname'
        with pytest.raises(interest_registry_exceptions.DoesNotExist) as excinfo:
            i_reg.get(microservice_uuid=microservice_uuid,
                      category_name=category_name)

        # get when there is a result (use patch on 'get')
        with patch.object(InterestRegistry, 'and_filter', return_value=[1]):
            ret_val = i_reg.get(
                microservice_uuid=microservice_uuid, category_name=category_name)
        assert ret_val is not None
        assert ret_val == [1]
Beispiel #2
0
async def run(category_name):
    """ Callback run by configuration category to notify changes to interested microservices

    Note: this method is async as needed

    Args:
        configuration_name (str): name of category that was changed
    """

    # get all interest records regarding category_name
    cfg_mgr = ConfigurationManager()
    interest_registry = InterestRegistry(cfg_mgr)
    try:
        interest_records = interest_registry.get(category_name=category_name)
    except interest_registry_exceptions.DoesNotExist:
        return

    category_value = await cfg_mgr.get_category_all_items(category_name)
    payload = {"category": category_name, "items": category_value}
    headers = {'content-type': 'application/json'}

    # for each microservice interested in category_name, notify change
    for i in interest_records:
        # get microservice management server info of microservice through service registry
        try:
            service_record = ServiceRegistry.get(idx=i._microservice_uuid)[0]
        except service_registry_exceptions.DoesNotExist:
            _LOGGER.exception(
                "Unable to notify microservice with uuid %s as it is not found in the service registry",
                i._microservice_uuid)
            continue
        url = "{}://{}:{}/foglamp/change".format(
            service_record._protocol, service_record._address,
            service_record._management_port)
        async with aiohttp.ClientSession() as session:
            try:
                async with session.post(url,
                                        data=json.dumps(payload,
                                                        sort_keys=True),
                                        headers=headers) as resp:
                    result = await resp.text()
                    status_code = resp.status
                    if status_code in range(400, 500):
                        _LOGGER.error("Bad request error code: %d, reason: %s",
                                      status_code, resp.reason)
                    if status_code in range(500, 600):
                        _LOGGER.error("Server error code: %d, reason: %s",
                                      status_code, resp.reason)
            except Exception as ex:
                _LOGGER.exception(
                    "Unable to notify microservice with uuid %s due to exception: %s",
                    i._microservice_uuid, str(ex))
                continue
    def test_get_with_and_filter(self, reset_singleton):
        configuration_manager_mock = MagicMock(spec=ConfigurationManager)
        i_reg = InterestRegistry(configuration_manager_mock)
        # register some interts
        id_1_1 = i_reg.register('muuid1', 'catname1')
        id_1_2 = i_reg.register('muuid1', 'catname2')
        id_2_1 = i_reg.register('muuid2', 'catname1')
        id_2_2 = i_reg.register('muuid2', 'catname2')
        id_3_3 = i_reg.register('muuid3', 'catname3')

        ret_val = i_reg.get(microservice_uuid='muuid1')
        assert len(ret_val) is 2
        for i in ret_val:
            assert isinstance(i, InterestRecord)
        assert ret_val[0]._registration_id is id_1_1
        assert ret_val[0]._microservice_uuid is 'muuid1'
        assert ret_val[0]._category_name is 'catname1'
        assert ret_val[1]._registration_id is id_1_2
        assert ret_val[1]._microservice_uuid is 'muuid1'
        assert ret_val[1]._category_name is 'catname2'

        ret_val = i_reg.get(category_name='catname2')
        assert len(ret_val) is 2
        for i in ret_val:
            assert isinstance(i, InterestRecord)
        assert ret_val[0]._registration_id is id_1_2
        assert ret_val[0]._microservice_uuid is 'muuid1'
        assert ret_val[0]._category_name is 'catname2'
        assert ret_val[1]._registration_id is id_2_2
        assert ret_val[1]._microservice_uuid is 'muuid2'
        assert ret_val[1]._category_name is 'catname2'

        ret_val = i_reg.get(category_name='catname2',
                            microservice_uuid='muuid2')
        assert len(ret_val) is 1
        for i in ret_val:
            assert isinstance(i, InterestRecord)
        assert ret_val[0]._registration_id is id_2_2
        assert ret_val[0]._microservice_uuid is 'muuid2'
        assert ret_val[0]._category_name is 'catname2'