From 6af32885fac7e0819ca3d8ead1e9c161521e77d6 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 14 May 2024 12:53:54 +0930 Subject: [PATCH] libplugin: add test for dynamic setting values. Serves as an example, but also shows a quirk which we fix in the next patch. Signed-off-by: Rusty Russell --- tests/plugins/test_libplugin.c | 23 +++++++++++++++++++++++ tests/test_plugin.py | 19 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/tests/plugins/test_libplugin.c b/tests/plugins/test_libplugin.c index a27ac3a48..b082962e9 100644 --- a/tests/plugins/test_libplugin.c +++ b/tests/plugins/test_libplugin.c @@ -9,6 +9,7 @@ static char *somearg; static bool self_disable = false; static bool dont_shutdown = false; +static int dynamic_opt = 7; static struct command_result *get_ds_done(struct command *cmd, const char *val, @@ -177,6 +178,24 @@ static struct command_result *json_checkthis(struct command *cmd, return send_outreq(cmd->plugin, req); } +static char *set_dynamic(struct plugin *plugin, + const char *arg, + bool check_only, + int *dynamic_opt) +{ + int val = atol(arg); + + /* Whee, let's allow odd */ + if (val % 2 == 0) + return "I don't like \"even\" numbers (valid JSON? Try {})!"; + + if (check_only) + return NULL; + + *dynamic_opt = val; + return NULL; +} + static const char *init(struct plugin *p, const char *buf UNUSED, const jsmntok_t *config UNUSED) @@ -293,5 +312,9 @@ int main(int argc, char *argv[]) "flag", "Whether to timeout when asked to shutdown.", flag_option, &dont_shutdown), + plugin_option_dynamic("dynamicopt", + "int", + "Set me!", + set_dynamic, &dynamic_opt), NULL); } diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 16c9bd931..a4551fddd 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -1560,6 +1560,25 @@ def test_libplugin(node_factory): assert l1.rpc.call("helloworld") == {"hello": "NOT FOUND"} l1.daemon.wait_for_log("get_ds_bin_done: NOT FOUND") + # Check dynamic! + with pytest.raises(RpcError) as err: + l1.rpc.setconfig('dynamicopt', 4) + assert err.value.error['message'] == 'I don\'t like \\"even\\" numbers (valid JSON? Try {})!' + + with pytest.raises(RpcError) as err: + l1.rpc.check(command_to_check='setconfig', config='dynamicopt', val=4) + assert err.value.error['message'] == 'I don\'t like \\"even\\" numbers (valid JSON? Try {})!' + + l1.rpc.check(command_to_check='setconfig', config='dynamicopt', val=9) + # FIXME: libplugin doesn't populate defaults! + assert 'dynamicopt' not in l1.rpc.listconfigs()['configs'] + + l1.rpc.setconfig('dynamicopt', 9) + conf = l1.rpc.listconfigs('dynamicopt')['configs']['dynamicopt'] + assert conf['value_int'] == 9 + assert conf['plugin'] == plugin + assert conf['dynamic'] is True + # Test dynamic startup l1.rpc.plugin_stop(plugin) # Non-string datastore value: