'shutdown' ad-hoc command implementation

darcs-hash:20070907182115-86b55-fc37f848afb705530838456d45ea8e5c5528e8f0.gz
This commit is contained in:
David Rousselie
2007-09-07 20:21:15 +02:00
parent f285836f92
commit afbbda695c
2 changed files with 140 additions and 10 deletions

View File

@@ -929,8 +929,48 @@ class JCLCommandManager(CommandManager):
restart_thread.start()
return (None, result)
def execute_shutdown(self, info_query):
return []
def execute_shutdown_1(self, info_query, session_context,
command_node, lang_class):
self.add_actions(command_node, [ACTION_NEXT])
result_form = Form(xmlnode_or_type="result",
title="TODO",
instructions="TODO")
result_form.add_field(field_type="hidden",
name="FORM_TYPE",
value="http://jabber.org/protocol/admin")
result_form.add_field(name="delay",
field_type="list-multi",
label="TODO",
required=True)
result_form.add_field(name="announcement",
field_type="text-multi",
label="TODO")
result_form.as_xml(command_node)
return (result_form, [])
def execute_shutdown_2(self, info_query, session_context,
command_node, lang_class):
self.__logger.debug("Executing command 'shutdown' step 2")
announcement = session_context["announcement"][0]
delay = int(session_context["delay"][0])
if announcement is not None and announcement != "":
users = account.get_all_users(\
filter=AND(Account.q.userID == User.q.id,
Account.q._status != account.OFFLINE),
distinct=True)
result = []
for user in users:
result.append(Message(from_jid=self.component.jid,
to_jid=user.jid,
body=announcement))
command_node.setProp("status", STATUS_COMPLETED)
def delayed_restart(self, delay):
threading.Event().wait(delay)
self.component.running = False
restart_thread = threading.Thread(target=lambda : delayed_restart(self, delay),
name="TimerThread")
restart_thread.start()
return (None, result)
class CommandRootDiscoGetInfoHandler(RootDiscoGetInfoHandler):

View File

@@ -2913,14 +2913,104 @@ class JCLCommandManager_TestCase(JCLTestCase):
self.assertTrue(self.comp.restart)
self.assertFalse(self.comp.running)
# def test_execute_shutdown(self):
# #TODO : implement command
# info_query = Iq(stanza_type="set",
# from_jid="admin@test.com",
# to_jid="jcl.test.com")
# result = self.command_manager.execute_add_user(info_query)
# self.assertNotEquals(result, None)
# self.assertEquals(len(result), 1)
def test_execute_shutdown(self):
self.comp.account_manager.account_classes = (ExampleAccount,
Example2Account)
self.comp.running = True
model.db_connect()
user1 = User(jid="test1@test.com")
account11 = ExampleAccount(user=user1,
name="account11",
jid="account11@jcl.test.com")
account11.status = account.ONLINE
account12 = Example2Account(user=user1,
name="account12",
jid="account12@jcl.test.com")
account12.status = "away"
user2 = User(jid="test2@test.com")
account21 = ExampleAccount(user=user2,
name="account21",
jid="account21@jcl.test.com")
account22 = ExampleAccount(user=user2,
name="account11",
jid="account11@jcl.test.com")
account22.status = "xa"
model.db_disconnect()
info_query = Iq(stanza_type="set",
from_jid="admin@test.com",
to_jid="jcl.test.com")
command_node = info_query.set_new_content(command.COMMAND_NS, "command")
command_node.setProp("node",
"http://jabber.org/protocol/admin#shutdown")
result = self.command_manager.apply_command_action(\
info_query,
"http://jabber.org/protocol/admin#shutdown",
"execute")
self.assertNotEquals(result, None)
self.assertEquals(len(result), 1)
xml_command = result[0].xpath_eval("c:command",
{"c": "http://jabber.org/protocol/commands"})[0]
self.assertEquals(xml_command.prop("status"), "executing")
self.assertNotEquals(xml_command.prop("sessionid"), None)
self.__check_actions(result[0], ["next"])
fields = result[0].xpath_eval("c:command/data:x/data:field",
{"c": "http://jabber.org/protocol/commands",
"data": "jabber:x:data"})
self.assertEquals(len(fields), 3)
self.assertEquals(fields[1].prop("var"), "delay")
self.assertEquals(fields[1].prop("type"), "list-multi")
self.assertEquals(fields[2].prop("var"), "announcement")
self.assertEquals(fields[2].prop("type"), "text-multi")
# Second step
info_query = Iq(stanza_type="set",
from_jid="admin@test.com",
to_jid="jcl.test.com")
command_node = info_query.set_new_content(command.COMMAND_NS, "command")
command_node.setProp("node",
"http://jabber.org/protocol/admin#shutdown")
session_id = xml_command.prop("sessionid")
command_node.setProp("sessionid", session_id)
command_node.setProp("action", "next")
submit_form = Form(xmlnode_or_type="submit")
submit_form.add_field(field_type="list-multi",
name="delay",
value=[0])
submit_form.add_field(field_type="text-multi",
name="announcement",
value=["service will be shut in 0 second"])
submit_form.as_xml(command_node)
result = self.command_manager.apply_command_action(\
info_query,
"http://jabber.org/protocol/admin#shutdown",
"execute")
self.assertNotEquals(result, None)
self.assertEquals(len(result), 3)
xml_command = result[0].xpath_eval("c:command",
{"c": "http://jabber.org/protocol/commands"})[0]
self.assertEquals(xml_command.prop("status"), "completed")
self.assertEquals(xml_command.prop("sessionid"), session_id)
self.__check_actions(result[0])
context_session = self.command_manager.sessions[session_id][1]
self.assertEquals(context_session["announcement"],
["service will be shut in 0 second"])
self.assertEquals(context_session["delay"],
["0"])
self.assertEquals(result[1].get_from(), "jcl.test.com")
self.assertEquals(result[1].get_to(), "test1@test.com")
self.assertEquals(result[1].get_body(), "service will be shut in 0 second")
self.assertEquals(result[2].get_from(), "jcl.test.com")
self.assertEquals(result[2].get_to(), "test2@test.com")
self.assertEquals(result[2].get_body(), "service will be shut in 0 second")
self.assertFalse(self.comp.restart)
self.assertTrue(self.comp.running)
threads = threading.enumerate()
self.assertEquals(len(threads), 2)
threading.Event().wait(1)
threads = threading.enumerate()
self.assertEquals(len(threads), 1)
self.assertFalse(self.comp.restart)
self.assertFalse(self.comp.running)
def suite():
test_suite = unittest.TestSuite()