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

Andrea Bolognani abologna at redhat.com
Fri Jan 14 16:49:09 UTC 2022


On Wed, Jan 12, 2022 at 09:47:56AM +0100, Michal Privoznik wrote:
> 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(-)

This all works, but I wonder if we couldn't just create a trivial
shell script that behaves minimally the way we expect dnsmasq to, and
change our virFindFileInPath() mock so that it returns the absolute
path to it? That way we wouldn't need to implement any additional
mocking and the code would end up being much simpler. Diff below.

Also note that there is a pre-existing issue with the test, in that
we don't check that the value returned by dnsmasqCapsNewFrom*() is
non-NULL, and as a result if you change the version number in the
test string to something like 0.1 the test will still pass where it
definitely shouldn't.


diff --git a/tests/networkmock.c b/tests/networkmock.c
index a9c13311a6..dc1209a367 100644
--- a/tests/networkmock.c
+++ b/tests/networkmock.c
@@ -23,7 +23,7 @@ char *
 virFindFileInPath(const char *file)
 {
     if (file && g_strrstr(file, "dnsmasq"))
-        return g_strdup(file);
+        return g_strdup_printf("%s/virdnsmasqmock.sh", abs_srcdir);

     /* We should not need any other binaries so return NULL. */
     return NULL;
diff --git a/tests/networkxml2conftest.c b/tests/networkxml2conftest.c
index 8a6657654a..fd2116756e 100644
--- a/tests/networkxml2conftest.c
+++ b/tests/networkxml2conftest.c
@@ -114,7 +114,7 @@ mymain(void)
     int ret = 0;
     g_autoptr(dnsmasqCaps) full = NULL;

-    full = dnsmasqCapsNewFromBuffer("Dnsmasq version
2.67\n--bind-dynamic\n--ra-param");
+    full = dnsmasqCapsNewFromBinary();

 #define DO_TEST(xname, xcaps) \
     do { \
diff --git a/tests/virdnsmasqmock.sh b/tests/virdnsmasqmock.sh
new file mode 100755
index 0000000000..faaa94268c
--- /dev/null
+++ b/tests/virdnsmasqmock.sh
@@ -0,0 +1,16 @@
+#!/bin/sh
+
+case "$1" in
+   "--version")
+     echo "Dnsmasq version 2.67"
+     ;;
+   "--help")
+     echo "--bind-dynamic"
+     echo "--ra-param"
+     ;;
+   *)
+     exit 1
+     ;;
+esac
+
+exit 0
-- 
Andrea Bolognani / Red Hat / Virtualization




More information about the libvir-list mailing list