def test_nromal_dict(self): """test config is normal dict.""" config_list = [ {'abc': 'abc'}, {'abc': [1, 2, 3]}, {'abc': {'1': '123'}}, [1, 2, 3], 'abc', ] for config in config_list: self.assertEqual(config, config_reference.get_clean_config(config))
def test_partial_clean(self): """test config is partial cleaned.""" config_and_cleans = [ ({'abc': 1, 'bcd': None}, {'abc': 1}), ({'abc': 1, 'bcd': {}}, {'abc': 1}), ({'abc': 1, 'bcd': {'m': {}}}, {'abc': 1}), ({'abc': 1, 'b': {'m': {}, 'n': 2}}, {'abc': 1, 'b': {'n': 2}}), ] for config, expected_config in config_and_cleans: self.assertEqual( expected_config, config_reference.get_clean_config(config))
def filter(self, config): """Filter config :param config: configuration to filter. :type config: dict :returns: filtered configuration as dict """ ref = config_reference.ConfigReference(config) filtered_ref = config_reference.ConfigReference({}) self._filter_allows(ref, filtered_ref) self._filter_denies(filtered_ref) filtered_config = config_reference.get_clean_config( filtered_ref.config) logging.debug('filter config %s to %s', config, filtered_config) return filtered_config
def filter(self, config): """Filter config :param config: configuration to filter. :type config: dict :returns: filtered configuration as dict """ ref = config_reference.ConfigReference(config) filtered_ref = config_reference.ConfigReference({}) self._filter_allows(ref, filtered_ref) self._filter_denies(filtered_ref) filtered_config = config_reference.get_clean_config( filtered_ref.config) logging.debug('filter config %s to %s', config, filtered_config) return filtered_config
def translate(self, config): """Translate config. :param config: configuration to translate. :returns: the translated configuration. """ ref = config_reference.ConfigReference(config) translated_ref = config_reference.ConfigReference({}) for key, values in self.mapping_.items(): for value in values: value.translate(ref, key, translated_ref) translated_config = config_reference.get_clean_config( translated_ref.config) logging.debug('translate config\n%s\nto\n%s', config, translated_config) return translated_config
def translate(self, config): """Translate config. :param config: configuration to translate. :returns: the translated configuration. """ ref = config_reference.ConfigReference(config) translated_ref = config_reference.ConfigReference({}) for key, values in self.mapping_.items(): for value in values: value.translate(ref, key, translated_ref) translated_config = config_reference.get_clean_config( translated_ref.config) logging.debug('translate config\n%s\nto\n%s', config, translated_config) return translated_config
def test_nromal_dict(self): """test config is normal dict.""" config_list = [ { 'abc': 'abc' }, { 'abc': [1, 2, 3] }, { 'abc': { '1': '123' } }, [1, 2, 3], 'abc', ] for config in config_list: self.assertEqual(config, config_reference.get_clean_config(config))
def test_partial_clean(self): """test config is partial cleaned.""" config_and_cleans = [ ({ 'abc': 1, 'bcd': None }, { 'abc': 1 }), ({ 'abc': 1, 'bcd': {} }, { 'abc': 1 }), ({ 'abc': 1, 'bcd': { 'm': {} } }, { 'abc': 1 }), ({ 'abc': 1, 'b': { 'm': {}, 'n': 2 } }, { 'abc': 1, 'b': { 'n': 2 } }), ] for config, expected_config in config_and_cleans: self.assertEqual(expected_config, config_reference.get_clean_config(config))
def merge(self, upper_config, lower_configs): """Merge cluster config to host configs. :param upper_config: cluster configuration to merge from. :type upper_config: dict :param lower_configs: host configurations to merge to. :type lower_configs: dict of host id to host config as dict """ upper_ref = config_reference.ConfigReference(upper_config) lower_refs = {} for lower_key, lower_config in lower_configs.items(): lower_refs[lower_key] = config_reference.ConfigReference( lower_config) for mapping in self.mappings_: logging.debug('apply merging from the rule %s', mapping) mapping.merge(upper_ref, lower_refs) for lower_key, lower_config in lower_configs.items(): lower_configs[lower_key] = config_reference.get_clean_config( lower_config) logging.debug('merged upper config\n%s\nto lower configs:\n%s', upper_config, lower_configs)
def merge(self, upper_config, lower_configs): """Merge cluster config to host configs. :param upper_config: cluster configuration to merge from. :type upper_config: dict :param lower_configs: host configurations to merge to. :type lower_configs: dict of host id to host config as dict """ upper_ref = config_reference.ConfigReference(upper_config) lower_refs = {} for lower_key, lower_config in lower_configs.items(): lower_refs[lower_key] = config_reference.ConfigReference( lower_config) for mapping in self.mappings_: logging.debug('apply merging from the rule %s', mapping) mapping.merge(upper_ref, lower_refs) for lower_key, lower_config in lower_configs.items(): lower_configs[lower_key] = config_reference.get_clean_config( lower_config) logging.debug('merged upper config\n%s\nto lower configs:\n%s', upper_config, lower_configs)
def test_recursive_empty_dict(self): """test config is recursively empty.""" self.assertIsNone(config_reference.get_clean_config({'test': {}})) self.assertIsNone(config_reference.get_clean_config({'test': None}))
def test_config_empty(self): """test config is empty.""" self.assertIsNone(config_reference.get_clean_config(None)) self.assertIsNone(config_reference.get_clean_config({})) self.assertEqual([], config_reference.get_clean_config([])) self.assertEqual('', config_reference.get_clean_config(''))
def test_recursive_empty_dict(self): """test config is recursively empty.""" self.assertIsNone(config_reference.get_clean_config({'test': {}})) self.assertIsNone(config_reference.get_clean_config({'test': None}))
def test_config_empty(self): """test config is empty.""" self.assertIsNone(config_reference.get_clean_config(None)) self.assertIsNone(config_reference.get_clean_config({})) self.assertEqual([], config_reference.get_clean_config([])) self.assertEqual('', config_reference.get_clean_config(''))