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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2024-05-14 12:53:54 +09:30 committed by Alex Myers
parent f7afe1a35f
commit 6af32885fa
2 changed files with 42 additions and 0 deletions

View File

@ -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);
}

View File

@ -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: