pytest: Test the new peer_connected hook with a reject plugin

This plugin just rejects `node_id`s it gets told about.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
Christian Decker 2019-02-07 16:57:59 +01:00
parent 2c443cc0fd
commit 5d9d5ceed1
2 changed files with 58 additions and 0 deletions

38
tests/plugins/reject.py Executable file
View File

@ -0,0 +1,38 @@
#!/usr/bin/env python3
"""Simple plugin to test the connected_hook.
It can mark some node_ids as rejects and it'll check for each
connection if it should be disconnected immediately or if it can
continue.
"""
from lightning import Plugin
plugin = Plugin()
@plugin.hook('peer_connected')
def on_connected(peer, plugin):
if peer['id'] in plugin.reject_ids:
print("{} is in reject list, disconnecting".format(peer['id']))
return {'result': 'disconnect'}
print("{} is allowed".format(peer['id']))
return {'result': 'continue'}
@plugin.init()
def init(configuration, options, plugin):
plugin.reject_ids = []
@plugin.method('reject')
def reject(node_id, plugin):
"""Mark a given node_id as reject for future connections.
"""
print("Rejecting connections from {}".format(node_id))
plugin.reject_ids.append(node_id)
plugin.run()

View File

@ -126,3 +126,23 @@ def test_pay_plugin(node_factory):
# Make sure usage messages are present.
assert only_one(l1.rpc.help('pay')['help'])['command'] == 'pay bolt11 [msatoshi] [description] [riskfactor] [maxfeepercent] [retry_for] [maxdelay] [exemptfee]'
assert only_one(l1.rpc.help('paystatus')['help'])['command'] == 'paystatus [bolt11]'
def test_plugin_connected_hook(node_factory):
""" l1 uses the reject plugin to reject connections.
l1 is configured to accept connections from l2, but not from l3.
"""
opts = [{'plugin': 'tests/plugins/reject.py'}, {}, {}]
l1, l2, l3 = node_factory.get_nodes(3, opts=opts)
l1.rpc.reject(l3.info['id'])
l2.connect(l1)
l1.daemon.wait_for_log(r"{} is allowed".format(l2.info['id']))
assert len(l1.rpc.listpeers(l2.info['id'])['peers']) == 1
l3.connect(l1)
l1.daemon.wait_for_log(r"{} is in reject list".format(l3.info['id']))
peer = l1.rpc.listpeers(l3.info['id'])['peers']
assert(peer == [] or not peer[0]['connected'])