From ec9ee676b46dd9d9e778419d4ad6ec99872db7b1 Mon Sep 17 00:00:00 2001 From: Michal Privoznik Date: Wed, 12 Jan 2022 05:42:41 +0100 Subject: [PATCH] networkxml2conftest: Use dnsmasqCapsNewFromBinary() to construct caps DISCLAIMER: dnsmasq capabilities are empty as of v8.0.0-rc1~145. In a real environment the dnsmasq capabilities are constructed using dnsmasqCapsNewFromBinary(). We also have dnsmasqCapsNewFromBuffer() to bypass checks that real code is doing and just get capabilities object. The latter is used from test suite. However, with a little bit of mocking we can test the real life code. All that's needed is to simulate dnsmasq's output for --version and --help and mock a stat() that's done in dnsmasqCapsRefreshInternal(). Signed-off-by: Michal Privoznik Reviewed-by: Andrea Bolognani --- tests/networkxml2conftest.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/tests/networkxml2conftest.c b/tests/networkxml2conftest.c index 13257c749b..718a031879 100644 --- a/tests/networkxml2conftest.c +++ b/tests/networkxml2conftest.c @@ -12,6 +12,8 @@ #include "viralloc.h" #include "network/bridge_driver.h" #include "virstring.h" +#define LIBVIRT_VIRCOMMANDPRIV_H_ALLOW +#include "vircommandpriv.h" #define VIR_FROM_THIS VIR_FROM_NONE @@ -108,13 +110,44 @@ testCompareXMLToConfHelper(const void *data) return result; } +static void +buildCapsCallback(const char *const*args, + const char *const*env G_GNUC_UNUSED, + const char *input G_GNUC_UNUSED, + char **output, + char **error G_GNUC_UNUSED, + int *status, + void *opaque G_GNUC_UNUSED) +{ + if (STREQ(args[0], "/usr/sbin/dnsmasq") && STREQ(args[1], "--version")) { + *output = g_strdup("Dnsmasq version 2.67\n"); + *status = EXIT_SUCCESS; + } else { + *status = EXIT_FAILURE; + } +} + +static dnsmasqCaps * +buildCaps(void) +{ + g_autoptr(dnsmasqCaps) caps = NULL; + g_autoptr(virCommandDryRunToken) dryRunToken = virCommandDryRunTokenNew(); + + virCommandSetDryRun(dryRunToken, NULL, true, true, buildCapsCallback, NULL); + + caps = dnsmasqCapsNewFromBinary(); + + return g_steal_pointer(&caps); +} + + static int mymain(void) { int ret = 0; g_autoptr(dnsmasqCaps) full = NULL; - full = dnsmasqCapsNewFromBuffer("Dnsmasq version 2.67"); + full = buildCaps(); #define DO_TEST(xname, xcaps) \ do { \