From 2605e117c9655a2d0241f6935ea35c8327d60703 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Wed, 26 Oct 2022 13:10:19 +0200 Subject: [PATCH] pytest: Add test for optional options in cln-plugin Changelog-Added: cln-plugin: Options are no longer required to have a default value --- plugins/examples/cln-plugin-startup.rs | 16 ++++++++++++++++ tests/test_cln_rs.py | 14 ++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/plugins/examples/cln-plugin-startup.rs b/plugins/examples/cln-plugin-startup.rs index 3fdd6bdcf..b283bbc9a 100644 --- a/plugins/examples/cln-plugin-startup.rs +++ b/plugins/examples/cln-plugin-startup.rs @@ -14,7 +14,17 @@ async fn main() -> Result<(), anyhow::Error> { options::Value::Integer(42), "a test-option with default 42", )) + .option(options::ConfigOption::new( + "opt-option", + options::Value::OptInteger, + "An optional option", + )) .rpcmethod("testmethod", "This is a test", testmethod) + .rpcmethod( + "testoptions", + "Retrieve options from this plugin", + testoptions, + ) .subscribe("connect", connect_handler) .hook("peer_connected", peer_connected_handler) .start(state) @@ -26,6 +36,12 @@ async fn main() -> Result<(), anyhow::Error> { } } +async fn testoptions(p: Plugin<()>, _v: serde_json::Value) -> Result { + Ok(json!({ + "opt-option": format!("{:?}", p.option("opt-option").unwrap()) + })) +} + async fn testmethod(_p: Plugin<()>, _v: serde_json::Value) -> Result { Ok(json!("Hello")) } diff --git a/tests/test_cln_rs.py b/tests/test_cln_rs.py index 6d408d9cb..9d6e619a6 100644 --- a/tests/test_cln_rs.py +++ b/tests/test_cln_rs.py @@ -72,6 +72,20 @@ def test_plugin_start(node_factory): l1.daemon.wait_for_log(r'Got a connect notification') +def test_plugin_optional_opts(node_factory): + """Start a minimal plugin and ensure it is well-behaved + """ + bin_path = Path.cwd() / "target" / "debug" / "examples" / "cln-plugin-startup" + l1 = node_factory.get_node(options={"plugin": str(bin_path), 'opt-option': 31337}) + opts = l1.rpc.testoptions() + print(opts) + + # Do not set any value, should be None now + l1 = node_factory.get_node(options={"plugin": str(bin_path)}) + opts = l1.rpc.testoptions() + print(opts) + + def test_grpc_connect(node_factory): """Attempts to connect to the grpc interface and call getinfo""" # These only exist if we have rust!