def test_resolve_property5(self): '''Test resolution with a property containing special characters''' service = BaseEntity('test_service') service.add_var('NODES', HOSTNAME) service.target = '%NODES' self.assertEqual(service.resolve_property('target'), NodeSet(HOSTNAME))
def test_resolve_2_command_substitutions(self): '''Test 2 command substitutions''' service = BaseEntity('test_service') service.add_var('EXCLUDED_NODES', 'foo') service.add_var('SELECTED_NODES', 'bar') expr = '%([ -n "%SELECTED_NODES" ] && echo "-n %SELECTED_NODES")' + \ ' %([ -n "%EXCLUDED_NODES" ] && echo "-x %EXCLUDED_NODES")' self.assertEqual(service._resolve(expr), '-n bar -x foo')
def test_lookup_variables3(self): '''Test variables resolution with an undefined var''' service = BaseEntity('test_service') service.add_var('VAR', 'test') group = BaseEntity('group_service') group.add_var('GVAR', 'group') service.parent = group self.assertRaises(UndefinedVariableError, service._lookup_variable, 'BAD_VAR')
def test_lookup_variables2(self): '''Test variables resolution through multiple entities''' service = BaseEntity('test_service') service.add_var('VAR', 'test') group = BaseEntity('group_service') group.add_var('GVAR', 'group') service.parent = group self.assertEqual(service._lookup_variable('VAR'), 'test') self.assertEqual(service._lookup_variable('GVAR'), 'group')
def test_lookup_variables4(self): '''Test variables resolution with a var referencing a property''' service = BaseEntity('test_service') service.add_var('VAR', 'test') group = BaseEntity('group_service') group.add_var('GVAR', 'group') service.parent = group self.assertEqual(service._lookup_variable('GVAR'), 'group') self.assertEqual(service._lookup_variable('TARGET'), None) self.assertEqual(service._lookup_variable('NAME'), 'test_service')
def test_resolve_property1(self): '''Test replacement of symbols within a property''' service = BaseEntity('test_service') service.add_var('NODES', 'localhost,127.0.0.1') service.desc = 'start %NAME on %TARGET' service.target = '%NODES' self.assertEqual(service.resolve_property('target'), NodeSet('localhost,127.0.0.1')) self.assertEqual(service.resolve_property('name'), 'test_service') self.assertEqual(service.resolve_property('desc'), 'start test_service on 127.0.0.1,localhost')
def test_resolve_all_variables(self): """resolve_all() resolves variables only once""" entity = BaseEntity('entity') entity.add_var('foo', 'cool') entity.add_var('bar', '%foo') entity.add_var('baz', '%foo') entity.fromdict({'desc': 'New %bar'}) entity.resolve_all() self.assertEqual(entity.variables['foo'], entity.variables['bar']) self.assertEqual(entity.variables['foo'], entity.variables['baz']) self.assertEqual(entity.desc, 'New cool')
def test_remove_variable(self): '''Remove a variable, defined or not, is fine.''' svc = BaseEntity('test_var') svc.add_var('var', 'foo') self.assertEqual(svc._lookup_variable('var'), 'foo') # Remove it svc.remove_var('var') self.assertRaises(UndefinedVariableError, svc._lookup_variable, 'var') # Remove it again does not raise an exception. svc.remove_var('var') self.assertRaises(UndefinedVariableError, svc._lookup_variable, 'var')
def test_resolve_value3(self): '''Test multiple variable replacements''' service = BaseEntity('test_service') service.add_var('NODES', 'localhost,127.0.0.1') self.assertEqual(service._resolve('%NODES %NAME'), 'localhost,127.0.0.1 test_service')
def test_resolve_value5(self): '''Test combining resolution of variables and expressions''' service = BaseEntity('test_service') service.add_var('NODES', 'localhost,127.0.0.1') self.assertEqual(service._resolve('%NODES %(echo hello world) %NAME'), 'localhost,127.0.0.1 hello world test_service')
def test_resolve_value2(self): '''Test replacement of variable referenced in the entity''' service = BaseEntity('test_service') service.add_var('NODES', 'localhost,127.0.0.1') self.assertEqual(service._resolve('%NODES'), 'localhost,127.0.0.1')
def test_resolve_escape_char(self): '''Test resolution with a variable escaping %''' service = BaseEntity('test_service') service.add_var('FOO', 'Keep my %%noeval!') self.assertEqual(service._resolve('%FOO'), 'Keep my %noeval!')
def test_lookup_variables1(self): '''Test variables resolution through a single entity''' service = BaseEntity('test_service') service.add_var('VAR', 'test') self.assertEqual(service._lookup_variable('VAR'), 'test')
def test_resolve_value7(self): '''Test resolution of variable inside an expression (2)''' service = BaseEntity('test_service') service.add_var('CMD', 'echo') self.assertEqual(service._resolve('%(%CMD foo)'), 'foo')
def test_resolve_property4(self): '''Command substitution with a non-zero exit code should be ok''' service = BaseEntity('test_service') service.add_var('NODES', '%(/bin/false)') self.assertEqual(service._resolve('%NODES'), '')
def test_resolve_command_substitution(self): '''Test command substitution''' service = BaseEntity('test_service') service.add_var('EXCLUDED_NODES', 'foo') expr = '%([ -n "%EXCLUDED_NODES" ] && echo "-n %EXCLUDED_NODES")' self.assertEqual(service._resolve(expr), '-n foo')
def test_resolve_recurse(self): '''Test recursive variable resolution''' service = BaseEntity('test_service') service.add_var('foo', 'key') service.add_var('bar', 'Keep my %foo') self.assertEqual(service._resolve('%bar'), 'Keep my key')
def test_resolve_2_variables(self): '''Test resolution with two adjacent variables''' service = BaseEntity('test_service') service.add_var('FOO', 'foo') service.add_var('BAR', 'bar') self.assertEqual(service._resolve('%FOO%BAR'), 'foobar')
def test_resolve_all(self): """resolve_all() resolves all BaseEntity properties""" entity = BaseEntity('entity') entity.add_var('TGT', 'localhost') entity.add_var('NBR1', 1) entity.add_var('NBR2', 2) entity.add_var('NBR3', 3) entity.add_var('NBR4', 4) entity.add_var('NBR5', 5) entity.add_var('NBR6', 6) entity.add_var('KEYWORD', 'delegate') entity.add_var('TXT', 'I am the entity') entity.fromdict({ 'target': "%TGT", 'timeout': "%NBR1", 'retry': "%NBR2", 'fanout': "%NBR3", 'delay': "%NBR4", 'warnings': "%NBR5", 'errors': "%NBR6", 'desc': "%TXT", 'mode': "%KEYWORD" }) entity.resolve_all() self.assertEqual(entity.desc, "I am the entity") self.assertEqual(entity.target, NodeSet('localhost')) self.assertEqual(entity.timeout, 1) self.assertEqual(entity.maxretry, 2) self.assertEqual(entity.fanout, 3) self.assertEqual(entity.delay, 4) self.assertEqual(entity.errors, 6) self.assertEqual(entity.warnings, 5) self.assertEqual(entity.mode, 'delegate')
def test_resolve_list_value(self): """resolve a variable with a non-string value (list)""" service = BaseEntity('test_service') service.add_var('list', ['a', 'b']) self.assertEqual(service._resolve("%list"), ['a', 'b'])
def test_add_variable_twice(self): '''Add a variable twice raises VariableAlreadyExistError''' svc = BaseEntity('test_var') svc.add_var('var', 'foo') self.assertRaises(VariableAlreadyExistError, svc.add_var, 'var', 'foo')