def test_action_with_variables(self): """Test variables in action command""" cmd = 'echo %([ "%VAR1" != "" ] && echo "-x %VAR1")' action = Action("start", command=cmd) service = Service("TEST") service.add_actions(action) service.add_var("VAR1", "foo") action.run() self.assertEqual(action.worker.command, "echo -x foo")
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.add_service(svc1) self.manager.add_service(svc2) self.manager.add_service(svc3) self.manager.add_service(group1)
def test_failed_nodes(self): """failed nodes are backup""" action = Action('start', command='/bin/false', target=HOSTNAME) service = Service('test') service.add_actions(action) action.run() self.assertEqual(action.failed_nodes, NodeSet(HOSTNAME)) self.assertEqual(action.status, ERROR) # This is propagated to action service self.assertEqual(service.failed_nodes, action.failed_nodes)
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) self.assertTrue(0.9 < a2.duration and a2.duration < 1.2)
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_action_with_variables(self): """Test variables in action command""" cmd = 'echo %([ "%VAR1" != "" ] && echo "-x %VAR1")' action = Action('start', command=cmd) service = Service('TEST') service.add_actions(action) service.add_var('VAR1', 'foo') service.resolve_all() action.run() self.assertEqual(action.worker.command, 'echo -x foo')
def test_failed_nodes(self): """failed nodes are backup""" action = Action('start', command='/bin/false', target=HOSTNAME) service = Service('test') service.add_actions(action) action.run() self.assertEqual(action.failed_nodes, NodeSet(HOSTNAME)) self.assertEqual(action.status, ERROR) # This is propagated to action service self.assertEqual(service.failed_nodes, action.failed_nodes)
def test_action_with_variables(self): """Test variables in action command""" cmd = 'echo %([ "%VAR1" != "" ] && echo "-x %VAR1")' action = Action('start', command=cmd) service = Service('TEST') service.add_actions(action) service.add_var('VAR1', 'foo') service.resolve_all() action.run() self.assertEqual(action.worker.command, 'echo -x foo')
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_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 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_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_prepare_dep_success(self): """Test prepare an action with a single successful dependency""" a1 = Action("start", command="/bin/true") a2 = Action("status", command="/bin/true") ser = Service("TEST") a1.add_dep(a2) ser.add_actions(a1, a2) a1.run() self.assertEqual(a1.status, DONE) self.assertFalse(a1.duration) self.assertEqual(a2.status, DONE) self.assertTrue(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_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_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_desc(self): """Test action inherits 'desc'""" # No service description, no action description action1 = Action('status', command='/bin/true') service = Service('TEST') service.add_actions(action1) self.assertEqual(action1.desc, None) # Service description, actions inherits the description action2 = Action('status', command='/bin/true') service2 = Service('TEST2') service2.desc = "Service TEST" service2.add_actions(action2) action2.inherits_from(service2) self.assertEqual(action2.desc, "Service TEST")
def test_desc(self): """Test action inherits 'desc'""" # No service description, no action description action1 = Action('status', command='/bin/true') service = Service('TEST') service.add_actions(action1) self.assertEqual(action1.desc, None) # Service description, actions inherits the description action2 = Action('status', command='/bin/true') service2 = Service('TEST2') service2.desc = "Service TEST" service2.add_actions(action2) action2.inherits_from(service2) self.assertEqual(action2.desc, "Service TEST")
def test_run_with_locked_service(self): '''Test run services with locked dependencies''' s1 = Service('S1') s2 = Service('S2') s3 = Service('S3') s4 = Service('S4') s5 = Service('S5') # Actions S1 start_s1 = Action('start', command='/bin/true') stop_s1 = Action('stop', command='/bin/true') s1.add_actions(start_s1, stop_s1) # Actions S2 start_s2 = Action('start', command='/bin/true') stop_s2 = Action('stop', command='/bin/true') s2.add_actions(start_s2, stop_s2) # Actions S3 start_s3 = Action('start', command='/bin/false') stop_s3 = Action('stop', command='/bin/false') s3.add_actions(start_s3, stop_s3) # Actions S4 start_s4 = Action('start', command='/bin/true') stop_s4 = Action('stop', command='/bin/true') s4.add_actions(start_s4, stop_s4) # Actions I1 start_s5 = Action('start', command='/bin/true') stop_s5 = Action('stop', command='/bin/true') s5.add_actions(start_s5, stop_s5) # Locked services s3.status = LOCKED # Build graph s1.add_dep(target=s2) s1.add_dep(target=s3) s3.add_dep(target=s4) s3.add_dep(target=s5) # Run service S1 s1.run('start') self.assertEqual(s1.status, DONE) self.assertEqual(s2.status, DONE) self.assertEqual(s3.status, LOCKED) self.assertEqual(s4.status, NO_STATUS) self.assertEqual(s5.status, NO_STATUS)
def test_run_with_locked_service(self): '''Test run services with locked dependencies''' s1 = Service('S1') s2 = Service('S2') s3 = Service('S3') s4 = Service('S4') s5 = Service('S5') # Actions S1 start_s1 = Action('start', command='/bin/true') stop_s1 = Action('stop', command='/bin/true') s1.add_actions(start_s1, stop_s1) # Actions S2 start_s2 = Action('start', command='/bin/true') stop_s2 = Action('stop', command='/bin/true') s2.add_actions(start_s2, stop_s2) # Actions S3 start_s3 = Action('start', command='/bin/false') stop_s3 = Action('stop', command='/bin/false') s3.add_actions(start_s3, stop_s3) # Actions S4 start_s4 = Action('start', command='/bin/true') stop_s4 = Action('stop', command='/bin/true') s4.add_actions(start_s4, stop_s4) # Actions I1 start_s5 = Action('start', command='/bin/true') stop_s5 = Action('stop', command='/bin/true') s5.add_actions(start_s5, stop_s5) # Locked services s3.status = LOCKED # Build graph s1.add_dep(target=s2) s1.add_dep(target=s3) s3.add_dep(target=s4) s3.add_dep(target=s5) # Run service S1 s1.run('start') self.assertEqual(s1.status, DONE) self.assertEqual(s2.status, DONE) self.assertEqual(s3.status, LOCKED) self.assertEqual(s4.status, NO_STATUS) self.assertEqual(s5.status, NO_STATUS)
def test_prepare_actions_graph_with_errors(self): """Test prepare an action graph with errors""" a1 = Action('start', command='/bin/true') a2 = Action('start_engine', command='/bin/true') a3 = Action('start_gui', command='/bin/false') a4 = Action('empty_home', command='/bin/false') a1.add_dep(a2) a1.add_dep(a3) a2.add_dep(a4) a3.add_dep(a4) ser = Service('TEST') ser.add_actions(a1, a2, a3, a4) a1.run() self.assertEqual(a1.status, DONE) self.assertTrue(a1.duration) self.assertEqual(a2.status, DONE) self.assertTrue(a2.duration) self.assertEqual(a3.status, ERROR) self.assertTrue(a3.duration) self.assertEqual(a4.status, ERROR) self.assertTrue(a4.duration)
def test_prepare_actions_graph_with_errors(self): """Test prepare an action graph with errors""" a1 = Action('start', command='/bin/true') a2 = Action('start_engine', command='/bin/true') a3 = Action('start_gui', command='/bin/false') a4 = Action('empty_home', command='/bin/false') a1.add_dep(a2) a1.add_dep(a3) a2.add_dep(a4) a3.add_dep(a4) ser = Service('TEST') ser.add_actions(a1, a2, a3, a4) a1.run() self.assertEqual(a1.status, DONE) self.assertTrue(a1.duration) self.assertEqual(a2.status, DONE) self.assertTrue(a2.duration) self.assertEqual(a3.status, ERROR) self.assertTrue(a3.duration) self.assertEqual(a4.status, ERROR) self.assertTrue(a4.duration)
def test_prepare_actions_graph(self): """Test prepare an action graph without errors""" a1 = Action("start", command="/bin/true") a2 = Action("start_engine", command="/bin/true") a3 = Action("start_gui", command="/bin/true") a4 = Action("empty_home", command="/bin/true") a1.add_dep(a2) a1.add_dep(a3) a2.add_dep(a4) a3.add_dep(a4) ser = Service("TEST") ser.add_actions(a1, a2, a3, a4) a1.run() self.assertEqual(a1.status, DONE) self.assertFalse(a1.duration) self.assertEqual(a2.status, DONE) self.assertFalse(a2.duration) self.assertEqual(a3.status, DONE) self.assertFalse(a3.duration) self.assertEqual(a4.status, DONE) self.assertTrue(a4.duration)
def test_run_multiple_action_with_subaction(self): """Test with multiple actions running a sub action (start-> status)""" nemesis = Service("NEMESIS") zombie_one = Service("ZOMBIE_ONE") zombie_two = Service("ZOMBIE_TWO") hive = Service("THE_HIVE") act_start1 = Action("start", command="/bin/true") act_start2 = Action("start", command="/bin/false") act_start3 = Action("start", command="/bin/true") act_start4 = Action("start", command="/bin/true") act_sta = Action("status", command="/bin/true") act_sta_fai = Action("status", command="/bin/false") act_start2.add_dep(act_sta_fai) act_start4.add_dep(act_sta) nemesis.add_action(act_start1) zombie_one.add_actions(act_start2, act_sta_fai) zombie_two.add_action(act_start3) hive.add_actions(act_start4, act_sta) zombie_one.add_dep(hive) zombie_two.add_dep(hive) nemesis.add_dep(zombie_one) nemesis.add_dep(zombie_two) nemesis.run("start") self.assertEqual(hive.status, DONE) self.assertEqual(zombie_one.status, ERROR) self.assertEqual(zombie_two.status, DONE) self.assertEqual(nemesis.status, DEP_ERROR) self.assertFalse(act_start1.duration) self.assertTrue(act_start2.duration) self.assertTrue(act_start3.duration) self.assertFalse(act_start4.duration) self.assertTrue(act_sta_fai.duration) self.assertTrue(act_sta.duration)
def test_run_multiple_action_with_subaction(self): """Test with multiple actions running a sub action (start-> status)""" nemesis = Service("NEMESIS") zombie_one = Service("ZOMBIE_ONE") zombie_two = Service("ZOMBIE_TWO") hive = Service("THE_HIVE") act_start1 = Action("start", command="/bin/true") act_start2 = Action("start", command="/bin/false") act_start3 = Action("start", command="/bin/true") act_start4 = Action("start", command="/bin/true") act_sta = Action("status", command="/bin/true") act_sta_fai = Action("status", command="/bin/false") act_start2.add_dep(act_sta_fai) act_start4.add_dep(act_sta) nemesis.add_action(act_start1) zombie_one.add_actions(act_start2, act_sta_fai) zombie_two.add_action(act_start3) hive.add_actions(act_start4, act_sta) zombie_one.add_dep(hive) zombie_two.add_dep(hive) nemesis.add_dep(zombie_one) nemesis.add_dep(zombie_two) nemesis.run("start") self.assertEqual(hive.status, DONE) self.assertEqual(zombie_one.status, ERROR) self.assertEqual(zombie_two.status, DONE) self.assertEqual(nemesis.status, DEP_ERROR) self.assertFalse(act_start1.duration) self.assertTrue(act_start2.duration) self.assertTrue(act_start3.duration) self.assertFalse(act_start4.duration) self.assertTrue(act_sta_fai.duration) self.assertTrue(act_sta.duration)