def test_core_behaviour_reverse(self): '''Test ability of the core to solve a large graph in reverse mode''' # Define Group 1 grp1 = ServiceGroup('GRP1') grp1.algo_reversed = True grp1_i1 = Service('I1') grp1_i1.algo_reversed = True grp1_i2 = Service('I2') grp1_i2.algo_reversed = True grp1_i3 = Service('I3') grp1_i3.algo_reversed = True grp1_i1.add_action(Action('stop', command='/bin/true')) grp1_i2.add_action(Action('stop', command='/bin/true')) grp1_i3.add_action(Action('stop', command='/bin/true')) grp1.add_inter_dep(target=grp1_i1) grp1.add_inter_dep(base=grp1_i1, target=grp1_i2) grp1.add_inter_dep(target=grp1_i3) # Define Group 2 grp2 = ServiceGroup('GRP2') grp2.algo_reversed = True grp2_i1 = Service('I1') grp2_i1.algo_reversed = True grp2_i2 = Service('I2') grp2_i2.algo_reversed = True grp2_i1.add_action(Action('stop', command='/bin/true')) grp2_i2.add_action(Action('stop', command='/bin/true')) grp2.add_inter_dep(target=grp2_i1) grp2.add_inter_dep(base=grp2_i1, target=grp2_i2) # Define Group init svc1 = Service('S1') svc1.algo_reversed = True svc1.add_action(Action('stop', command='/bin/true')) svc2 = Service('S2') svc2.algo_reversed = True svc2.add_action(Action('stop', command='/bin/true')) svc3 = Service('S3') svc3.algo_reversed = True svc3.add_action(Action('stop', command='/bin/true')) group_init = ServiceGroup('GROUP_INIT') group_init.algo_reversed = True group_init.add_inter_dep(target=svc1) group_init.add_inter_dep(base=svc1, target=svc2, sgth=REQUIRE_WEAK) group_init.add_inter_dep(base=svc1, target=grp1) group_init.add_inter_dep(base=svc2, target=svc3) group_init.add_inter_dep(base=grp1, target=svc3) group_init.add_inter_dep(base=svc3, target=grp2) # Solve the graph group_init.run('stop') # Assertions self.assertEqual(grp2.status, DONE) self.assertEqual(svc3.status, DONE) self.assertEqual(svc2.status, DONE) self.assertEqual(grp1.status, DONE) self.assertEqual(svc1.status, DONE) self.assertEqual(group_init.status, DONE)
def test_graph_in_graph(self): """Test the DOT graph output with a servicegroup within a servicegroup""" grp = ServiceGroup('Group') subgrp = ServiceGroup('subGroup') grp.add_inter_dep(subgrp) self.assertEqual( grp.graph(), 'subgraph "cluster_Group" {\nlabel="Group";\nstyle=rounded;\nnode [style=filled];\n"Group.__hook" [style=invis];\nsubgraph "cluster_Group.subGroup" {\nlabel="Group.subGroup";\nstyle=rounded;\nnode [style=filled];\n"Group.subGroup.__hook" [style=invis];\n}\n}\n' )
def test_graph_entity(self): """Test the DOT graph output""" grp = ServiceGroup('Group') self.assertEqual( grp.graph(), 'subgraph "cluster_Group" {\nlabel="Group";\nstyle=rounded;\nnode [style=filled];\n"Group.__hook" [style=invis];\n}\n' )
def test_has_subservice(self): '''Test whether a service is an internal dependency of a group''' group = ServiceGroup('group') serv = Service('intern_service') self.assertFalse(group.has_subservice(serv.name)) group.add_inter_dep(target=serv) self.assertTrue(group.has_subservice(serv.name))
def test_remove_inter_dep(self): '''Test ability to remove a dependency in a subgraph''' group = ServiceGroup('GROUP') s1 = Service('alpha') s2 = Service('beta') s3 = Service('lambda') group.add_inter_dep(target=s1) group.add_inter_dep(target=s2) group.add_inter_dep(target=s3) group.add_inter_dep(base=s1, target=s3) group.add_inter_dep(base=s2, target=s3) group.remove_inter_dep('lambda') self.assertTrue(s1.has_parent_dep('sink')) self.assertTrue(s2.has_parent_dep('sink')) self.assertTrue(s1.has_child_dep('source')) self.assertTrue(s2.has_child_dep('source')) self.assertFalse(s1.has_parent_dep('lambda')) self.assertFalse(s2.has_parent_dep('lambda')) group.remove_inter_dep('alpha') self.assertFalse(group._source.has_parent_dep('alpha')) self.assertTrue(group._source.has_parent_dep('beta')) self.assertFalse(group._sink.has_child_dep('alpha')) self.assertTrue(group._sink.has_child_dep('beta')) group.remove_inter_dep('beta') self.assertFalse(group._source.parents) self.assertFalse(group._sink.children)
def test_prepare_group_subservices(self): '''Test prepare group with multiple internal dependencies.''' group = ServiceGroup('GROUP') ac_suc1 = Action('start', command='/bin/true') ac_suc2 = Action('start', command='/bin/true') ac_suc3 = Action('start', command='/bin/true') subserv_a = Service('SUB1') subserv_b = Service('SUB2') subserv_c = Service('SUB3') subserv_a.add_action(ac_suc1) subserv_b.add_action(ac_suc2) subserv_c.add_action(ac_suc3) group.add_inter_dep(target=subserv_a) group.add_inter_dep(target=subserv_b) group.add_inter_dep(base=subserv_a, target=subserv_c) group.add_inter_dep(base=subserv_b, target=subserv_c) group.run('start') self.assertEqual(group.status, DONE) self.assertEqual(subserv_a.status, DONE) self.assertEqual(subserv_b.status, DONE) self.assertEqual(subserv_c.status, DONE)
def setUp(self): ''' Set up the graph of services within the service manager Graph _ start group --> service / `- stop ''' CLICommon.setUp(self) # ServiceGroup group = ServiceGroup('ServiceGroup') # Service self.service = service = Service('service') service.desc = 'I am the service' # Actions start_action = Action('start', command='/bin/true') stop_action = Action('stop', command='/bin/false') self.timeout_action = Action('timeout', command='sleep 1', timeout=0.1) start_action.inherits_from(service) stop_action.inherits_from(service) service.add_action(start_action) service.add_action(stop_action) service.add_action(self.timeout_action) # Build graph group.add_inter_dep(target=service) # Register services within the manager self.manager.register_services(group, service)
def test_inheritance(self): '''Test properties inherited from ServiceGroup to Service and Action''' sergrp = ServiceGroup('groupinit') sergrp.fromdict({ 'services': { 'svcA': { 'require': ['subgroup'], 'actions': { 'start': { 'cmd': '/bin/True' }, 'stop': { 'cmd': '/bin/False' } }, 'desc': 'I am the subservice $NAME' }, 'subgroup': { 'services': { 'svcB': { 'require_weak': ['svcC'], 'actions': { 'start': { 'cmd': '/bin/True' }, ' stop': { 'cmd': '/bin/False' } }, 'desc': 'I am the subservice $NAME' }, 'svcC': { 'actions': { 'start': { 'cmd': '/bin/True' }, 'stop': { 'cmd': '/bin/False' } }, 'desc': 'I am the subservice $NAME' } }, 'target': '127.0.0.1', 'desc': "I'm the service $NAME" } }, 'desc': 'I am a group', 'target': 'localhost', }) self.assertEqual(sergrp._subservices['svcA'].target, NodeSet('localhost')) self.assertEqual(sergrp._subservices['subgroup'].target, NodeSet('127.0.0.1')) subgroup = sergrp._subservices['subgroup'] self.assertEqual(subgroup._subservices['svcB'].target, NodeSet('127.0.0.1')) self.assertEqual(subgroup._subservices['svcC'].target, NodeSet('127.0.0.1'))
def test_missing_group(self): """A group with only MISSING services should be MISSING""" grp = ServiceGroup('group') svc1 = Service('svc1') svc1.add_action(Action('stop', command='/bin/true')) grp.add_inter_dep(target=svc1) grp.run('start') self.assertEqual(grp.status, MISSING)
def test_update_target(self): '''Test update of the target of a group of services''' grp = ServiceGroup('G') srva = Service('A') grp.add_inter_dep(target=srva) grp.update_target('fortoy[5-10]') self.assertTrue(grp.target == NodeSet('fortoy[5-10]')) self.assertTrue(srva.target == NodeSet('fortoy[5-10]'))
def test_create_service_imbrications(self): '''Test create service with mutliple level of subservices''' sergrp = ServiceGroup('groupinit') sergrp.fromdict({ 'services': { 'svcA': { 'require': ['subgroup'], 'actions': { 'start': { 'cmd': '/bin/True' }, 'stop': { 'cmd': '/bin/False' } }, 'desc': 'I am the subservice $NAME' }, 'subgroup': { 'services': { 'svcB': { 'require_weak': ['svcC'], 'actions': { 'start': { 'cmd': '/bin/True' }, ' stop': { 'cmd': '/bin/False' } }, 'desc': 'I am the subservice $NAME' }, 'svcC': { 'actions': { 'start': { 'cmd': '/bin/True' }, 'stop': { 'cmd': '/bin/False' } }, 'desc': 'I am the subservice $NAME' } }, 'target': '127.0.0.1', 'desc': "I'm the service $NAME" } }, 'desc': 'I am a group', 'target': 'localhost', }) for subservice in ('svcA', 'subgroup'): if isinstance(sergrp._subservices[subservice], ServiceGroup): for subsubser in ('svcB', 'svcC'): self.assertTrue( sergrp._subservices[subservice].has_subservice( subsubser)) self.assertTrue(sergrp.has_subservice(subservice))
def test_prepare_group_subservice(self): '''Test prepare group with an internal dependency.''' group = ServiceGroup('GROUP') subserv = Service('SUB1') subserv.add_action(Action('start', command='/bin/true')) group.add_inter_dep(target=subserv) group.run('start') self.assertEqual(group.status, DONE) self.assertEqual(subserv.status, DONE)
def test_add_bad_inter_dep(self): """Adding a subservice on a bad base raises an Exception""" grp = ServiceGroup('grp') srv1 = Service('foo1') srv2 = Service('foo2') self.assertRaises(ServiceNotFoundError, grp.add_inter_dep, target=srv2, base=srv1)
def test_subservices_with_different_actions(self): '''Test a service group with subservices with different actions''' sergrp = ServiceGroup('group1') sergrp.fromdict({ 'services': { 'svc1': { 'actions': { 'start': { 'cmd': '/bin/True' }, 'status': { 'cmd': '/bin/True' }, 'stop': { 'cmd': '/bin/True' }, } }, 'svc2': { 'require': ['svc1'], 'actions': { 'start': { 'cmd': '/bin/True' }, 'stop': { 'cmd': '/bin/True' }, 'status': { 'cmd': '/bin/True' }, } }, 'svc3': { 'require': ['svc1'], 'actions': { 'start': { 'cmd': '/bin/True' }, 'stop': { 'cmd': '/bin/True' }, 'status': { 'cmd': '/bin/True' }, } }, } }) self.assertEqual(len(sergrp._subservices), 3) self.assertTrue(sergrp.has_subservice('svc1')) self.assertTrue(sergrp.has_subservice('svc2')) self.assertTrue(sergrp.has_subservice('svc3')) self.assertEqual(len(sergrp._subservices['svc1']._actions), 3) self.assertEqual(len(sergrp._subservices['svc2']._actions), 3) self.assertEqual(len(sergrp._subservices['svc3']._actions), 3)
def test_prepare_empty_group_external_deps(self): '''Test prepare an empty group with a single external dependency.''' group = ServiceGroup('GROUP') ext_serv = Service('EXT_SERV') ac_suc = Action('start', command='/bin/true') ext_serv.add_action(ac_suc) group.add_dep(ext_serv) group.run('start') self.assertEqual(ext_serv.status, DONE) self.assertEqual(group.status, MISSING)
def test_set_algo_reversed(self): '''Test updates dependencies in changing the reversed flag''' group = ServiceGroup('group') self.assertTrue(group._source.has_child_dep('group')) self.assertFalse(group._sink.has_parent_dep('group')) group.algo_reversed = True self.assertFalse(group._source.has_child_dep('group')) self.assertTrue(group._sink.has_parent_dep('group')) group.algo_reversed = False self.assertTrue(group._source.has_child_dep('group')) self.assertFalse(group._sink.has_parent_dep('group'))
def test_graph(self): """Test the DOT output of a dependency""" p_service = BaseEntity("PARENT") c_service = BaseEntity("CHILD") dep = Dependency(c_service, REQUIRE) #self.assertEqual(dep.graph(p_service), '"CHILD" -> "PARENT";\n') self.assertEqual(dep.graph(p_service), '"PARENT" -> "CHILD";\n') p_group = ServiceGroup('P_Group') c_group = ServiceGroup('C_Group') p_dep = Dependency(p_group) c_dep = Dependency(c_group) self.assertEqual( c_dep.graph(p_group), '"P_Group.__hook" -> "C_Group.__hook" ' '[ltail="cluster_P_Group",lhead="cluster_C_Group"];\n') self.assertEqual( c_dep.graph(p_service), '"PARENT" -> "C_Group.__hook" ' '[lhead="cluster_C_Group"];\n') self.assertEqual( dep.graph(p_group), '"P_Group.__hook" -> "CHILD" ' '[ltail="cluster_P_Group"];\n')
def test_skipped_group(self): """A group with only SKIPPED services should be SKIPPED""" grp = ServiceGroup('group') svc1 = Service('svc1') svc1.add_action(Action('start', target="@NOTHING", command=':')) svc2 = Service('svc2') svc2.add_action(Action('start', target="@NOTHING", command=':')) grp.add_inter_dep(target=svc1) grp.add_inter_dep(target=svc2) grp.run('start') self.assertEqual(grp.status, SKIPPED)
def setUp(self): ''' Set up the graph of services within the service manager Graph __ S2 __ I1 S1 / -- G1 -- (src) / ^ -- (sink) `-- S3 --/ `-- I2 Each node has an action start and an action stop ''' CLICommon.setUp(self) svc1 = Service('S1') svc1.desc = 'I am the service S1' self.svc2 = svc2 = Service('S2') svc2.desc = 'I am the service S2' svc3 = Service('S3') svc3.desc = 'I am the service S3' group1 = ServiceGroup('G1') inter1 = Service('I1') inter1.desc = 'I am the service I1' inter2 = Service('I2') inter2.desc = 'I am the service I2' # Actions S1 start_svc1 = Action('start', HOSTNAME + ', BADNODE', '/bin/true') start_svc1.delay = 1 stop_svc1 = Action('stop', HOSTNAME + ',BADNODE', '/bin/true') stop_svc1.delay = 1 svc1.add_actions(start_svc1, stop_svc1) # Actions S2 svc2.add_action(Action('start', HOSTNAME + ',BADNODE', '/bin/true')) svc2.add_action(Action('stop', HOSTNAME + ',BADNODE', '/bin/true')) # Actions S3 svc3.add_action(Action('start', HOSTNAME + ',BADNODE', '/bin/false')) svc3.add_action(Action('stop', HOSTNAME + ',BADNODE', '/bin/false')) # Actions I1 inter1.add_action(Action('start', HOSTNAME, 'echo ok')) inter1.add_action(Action('stop', HOSTNAME, 'echo ok')) # Actions I2 inter2.add_action(Action('start', HOSTNAME + ',BADNODE', '/bin/true')) inter2.add_action(Action('stop', HOSTNAME + ',BADNODE', '/bin/true')) # Build graph svc1.add_dep(target=svc2) svc1.add_dep(target=svc3) svc3.add_dep(target=group1) inter2.add_dep(inter1) group1.add_inter_dep(target=inter1) group1.add_inter_dep(target=inter2) # Register services within the manager self.manager.register_services(svc1, svc2, svc3, group1)
def test_eval_deps_status_error(self): '''Test the method eval_deps_status DEP_ERROR''' group = ServiceGroup('group') e1 = Service('E1') e2 = Service('E2') e1.status = DEP_ERROR group.add_dep(target=e1) group.add_dep(target=e2) group.add_inter_dep(target=Service('I1')) self.assertEqual(group.eval_deps_status(), DEP_ERROR) self.assertEqual(group.eval_deps_status(), DEP_ERROR)
def test_eval_deps_status_done(self): '''Test the method eval_deps_status NO_STATUS''' group = ServiceGroup('group') e1 = Service('E1') e2 = Service('E2') group.add_dep(target=e1) group.add_dep(target=e2) group.add_inter_dep(target=Service('I1')) self.assertEqual(group.eval_deps_status(), NO_STATUS) e1.status = DONE e2.status = DONE self.assertEqual(group.eval_deps_status(), NO_STATUS)
def test_eval_deps_status_ws(self): '''Test the method eval_deps_status WAITING_STATUS''' group = ServiceGroup('group') ext1 = Service('E1') ext2 = Service('E2') ext1.status = DONE ext2.status = WARNING group.add_dep(target=ext1) group.add_dep(target=ext2) int1 = Service('E1') group.add_inter_dep(target=int1) int1.status = WAITING_STATUS self.assertEqual(group.eval_deps_status(), WAITING_STATUS)
def test_graph(self): '''Test DOT graph output''' manager = service_manager_self() sergrp = ServiceGroup('S1') sergrp.fromdict( {'services': {'srv1': {'target': 'localhost', 'actions': {'start': {'cmd':'/bin/True'}, 'stop': {'cmd': '/bin/False'}}, 'desc': "I'm the service srv1" }, 'subgroup': {'services': {'svcB': {'require_weak':['svcC'], 'actions': {'start': {'cmd': '/bin/True'}, ' stop': {'cmd': '/bin/False'}}, 'desc': 'I am the subservice $NAME'}, 'svcC': {'actions': {'start': {'cmd': '/bin/True'}, 'stop': {'cmd': '/bin/False'}}, 'desc': 'I am the subservice $NAME'}}, 'target': '127.0.0.1', 'desc': "I'm the service $NAME"}}, }) manager.register_services(sergrp) self.assertEqual(manager.output_graph(), """digraph dependency { compound=true; node [style=filled]; subgraph "cluster_S1" { label="S1"; style=rounded; node [style=filled]; "S1.__hook" [style=invis]; "S1.srv1"; subgraph "cluster_S1.subgroup" { label="S1.subgroup"; style=rounded; node [style=filled]; "S1.subgroup.__hook" [style=invis]; "S1.subgroup.svcB" -> "S1.subgroup.svcC" [style=dashed]; "S1.subgroup.svcC"; } } } """)
def test_group_with_weak_dep_error(self): """A group with a weak dep error runs fine (add_inter_dep()).""" dep1 = Service('dep1') dep1.add_action(Action('stop', command='/bin/false')) grp = ServiceGroup('group') svc = Service('svc') svc.add_action(Action('stop', command='/bin/true')) grp.add_inter_dep(svc) grp.add_dep(dep1, sgth=REQUIRE_WEAK) grp.run('stop') self.assertEqual(grp.status, DONE)
def test_run_stop_on_group(self): '''Test stop algorithm on a group''' group = ServiceGroup('G1') i1 = Service('I1') i1.add_action(Action('stop', command='/bin/true')) group.add_inter_dep(target=i1) s1 = Service('S1') s1.add_action(Action('stop', command='/bin/true')) s1.add_dep(target=group) s1.algo_reversed = True group.algo_reversed = True group.run('stop') self.assertEqual(s1.status, DONE) self.assertEqual(i1.status, DONE) self.assertEqual(group.status, DONE)
def test_fromdict1(self): '''Test instanciation of a service group from a dictionnary''' sergrp = ServiceGroup('S1') sergrp.fromdict({ 'services': { 'hpss_nfs': { 'target': 'localhost', 'actions': { 'start': { 'cmd': '/bin/True' }, 'stop': { 'cmd': '/bin/False' } }, 'desc': "I'm the service hpss_nfs" }, 'lustre': { 'target': 'localhost', 'actions': { 'start': { 'cmd': '/bin/True' }, 'stop': { 'cmd': '/bin/False' } }, 'desc': "I'm the service lustre" } }, 'desc': "I'm the service S1", 'target': 'localhost', 'variables': { 'var1': 'toto', 'var2': 'titi' }, }) self.assertEqual(len(sergrp.variables), 2) self.assertTrue('var1' in sergrp.variables) self.assertTrue('var2' in sergrp.variables) self.assertTrue(sergrp.has_subservice('hpss_nfs')) self.assertTrue(sergrp.has_subservice('lustre')) self.assertTrue(sergrp._subservices['hpss_nfs'].has_parent_dep('sink')) self.assertTrue( sergrp._subservices['hpss_nfs'].has_child_dep('source')) self.assertTrue(sergrp._subservices['lustre'].has_parent_dep('sink')) self.assertTrue(sergrp._subservices['lustre'].has_child_dep('source'))
def test_skipped_group_dep_error(self): """A full SKIPPED service group with DEP_ERROR should be SKIPPED""" svc = Service('error') svc.add_action(Action('start', command='/bin/false')) grp = ServiceGroup('group') svc1 = Service('svc1') svc1.add_action(Action('start', target="@NOTHING", command=':')) svc2 = Service('svc2') svc2.add_action(Action('start', target="@NOTHING", command=':')) grp.add_inter_dep(target=svc1) grp.add_inter_dep(target=svc2) grp.add_dep(svc, sgth=REQUIRE) grp.run('start') self.assertEqual(svc.status, ERROR) self.assertEqual(grp.status, SKIPPED)
def test_missing_dep_raises_error(self): """Require a missing deps raises UnknownDependencyError""" grp = ServiceGroup('group') self.assertRaises( UnknownDependencyError, grp.fromdict, { 'services': { 'svc1': { 'require': ['bad'], 'actions': { 'stop': { 'cmd': "/bin/true" }, } } } })
def test_non_existing_target(self): '''Test expected behaviour on an non-existing target''' sergrp = ServiceGroup('group1') sergrp.fromdict({ 'services': { 'svc1': { 'actions': { 'start': { 'cmd': '/bin/True' }, }, 'target': '@none', } } }) self.assertEqual(sergrp._subservices['svc1'].target, NodeSet())
def test_reset_service_group(self): '''Test the ability to reset values of a service group''' group = ServiceGroup('GROUP') ser1 = Service('I1') action = Action(name='start', delay=3) action.maxretry = 5 action.tries = 3 action.status = DONE ser1.add_action(action) ser1.status = ERROR group.add_inter_dep(target=ser1) group.status = DEP_ERROR group.reset() self.assertEqual(group.status, NO_STATUS) self.assertEqual(ser1.status, NO_STATUS) self.assertEqual(action.status, NO_STATUS) self.assertEqual(action.tries, 0)