def __init__(self): EntityManager.__init__(self) # Variables declared in the global scope self.variables = {} # Top service self.source = Service('root') self.source.simulate = True
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_skip(self): """Test skip method for services""" srv = Service('skipped') srv.add_action(Action('start', target=NodeSet('foo'), command='/bin/true')) srv.skip() self.assertTrue(srv._actions['start'].to_skip())
def reset(self): '''Reset values of attributes in order to perform multiple exec''' Service.reset(self) for service in self._subservices.values(): service.reset() self._sink.reset() self._source.reset()
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 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_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_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_command_output_warning(self): '''Test command line output with warning''' svc_warn = Service('service_failled') svc_warn.desc = 'I am the failled service' svc_ok = Service('service_ok') svc_ok.desc = 'I am the ok service' # Actions action = Action('warning', command='/bin/false') action.inherits_from(svc_warn) svc_warn.add_action(action) action = Action('warning', command='/bin/true') action.inherits_from(svc_ok) svc_ok.add_action(action) # Register services within the manager svc_ok.add_dep(target=svc_warn, sgth=REQUIRE_WEAK) self.manager.add_service(svc_warn) self.manager.add_service(svc_ok) self._output_check(['service_ok', 'warning'], RC_OK, """warning service_failled ran in 0.00 s > localhost exited with 1 service_failled - I am the failled service [ ERROR ] service_ok - I am the ok service [ OK ] """)
def test_local_variables(self): """Test Action local variables""" action = Action("bar") self.assertEqual(action._resolve("I'm %ACTION"), "I'm bar") svc = Service("foo") svc.add_action(action) self.assertEqual(action._resolve("I'm %SERVICE.%ACTION"), "I'm foo.bar")
def test_local_variables(self): '''Test Action local variables''' action = Action('bar') self.assertEqual(action._resolve("I'm %ACTION"), "I'm bar") svc = Service('foo') svc.add_action(action) self.assertEqual(action._resolve("I'm %SERVICE.%ACTION"), "I'm foo.bar")
def test_nb_errors_remote2(self): """Test the method nb_errors() with no error (remote).""" action = Action(name="test", target=HOSTNAME, command="/bin/true") service = Service("test_service") service.add_action(action) service.run("test") self.assertEqual(action.nb_errors(), 0) self.assertEqual(action.status, DONE)
def test_prepare_delayed_action(self): """Test prepare Service with a delayed action""" serv = Service('DELAYED_SERVICE') act = Action(name='start', command='/bin/true', delay=1) serv.add_action(act) serv.run('start') self.assertEqual(serv.status, DONE) self.assert_near(1.0, 0.3, act.duration)
def test_update_target(self): '''Test update of the target of an service''' serv = Service('A') act = Action('start', 'fortoy[5-10]', '/bin/true') serv.add_action(act) serv.update_target('aury[1-12]^fortoy[3-6]') self.assertTrue(serv.target == NodeSet('aury[1-12]^fortoy[3-6]')) self.assertTrue(act.target == NodeSet('aury[1-12]^fortoy[3-6]'))
def test_prepare_single_service(self): """Test prepare without dependencies between services.""" serv_test = Service('test_service') ac_start = Action(name='start', command='/bin/true') serv_test.add_action(ac_start) serv_test.run('start') self.assertTrue(serv_test.origin) self.assertEqual(serv_test.status, DONE)
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_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_nb_timeout_remote(self): """Test nb_timeout() method (remote mode)""" action = Action(name="start", target=HOSTNAME, command="sleep 3", timeout=0.5) service = Service("test_service") service.add_action(action) service.run("start") self.assertEqual(action.nb_timeout(), 1) self.assertEqual(action.status, TIMEOUT)
def test_nb_errors_remote(self): """Test the method nb_errors() (remote).""" action = Action(name="start", target="aury[12,13,21]", command="/bin/false") action.errors = 2 service = Service("test_service") service.add_action(action) service.run("start") self.assertEqual(action.nb_errors(), 3) self.assertEqual(action.status, ERROR)
def test_perform_action(self): """test perform an action without any delay""" action = Action("start", command="/bin/true") ser = Service("TEST") ser.add_action(action) ser.run("start") task_manager = action_manager_self() self.assertEqual(task_manager.tasks_done_count, 1) self.assertTrue(action.duration < 0.5, "Too long: %.2f > 0.5" % action.duration)
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 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_nb_timeout_local(self): """Test nb_timeout() method (local)""" action = Action(name="start", command="sleep 3", timeout=0.3) service = Service("test_service") service.add_action(action) service.run("start") self.assertEqual(action.nb_errors(), 0) self.assertEqual(action.nb_timeout(), 1) self.assertEqual(action.status, TIMEOUT)
def test_nb_errors_remote2(self): """Test the method nb_errors() with no error (remote).""" action = Action(name='test', target=HOSTNAME, command='/bin/true') service = Service('test_service') service.add_action(action) service.run('test') self.assertEqual(action.nodes_error(), NodeSet()) self.assertEqual(action.nb_errors(), 0) self.assertEqual(action.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_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_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_run_skipped(self): """Run a service with empty target is SKIPPED""" svc = Service('test_service') action = Action('start', target="TEMPNODE", command=":") svc.add_action(action) action.update_target("TEMPNODE", 'DIF') svc.run('start') self.assertEqual(action.status, SKIPPED) self.assertEqual(svc.status, SKIPPED)
def test_skip(self): """Test skip method for ServiceGroup""" grp = ServiceGroup('group') srv = Service('skipped') srv.add_action(Action('start', target=NodeSet('foo'), command='/bin/true')) grp.add_inter_dep(target=srv) self.assertFalse(grp.to_skip('start')) grp.skip() self.assertTrue(grp.to_skip('start'))
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_custom_defines(self): '''Test command line output custom variables''' svc = Service('one') svc.add_action(Action('go', command='/bin/echo %foo')) self.manager.register_service(svc) self._output_check(['one', 'go', '-v', '--define=foo=bar'], RC_OK, """go one on localhost > /bin/echo bar one [ OK ] """)
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_nb_timeout_remote(self): """Test nb_timeout() method (remote mode)""" action = Action(name='start', target=HOSTNAME, command='sleep 3', timeout=0.5) service = Service('test_service') service.add_action(action) service.run('start') self.assertEqual(action.nodes_timeout(), NodeSet(HOSTNAME)) self.assertEqual(action.nb_timeout(), 1) self.assertEqual(action.status, TIMEOUT)
def test_command_output_warning_status(self): '''Test command line output with one action WARNING''' svc = Service('warn') act = Action('go', command='/bin/false') act.errors = 1 svc.add_action(act) self.manager.register_service(svc) self._output_check(['warn', 'go', '-q'], RC_WARNING, """warn [ WARNING ] """)
def test_perform_delayed_action(self): """test perform an action with a delay""" action = Action('start', command='sleep 0.3') ser = Service('TEST') ser.add_action(action) ser.run('start') task_manager = action_manager_self() ActionManager._instance = None self.assertEqual(task_manager.tasks_done_count, 1) self.assert_near(0.3, 0.1, action.duration)
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_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_service_registration(self): '''Test the resgistration of a service within the manager''' manager = service_manager_self() srvtest = Service('test') manager.register_service(srvtest) self.assertTrue(manager.has_service(srvtest)) self.assertRaises(ServiceAlreadyReferencedError, manager.register_service, Service('test')) srva = Service('A') srvb = Service('B') manager.register_services(srva, srvb) self.assertTrue(manager.has_service(srva)) self.assertTrue(manager.has_service(srvb))
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_command_output_interactive_delayed(self): '''Test command line output in interactive mode with delayed actions''' action = Action('start', command='/bin/sleep 0.1', delay=0.3) srv = Service('service') srv.desc = 'I am the service' action.inherits_from(srv) srv.add_action(action) self.manager.register_services(srv) self._output_check(['service', 'start'], RC_OK, ''' Actions in progress > service.start (delayed for 0.3s) on localhost service - I am the service [ OK ] ''')
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 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_call_services_reversed_multiple(self): '''Test service_manager with multiple custom reversed actions''' manager = service_manager_self() s1 = Service('S1') s2 = Service('S2') s1.add_action(Action('stop', command='/bin/true')) s2.add_action(Action('wait', command='/bin/true')) manager.register_services(s1, s2) actions = ['stop', 'wait'] for act in actions: s1._algo_reversed = False s2._algo_reversed = False manager.call_services(['S1'], act, conf={"reverse_actions": actions}) self.assertTrue(s1._algo_reversed) self.assertTrue(s2._algo_reversed)
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_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_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_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)
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_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_remove_action(self): """Test remove_action behaviour.""" service = Service('brutus') service.add_action(Action('start')) service.remove_action('start') self.assertFalse(service.has_action('start')) self.assertRaises(ActionNotFoundError, service.remove_action, 'start')