def test_should_read_configuration_file (self, mock_exists): mock_parser = Mock(YadtConfigParser) mock_wrapped_parser = Mock() mock_parser._parser = mock_wrapped_parser mock_exists.return_value = True YadtConfigParser.read_configuration_file(mock_parser, 'some.cfg') self.assertEqual(call(['some.cfg']), mock_wrapped_parser.read.call_args)
def test_should_read_configuration_file(self, mock_exists): mock_parser = Mock(YadtConfigParser) mock_wrapped_parser = Mock() mock_parser._parser = mock_wrapped_parser mock_exists.return_value = True YadtConfigParser.read_configuration_file(mock_parser, 'some.cfg') self.assertEqual(call(['some.cfg']), mock_wrapped_parser.read.call_args)
def test_should_return_default_when_option_not_available(self): mock_parser = Mock(YadtConfigParser) mock_parser.get_option.return_value = '' actual_option = YadtConfigParser.get_option_as_list(mock_parser, 'section', 'option', 'default_value') self.assertEqual('default_value', actual_option) self.assertEqual(call('section', 'option', ''), mock_parser.get_option.call_args)
def test_should_return_list_separated_by_comma(self): mock_parser = Mock(YadtConfigParser) mock_parser.get_option.return_value = ' abc, def,ghi,jkl ' actual_option = YadtConfigParser.get_option_as_list(mock_parser, 'section', 'option', 'default_value') self.assertEqual(['abc', 'def', 'ghi', 'jkl'], actual_option) self.assertEqual(call('section', 'option', ''), mock_parser.get_option.call_args)
def test_should_return_a_set (self): mock_parser = Mock(YadtConfigParser) mock_parser.get_option_as_list.return_value = ['abc', 'def', 'ghi', 'jkl'] actual_option = YadtConfigParser.get_option_as_set(mock_parser, 'section', 'option', 'default_value') self.assertEqual(set(['abc', 'def', 'ghi', 'jkl']), actual_option) self.assertEqual(call('section', 'option', 'default_value'), mock_parser.get_option_as_list.call_args)
def test_should_return_yes_as_boolean_value_true(self): mock_parser = Mock(YadtConfigParser) mock_parser.get_option.return_value = 'yes' actual_option = YadtConfigParser.get_option_as_yes_or_no_boolean(mock_parser, 'section', 'option', 'default_value') self.assertEqual(True, actual_option) self.assertEqual(call('section', 'option', 'default_value'), mock_parser.get_option.call_args)
def test_should_return_option_as_int (self): mock_parser = Mock(YadtConfigParser) mock_parser.get_option.return_value = '123456' actual_option = YadtConfigParser.get_option_as_int(mock_parser, 'section', 'option', 'default_value') self.assertEqual(123456, actual_option) self.assertEqual(call('section', 'option', 'default_value'), mock_parser.get_option.call_args)
def __init__(self, defaults=None): """ Creates instance of YadtConfigParser which will be used to parse the configuration file. """ if defaults: self.DEFAULT_BROADCASTER_HOST = defaults.get('--broadcaster-host') self.DEFAULT_BROADCASTER_PORT = defaults.get('--broadcaster-port') self._parser = YadtConfigParser()
def test_should_return_default_when_option_not_available(self): mock_parser = Mock(YadtConfigParser) mock_parser.get_option.return_value = '' actual_option = YadtConfigParser.get_option_as_list( mock_parser, 'section', 'option', 'default_value') self.assertEqual('default_value', actual_option) self.assertEqual(call('section', 'option', ''), mock_parser.get_option.call_args)
def test_should_return_option_as_int(self): mock_parser = Mock(YadtConfigParser) mock_parser.get_option.return_value = '123456' actual_option = YadtConfigParser.get_option_as_int( mock_parser, 'section', 'option', 'default_value') self.assertEqual(123456, actual_option) self.assertEqual(call('section', 'option', 'default_value'), mock_parser.get_option.call_args)
def test_should_return_no_as_boolean_value_false(self): mock_parser = Mock(YadtConfigParser) mock_parser.get_option.return_value = 'no' actual_option = YadtConfigParser.get_option_as_yes_or_no_boolean( mock_parser, 'section', 'option', 'default_value') self.assertEqual(False, actual_option) self.assertEqual(call('section', 'option', 'default_value'), mock_parser.get_option.call_args)
def test_should_return_list_separated_by_comma(self): mock_parser = Mock(YadtConfigParser) mock_parser.get_option.return_value = ' abc, def,ghi,jkl ' actual_option = YadtConfigParser.get_option_as_list( mock_parser, 'section', 'option', 'default_value') self.assertEqual(['abc', 'def', 'ghi', 'jkl'], actual_option) self.assertEqual(call('section', 'option', ''), mock_parser.get_option.call_args)
def test_should_return_default_value_when_option_not_in_section (self): mock_parser = Mock(YadtConfigParser) mock_wrapped_parser = Mock() mock_wrapped_parser.has_section.return_value = True mock_wrapped_parser.has_option.return_value = False mock_parser._parser = mock_wrapped_parser actual_option = YadtConfigParser.get_option(mock_parser, 'section', 'option', 'default_value') self.assertEqual('default_value', actual_option) self.assertEqual(call('section', 'option'), mock_wrapped_parser.has_option.call_args)
def test_should_return_a_set(self): mock_parser = Mock(YadtConfigParser) mock_parser.get_option_as_list.return_value = [ 'abc', 'def', 'ghi', 'jkl' ] actual_option = YadtConfigParser.get_option_as_set( mock_parser, 'section', 'option', 'default_value') self.assertEqual(set(['abc', 'def', 'ghi', 'jkl']), actual_option) self.assertEqual(call('section', 'option', 'default_value'), mock_parser.get_option_as_list.call_args)
def test_should_return_default_value_when_option_not_in_section(self): mock_parser = Mock(YadtConfigParser) mock_wrapped_parser = Mock() mock_wrapped_parser.has_section.return_value = True mock_wrapped_parser.has_option.return_value = False mock_parser._parser = mock_wrapped_parser actual_option = YadtConfigParser.get_option(mock_parser, 'section', 'option', 'default_value') self.assertEqual('default_value', actual_option) self.assertEqual(call('section', 'option'), mock_wrapped_parser.has_option.call_args)
class ControllerConfigLoader (object): """ uses a YadtConfigParser which offers convenience methods. """ DEFAULT_BROADCASTER_HOST = 'localhost' DEFAULT_BROADCASTER_PORT = '8081' def __init__(self, defaults=None): """ Creates instance of YadtConfigParser which will be used to parse the configuration file. """ if defaults: self.DEFAULT_BROADCASTER_HOST = defaults.get('--broadcaster-host') self.DEFAULT_BROADCASTER_PORT = defaults.get('--broadcaster-port') self._parser = YadtConfigParser() def get_broadcaster_host(self): """ @return: the broadcaster host from the configuration file, otherwise DEFAULT_BROADCASTER_HOST. """ return self._parser.get_option(SECTION_BROADCASTER, 'host', self.DEFAULT_BROADCASTER_HOST) def get_broadcaster_port(self): """ @return: the broadcaster port from the configuration file as int, otherwise DEFAULT_BROADCASTER_PORT. """ return self._parser.get_option_as_int(SECTION_BROADCASTER, 'port', self.DEFAULT_BROADCASTER_PORT) def read_configuration_file(self, filename): """ Reads the given configuration file. Uses the YadtConfigParser. @return: configuration dictionary """ return self._parser.read_configuration_file(filename)
class ReceiverConfigLoader(object): """ uses a YadtConfigParser which offers convenience methods. """ def __init__(self): """ Creates instance of YadtConfigParser which will be used to parse the configuration file. """ self._parser = YadtConfigParser() def get_app_status_port(self): """ @return: the app status port from the configuration file as int, otherwise DEFAULT_APP_STATUS_PORT. """ return self._parser.get_option_as_int(SECTION_RECEIVER, 'app_status_port', DEFAULT_APP_STATUS_PORT) def get_broadcaster_host(self): """ @return: the broadcaster host from the configuration file, otherwise DEFAULT_BROADCASTER_HOST. """ return self._parser.get_option(SECTION_BROADCASTER, 'host', DEFAULT_BROADCASTER_HOST) def get_broadcaster_port(self): """ @return: the broadcaster port from the configuration file as int, otherwise DEFAULT_BROADCASTER_PORT. """ return self._parser.get_option_as_int(SECTION_BROADCASTER, 'port', DEFAULT_BROADCASTER_PORT) def get_hostname(self): """ @return: if a hostname is given in the configuration file it will return it, otherwise it will return the host name as given by socket.gethostname """ return self._parser.get_option(SECTION_RECEIVER, 'hostname', socket.gethostname()) def get_log_filename(self): """ @return: the log filename from the configuration file if given, otherwise DEFAULT_LOG_FILENAME """ return self._parser.get_option(SECTION_RECEIVER, 'log_filename', DEFAULT_LOG_FILENAME) def get_python_command(self): """ @return: the python command from the configuration file if given, otherwise DEFAULT_PYTHON_COMMAND """ return self._parser.get_option(SECTION_RECEIVER, 'python_command', DEFAULT_PYTHON_COMMAND) def get_script_to_execute(self): """ @return: if the configuration file provides a script to execute, otherwise DEFAULT_SCRIPT_TO_EXECUTE. """ return self._parser.get_option(SECTION_RECEIVER, 'script_to_execute', DEFAULT_SCRIPT_TO_EXECUTE) def get_targets(self): """ @return: the set of targets given in the configuration file, otherwise DEFAULT_TARGETS. """ return self._parser.get_option_as_set(SECTION_RECEIVER, 'targets', DEFAULT_TARGETS) def get_targets_directory(self): """ @return: the targets directory from the configuration file, otherwise DEFAULT_TARGETS_DIRECTORY. """ return self._parser.get_option(SECTION_RECEIVER, 'targets_directory', DEFAULT_TARGETS_DIRECTORY) def read_configuration_file(self, filename): """ Reads the given configuration file. Uses the YadtConfigParser. @return: configuration dictionary """ return self._parser.read_configuration_file(filename) def get_metrics_directory(self): """ @return: the metrics_directory from the receiver configuration and None otherwise """ return self._parser.get_option(SECTION_RECEIVER, 'metrics_directory', None) def get_metrics_file(self): """ @return: the derived metrics_file or None """ metrics_directory = self.get_metrics_directory() if metrics_directory is not None: return os.path.join(metrics_directory, 'yrc.metrics')
class ReceiverConfigLoader (object): """ uses a YadtConfigParser which offers convenience methods. """ def __init__(self): """ Creates instance of YadtConfigParser which will be used to parse the configuration file. """ self._parser = YadtConfigParser() def get_app_status_port(self): """ @return: the app status port from the configuration file as int, otherwise DEFAULT_APP_STATUS_PORT. """ return self._parser.get_option_as_int(SECTION_RECEIVER, 'app_status_port', DEFAULT_APP_STATUS_PORT) def get_broadcaster_host(self): """ @return: the broadcaster host from the configuration file, otherwise DEFAULT_BROADCASTER_HOST. """ return self._parser.get_option(SECTION_BROADCASTER, 'host', DEFAULT_BROADCASTER_HOST) def get_broadcaster_port(self): """ @return: the broadcaster port from the configuration file as int, otherwise DEFAULT_BROADCASTER_PORT. """ return self._parser.get_option_as_int(SECTION_BROADCASTER, 'port', DEFAULT_BROADCASTER_PORT) def get_hostname(self): """ @return: if a hostname is given in the configuration file it will return it, otherwise it will return the host name as given by socket.gethostname """ return self._parser.get_option(SECTION_RECEIVER, 'hostname', socket.gethostname()) def get_log_filename(self): """ @return: the log filename from the configuration file if given, otherwise DEFAULT_LOG_FILENAME """ return self._parser.get_option(SECTION_RECEIVER, 'log_filename', DEFAULT_LOG_FILENAME) def get_python_command(self): """ @return: the python command from the configuration file if given, otherwise DEFAULT_PYTHON_COMMAND """ return self._parser.get_option(SECTION_RECEIVER, 'python_command', DEFAULT_PYTHON_COMMAND) def get_script_to_execute(self): """ @return: if the configuration file provides a script to execute, otherwise DEFAULT_SCRIPT_TO_EXECUTE. """ return self._parser.get_option(SECTION_RECEIVER, 'script_to_execute', DEFAULT_SCRIPT_TO_EXECUTE) def get_targets(self): """ @return: the set of targets given in the configuration file, otherwise DEFAULT_TARGETS. """ return self._parser.get_option_as_set(SECTION_RECEIVER, 'targets', DEFAULT_TARGETS) def get_targets_directory(self): """ @return: the targets directory from the configuration file, otherwise DEFAULT_TARGETS_DIRECTORY. """ return self._parser.get_option(SECTION_RECEIVER, 'targets_directory', DEFAULT_TARGETS_DIRECTORY) def read_configuration_file(self, filename): """ Reads the given configuration file. Uses the YadtConfigParser. @return: configuration dictionary """ return self._parser.read_configuration_file(filename) def get_metrics_directory(self): """ @return: the metrics_directory from the receiver configuration and None otherwise """ return self._parser.get_option(SECTION_RECEIVER, 'metrics_directory', None) def get_metrics_file(self): """ @return: the derived metrics_file or None """ metrics_directory = self.get_metrics_directory() if metrics_directory is not None: return os.path.join(metrics_directory, 'yrc.metrics')
def __init__(self): """ Creates instance of YadtConfigParser which will be used to parse the configuration file. """ self._parser = YadtConfigParser()
def test_should_create_instance_of_YadtConfigParser(self): parser = YadtConfigParser() name_of_type = parser._parser.__class__.__name__ self.assertEqual('SafeConfigParser', name_of_type)