cln_plugin : Test default values for ConfigOptions

This commit is contained in:
Erik De Smedt 2024-02-06 10:52:50 +01:00 committed by Christian Decker
parent a9797a4ff2
commit 3f4306eea9
3 changed files with 26 additions and 19 deletions

View file

@ -2,25 +2,28 @@
//! plugins using the Rust API against Core Lightning.
#[macro_use]
extern crate serde_json;
use cln_plugin::{messages, options, Builder, Error, Plugin};
use cln_plugin::options::{DefaultIntegerConfigOption, IntegerConfigOption};
use cln_plugin::{messages, Builder, Error, Plugin};
use tokio;
const TEST_NOTIF_TAG: &str = "test_custom_notification";
const TEST_OPTION: DefaultIntegerConfigOption = DefaultIntegerConfigOption::new_i64_with_default(
"test-option",
42,
"a test-option with default 42",
);
const TEST_OPTION_NO_DEFAULT: IntegerConfigOption =
IntegerConfigOption::new_i64_no_default("opt-option", "An option without a default");
#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
let state = ();
if let Some(plugin) = Builder::new(tokio::io::stdin(), tokio::io::stdout())
.option(options::ConfigOption::new_i64_with_default(
"test-option",
42,
"a test-option with default 42",
))
.option(options::ConfigOption::new_i64_no_default(
"opt-option",
"An optional option",
))
.option(TEST_OPTION)
.option(TEST_OPTION_NO_DEFAULT)
.rpcmethod("testmethod", "This is a test", testmethod)
.rpcmethod(
"testoptions",
@ -46,8 +49,13 @@ async fn main() -> Result<(), anyhow::Error> {
}
async fn testoptions(p: Plugin<()>, _v: serde_json::Value) -> Result<serde_json::Value, Error> {
let test_option = p.option(&TEST_OPTION)?;
let test_option_no_default = p
.option(&TEST_OPTION_NO_DEFAULT)?;
Ok(json!({
"opt-option": format!("{:?}", p.option_str("opt-option").unwrap())
"test-option": test_option,
"opt-option" : test_option_no_default
}))
}

View file

@ -143,7 +143,6 @@ pub mod config_type {
pub struct Flag;
}
/// Config values are represented as an i64. No default is used
pub type IntegerConfigOption<'a> = ConfigOption<'a, config_type::Integer>;
/// Config values are represented as a String. No default is used.
@ -159,7 +158,6 @@ pub type DefaultBooleanConfigOption<'a> = ConfigOption<'a, config_type::DefaultB
/// Config value is represented as a flag
pub type FlagConfigOption<'a> = ConfigOption<'a, config_type::Flag>;
pub trait OptionType {
type OutputValue;
type DefaultValue;
@ -614,8 +612,7 @@ mod test {
// The main goal of this test is to test compilation
// Initiate every type as a const
const _: FlagConfigOption =
ConfigOption::new_flag("flag-option", "A flag option");
const _: FlagConfigOption = ConfigOption::new_flag("flag-option", "A flag option");
const _: DefaultBooleanConfigOption =
ConfigOption::new_bool_with_default("bool-option", false, "A boolean option");
const _: BooleanConfigOption =

View file

@ -67,18 +67,20 @@ def test_plugin_start(node_factory):
l1.daemon.wait_for_log(r'Got a connect notification')
def test_plugin_optional_opts(node_factory):
def test_plugin_options_handle_defaults(node_factory):
"""Start a minimal plugin and ensure it is well-behaved
"""
bin_path = Path.cwd() / "target" / RUST_PROFILE / "examples" / "cln-plugin-startup"
l1 = node_factory.get_node(options={"plugin": str(bin_path), 'opt-option': 31337})
l1 = node_factory.get_node(options={"plugin": str(bin_path), 'opt-option': 31337, "test-option": 31338})
opts = l1.rpc.testoptions()
print(opts)
assert opts["opt-option"] == 31337
assert opts["test-option"] == 31338
# 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)
assert opts["opt-option"] is None, "opt-option has no default"
assert opts["test-option"] == 42, "test-option has a default of 42"
def test_grpc_connect(node_factory):