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_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 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')
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_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 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 test_mix_errors_timeout(self): """Test the result of mixed timeout and error actions.""" cmd = 'echo "${SSH_CLIENT%%%% *}" | egrep "^(127.0.0.1|::1)$" ||sleep 1' action = Action(name="start", target="badname,%s,localhost" % HOSTNAME, command=cmd, timeout=0.6) action.errors = 1 service = Service("test_service") service.add_action(action) service.run("start") self.assertEqual(action.nb_errors(), 1) self.assertEqual(action.nb_timeout(), 1) self.assertEqual(action.status, ERROR) service.reset() action.errors = 2 service.run("start") self.assertEqual(action.nb_errors(), 1) self.assertEqual(action.nb_timeout(), 1) self.assertEqual(action.status, WARNING) service.reset() action.errors = 2 action.warnings = 2 service.run("start") self.assertEqual(action.nb_errors(), 1) self.assertEqual(action.nb_timeout(), 1) self.assertEqual(action.status, DONE)
def test_mix_errors_timeout(self): """Test the result of mixed timeout and error actions.""" action = Action(name='start', target='badname,timeout,localhost', command='/bin/true', timeout=0.9) action.errors = 1 service = Service('test_service') service.add_action(action) service.run('start') self.assertEqual(action.nodes_error(), NodeSet('badname')) self.assertEqual(action.nb_errors(), 1) self.assertEqual(action.nodes_timeout(), NodeSet('timeout')) self.assertEqual(action.nb_timeout(), 1) self.assertEqual(action.status, ERROR) service.reset() action.errors = 2 service.run('start') self.assertEqual(action.nodes_error(), NodeSet('badname')) self.assertEqual(action.nb_errors(), 1) self.assertEqual(action.nodes_timeout(), NodeSet('timeout')) self.assertEqual(action.nb_timeout(), 1) self.assertEqual(action.status, WARNING) service.reset() action.errors = 2 action.warnings = 2 service.run('start') self.assertEqual(action.nodes_error(), NodeSet('badname')) self.assertEqual(action.nb_errors(), 1) self.assertEqual(action.nodes_timeout(), NodeSet('timeout')) self.assertEqual(action.nb_timeout(), 1) self.assertEqual(action.status, DONE)
def test_mix_errors_timeout(self): """Test the result of mixed timeout and error actions.""" # timeout host configuration is in setup_sshconfig (__init__.py) action = Action(name='start', target='badname,timeout,localhost', command='/bin/true', timeout=0.9) action.errors = 1 service = Service('test_service') service.add_action(action) service.run('start') self.assertEqual(action.nodes_error(), NodeSet('badname')) self.assertEqual(action.nb_errors(), 1) self.assertEqual(action.nodes_timeout(), NodeSet('timeout')) self.assertEqual(action.nb_timeout(), 1) self.assertEqual(action.status, ERROR) service.reset() action.errors = 2 service.run('start') self.assertEqual(action.nodes_error(), NodeSet('badname')) self.assertEqual(action.nb_errors(), 1) self.assertEqual(action.nodes_timeout(), NodeSet('timeout')) self.assertEqual(action.nb_timeout(), 1) self.assertEqual(action.status, WARNING) service.reset() action.errors = 2 action.warnings = 2 service.run('start') self.assertEqual(action.nodes_error(), NodeSet('badname')) self.assertEqual(action.nb_errors(), 1) self.assertEqual(action.nodes_timeout(), NodeSet('timeout')) self.assertEqual(action.nb_timeout(), 1) self.assertEqual(action.status, DONE)
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_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_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_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_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_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_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_perform_action_bad_service(self): """test perform action with a simulate service hooked to the action""" action = Action(name="start", command=":") ser = Service("TEST") ser.simulate = True ser.add_action(action) ser.run("start") task_manager = action_manager_self() self.assertEqual(task_manager.tasks_done_count, 0)
def test_run_reverse_single_service(self): """Test run action stop on service (reverse algorithm)""" ser = Service('REVERSE') ser.algo_reversed = True stop = Action('stop', command='/bin/true') ser.add_action(stop) ser.run('stop') self.assertEqual(ser.status, DONE) self.assertTrue(stop.duration)
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_perform_action_bad_service(self): '''test perform action with a simulate service hooked to the action''' action = Action(name='start', command=':') ser = Service('TEST') ser.simulate = True ser.add_action(action) ser.run('start') task_manager = action_manager_self() self.assertEqual(task_manager.tasks_done_count, 0)
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_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_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_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_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_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_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_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_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_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.nodes_error(), NodeSet()) self.assertEqual(action.nb_errors(), 0) self.assertEqual(action.nodes_timeout(), NodeSet("localhost")) self.assertEqual(action.nb_timeout(), 1) self.assertEqual(action.status, TIMEOUT)
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_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_retry_error(self): """Test retry behaviour when errors""" action = Action('start', command='/bin/false') action.delay = 0.1 action.maxretry = 3 service = Service('retry') service.add_action(action) service.run('start') self.assertEqual(action.tries, 4) self.assertEqual(action.status, ERROR) self.assertTrue(0.3 < action.duration < 0.5, "%.3f is not between 0.3 and 0.5" % action.duration)
def test_retry_timeout(self): """Test retry behaviour when timeout""" action = Action('start', command='/bin/sleep 0.5', timeout=0.1) action.delay = 0.1 action.maxretry = 2 service = Service('retry') service.add_action(action) service.run('start') self.assertEqual(action.tries, 3) self.assertEqual(action.status, TIMEOUT) self.assertTrue(0.59 <= action.duration <= 0.8, "%.3f is not between 0.59 and 0.8" % action.duration)
def test_nb_errors_remote(self): """Test the method nb_errors() (remote).""" action = Action(name='start', target='badname[12,13,21]', command='/bin/false') action.errors = 2 service = Service('test_service') service.add_action(action) service.run('start') self.assertEqual(action.nodes_error(), NodeSet("badname[12,13,21]")) self.assertEqual(action.nb_errors(), 3) self.assertEqual(action.status, ERROR)
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_reset_service(self): '''Test resest values of a service''' service = Service('brutus') action = Action('start') service.origin = True action.status = DONE service.add_action(action) service._last_action = 'start' service.reset() self.assertFalse(service.origin) self.assertFalse(service._last_action) self.assertEqual(action.status, NO_STATUS) self.assertEqual(service.status, NO_STATUS)