def test_fromdict2(self): ''' Test instanciate a service from a dictionnary with dependant actions ''' ser = Service('S1') ser.fromdict({ 'desc': 'I am the service S1', 'target': 'localhost', 'actions': { 'start': { 'check': ['status'], 'cmd': '/bin/True' }, 'stop': { 'cmd': '/bin/True' }, 'status': { 'cmd': '/bin/True' } } }) self.assertTrue(ser) self.assertEqual(len(ser._actions), 3) self.assertTrue('start' in ser._actions) self.assertTrue('stop' in ser._actions) self.assertTrue('status' in ser._actions) self.assertTrue(ser._actions['start'].has_parent_dep('status'))
def test_fromdict2(self): ''' Test instanciate a service from a dictionnary with dependant actions ''' ser = Service('S1') ser.fromdict( { 'desc': 'I am the service S1', 'target': 'localhost', 'actions': { 'start': { 'check': ['status'], 'cmd': '/bin/True' }, 'stop': {'cmd': '/bin/True'}, 'status': {'cmd': '/bin/True'} } } ) self.assertTrue(ser) self.assertEqual(len(ser._actions), 3) self.assertTrue('start' in ser._actions) self.assertTrue('stop' in ser._actions) self.assertTrue('status' in ser._actions) self.assertTrue(ser._actions['start'].has_parent_dep('status'))
def test_fromdict1(self): '''Test instanciate a service from a dictionnary''' ser = Service('S1') ser.fromdict({ 'desc': 'I am the service S1', 'target': 'localhost', 'variables': { 'var1': 'toto', 'var2': 'titi' }, 'actions': { 'start': { 'cmd': '/bin/True' }, 'stop': { 'cmd': '/bin/True' } } }) self.assertTrue(ser) self.assertEqual(ser.name, 'S1') self.assertEqual(ser.desc, 'I am the service S1') self.assertEqual(ser.target, NodeSet('localhost')) self.assertEqual(len(ser.variables), 2) self.assertTrue('var1' in ser.variables) self.assertTrue('var2' in ser.variables)
def test_fromdict1(self): '''Test instanciate a service from a dictionnary''' ser = Service('S1') ser.fromdict( { 'desc': 'I am the service S1', 'target': 'localhost', 'variables':{ 'var1': 'toto', 'var2': 'titi' }, 'actions': { 'start': {'cmd': '/bin/True'}, 'stop': {'cmd': '/bin/True'} } } ) self.assertTrue(ser) self.assertEqual(ser.name, 'S1') self.assertEqual(ser.desc, 'I am the service S1') self.assertEqual(ser.target, NodeSet('localhost')) self.assertEqual(len(ser.variables), 2) self.assertTrue('var1' in ser.variables) self.assertTrue('var2' in ser.variables)
def test_resolve_all(self): """Test variable resolution in resolve_all()""" srv = Service('svc1') srv.add_var('label', "I am a service") srv.fromdict({ 'desc': "%label", 'actions': { 'start': { 'cmd': 'service foo %ACTION' }, } }) srv.resolve_all() self.assertEqual(srv.desc, "I am a service") self.assertEqual(srv._actions['start'].command, "service foo start")
def test_tagged_run(self): """Test that services without the configuration tags are skipped""" manager = ServiceManager() srv = Service('service') srv.fromdict({ 'target': 'localhost', 'tags': ['foo'], 'desc': "I'm a service", 'actions': {'start': {'cmd': '/bin/true'}}, }) manager.add_service(srv) manager._apply_config({'tags': set(['foo'])}) self.assertFalse(srv.to_skip('start')) manager._apply_config({'tags': set(['bar'])}) self.assertTrue(srv.to_skip('start'))
def test_resolve_all(self): """Test variable resolution in resolve_all()""" srv = Service('svc1') srv.add_var('label', "I am a service") srv.fromdict({ 'desc': "%label", 'actions': { 'start': { 'cmd': 'service foo %ACTION' }, } }) srv.resolve_all() self.assertEqual(srv.desc, "I am a service") self.assertEqual(srv._actions['start'].command, "service foo start")
def test_service_with_actions_with_one_decl(self): """create a service with two actions with comma declaration""" svc = Service('foo') svc.fromdict( {'actions': { 'start,stop': { 'cmd': 'service foo %ACTION' }, }}) self.assertTrue(svc) self.assertEqual(len(svc._actions), 2) self.assertTrue('start' in svc._actions) self.assertTrue('stop' in svc._actions) self.assertEqual(svc._actions['start'].command, 'service foo %ACTION') self.assertEqual(svc._actions['stop'].command, 'service foo %ACTION')
def test_delay_to_action(self): """ test if the delay defined in the service dict is correctly given to the action """ svc = Service('foo') svc.fromdict({ 'name': 'foo', 'delay': 1, 'actions': { 'wait': { 'cmd': 'service wait %ACTION' }, } }) self.assertEqual(svc._actions['wait'].delay, 1)
def test_resolve_target_from_parent(self): """resolve action target using variable declared in parent service""" # 'target' property is resolved very early and not in resolve_all() data = { 'variables': { 'targets': 'foo' }, 'actions': { 'start': { 'target': '%targets', 'cmd': '/bin/true', } } } svc = Service('svc') svc.fromdict(data) self.assertEqual(str(svc._actions['start'].target), 'foo')
def test_resolve_target_from_parent(self): """resolve action target using variable declared in parent service""" # 'target' property is resolved very early and not in resolve_all() data = { 'variables': { 'targets': 'foo' }, 'actions': { 'start': { 'target': '%targets', 'cmd': '/bin/true', } } } svc = Service('svc') svc.fromdict(data) self.assertEqual(str(svc._actions['start'].target), 'foo')
def test_service_with_nodeset_like_actions_with_one_decl(self): """create a service with two actions with nodeset-like declaration""" svc = Service('foo') svc.fromdict({ 'name': 'foo', 'actions': { 'foo[1-2]': { 'cmd': 'service foo %ACTION' }, } }) self.assertTrue(svc) self.assertEqual(len(svc._actions), 2) self.assertTrue('foo1' in svc._actions) self.assertTrue('foo2' in svc._actions) self.assertEqual(svc._actions['foo1'].command, 'service foo %ACTION') self.assertEqual(svc._actions['foo2'].command, 'service foo %ACTION')
def test_delay_to_action(self): """ test if the delay defined in the service dict is correctly given to the action """ svc = Service('foo') svc.fromdict( { 'name': 'foo', 'delay': 1, 'actions': { 'wait': { 'cmd': 'service wait %ACTION' }, } } ) self.assertEqual(svc._actions['wait'].delay, 1)
def test_service_with_actions_with_one_decl(self): """create a service with two actions with comma declaration""" svc = Service('foo') svc.fromdict( { 'actions': { 'start,stop': { 'cmd': 'service foo %ACTION' }, } } ) self.assertTrue(svc) self.assertEqual(len(svc._actions), 2) self.assertTrue('start' in svc._actions) self.assertTrue('stop' in svc._actions) self.assertEqual(svc._actions['start'].command, 'service foo %ACTION') self.assertEqual(svc._actions['stop'].command, 'service foo %ACTION')
def test_service_with_nodeset_like_actions_with_one_decl(self): """create a service with two actions with nodeset-like declaration""" svc = Service('foo') svc.fromdict( { 'name': 'foo', 'actions': { 'foo[1-2]': { 'cmd': 'service foo %ACTION' }, } } ) self.assertTrue(svc) self.assertEqual(len(svc._actions), 2) self.assertTrue('foo1' in svc._actions) self.assertTrue('foo2' in svc._actions) self.assertEqual(svc._actions['foo1'].command, 'service foo %ACTION') self.assertEqual(svc._actions['foo2'].command, 'service foo %ACTION')
def _build_services(self): ''' Instanciate services, variables and service group. This methods also populate the service manager. ''' # Get back the manager manager = service_manager_self() dependencies = {} # We need to parse variables first to be sure that variables used # in services are already parsed (see #15) for data in self._flow: # Parse variables if 'variables' in data: for varname, value in data['variables'].items(): if varname not in manager.variables: manager.add_var(varname, value) # Go through data registred within flow for data in self._flow: for elem, subelems in data.items(): # Parse service if elem == 'service' and 'actions' in subelems: ser = Service(subelems['name']) ser.fromdict(subelems) wrap = self._parse_deps(subelems) wrap.source = ser dependencies[ser.name] = wrap # Parse service group elif elem == 'service' and 'services' in subelems: ser = ServiceGroup(subelems['name']) ser.fromdict(subelems) wrap = self._parse_deps(subelems) wrap.source = ser dependencies[ser.name] = wrap # Support for new style syntax to declare services # This is a simple mode, for compatibility, with old-style # syntax. elif elem == 'services': for names, props in subelems.items(): for svcname in NodeSet(names): service = None if 'services' in props: service = ServiceGroup(svcname) else: service = Service(svcname) service.fromdict(props) wrap = self._parse_deps(props) wrap.source = service dependencies[service.name] = wrap elif elem != 'variables': raise ConfigurationError("Bad rule '%s'" % elem) # Build relations between services for wrap in dependencies.values(): for (dtype, values) in wrap.deps.items(): for dep in values: if dep not in dependencies: raise UnknownDependencyError(dep) wrap.source.add_dep( target=dependencies[dep].source, sgth=dtype.upper()) # Populate the manager and set up inheritance for wrap in dependencies.values(): manager.register_service(wrap.source)
def _build_services(self): ''' Instanciate services, variables and service group. This methods also populate the service manager. ''' # Get back the manager manager = service_manager_self() dependencies = {} # Go through data registred within flow for data in self._flow: for elem, subelems in data.items(): # Parse variables if elem == 'variables': for (varname, value) in subelems.items(): # Variables could have been already defined on command # line through defines, and they have priority. # If several 'variables' section are defined, the first # ones will have priority, and it is fine are there is # no ordering guarantee. if varname not in manager.variables: manager.add_var(varname, value) # Parse service elif elem == 'service' and 'actions' in subelems: ser = Service(subelems['name']) ser.fromdict(subelems) wrap = self._parse_deps(subelems) wrap.source = ser dependencies[ser.name] = wrap # Parse service group elif elem == 'service' and 'services' in subelems: ser = ServiceGroup(subelems['name']) ser.fromdict(subelems) wrap = self._parse_deps(subelems) wrap.source = ser dependencies[ser.name] = wrap # Support for new style syntax to declare services # This is a simple mode, for compatibility, with old-style # syntax. elif elem == 'services': for names, props in subelems.items(): for svcname in NodeSet(names): service = None if 'services' in props: service = ServiceGroup(svcname) else: service = Service(svcname) service.fromdict(props) wrap = self._parse_deps(props) wrap.source = service dependencies[service.name] = wrap else: raise ConfigurationError("Bad rule '%s'" % elem) # Build relations between services for wrap in dependencies.values(): for (dtype, values) in wrap.deps.items(): for dep in values: if dep not in dependencies: raise UnknownDependencyError(dep) wrap.source.add_dep(target=dependencies[dep].source, sgth=dtype.upper()) # Populate the manager and set up inheritance for wrap in dependencies.values(): manager.register_service(wrap.source)
def _build_services(self): ''' Instanciate services, variables and service group. This methods also populate the service manager. ''' # Get back the manager manager = service_manager_self() dependencies = {} # Go through data registred within flow for data in self._flow: for elem, subelems in data.items(): # Parse variables if elem == 'variables': for (varname, value) in subelems.items(): # Variables could have been already defined on command # line through defines, and they have priority. # If several 'variables' section are defined, the first # ones will have priority, and it is fine are there is # no ordering guarantee. if varname not in manager.variables: manager.add_var(varname, value) # Parse service elif elem == 'service' and 'actions' in subelems: ser = Service(subelems['name']) ser.fromdict(subelems) wrap = self._parse_deps(subelems) wrap.source = ser dependencies[ser.name] = wrap # Parse service group elif elem == 'service' and 'services' in subelems: ser = ServiceGroup(subelems['name']) ser.fromdict(subelems) wrap = self._parse_deps(subelems) wrap.source = ser dependencies[ser.name] = wrap # Support for new style syntax to declare services # This is a simple mode, for compatibility, with old-style # syntax. elif elem == 'services': for names, props in subelems.items(): for svcname in NodeSet(names): service = None if 'services' in props: service = ServiceGroup(svcname) else: service = Service(svcname) service.fromdict(props) wrap = self._parse_deps(props) wrap.source = service dependencies[service.name] = wrap else: raise ConfigurationError("Bad rule '%s'" % elem) # Build relations between services for wrap in dependencies.values(): for (dtype, values) in wrap.deps.items(): for dep in values: if dep not in dependencies: raise UnknownDependencyError(dep) wrap.source.add_dep( target=dependencies[dep].source, sgth=dtype.upper()) # Populate the manager and set up inheritance for wrap in dependencies.values(): manager.register_service(wrap.source)