From f5e08c3dae2187e1cda0766f872f1dcd21d6086c Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 23 Jul 2021 09:57:02 +0930 Subject: [PATCH] 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 --- contrib/pyln-client/README.md | 2 ++ tests/plugins/selfdisable.py | 12 ++++++++++++ tests/test_plugin.py | 12 ++++++++++-- 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100755 tests/plugins/selfdisable.py diff --git a/contrib/pyln-client/README.md b/contrib/pyln-client/README.md index 2e515eb4c..55c6051fb 100644 --- a/contrib/pyln-client/README.md +++ b/contrib/pyln-client/README.md @@ -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': } to self-disable, + # but normally it returns None. @plugin.subscribe("connect") diff --git a/tests/plugins/selfdisable.py b/tests/plugins/selfdisable.py new file mode 100755 index 000000000..5daa2ce6e --- /dev/null +++ b/tests/plugins/selfdisable.py @@ -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() diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 4d5319aa5..e8f830ac4 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -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"):