pyln-client: document and test that init can self-disable.

mypy says it returns None, but it actually doesn't have to!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2021-07-23 09:57:02 +09:30
parent 5d9be5ab97
commit f5e08c3dae
3 changed files with 24 additions and 2 deletions

View File

@ -91,6 +91,8 @@ def hello(plugin, name="world"):
@plugin.init()
def init(options, configuration, plugin):
plugin.log("Plugin helloworld.py initialized")
# This can also return {'disabled': <reason>} to self-disable,
# but normally it returns None.
@plugin.subscribe("connect")

12
tests/plugins/selfdisable.py Executable file
View File

@ -0,0 +1,12 @@
#!/usr/bin/env python3
from pyln.client import Plugin
plugin = Plugin()
@plugin.init()
def init(configuration, options, plugin):
return {'disable': 'init saying disable'}
plugin.run()

View File

@ -2446,15 +2446,23 @@ def test_self_disable(node_factory):
)
# This disables in response to init.
p2 = os.path.join(os.getcwd(), "tests/plugins/test_libplugin")
l1 = node_factory.get_node(options={'important-plugin': [p1, p2], 'selfdisable': None})
pydisable = os.path.join(
os.path.dirname(__file__), 'plugins/selfdisable.py'
)
l1 = node_factory.get_node(options={'important-plugin': [p1, p2],
'plugin': pydisable,
'selfdisable': None})
# Could happen before it gets set up.
l1.daemon.logsearch_start = 0
l1.daemon.wait_for_logs(['test_selfdisable_after_getmanifest: .* disabled itself: Self-disable test after getmanifest',
'test_libplugin: .* disabled itself at init: Disabled via selfdisable option'])
'test_libplugin: .* disabled itself at init: Disabled via selfdisable option',
'selfdisable.py: .* disabled itself at init: init saying disable'])
assert p1 not in [p['name'] for p in l1.rpc.plugin_list()['plugins']]
assert p2 not in [p['name'] for p in l1.rpc.plugin_list()['plugins']]
assert pydisable not in [p['name'] for p in l1.rpc.plugin_list()['plugins']]
# Also works with dynamic load attempts
with pytest.raises(RpcError, match="Self-disable test after getmanifest"):