[PATCH v3 5/7] networkxml2conftest: Use dnsmasqCapsNewFromBinary() to construct caps

Michal Privoznik mprivozn at redhat.com
Wed Jan 12 08:47:56 UTC 2022


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 <mprivozn at redhat.com>
---
 tests/networkmock.c         | 16 ++++++++++++++++
 tests/networkxml2conftest.c | 38 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 53 insertions(+), 1 deletion(-)

diff --git a/tests/networkmock.c b/tests/networkmock.c
index a9c13311a6..25014de8b8 100644
--- a/tests/networkmock.c
+++ b/tests/networkmock.c
@@ -28,3 +28,19 @@ virFindFileInPath(const char *file)
     /* We should not need any other binaries so return NULL. */
     return NULL;
 }
+
+static int
+virMockStatRedirect(const char *path G_GNUC_UNUSED,
+                    char **newpath G_GNUC_UNUSED)
+{
+    /* We don't need to redirect stat. Do nothing. */
+    return 0;
+}
+
+#define VIR_MOCK_STAT_HOOK \
+    if (strstr(path, "dnsmasq")) { \
+        memset(sb, 0, sizeof(*sb)); \
+        return 0; \
+    }
+
+#include "virmockstathelpers.c"
diff --git a/tests/networkxml2conftest.c b/tests/networkxml2conftest.c
index 8a6657654a..68dd3023e1 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,47 @@ 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[1], "--version")) {
+        *output = g_strdup("Dnsmasq version 2.67\n");
+        *status = EXIT_SUCCESS;
+    } else if (STREQ(args[1], "--help")) {
+        *output = g_strdup("--bind-dynamic\n--ra-param");
+        *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\n--bind-dynamic\n--ra-param");
+    full = buildCaps();
 
 #define DO_TEST(xname, xcaps) \
     do { \
-- 
2.34.1




More information about the libvir-list mailing list