def test_validate_optional_node_mandatory_attr(): # validation for optional node with mandatory attributes without default value should pass template = cfg.TemplateNodeFixed('root', optional=False) opt_node = cfg.TemplateNodeFixed('opt_node', optional=True) opt_node.add(cfg.TemplateAttributeFixed('mand_attr', optional=False)) template.add(opt_node) cfg_node = cfg.ConfigNode('root', node_type='CN') cfg_node.add(cfg.ConfigNode('some_node', attributes={'attr1': 'val1'})) res = template.validate(cfg_node) res.print_fmt() assert res.list_nodes() == ['some_node']
def test_validate_varnode_empty(): template = cfg.TemplateNodeVariableAttr( 'varnode', cfg.TemplateAttributeVariable(validator=lambda x: x in ['YES', 'NO']), optional=False) with nose.tools.assert_raises_regexp(ValueError, 'Node /varnode cannot be empty') as e: template.validate(cfg.ConfigNode('varnode'))
def test_validate_missing_mandatory_attr(): template = cfg.TemplateNodeFixed('node1') template.add(cfg.TemplateAttributeFixed('attr', optional=False)) with nose.tools.assert_raises_regexp( ValueError, 'Mandatory parameter /node1/attr has not been set, and has no default value' ) as e: template.validate(cfg.ConfigNode('node1'))
def test_validate_json_value_type_ok(): # validation for attribute of json value type template = cfg.TemplateNodeFixed('root', optional=False) json_node = cfg.TemplateNodeFixed('json_node', optional=False) json_node.add( cfg.TemplateAttributeFixed( 'json_attr', optional=False, value_type=str, validator=lambda x: json.loads(x)['a'] == 1)) template.add(json_node) cfg_node = cfg.ConfigNode('root', node_type='CN') cfg_node.add( cfg.ConfigNode('json_node', attributes={'json_attr': json.dumps({ 'a': 1, 'b': 2 })})) cfg_node.print_fmt() res = template.validate(cfg_node) res.print_fmt() assert json.loads(res.get('/json_node/json_attr'))['b'] == 2
def test_validate_varnode_validator_fail(): template = cfg.TemplateNodeVariableAttr( 'varnode', cfg.TemplateAttributeVariable(validator=lambda x: x in ['YES', 'NO'])) with nose.tools.assert_raises_regexp( ValueError, 'Parameter /varnode/attr2 failed validation for value yes') as e: template.validate( cfg.ConfigNode('varnode', attributes={ 'attr1': 'YES', 'attr2': 'yes' }))
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)