[libvirt PATCH 02/14] tests: mock libdbus in networkxml2firewalltest

Pavel Hrdina phrdina at redhat.com
Thu Sep 17 08:29:37 UTC 2020


This test calls into src/util/virfirewalld.c where it uses DBus to
figure out if firewalld is registered. Without the mock it luckily
fails and the test works correctly.

To isolate the tests from host environment we should mock the DBus
calls.

Signed-off-by: Pavel Hrdina <phrdina at redhat.com>
---
 tests/meson.build               |  2 +-
 tests/networkxml2firewalltest.c | 25 +++++++++++++++++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/tests/meson.build b/tests/meson.build
index 0a204c46e4..f0f3d8c1ef 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -404,7 +404,7 @@ endif
 if conf.has('WITH_NETWORK')
   tests += [
     { 'name': 'networkxml2conftest', 'link_with': [ network_driver_impl ] },
-    { 'name': 'networkxml2firewalltest', 'link_with': [ network_driver_impl ] },
+    { 'name': 'networkxml2firewalltest', 'link_with': [ network_driver_impl ], 'deps': [ dbus_dep ] },
     { 'name': 'networkxml2xmltest', 'link_with': [ network_driver_impl ] },
   ]
 endif
diff --git a/tests/networkxml2firewalltest.c b/tests/networkxml2firewalltest.c
index 29e7d8bc38..80e2d6a035 100644
--- a/tests/networkxml2firewalltest.c
+++ b/tests/networkxml2firewalltest.c
@@ -26,8 +26,13 @@
 
 #if defined (__linux__)
 
+# if WITH_DBUS
+#  include <dbus/dbus.h>
+# endif
+
 # include "network/bridge_driver_platform.h"
 # include "virbuffer.h"
+# include "virmock.h"
 
 # define LIBVIRT_VIRFIREWALLPRIV_H_ALLOW
 # include "virfirewallpriv.h"
@@ -43,6 +48,22 @@
 #  error "test case not ported to this platform"
 # endif
 
+# if WITH_DBUS
+VIR_MOCK_WRAP_RET_ARGS(dbus_connection_send_with_reply_and_block,
+                       DBusMessage *,
+                       DBusConnection *, connection,
+                       DBusMessage *, message,
+                       int, timeout_milliseconds,
+                       DBusError *, error)
+{
+    VIR_MOCK_REAL_INIT(dbus_connection_send_with_reply_and_block);
+
+    dbus_set_error_const(error, "org.freedesktop.error", "dbus is disabled");
+
+    return NULL;
+}
+# endif
+
 static void
 testCommandDryRun(const char *const*args G_GNUC_UNUSED,
                   const char *const*env G_GNUC_UNUSED,
@@ -176,7 +197,11 @@ mymain(void)
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }
 
+# if WITH_DBUS
+VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("virdbus"))
+# else
 VIR_TEST_MAIN(mymain)
+# endif
 
 #else /* ! defined (__linux__) */
 
-- 
2.26.2




More information about the libvir-list mailing list