2018-12-04 04:04:14 +01:00
|
|
|
from collections import OrderedDict
|
2018-11-17 15:18:34 +01:00
|
|
|
from fixtures import * # noqa: F401,F403
|
2018-11-26 19:56:44 +01:00
|
|
|
from lightning import RpcError
|
2018-11-17 15:18:34 +01:00
|
|
|
|
2018-11-26 19:56:44 +01:00
|
|
|
import pytest
|
2018-11-17 15:18:34 +01:00
|
|
|
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
def test_option_passthrough(node_factory):
|
|
|
|
""" Ensure that registering options works.
|
|
|
|
|
|
|
|
First attempts without the plugin and then with the plugin.
|
|
|
|
"""
|
2018-11-16 15:37:02 +01:00
|
|
|
plugin_path = 'contrib/plugins/helloworld.py'
|
2018-11-17 15:18:34 +01:00
|
|
|
|
|
|
|
help_out = subprocess.check_output([
|
|
|
|
'lightningd/lightningd',
|
|
|
|
'--help'
|
|
|
|
]).decode('utf-8')
|
|
|
|
assert('--greeting' not in help_out)
|
|
|
|
|
|
|
|
help_out = subprocess.check_output([
|
|
|
|
'lightningd/lightningd',
|
|
|
|
'--plugin={}'.format(plugin_path),
|
|
|
|
'--help'
|
|
|
|
]).decode('utf-8')
|
|
|
|
assert('--greeting' in help_out)
|
|
|
|
|
|
|
|
# Now try to see if it gets accepted, would fail to start if the
|
|
|
|
# option didn't exist
|
2018-12-10 16:38:36 +01:00
|
|
|
n = node_factory.get_node(options={'plugin': plugin_path, 'greeting': 'Ciao'})
|
2018-11-17 15:18:34 +01:00
|
|
|
n.stop()
|
2018-11-26 19:56:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_rpc_passthrough(node_factory):
|
|
|
|
"""Starting with a plugin exposes its RPC methods.
|
|
|
|
|
|
|
|
First check that the RPC method appears in the help output and
|
|
|
|
then try to call it.
|
|
|
|
|
|
|
|
"""
|
|
|
|
plugin_path = 'contrib/plugins/helloworld.py'
|
2018-12-10 16:38:36 +01:00
|
|
|
n = node_factory.get_node(options={'plugin': plugin_path, 'greeting': 'Ciao'})
|
2018-11-26 19:56:44 +01:00
|
|
|
|
|
|
|
# Make sure that the 'hello' command that the helloworld.py plugin
|
|
|
|
# has registered is available.
|
|
|
|
cmd = [hlp for hlp in n.rpc.help()['help'] if 'hello' in hlp['command']]
|
|
|
|
assert(len(cmd) == 1)
|
|
|
|
|
2018-12-06 16:00:08 +01:00
|
|
|
# While we're at it, let's check that helloworld.py is logging
|
|
|
|
# correctly via the notifications plugin->lightningd
|
|
|
|
assert n.daemon.is_in_log('Plugin helloworld.py initialized')
|
|
|
|
|
2018-11-26 19:56:44 +01:00
|
|
|
# Now try to call it and see what it returns:
|
2018-12-10 16:38:36 +01:00
|
|
|
greet = n.rpc.hello(name='World')
|
|
|
|
assert(greet == "Ciao World")
|
2018-11-26 19:56:44 +01:00
|
|
|
with pytest.raises(RpcError):
|
|
|
|
n.rpc.fail()
|
2018-12-03 10:26:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_plugin_dir(node_factory):
|
|
|
|
"""--plugin-dir works"""
|
|
|
|
plugin_dir = 'contrib/plugins'
|
|
|
|
node_factory.get_node(options={'plugin-dir': plugin_dir, 'greeting': 'Mars'})
|
2018-12-04 04:04:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_plugin_disable(node_factory):
|
|
|
|
"""--disable-plugin works"""
|
|
|
|
plugin_dir = 'contrib/plugins'
|
|
|
|
# We need plugin-dir before disable-plugin!
|
|
|
|
n = node_factory.get_node(options=OrderedDict([('plugin-dir', plugin_dir),
|
|
|
|
('disable-plugin',
|
|
|
|
'{}/helloworld.py'
|
|
|
|
.format(plugin_dir))]))
|
|
|
|
with pytest.raises(RpcError):
|
|
|
|
n.rpc.hello(name='Sun')
|
|
|
|
|
|
|
|
# Also works by basename.
|
|
|
|
n = node_factory.get_node(options=OrderedDict([('plugin-dir', plugin_dir),
|
|
|
|
('disable-plugin',
|
|
|
|
'helloworld.py')]))
|
|
|
|
with pytest.raises(RpcError):
|
|
|
|
n.rpc.hello(name='Sun')
|
2018-12-03 22:00:27 +01:00
|
|
|
|
|
|
|
|
2018-12-13 17:35:01 +01:00
|
|
|
def test_plugin_notifications(node_factory):
|
|
|
|
l1, l2 = node_factory.get_nodes(2, opts={'plugin': 'contrib/plugins/helloworld.py'})
|
|
|
|
|
|
|
|
l1.connect(l2)
|
|
|
|
l1.daemon.wait_for_log(r'Received connect event')
|
|
|
|
l2.daemon.wait_for_log(r'Received connect event')
|
|
|
|
|
|
|
|
l2.rpc.disconnect(l1.info['id'])
|
|
|
|
l1.daemon.wait_for_log(r'Received disconnect event')
|
|
|
|
l2.daemon.wait_for_log(r'Received disconnect event')
|
|
|
|
|
|
|
|
|
2018-12-03 22:00:27 +01:00
|
|
|
def test_failing_plugins():
|
|
|
|
fail_plugins = [
|
|
|
|
'contrib/plugins/fail/failtimeout.py',
|
|
|
|
'contrib/plugins/fail/doesnotexist.py',
|
|
|
|
]
|
|
|
|
|
|
|
|
for p in fail_plugins:
|
|
|
|
with pytest.raises(subprocess.CalledProcessError):
|
|
|
|
subprocess.check_output([
|
|
|
|
'lightningd/lightningd',
|
|
|
|
'--plugin={}'.format(p),
|
|
|
|
'--help',
|
|
|
|
])
|
2019-01-15 05:14:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_pay_plugin(node_factory):
|
|
|
|
l1, l2 = node_factory.line_graph(2)
|
|
|
|
inv = l2.rpc.invoice(123000, 'label', 'description', 3700)
|
|
|
|
|
|
|
|
res = l1.rpc.pay2(bolt11=inv['bolt11'])
|
|
|
|
assert res['status'] == 'complete'
|