def test_create_configuration_category_exception(self, status_code, host): microservice_management_host = 'host1' microservice_management_port = 1 ms_mgt_client = MicroserviceManagementClient( microservice_management_host, microservice_management_port) response_mock = MagicMock(type=HTTPResponse) undecoded_data_mock = MagicMock() test_dict = json.dumps({ 'key': 'TEST', 'description': 'description', 'value': { 'ping_timeout': { 'type': 'integer', 'description': 'Timeout for a response from any given micro-service. (must be greater than 0)', 'value': '1', 'default': '1' }, 'sleep_interval': { 'type': 'integer', 'description': 'The time (in seconds) to sleep between health checks. (must be greater than 5)', 'value': '5', 'default': '5' } } }) undecoded_data_mock.decode.return_value = test_dict response_mock.read.return_value = undecoded_data_mock response_mock.status = status_code response_mock.reason = 'this is the reason' with patch.object(HTTPConnection, 'request') as request_patch: with patch.object(HTTPConnection, 'getresponse', return_value=response_mock) as response_patch: with patch.object(_logger, "error") as log_error: with pytest.raises(Exception) as excinfo: ms_mgt_client.create_configuration_category(test_dict) assert excinfo.type is client_exceptions.MicroserviceManagementClientError assert 1 == log_error.call_count msg = '{} error code: %d, Reason: %s'.format(host) log_error.assert_called_once_with(msg, status_code, 'this is the reason') response_patch.assert_called_once_with() args, kwargs = request_patch.call_args_list[0] assert 'POST' == kwargs['method'] assert '/fledge/service/category' == kwargs['url'] assert json.loads(test_dict) == json.loads(kwargs['body'])
def test_create_configuration_category(self): microservice_management_host = 'host1' microservice_management_port = 1 ms_mgt_client = MicroserviceManagementClient( microservice_management_host, microservice_management_port) response_mock = MagicMock(type=HTTPResponse) undecoded_data_mock = MagicMock() response_mock.read.return_value = undecoded_data_mock test_dict = json.dumps({ 'key': 'TEST', 'description': 'description', 'value': { 'ping_timeout': { 'type': 'integer', 'description': 'Timeout for a response from any given micro-service. (must be greater than 0)', 'value': '1', 'default': '1' }, 'sleep_interval': { 'type': 'integer', 'description': 'The time (in seconds) to sleep between health checks. (must be greater than 5)', 'value': '5', 'default': '5' } } }) undecoded_data_mock.decode.return_value = test_dict response_mock.status = 200 with patch.object(HTTPConnection, 'request') as request_patch: with patch.object(HTTPConnection, 'getresponse', return_value=response_mock) as response_patch: ret_value = ms_mgt_client.create_configuration_category( test_dict) assert json.loads(test_dict) == ret_value response_patch.assert_called_once_with() args, kwargs = request_patch.call_args_list[0] assert 'POST' == kwargs['method'] assert '/fledge/service/category' == kwargs['url'] assert json.loads(test_dict) == json.loads(kwargs['body'])
class FledgeProcess(ABC): """ FledgeProcess for all non-core python processes. All processes will inherit from FledgeProcess and must implement pure virtual method run() """ _core_management_host = None """ string containing core's micro-service management host """ _core_management_port = None """ int containing core's micro-service management port """ _name = None """ name of process """ _core_microservice_management_client = None """ MicroserviceManagementClient instance """ _readings_storage_async = None """ fledge.common.storage_client.storage_client.ReadingsStorageClientAsync """ _storage_async = None """ async fledge.common.storage_client.storage_client.StorageClientAsync """ _start_time = None """ time at which this python process started """ def __init__(self): """ All processes must have these three command line arguments passed: --address [core microservice management host] --port [core microservice management port] --name [process name] """ self._start_time = time.time() try: parser = SilentArgParse() parser.add_argument("--name", required=True) parser.add_argument("--address", required=True) parser.add_argument("--port", required=True, type=int) namespace, args = parser.parse_known_args() self._name = getattr(namespace, 'name') self._core_management_host = getattr(namespace, 'address') self._core_management_port = getattr(namespace, 'port') r = range(1, 65536) if self._core_management_port not in r: raise ArgumentParserError("Invalid Port: {}".format( self._core_management_port)) for item in args: if item.startswith('--'): kv = item.split('=') if len(kv) == 2: if len(kv[1].strip()) == 0: raise ArgumentParserError( "Invalid value {} for optional arg {}".format( kv[1], kv[0])) except ArgumentParserError as ex: _logger.error("Arg parser error: %s", str(ex)) raise self._core_microservice_management_client = MicroserviceManagementClient( self._core_management_host, self._core_management_port) self._readings_storage_async = ReadingsStorageClientAsync( self._core_management_host, self._core_management_port) self._storage_async = StorageClientAsync(self._core_management_host, self._core_management_port) # pure virtual method run() to be implemented by child class @abstractmethod def run(self): pass def get_services_from_core(self, name=None, _type=None): return self._core_microservice_management_client.get_services( name, _type) def register_service_with_core(self, service_registration_payload): """ Register a microservice with core Keyword Arguments: service_registration_payload -- json format dictionary Return Values: Argument value (as a string) None (if argument was not passed) Known Exceptions: HTTPError """ return self._core_microservice_management_client.register_service( service_registration_payload) def unregister_service_with_core(self, microservice_id): """ Unregister a microservice with core Keyword Arguments: microservice_id (uuid as a string) """ return self._core_microservice_management_client.unregister_service( microservice_id) def register_interest_with_core(self): # cat name # callback module # self.microservice_id raise NotImplementedError def unregister_interest_with_core(self): # cat name # self.microservice_id raise NotImplementedError def get_configuration_categories(self): """ :return: """ return self._core_microservice_management_client.get_configuration_category( ) def get_configuration_category(self, category_name=None): """ :param category_name: :return: """ return self._core_microservice_management_client.get_configuration_category( category_name) def get_configuration_item(self, category_name, config_item): """ :param category_name: :param config_item: :return: """ return self._core_microservice_management_client.get_configuration_item( category_name, config_item) def create_configuration_category(self, category_data): """ :param category_data: :return: """ return self._core_microservice_management_client.create_configuration_category( category_data) def update_configuration_item(self, category_name, config_item): """ :param category_name: :param config_item: :return: """ return self._core_microservice_management_client.update_configuration_item( category_name, config_item) def delete_configuration_item(self, category_name, config_item): """ :param category_name: :param config_item: :return: """ return self._core_microservice_management_client.delete_configuration_item( category_name, config_item)