コード例 #1
0
    async def test_run_missing_service_record(self, reset_state):
        storage_client_mock = MagicMock(spec=StorageClient)
        cfg_mgr = ConfigurationManager(storage_client_mock)

        s_id_1 = ServiceRegistry.register('sname1', 'Storage', 'saddress1', 1,
                                          1, 'http')
        s_id_2 = ServiceRegistry.register('sname2', 'Southbound', 'saddress2',
                                          2, 2, 'http')
        s_id_3 = ServiceRegistry.register('sname3', 'Southbound', 'saddress3',
                                          3, 3, 'http')
        i_reg = InterestRegistry(cfg_mgr)
        id_fake_1 = i_reg.register('fakeid', 'catname1')
        id_1_1 = i_reg.register(s_id_1, 'catname1')
        id_1_2 = i_reg.register(s_id_1, 'catname2')
        id_2_1 = i_reg.register(s_id_2, 'catname1')
        id_2_2 = i_reg.register(s_id_2, 'catname2')
        id_3_3 = i_reg.register(s_id_3, 'catname3')

        # used to mock client session context manager
        async def async_mock(return_value):
            return return_value

        class AsyncSessionContextManagerMock(MagicMock):
            def __init__(self, *args, **kwargs):
                super().__init__(*args, **kwargs)

            async def __aenter__(self):
                client_response_mock = MagicMock(spec=aiohttp.ClientResponse)
                client_response_mock.text.side_effect = [async_mock(None)]
                status_mock = Mock()
                status_mock.side_effect = [200]
                client_response_mock.status = status_mock()
                return client_response_mock

            async def __aexit__(self, *args):
                return None

        with patch.object(ConfigurationManager,
                          'get_category_all_items',
                          return_value=async_mock(None)) as cm_get_patch:
            with patch.object(aiohttp.ClientSession,
                              'post',
                              return_value=AsyncSessionContextManagerMock()
                              ) as post_patch:
                with patch.object(cb._LOGGER, 'exception') as exception_patch:
                    await cb.run('catname1')
        cm_get_patch.assert_called_once_with('catname1')
        exception_patch.assert_called_once_with(
            'Unable to notify microservice with uuid %s as it is not found in the service registry',
            'fakeid')
        post_patch.assert_has_calls([
            call('http://saddress1:1/foglamp/change',
                 data='{"category": "catname1", "items": null}',
                 headers={'content-type': 'application/json'}),
            call('http://saddress2:2/foglamp/change',
                 data='{"category": "catname1", "items": null}',
                 headers={'content-type': 'application/json'})
        ])
コード例 #2
0
    async def test_run_general_exception(self):
        storage_client_mock = MagicMock(spec=StorageClientAsync)
        cfg_mgr = ConfigurationManager(storage_client_mock)

        with patch.object(ServiceRegistry._logger, 'info') as log_info:
            s_id_1 = ServiceRegistry.register('sname1', 'Storage', 'saddress1',
                                              1, 1, 'http')
        assert 1 == log_info.call_count
        args, kwargs = log_info.call_args
        assert args[0].startswith('Registered service instance id=')
        assert args[0].endswith(
            ': <sname1, type=Storage, protocol=http, address=saddress1, service port=1, management port=1, status=1>'
        )

        i_reg = InterestRegistry(cfg_mgr)
        i_reg.register(s_id_1, 'catname1')

        # used to mock client session context manager
        async def async_mock(return_value):
            return return_value

        class AsyncSessionContextManagerMock(MagicMock):
            def __init__(self, *args, **kwargs):
                super().__init__(*args, **kwargs)

            async def __aenter__(self):
                raise Exception

            async def __aexit__(self, *args):
                return None

        with patch.object(ConfigurationManager,
                          'get_category_all_items',
                          return_value=async_mock(None)) as cm_get_patch:
            with patch.object(aiohttp.ClientSession,
                              'post',
                              return_value=AsyncSessionContextManagerMock()
                              ) as post_patch:
                with patch.object(cb._LOGGER, 'exception') as exception_patch:
                    await cb.run('catname1')
                exception_patch.assert_called_once_with(
                    'Unable to notify microservice with uuid %s due to exception: %s',
                    s_id_1, '')
            post_patch.assert_has_calls([
                call('http://saddress1:1/foglamp/change',
                     data='{"category": "catname1", "items": null}',
                     headers={'content-type': 'application/json'})
            ])
        cm_get_patch.assert_called_once_with('catname1')
コード例 #3
0
    def test_register(self, reset_singleton):
        configuration_manager_mock = MagicMock(spec=ConfigurationManager)
        i_reg = InterestRegistry(configuration_manager_mock)
        # register the first interest
        microservice_uuid = 'muuid'
        category_name = 'catname'
        ret_val = i_reg.register(microservice_uuid, category_name)
        assert ret_val is not None
        assert len(i_reg._registered_interests) is 1
        assert isinstance(i_reg._registered_interests[0], InterestRecord)
        assert i_reg._registered_interests[0]._registration_id is ret_val
        assert i_reg._registered_interests[0]._microservice_uuid is microservice_uuid
        assert i_reg._registered_interests[0]._category_name is category_name
        str_val = 'interest registration id={}: <microservice uuid={}, category_name={}>'.format(
            ret_val, microservice_uuid, category_name)
        assert str(i_reg._registered_interests[0]) == str_val

        # register an existing interest
        with pytest.raises(interest_registry_exceptions.ErrorInterestRegistrationAlreadyExists) as excinfo:
            ret_val = i_reg.register(microservice_uuid, category_name)
        assert ret_val is not None
        assert len(i_reg._registered_interests) is 1
        assert isinstance(i_reg._registered_interests[0], InterestRecord)
        assert i_reg._registered_interests[0]._registration_id is ret_val
        assert i_reg._registered_interests[0]._microservice_uuid is microservice_uuid
        assert i_reg._registered_interests[0]._category_name is category_name
        str_val = 'interest registration id={}: <microservice uuid={}, category_name={}>'.format(
            ret_val, microservice_uuid, category_name)
        assert str(i_reg._registered_interests[0]) == str_val

        # register a second interest
        category_name2 = 'catname2'
        ret_val = i_reg.register(microservice_uuid, category_name2)
        assert ret_val is not None
        assert len(i_reg._registered_interests) is 2
        assert isinstance(i_reg._registered_interests[1], InterestRecord)
        assert i_reg._registered_interests[1]._registration_id is ret_val
        assert i_reg._registered_interests[1]._microservice_uuid is microservice_uuid
        assert i_reg._registered_interests[1]._category_name is category_name2
        str_val = 'interest registration id={}: <microservice uuid={}, category_name={}>'.format(
            ret_val, microservice_uuid, category_name2)
        assert str(i_reg._registered_interests[1]) == str_val
コード例 #4
0
    async def test_run_no_interests_in_cat(self):
        storage_client_mock = MagicMock(spec=StorageClientAsync)
        cfg_mgr = ConfigurationManager(storage_client_mock)

        with patch.object(ServiceRegistry._logger, 'info') as log_info:
            s_id_1 = ServiceRegistry.register('sname1', 'Storage', 'saddress1',
                                              1, 1, 'http')
            s_id_2 = ServiceRegistry.register('sname2', 'Southbound',
                                              'saddress2', 2, 2, 'http')
            s_id_3 = ServiceRegistry.register('sname3', 'Southbound',
                                              'saddress3', 3, 3, 'http')
        assert 3 == log_info.call_count
        i_reg = InterestRegistry(cfg_mgr)
        i_reg.register(s_id_1, 'catname2')
        i_reg.register(s_id_2, 'catname2')
        i_reg.register(s_id_3, 'catname3')

        # used to mock client session context manager
        async def async_mock(return_value):
            return return_value

        class AsyncSessionContextManagerMock(MagicMock):
            def __init__(self, *args, **kwargs):
                super().__init__(*args, **kwargs)

            async def __aenter__(self):
                client_response_mock = MagicMock(spec=aiohttp.ClientResponse)
                client_response_mock.text.side_effect = [async_mock(None)]
                status_mock = Mock()
                status_mock.side_effect = [200]
                client_response_mock.status = status_mock()
                return client_response_mock

            async def __aexit__(self, *args):
                return None

        with patch.object(ConfigurationManager,
                          'get_category_all_items') as cm_get_patch:
            with patch.object(aiohttp.ClientSession, 'post') as post_patch:
                await cb.run('catname1')
            post_patch.assert_not_called()
        cm_get_patch.assert_not_called()
コード例 #5
0
    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'
コード例 #6
0
    def test_unregister(self, reset_singleton):
        configuration_manager_mock = MagicMock(spec=ConfigurationManager)
        i_reg = InterestRegistry(configuration_manager_mock)
        # unregister when no items exists
        fake_uuid = 'bla'
        with pytest.raises(interest_registry_exceptions.DoesNotExist) as excinfo:
            ret_val = i_reg.unregister(fake_uuid)

        # register 2 interests, then unregister 1
        id_1_1 = i_reg.register('muuid1', 'catname1')
        id_1_2 = i_reg.register('muuid1', 'catname2')
        ret_val = i_reg.unregister(id_1_1)
        assert ret_val == id_1_1
        assert len(i_reg._registered_interests) is 1
        assert isinstance(i_reg._registered_interests[0], InterestRecord)
        assert i_reg._registered_interests[0]._registration_id is id_1_2
        assert i_reg._registered_interests[0]._microservice_uuid is 'muuid1'
        assert i_reg._registered_interests[0]._category_name is 'catname2'

        # unregister the second one
        ret_val = i_reg.unregister(id_1_2)
        assert ret_val == id_1_2
        assert len(i_reg._registered_interests) is 0
コード例 #7
0
    async def test_run_good(self):
        storage_client_mock = MagicMock(spec=StorageClientAsync)
        cfg_mgr = ConfigurationManager(storage_client_mock)

        with patch.object(ServiceRegistry._logger, 'info') as log_info:
            s_id_1 = ServiceRegistry.register('sname1', 'Storage', 'saddress1',
                                              1, 1, 'http')
            s_id_2 = ServiceRegistry.register('sname2', 'Southbound',
                                              'saddress2', 2, 2, 'http')
            s_id_3 = ServiceRegistry.register('sname3', 'Southbound',
                                              'saddress3', 3, 3, 'http')
        assert 3 == log_info.call_count
        i_reg = InterestRegistry(cfg_mgr)
        i_reg.register(s_id_1, 'catname1')
        i_reg.register(s_id_1, 'catname2')
        i_reg.register(s_id_2, 'catname1')
        i_reg.register(s_id_2, 'catname2')
        i_reg.register(s_id_3, 'catname3')

        # used to mock client session context manager
        async def async_mock(return_value):
            return return_value

        class AsyncSessionContextManagerMock(MagicMock):
            def __init__(self, *args, **kwargs):
                super().__init__(*args, **kwargs)

            async def __aenter__(self):
                client_response_mock = MagicMock(spec=aiohttp.ClientResponse)
                client_response_mock.text.side_effect = [async_mock(None)]
                status_mock = Mock()
                status_mock.side_effect = [200]
                client_response_mock.status = status_mock()
                return client_response_mock

            async def __aexit__(self, *args):
                return None

        with patch.object(ConfigurationManager,
                          'get_category_all_items',
                          return_value=async_mock(None)) as cm_get_patch:
            with patch.object(aiohttp.ClientSession,
                              'post',
                              return_value=AsyncSessionContextManagerMock()
                              ) as post_patch:
                await cb.run('catname1')
            post_patch.assert_has_calls([
                call('http://saddress1:1/foglamp/change',
                     data='{"category": "catname1", "items": null}',
                     headers={'content-type': 'application/json'}),
                call('http://saddress2:2/foglamp/change',
                     data='{"category": "catname1", "items": null}',
                     headers={'content-type': 'application/json'})
            ])
        cm_get_patch.assert_called_once_with('catname1')

        with patch.object(ConfigurationManager,
                          'get_category_all_items',
                          return_value=async_mock(None)) as cm_get_patch:
            with patch.object(aiohttp.ClientSession,
                              'post',
                              return_value=AsyncSessionContextManagerMock()
                              ) as post_patch:
                await cb.run('catname2')
            post_patch.assert_has_calls([
                call('http://saddress1:1/foglamp/change',
                     data='{"category": "catname2", "items": null}',
                     headers={'content-type': 'application/json'}),
                call('http://saddress2:2/foglamp/change',
                     data='{"category": "catname2", "items": null}',
                     headers={'content-type': 'application/json'})
            ])
        cm_get_patch.assert_called_once_with('catname2')

        with patch.object(ConfigurationManager,
                          'get_category_all_items',
                          return_value=async_mock(None)) as cm_get_patch:
            with patch.object(aiohttp.ClientSession,
                              'post',
                              return_value=AsyncSessionContextManagerMock()
                              ) as post_patch:
                await cb.run('catname3')
            post_patch.assert_called_once_with(
                'http://saddress3:3/foglamp/change',
                data='{"category": "catname3", "items": null}',
                headers={'content-type': 'application/json'})
        cm_get_patch.assert_called_once_with('catname3')