diff --git a/contrib/pylightning/lightning/lightning.py b/contrib/pylightning/lightning/lightning.py index 23122728b..824a4b346 100644 --- a/contrib/pylightning/lightning/lightning.py +++ b/contrib/pylightning/lightning/lightning.py @@ -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} diff --git a/tests/test_plugin.py b/tests/test_plugin.py index ed5ee19b7..e13154a3d 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -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'