From da476848d1c6328ea91b5ce47df8b8bd1180eb9d Mon Sep 17 00:00:00 2001 From: darosior Date: Fri, 31 Jan 2020 18:36:26 +0100 Subject: [PATCH] pytest: add a C testing plugin to test libplugin We mark the test as xfail() as it exposes that libplugin's PLUGIN_RESTARTABLE was not taken into account ! --- Makefile | 1 + tests/plugins/Makefile | 18 ++++++++++++ tests/plugins/test_libplugin.c | 52 ++++++++++++++++++++++++++++++++++ tests/test_plugin.py | 25 ++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 tests/plugins/Makefile create mode 100644 tests/plugins/test_libplugin.c diff --git a/Makefile b/Makefile index 24d39063a..35b91d553 100644 --- a/Makefile +++ b/Makefile @@ -237,6 +237,7 @@ include doc/Makefile include devtools/Makefile include tools/Makefile include plugins/Makefile +include tests/plugins/Makefile # Git doesn't maintain timestamps, so we only regen if git says we should. CHANGED_FROM_GIT = [ x"`git log $@ | head -n1`" != x"`git log $< | head -n1`" -o x"`git diff $<`" != x"" ] diff --git a/tests/plugins/Makefile b/tests/plugins/Makefile new file mode 100644 index 000000000..0b0eee0b4 --- /dev/null +++ b/tests/plugins/Makefile @@ -0,0 +1,18 @@ +PLUGIN_TESTLIBPLUGIN_SRC := tests/plugins/test_libplugin.c +PLUGIN_TESTLIBPLUGIN_OBJS := $(PLUGIN_TESTLIBPLUGIN_SRC:.c=.o) + +tests/plugins/test_libplugin: bitcoin/chainparams.o $(PLUGIN_TESTLIBPLUGIN_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_COMMON_OBJS) $(JSMN_OBJS) $(CCAN_OBJS) + +$(PLUGIN_TESTLIBPLUGIN_OBJS): $(PLUGIN_LIB_HEADER) + +# Make sure these depend on everything. +ALL_PROGRAMS += tests/plugins/test_libplugin +ALL_OBJS += $(PLUGIN_TESTLIBPLUGIN_OBJS) + +check-source: $(PLUGIN_TESTLIBPLUGIN_SRC:%=check-src-include-order/%) +check-whitespace: $(PLUGIN_TESTLIBPLUGIN_SRC:%=check-whitespace/%) + +clean: test-plugin-clean + +test-plugin-clean: + $(RM) $(PLUGIN_TESTLIBPLUGIN_OBJS) diff --git a/tests/plugins/test_libplugin.c b/tests/plugins/test_libplugin.c new file mode 100644 index 000000000..9630d10be --- /dev/null +++ b/tests/plugins/test_libplugin.c @@ -0,0 +1,52 @@ +#include +#include + + +const char *name_option; + + +static struct command_result *json_helloworld(struct command *cmd, + const char *buf, + const jsmntok_t *params) +{ + const char *name; + + if (!param(cmd, buf, params, + p_opt("name", param_string, &name), + NULL)) + return command_param_failed(); + + if (!name) + name = name_option ? name_option : tal_strdup(tmpctx, "world"); + + return command_success_str(cmd, tal_fmt(tmpctx, "hello %s", name)); +} + +static void init(struct plugin *p, + const char *buf UNUSED, const jsmntok_t *config UNUSED) +{ + plugin_log(p, LOG_DBG, "test_libplugin initialised!"); +} + +static const struct plugin_command commands[] = { { + "helloworld", + "utils", + "Say hello to the world.", + "Returns 'hello world' by default, 'hello {name}' if the name" + " option was set, and 'hello {name}' if the name parameter " + "was passed (takes over the option)", + json_helloworld, + } +}; + +int main(int argc, char *argv[]) +{ + setup_locale(); + plugin_main(argv, init, PLUGIN_RESTARTABLE, commands, ARRAY_SIZE(commands), + NULL, 0, NULL, 0, + plugin_option("name", + "string", + "Who to say hello to.", + charp_option, &name_option), + NULL); +} diff --git a/tests/test_plugin.py b/tests/test_plugin.py index c4bb19cd9..55dd82624 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -789,3 +789,28 @@ def test_rpc_command_hook(node_factory): # Test command which removes plugin itself! l1.rpc.plugin_stop('rpc_command.py') + + +@pytest.mark.xfail(strict=True) +def test_libplugin(node_factory): + """Sanity checks for plugins made with libplugin""" + plugin = os.path.join(os.getcwd(), "tests/plugins/test_libplugin") + l1 = node_factory.get_node(options={"plugin": plugin}) + + # Test startup + assert l1.daemon.is_in_log("test_libplugin initialised!") + # Test dynamic startup + l1.rpc.plugin_stop(plugin) + l1.rpc.plugin_start(plugin) + l1.rpc.check("helloworld") + + # Test commands + assert l1.rpc.call("helloworld") == "hello world" + assert l1.rpc.call("helloworld", {"name": "test"}) == "hello test" + l1.stop() + l1.daemon.opts["plugin"] = plugin + l1.daemon.opts["name"] = "test_opt" + l1.start() + assert l1.rpc.call("helloworld") == "hello test_opt" + # But param takes over! + assert l1.rpc.call("helloworld", {"name": "test"}) == "hello test"