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 -- service1 / -' _ start '-- service2 / ''' CLICommon.setUp(self) # Service service1 = Service('service1') service1.desc = 'I am the service 1' service2 = Service('service2') service2.desc = 'I am the service 2' # Actions action = Action('start', command='/bin/true') action.inherits_from(service1) service1.add_action(action) service2.add_dep(target=service1) action = Action('start', command='/bin/true') action.inherits_from(service2) service2.add_action(action) # Register services within the manager self.manager.register_services(service1, service2)
def test_remove_task(self): """Test the behaviour of the remove_task method""" task_manager = action_manager_self() task1 = Action('start') task1.fanout = 260 task2 = Action('stop') task2.fanout = 85 task3 = Action('status') task3.fanout = 85 task4 = Action('check') task4.fanout = 148 task_manager.add_task(task1) task_manager.add_task(task2) task_manager.add_task(task3) task_manager.add_task(task4) self.assertEqual(task_manager.fanout, 85) task_manager.remove_task(task2) self.assertEqual(task_manager.fanout, 85) task_manager.remove_task(task3) self.assertEqual(task_manager.fanout, 148) task_manager.remove_task(task4) self.assertEqual(task_manager.fanout, 260) task_manager.remove_task(task1) self.assertFalse(task_manager.fanout) self.assertEqual(task_manager.tasks_count, 0) self.assertEqual(task_manager.tasks_done_count, 4)
def test_prepare_with_multiple_require_errors(self): """Test multiple require dependencies errors at different levels.""" serv_base_error = Service('A') serv_ok_warnings = Service('B') serv_error = Service('C') serv_timeout = Service('D') ac_suc = Action(name='start', command='/bin/true') ac_suc2 = Action(name='start', command='/bin/true') ac_suc3 = Action(name='start', command='/bin/true') ac_tim = Action(name='start', command='sleep 3', timeout=0.3) serv_base_error.add_action(ac_suc) serv_ok_warnings.add_action(ac_suc2) serv_error.add_action(ac_suc3) serv_timeout.add_action(ac_tim) serv_base_error.add_dep(serv_ok_warnings) serv_base_error.add_dep(serv_error) serv_ok_warnings.add_dep(serv_timeout, REQUIRE_WEAK) serv_error.add_dep(serv_timeout) serv_base_error.run('start') self.assertEqual(serv_base_error.status, DEP_ERROR) self.assertEqual(serv_ok_warnings.status, DONE) self.assertEqual(serv_error.status, DEP_ERROR) self.assertEqual(serv_timeout.status, TIMEOUT)
def test_prepare_multiple_delay(self): '''Test prepare with dependencies and multiple delays''' serv = Service('BASE_DELAYED') serv_a = Service('A_NOT_DELAYED') serv_b = Service('B_DELAYED') serv_c = Service('C_DELAYED') act_a = Action(name='start', command='/bin/true') act_serv = Action(name='start', command='/bin/true', delay=0.3) act_b = Action(name='start', command='/bin/true', delay=0.3) act_c = Action(name='start', command='/bin/true', delay=0.5) serv.add_action(act_serv) serv_a.add_action(act_a) serv_b.add_action(act_b) serv_c.add_action(act_c) serv.add_dep(serv_a) serv.add_dep(serv_b) serv_a.add_dep(serv_c) serv_b.add_dep(serv_c) serv.run('start') self.assertEqual(serv.status, DONE) self.assert_near(0.3, 0.2, act_serv.duration) self.assertEqual(serv_a.status, DONE) self.assert_near(0.0, 0.2, act_a.duration) self.assertEqual(serv_b.status, DONE) self.assert_near(0.3, 0.2, act_b.duration) self.assertEqual(serv_c.status, DONE) self.assert_near(0.5, 0.2, act_c.duration)
def test_missing_action(self): """Test prepare with service with missing action is ok""" # Graph leaf has no 'status' action s1 = Service("1") s1.add_action(Action('start', command='/bin/true')) s1.add_action(Action('status', command='/bin/true')) s2 = Service("2") s2.add_action(Action('start', command='/bin/true')) s2.add_dep(s1) s2.run('status') self.assertEqual(s1.status, DONE) self.assertEqual(s2.status, MISSING) s1.reset() s2.reset() self.assertEqual(s1.status, NO_STATUS) self.assertEqual(s2.status, NO_STATUS) # 'status' action is propagated to leaf even if '2' has not the # requested action. s3 = Service("3") s3.add_action(Action('start', command='/bin/true')) s3.add_action(Action('status', command='/bin/true')) s3.add_dep(s2) s3.run('status') self.assertEqual(s1.status, DONE) self.assertEqual(s2.status, MISSING) self.assertEqual(s3.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_prepare_errors_same_level(self): """Test prepare behaviour with two errors at the same level""" serv = Service('BASE') serv_a = Service('DEP_A') serv_b = Service('DEP_B') serv_c = Service('DEP_C') ac_suc = Action(name='start', command='/bin/true') ac_suc2 = Action(name='start', command='/bin/true') ac_err = Action(name='start', command='/bin/false') ac_err2 = Action(name='start', command='dlvlfvlf') serv.add_action(ac_suc) serv_a.add_action(ac_suc2) serv_b.add_action(ac_err) serv_c.add_action(ac_err2) serv.add_dep(serv_a) serv_a.add_dep(serv_b) serv_a.add_dep(serv_c) serv.run('start') self.assertEqual(serv.status, DEP_ERROR) self.assertEqual(serv_a.status, DEP_ERROR) self.assertEqual(serv_b.status, ERROR) self.assertEqual(serv_c.status, ERROR)
def test_add_action(self): """Test add_action's behaviour.""" service = Service('brutus') service.add_action(Action('start')) self.assertTrue(service.has_action('start')) self.assertRaises(ActionAlreadyReferencedError, service.add_action, Action('start')) self.assertRaises(TypeError, service.add_action, None)
def test_add_actions(self): """Test the possibility to add multiple actions at the same time""" service = Service('SERV') act_a = Action('start', command='/bin/true') act_b = Action('stop', command='/bin/true') act_c = Action('status', command='/bin/true') service.add_actions(act_a, act_b, act_c) self.assertTrue(service.has_action('start')) self.assertTrue(service.has_action('stop')) self.assertTrue(service.has_action('status'))
def test_inheritance(self): '''Test inheritance between action and services''' ser1 = Service('parent') ser1.target = '127.0.0.1' ser2 = Service('inherited') ser2.add_action(Action('start')) ser2.add_action(Action('stop', "foo")) ser2.inherits_from(ser1) self.assertEqual(ser2.target, NodeSet('127.0.0.1')) self.assertEqual(ser2._actions['start'].target, NodeSet('127.0.0.1')) self.assertEqual(ser2._actions['stop'].target, NodeSet("foo"))
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 test_run_action_with_failed_subaction(self): """Test action running a failed sub action (start->status)""" serv = Service('BASE') act_start = Action('start', command='/bin/true') act_status_fail = Action('status', command='/bin/false') act_start.add_dep(target=act_status_fail) serv.add_actions(act_start, act_status_fail) serv.run('start') self.assertEqual(serv.status, DONE) self.assertTrue(act_start.duration) self.assertTrue(act_status_fail.duration)
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_schedule(self): """Test behaviour method schedule""" a1 = Action(name='start', command='/bin/true') a2 = Action(name='status', command='/bin/true', delay=1) ser = Service('TEST') ser.add_actions(a1, a2) a1.run() a2.run() self.assertTrue(0 < a1.duration and a1.duration <= 0.2, "%.3f is not between 0 and 0.2" % a1.duration) self.assertTrue(0.9 <= a2.duration and a2.duration <= 1.2, "%.3f is not between 0.9 and 1.2" % a2.duration)
def test_prepare_dep_failed(self): """Test prepare an action with a single failed dependency""" a1 = Action('start', command='/bin/true') a2 = Action('status', command='/bin/false') ser = Service('TEST') a1.add_dep(a2) ser.add_actions(a1, a2) a1.run() self.assertEqual(a1.status, DONE) self.assertTrue(a1.duration) self.assertEqual(a2.status, ERROR) self.assertTrue(a2.duration)
def test_filter_dep_one_error(self): """error nodes are propagated along 'filter' dependencies""" svc1 = Service('first') svc1.add_action(Action('start', command='false', target=HOSTNAME)) svc2 = Service('second') svc2.add_action(Action('start', command='true', target=HOSTNAME)) svc2.add_dep(svc1, sgth=FILTER) svc2.run('start') self.assertEqual(svc1.status, ERROR) self.assertEqual(svc2.status, SKIPPED)
def test_filter_mix_no_target(self): """service without target do not filter service with target""" svc1 = Service('top') svc1.add_action(Action('start', command='false')) svc2 = Service('bottom') svc2.add_action(Action('start', command='true', target='localhost')) svc2.add_dep(svc1, sgth=FILTER) svc2.run('start') self.assertEqual(svc1.status, ERROR) self.assertEqual(svc2.status, DONE)
def test_add_task_weird_values(self): """Test the method add task with task without fanout""" task_manager = action_manager_self() task1 = Action('start') task1.fanout = 60 task2 = Action('stop') task3 = Action('status') task3.fanout = 50 task_manager.add_task(task1) task_manager.add_task(task2) task_manager.add_task(task3) self.assertEqual(task_manager.fanout, 50) self.assertEqual(task_manager.tasks_count, 3)
def test_action_instanciation(self): """Test instanciation of an action.""" action = Action('start') self.assertNotEqual(action, None) self.assertEqual(action.name, 'start') action = Action(name='start', target=HOSTNAME, command='/bin/true') self.assertEqual(action.target, NodeSet(HOSTNAME)) self.assertEqual(action.command, '/bin/true') action = Action(name='start', command='/bin/true', timeout=10, delay=5) self.assertEqual(action.timeout, 10) self.assertEqual(action.delay, 5)
def test_filter_dep_no_error(self): """test FILTER dependency without error""" svc1 = Service('first') svc1.add_action(Action('start', command='/bin/true', target=HOSTNAME)) svc2 = Service('second') svc2.add_action(Action('start', command='/bin/true', target=HOSTNAME)) svc2.add_dep(svc1, sgth=FILTER) svc2.run('start') self.assertEqual(svc1.status, DONE) self.assertEqual(svc2.status, DONE)
def test_call_services_reversed(self): '''Test service_manager with custom reversed actions''' manager = service_manager_self() s1 = Service('S1') s2 = Service('S2') s1.add_action(Action('wait', command='/bin/true')) s2.add_action(Action('wait', command='/bin/true')) s1.add_dep(s2) manager.register_services(s1, s2) manager.call_services(['S1'], 'wait', conf={"reverse_actions": ['wait']}) self.assertTrue(s1._algo_reversed) self.assertTrue(s2._algo_reversed)
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_filter_dep_error_propagation(self): """error nodes are propagated along 'filter' dependencies (one node)""" svc1 = Service('first') tgt = '%s,fakenode' % HOSTNAME svc1.add_action(Action('start', command='true', target=tgt)) svc2 = Service('second') svc2.add_action(Action('start', command='true', target=HOSTNAME)) svc2.add_dep(svc1, sgth=FILTER) svc2.run('start') self.assertEqual(svc1.status, ERROR) self.assertEqual(svc2.status, DONE)
def test_filter_dep_timeout(self): """timeout nodes are propagated along 'filter' dependencies""" svc1 = Service('first') svc1.add_action( Action('start', command='sleep 1', target=HOSTNAME, timeout=0.1)) svc2 = Service('second') svc2.add_action(Action('start', command='true', target=HOSTNAME)) svc2.add_dep(svc1, sgth=FILTER) svc2.run('start') self.assertEqual(svc1.status, TIMEOUT) self.assertEqual(svc2.status, SKIPPED)
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_run_reverse_with_dependencies(self): ser = Service('REVERSE_BASE') ser_dep = Service('REVERSE_DEP') ser.algo_reversed = True ser_dep.algo_reversed = True stop1 = Action('stop', command='/bin/true') stop2 = Action('stop', command='/bin/true') ser.add_action(stop1) ser_dep.add_action(stop2) ser.add_dep(ser_dep) ser_dep.run('stop') self.assertEqual(ser.status, DONE) self.assertTrue(stop1.duration) self.assertEqual(ser_dep.status, DONE) self.assertTrue(stop2.duration)
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_filter_error_no_action(self): """ propagation along 'filter' dependencies works if action names mismatch """ svc1 = Service('first') svc1.add_action(Action('start', command='false', target=HOSTNAME)) svc2 = Service('second') svc2.add_action(Action('other', command='true', target=HOSTNAME)) svc2.add_dep(svc1, sgth=FILTER) svc2.run('start') self.assertEqual(svc1.status, ERROR) self.assertEqual(svc2.status, MISSING)
def test_skipped_action_overload(self): """Test action is not skipped if they overload target.""" # A dep on ERROR dep = Service('dep') dep.add_action(Action('start', command='/bin/false')) # A service running on empty nodeset... svc = Service('foo', target='@NOTEXIST') # ... with an action overloading the empty nodeset svc.add_action(Action('start', target=HOSTNAME, command=':')) svc.add_dep(dep) svc.run('start') self.assertEqual(dep.status, ERROR) self.assertEqual(svc.status, DEP_ERROR)