def test_get_immutable(): global global_val test_dict = {'i': 2, 'n': {'b': global_val, 'n2': {}}} cfg.set('/t1', test_dict) cfg_dict = cfg.get('/t1') # Should be 5 for both print 'Direct get: ' + str(cfg.get('/t1/n/b')) print 'Local map: ' + str(cfg_dict['n']['b']) # Change value of global variable global_val = 10 result1 = cfg.get('/t1/n/b') # Should print 5 print '1: ' + str(result1) # Change value of local map attribute cfg_dict['n']['b'] = global_val result2 = cfg.get('/t1/n/b') # Should still print 5 print '2: ' + str(result2) # Change local map node cfg_dict['n'] = {'b': 'abc'} result3 = cfg.get('/t1/n/b') # Should still print 5 print '3: ' + str(result3) cfg.root().delete('/t1') assert result1 == result2 == result3 == 5
def test_delete_attr(): test_dict = {'new_section': {'attr1': 'val1', 'attr2': 'val2'}} cfg.set('/', test_dict) cfg.root().delete('/new_section/attr1') result = cfg.get('/new_section/attr1') cfg.root().delete('/new_section') assert result is None
def test_write_json(): if os.path.isfile('test_cfg.json'): os.system('rm -f test_cfg.json') cfg.root().write().json('test_cfg.json') new_config = config.ConfigNode.read().json('test_cfg.json') if os.path.isfile('test_cfg.json'): os.system('rm -f test_cfg.json') assert cfg.get('/general/log_level') == new_config.get( '/general/log_level')
def test_initialize_zk(): cfg.init('tests/import.conf_test') if not config._zk_conn: config._zk_conn = utils.zk_connect( 'zookeeper://*****:*****@zookeeper:2181/') cfg.root().write().zk(path='/gconfiglib/test_config', force=True) config._cfg_root = None cfg.init('zookeeper://*****:*****@zookeeper:2181/gconfiglib/test_config') assert cfg.get('/target/database') == 'test'
def test_delete_node(): test_dict = {'new_section': {'attr1': 'val1', 'attr2': 'val2'}} cfg.set('/', test_dict) cfg.root().delete('/new_section') assert cfg.get('/new_section') is None
def test_get_ok(): test_dict = {'new_section': {'attr1': 'val1', 'attr2': 'val2'}} cfg.set('/', test_dict) result = cfg.get('/new_section') cfg.root().delete('/new_section') assert cmp(result, test_dict)
def test_path_to_obj_invalid(): result = cfg.get('/general/abc') assert result == None
def test_add_attr_ok(): cfg.set('/', cfg.ConfigAttribute('add_attr_ok', True)) result = cfg.get('/add_attr_ok') print result cfg.root().delete('/add_attr_ok') assert isinstance(result, bool) and result
def test_add_node_ok(): cfg.set('/', cfg.ConfigNode('add_node_ok')) result = cfg.get('/add_node_ok') print result cfg.root().delete('/add_node_ok') assert isinstance(result, dict)
def test_add_duplicate(): cfg.set('/', {'new_section': {'attr1': 'val1', 'attr2': 'val2'}}) cfg.set('/', {'new_section': {'attr3': 'val1', 'attr4': 'val2'}}) result = cfg.get('/new_section/attr1') cfg.root().delete('/new_section') assert result is None
def test_validate_ok(): template = cfg.TemplateNodeFixed('root', optional=False) # [general] def general_validator(spec): if 'post_type' in spec.keys(): if (spec['post_type'].upper() == 'SQL' and 'post_sql' not in spec.keys())\ or (spec['post_type'].upper() == 'SCRIPT' and 'post_script' not in spec.keys()): print 'Missing correct post processing attribute in node general' return False return True general = cfg.TemplateNodeFixed('general', optional=False, validator=general_validator) general.add( cfg.TemplateAttributeFixed( 'log_level', validator=lambda x: x.upper( ) in ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'])) general.add(cfg.TemplateAttributeFixed('log_file', optional=False)) general.add( cfg.TemplateAttributeFixed('log_days_to_keep', optional=False, value_type=int, default_value=30)) general.add( cfg.TemplateAttributeFixed( 'no_update', optional=False, validator=lambda x: x.upper() in ['YES', 'NO'])) general.add(cfg.TemplateAttributeFixed('file_archive', optional=False)) general.add( cfg.TemplateAttributeFixed( 'post_type', validator=lambda x: x.upper() in ['SQL', 'SCRIPT'])) general.add(cfg.TemplateAttributeFixed('post_sql')) general.add(cfg.TemplateAttributeFixed('post_script')) template.add(general) # [target] target = cfg.TemplateNodeFixed('target', optional=False) target.add(cfg.TemplateAttributeFixed('db_server', optional=False)) target.add(cfg.TemplateAttributeFixed('database', optional=False)) target.add(cfg.TemplateAttributeFixed('schema', optional=False)) template.add(target) # [zookeeper] zookeeper = cfg.TemplateNodeFixed('zookeeper', optional=False) zookeeper.add(cfg.TemplateAttributeFixed('zk_host', optional=False)) template.add(zookeeper) # [sources] sources = cfg.TemplateNodeVariableAttr( 'sources', cfg.TemplateAttributeVariable( validator=lambda x: x.upper() in ['YES', 'NO']), optional=False) template.add(sources) # Source spec def source_spec_validator(spec): if spec['method'] == 'local' and 'file_dir' not in spec.keys(): print 'Missing mandatory file_dir attribute' return False elif spec['method'] in ['ftp', 'sftp', 'http', 's3' ] and 'url' not in spec.keys(): print 'Missing mandatory url attribute' return False if 'reimport' in spec.keys() and spec['reimport'].upper() == 'YES': if 'reimport_start' not in spec.keys( ) or 'reimport_end' not in spec.keys(): print 'Missing reimport_start or reimport_end attribute(s)' return False elif spec['reimport_start'] > spec['reimport_end']: print 'reimport_start cannot be greater than reimport_end' return False if 'file_post_type' in spec.keys(): if (spec['file_post_type'].upper() == 'SQL' and 'file_post_sql' not in spec.keys())\ or (spec['file_post_type'].upper() == 'SCRIPT' and 'file_post_script' not in spec.keys()): print 'Missing correct file post processing attribute' return False if 'src_post_type' in spec.keys(): if (spec['src_post_type'].upper() == 'SQL' and 'src_post_sql' not in spec.keys())\ or (spec['src_post_type'].upper() == 'SCRIPT' and 'src_post_script' not in spec.keys()): print 'Missing correct source post processing attribute' return False return True source_spec = cfg.TemplateNodeFixed('source_spec', optional=False, validator=source_spec_validator) source_spec.add( cfg.TemplateAttributeFixed( 'method', optional=False, validator=lambda x: x in ['local', 'ftp', 'sftp', 'http', 's3'])) source_spec.add(cfg.TemplateAttributeFixed('file_dir')) source_spec.add(cfg.TemplateAttributeFixed('filename', optional=False)) source_spec.add(cfg.TemplateAttributeFixed('fileext', optional=False)) source_spec.add(cfg.TemplateAttributeFixed('source_tag', optional=False)) source_spec.add( cfg.TemplateAttributeFixed('file_date_lag', value_type=int, default_value=0)) source_spec.add( cfg.TemplateAttributeFixed('csv_header_row', value_type=int, default_value=0)) source_spec.add( cfg.TemplateAttributeFixed('csv_encoding', default_value='utf-8')) source_spec.add(cfg.TemplateAttributeFixed('csv_sep', default_value=',')) source_spec.add(cfg.TemplateAttributeFixed('csv_date_field')) source_spec.add(cfg.TemplateAttributeFixed('dest_table', optional=False)) source_spec.add(cfg.TemplateAttributeFixed('start_date', optional=False)) source_spec.add( cfg.TemplateAttributeFixed('start_interval', value_type=int, default_value=0)) source_spec.add( cfg.TemplateAttributeFixed( 'granularity', default_value='D', validator=lambda x: x in ['D', 'H', '30', '15'])) source_spec.add( cfg.TemplateAttributeFixed( 'reimport', default_value='no', validator=lambda x: x.upper() in ['YES', 'NO'])) source_spec.add(cfg.TemplateAttributeFixed('reimport_start')) source_spec.add(cfg.TemplateAttributeFixed('reimport_end')) source_list = cfg.get('/sources').keys() print source_list template.add(cfg.TemplateNodeSet('source_spec', source_spec, source_list)) def source_field_spec_validator(value): sizes = { 'identity': 5, 'integer': 2, 'float': 2, 'varchar': 3, 'date': 2, 'datetime': 2 } if len(value) < 2: print 'Field spec cannot be empty or have less than 2 elements' return False elif value[0] == 'S_filedate': if len(value) != 3 or value[1] != 'date': print 'S_filedate fieldspec misconfigured' return False elif value[0] == 'S_constant': if len(value) != sizes[value[1]] + 1: print '%s fieldspec should have %d attributes' % ( value[0], sizes[value[1]] + 1) return False elif len(value) != sizes[value[1]]: print '%s fieldspec should have %d attributes' % (value[0], sizes[value[1]]) return False elif value[0] == 'S_ignore' and value[1] != 'identity': print 'S_ignore fieldspec misconfigured' return False if value[0] == 'S_interval' and value[1] != 'integer': print 'S_interval should be an integer data type' return False elif value[0] == 'S_datetime' and value[1] != 'datetime': print 'S_datetime should be a datetime data type' return False return True source_field_spec = cfg.TemplateNodeVariableAttr( 'field_attr', cfg.TemplateAttributeVariable(value_type=list, validator=source_field_spec_validator), optional=False) template.add( cfg.TemplateNodeSet('source_field_spec', source_field_spec, [x + '_fields' for x in source_list])) print template.sample() print template.sample('TEXT') assert template.validate(cfg.root())