Add the 'plugin' subcommands to pylightning and add a test for them

This commit is contained in:
darosior 2019-07-23 01:24:47 +02:00 committed by Rusty Russell
parent 2864b4de01
commit 9ddc2e0334
2 changed files with 86 additions and 0 deletions

View File

@ -714,6 +714,51 @@ class LightningRpc(UnixDomainSocketRpc):
}
return self.call("ping", payload)
def plugin_start(self, plugin):
"""
Adds a plugin to lightningd.
"""
payload = {
"subcommand": "start",
"plugin": plugin
}
return self.call("plugin", payload)
def plugin_startdir(self, directory):
"""
Adds all plugins from a directory to lightningd.
"""
payload = {
"subcommand": "startdir",
"directory": directory
}
return self.call("plugin", payload)
def plugin_stop(self, plugin):
"""
Stops a lightningd plugin, will fail if plugin is not dynamic.
"""
payload = {
"subcommand": "stop",
"plugin": plugin
}
return self.call("plugin", payload)
def plugin_list(self):
"""
Lists all plugins lightningd knows about.
"""
payload = {
"subcommand": "list"
}
return self.call("plugin", payload)
def plugin_rescan(self):
payload = {
"subcommand": "rescan"
}
return self.call("plugin", payload)
def sendpay(self, route, payment_hash, description=None, msatoshi=None):
"""
Send along {route} in return for preimage of {payment_hash}

View File

@ -90,6 +90,47 @@ def test_plugin_dir(node_factory):
node_factory.get_node(options={'plugin-dir': plugin_dir, 'greeting': 'Mars'})
def test_plugin_command(node_factory):
"""Tests the 'plugin' RPC command"""
n = node_factory.get_node()
# Make sure that the 'hello' command from the helloworld.py plugin
# is not available.
cmd = [hlp for hlp in n.rpc.help()["help"] if "hello" in hlp["command"]]
assert(len(cmd) == 0)
# Add the 'contrib/plugins' test dir
time.sleep(2)
n.rpc.plugin_startdir(directory=os.path.join(os.getcwd(), "contrib/plugins"))
n.daemon.wait_for_log(r"Plugin helloworld.py initialized")
# Make sure that the 'hello' command from the helloworld.py plugin
# is now available.
cmd = [hlp for hlp in n.rpc.help()["help"] if "hello" in hlp["command"]]
assert(len(cmd) == 1)
# Make sure 'rescan' and 'list' controls dont crash
n.rpc.plugin_rescan()
n.rpc.plugin_list()
time.sleep(1)
# Make sure the plugin behaves normally after stop and restart
n.rpc.plugin_stop(plugin="helloworld.py")
n.daemon.wait_for_log(r"Killing plugin: helloworld.py")
time.sleep(1)
n.rpc.plugin_start(plugin=os.path.join(os.getcwd(), "contrib/plugins/helloworld.py"))
n.daemon.wait_for_log(r"Plugin helloworld.py initialized")
assert("Hello world" == n.rpc.call(method="hello"))
# Now stop the helloworld plugin
n.rpc.plugin_stop(plugin="helloworld.py")
n.daemon.wait_for_log(r"Killing plugin: helloworld.py")
time.sleep(1)
# Make sure that the 'hello' command from the helloworld.py plugin
# is not available anymore.
cmd = [hlp for hlp in n.rpc.help()["help"] if "hello" in hlp["command"]]
assert(len(cmd) == 0)
def test_plugin_disable(node_factory):
"""--disable-plugin works"""
plugin_dir = 'contrib/plugins'