]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
networkxml2xmltest: Do conf test
authorMichal Privoznik <mprivozn@redhat.com>
Wed, 17 Dec 2025 10:51:07 +0000 (11:51 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Thu, 29 Jan 2026 12:39:16 +0000 (13:39 +0100)
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
14 files changed:
src/network/bridge_driver.c
tests/networkxml2confdata/bandwidth-network.conf [new file with mode: 0644]
tests/networkxml2confdata/nat-network-dns-srv-records.conf [new file with mode: 0644]
tests/networkxml2confdata/nat-network-dns-srv-records.hostsfile [new file with mode: 0644]
tests/networkxml2confdata/nat-network-explicit-flood.conf [new file with mode: 0644]
tests/networkxml2confdata/nat-network-explicit-flood.hostsfile [new file with mode: 0644]
tests/networkxml2confdata/nat-network-forward-nat-address.conf [new file with mode: 0644]
tests/networkxml2confdata/nat-network-forward-nat-address.hostsfile [new file with mode: 0644]
tests/networkxml2confdata/nat-network-forward-nat-ipv6.conf [new file with mode: 0644]
tests/networkxml2confdata/nat-network-forward-nat-no-address.conf [new file with mode: 0644]
tests/networkxml2confdata/nat-network-forward-nat-no-address.hostsfile [new file with mode: 0644]
tests/networkxml2confdata/passthrough-pf.conf [new file with mode: 0644]
tests/networkxml2confdata/set-mtu.conf [new file with mode: 0644]
tests/networkxml2xmltest.c

index 96369843909daaa8828eba5d6e99a612d37f315b..104c2986833c2fd3af35f7dda7aa91d1f4fd5753 100644 (file)
@@ -1178,7 +1178,7 @@ networkDnsmasqConfContents(virNetworkObj *obj,
 
     /* create dnsmasq config file appropriate for this network */
 
-    /* Don't forget to update networkxml2conftest :-) */
+    /* Don't forget to update networkxml2conftest and networkxml2xmltest :-) */
     virBufferAsprintf(&configbuf,
                       "##WARNING:  THIS IS AN AUTO-GENERATED FILE. "
                       "CHANGES TO IT ARE LIKELY TO BE\n"
diff --git a/tests/networkxml2confdata/bandwidth-network.conf b/tests/networkxml2confdata/bandwidth-network.conf
new file mode 100644 (file)
index 0000000..d45b23a
--- /dev/null
@@ -0,0 +1,16 @@
+##WARNING:  THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
+##OVERWRITTEN AND LOST.  Changes to this configuration should be made using:
+##    virsh net-edit test-net
+## or other application using the libvirt API.
+##
+## dnsmasq conf file created by libvirt
+strict-order
+except-interface=lo
+bind-dynamic
+interface=virbr1
+dhcp-range=192.168.120.2,192.168.120.254,255.255.255.0
+dhcp-no-override
+dhcp-authoritative
+dhcp-lease-max=253
+dhcp-hostsfile=/var/lib/libvirt/dnsmasq/test-net.hostsfile
+addn-hosts=/var/lib/libvirt/dnsmasq/test-net.addnhosts
diff --git a/tests/networkxml2confdata/nat-network-dns-srv-records.conf b/tests/networkxml2confdata/nat-network-dns-srv-records.conf
new file mode 100644 (file)
index 0000000..b85eb85
--- /dev/null
@@ -0,0 +1,20 @@
+##WARNING:  THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
+##OVERWRITTEN AND LOST.  Changes to this configuration should be made using:
+##    virsh net-edit default
+## or other application using the libvirt API.
+##
+## dnsmasq conf file created by libvirt
+strict-order
+except-interface=lo
+bind-dynamic
+interface=virbr0
+srv-host=_name._tcp.test-domain-name
+srv-host=_name._tcp.donkey
+dhcp-range=192.168.122.2,192.168.122.254,255.255.255.0
+dhcp-no-override
+dhcp-authoritative
+dhcp-lease-max=253
+dhcp-hostsfile=/var/lib/libvirt/dnsmasq/default.hostsfile
+addn-hosts=/var/lib/libvirt/dnsmasq/default.addnhosts
+dhcp-range=2001:db8:ac10:fe01::1,ra-only
+dhcp-range=2001:db8:ac10:fd01::1,ra-only
diff --git a/tests/networkxml2confdata/nat-network-dns-srv-records.hostsfile b/tests/networkxml2confdata/nat-network-dns-srv-records.hostsfile
new file mode 100644 (file)
index 0000000..deb3f00
--- /dev/null
@@ -0,0 +1,2 @@
+00:16:3e:77:e2:ed,192.168.122.10,a.example.com
+00:16:3e:3e:a9:1a,192.168.122.11,b.example.com
diff --git a/tests/networkxml2confdata/nat-network-explicit-flood.conf b/tests/networkxml2confdata/nat-network-explicit-flood.conf
new file mode 100644 (file)
index 0000000..873a360
--- /dev/null
@@ -0,0 +1,18 @@
+##WARNING:  THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
+##OVERWRITTEN AND LOST.  Changes to this configuration should be made using:
+##    virsh net-edit default
+## or other application using the libvirt API.
+##
+## dnsmasq conf file created by libvirt
+strict-order
+except-interface=lo
+bind-dynamic
+interface=virbr0
+dhcp-range=192.168.122.2,192.168.122.254,255.255.255.0
+dhcp-no-override
+dhcp-authoritative
+dhcp-lease-max=253
+dhcp-hostsfile=/var/lib/libvirt/dnsmasq/default.hostsfile
+addn-hosts=/var/lib/libvirt/dnsmasq/default.addnhosts
+dhcp-range=2001:db8:ac10:fe01::1,ra-only
+dhcp-range=2001:db8:ac10:fd01::1,ra-only
diff --git a/tests/networkxml2confdata/nat-network-explicit-flood.hostsfile b/tests/networkxml2confdata/nat-network-explicit-flood.hostsfile
new file mode 100644 (file)
index 0000000..deb3f00
--- /dev/null
@@ -0,0 +1,2 @@
+00:16:3e:77:e2:ed,192.168.122.10,a.example.com
+00:16:3e:3e:a9:1a,192.168.122.11,b.example.com
diff --git a/tests/networkxml2confdata/nat-network-forward-nat-address.conf b/tests/networkxml2confdata/nat-network-forward-nat-address.conf
new file mode 100644 (file)
index 0000000..873a360
--- /dev/null
@@ -0,0 +1,18 @@
+##WARNING:  THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
+##OVERWRITTEN AND LOST.  Changes to this configuration should be made using:
+##    virsh net-edit default
+## or other application using the libvirt API.
+##
+## dnsmasq conf file created by libvirt
+strict-order
+except-interface=lo
+bind-dynamic
+interface=virbr0
+dhcp-range=192.168.122.2,192.168.122.254,255.255.255.0
+dhcp-no-override
+dhcp-authoritative
+dhcp-lease-max=253
+dhcp-hostsfile=/var/lib/libvirt/dnsmasq/default.hostsfile
+addn-hosts=/var/lib/libvirt/dnsmasq/default.addnhosts
+dhcp-range=2001:db8:ac10:fe01::1,ra-only
+dhcp-range=2001:db8:ac10:fd01::1,ra-only
diff --git a/tests/networkxml2confdata/nat-network-forward-nat-address.hostsfile b/tests/networkxml2confdata/nat-network-forward-nat-address.hostsfile
new file mode 100644 (file)
index 0000000..deb3f00
--- /dev/null
@@ -0,0 +1,2 @@
+00:16:3e:77:e2:ed,192.168.122.10,a.example.com
+00:16:3e:3e:a9:1a,192.168.122.11,b.example.com
diff --git a/tests/networkxml2confdata/nat-network-forward-nat-ipv6.conf b/tests/networkxml2confdata/nat-network-forward-nat-ipv6.conf
new file mode 100644 (file)
index 0000000..3627bba
--- /dev/null
@@ -0,0 +1,12 @@
+##WARNING:  THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
+##OVERWRITTEN AND LOST.  Changes to this configuration should be made using:
+##    virsh net-edit default
+## or other application using the libvirt API.
+##
+## dnsmasq conf file created by libvirt
+strict-order
+except-interface=lo
+bind-dynamic
+interface=virbr0
+addn-hosts=/var/lib/libvirt/dnsmasq/default.addnhosts
+dhcp-range=2001:db8:ac10:fe01::1,ra-only
diff --git a/tests/networkxml2confdata/nat-network-forward-nat-no-address.conf b/tests/networkxml2confdata/nat-network-forward-nat-no-address.conf
new file mode 100644 (file)
index 0000000..873a360
--- /dev/null
@@ -0,0 +1,18 @@
+##WARNING:  THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
+##OVERWRITTEN AND LOST.  Changes to this configuration should be made using:
+##    virsh net-edit default
+## or other application using the libvirt API.
+##
+## dnsmasq conf file created by libvirt
+strict-order
+except-interface=lo
+bind-dynamic
+interface=virbr0
+dhcp-range=192.168.122.2,192.168.122.254,255.255.255.0
+dhcp-no-override
+dhcp-authoritative
+dhcp-lease-max=253
+dhcp-hostsfile=/var/lib/libvirt/dnsmasq/default.hostsfile
+addn-hosts=/var/lib/libvirt/dnsmasq/default.addnhosts
+dhcp-range=2001:db8:ac10:fe01::1,ra-only
+dhcp-range=2001:db8:ac10:fd01::1,ra-only
diff --git a/tests/networkxml2confdata/nat-network-forward-nat-no-address.hostsfile b/tests/networkxml2confdata/nat-network-forward-nat-no-address.hostsfile
new file mode 100644 (file)
index 0000000..deb3f00
--- /dev/null
@@ -0,0 +1,2 @@
+00:16:3e:77:e2:ed,192.168.122.10,a.example.com
+00:16:3e:3e:a9:1a,192.168.122.11,b.example.com
diff --git a/tests/networkxml2confdata/passthrough-pf.conf b/tests/networkxml2confdata/passthrough-pf.conf
new file mode 100644 (file)
index 0000000..1957dc8
--- /dev/null
@@ -0,0 +1,11 @@
+##WARNING:  THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
+##OVERWRITTEN AND LOST.  Changes to this configuration should be made using:
+##    virsh net-edit local
+## or other application using the libvirt API.
+##
+## dnsmasq conf file created by libvirt
+strict-order
+except-interface=lo
+bind-dynamic
+interface=(null)
+addn-hosts=/var/lib/libvirt/dnsmasq/local.addnhosts
diff --git a/tests/networkxml2confdata/set-mtu.conf b/tests/networkxml2confdata/set-mtu.conf
new file mode 100644 (file)
index 0000000..011a288
--- /dev/null
@@ -0,0 +1,20 @@
+##WARNING:  THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
+##OVERWRITTEN AND LOST.  Changes to this configuration should be made using:
+##    virsh net-edit private
+## or other application using the libvirt API.
+##
+## dnsmasq conf file created by libvirt
+strict-order
+except-interface=lo
+bind-dynamic
+interface=virbr2
+dhcp-option=3
+no-resolv
+ra-param=*,0,0
+dhcp-range=192.168.152.2,192.168.152.254,255.255.255.0
+dhcp-no-override
+dhcp-authoritative
+dhcp-lease-max=253
+dhcp-hostsfile=/var/lib/libvirt/dnsmasq/private.hostsfile
+addn-hosts=/var/lib/libvirt/dnsmasq/private.addnhosts
+dhcp-option=option:mtu,9000
index a8dacfe0f36a666aeb943f7bc6afe08ae544e0b2..5961084c67dcd0474377b7170a0f991a62fd082b 100644 (file)
@@ -11,6 +11,8 @@
 #include "bridge_driver.h"
 #define LIBVIRT_BRIDGE_DRIVER_PRIV_H_ALLOW
 #include "bridge_driver_priv.h"
+#define LIBVIRT_VIRCOMMANDPRIV_H_ALLOW
+#include "vircommandpriv.h"
 
 #define VIR_FROM_THIS VIR_FROM_NONE
 
@@ -27,9 +29,12 @@ struct _testInfo {
     unsigned int flags;
     testCompareNetXML2XMLResult expectResult;
     virNetworkXMLOption *xmlopt; /* borrowed, immutable */
+    dnsmasqCaps *caps;
     virNetworkDef *def;
     char *inxml;
     char *outxml;
+    char *outconf;
+    char *outhostsfile;
 };
 
 typedef struct _testInfo testInfo;
@@ -44,6 +49,8 @@ void testInfoFree(testInfo *info)
     virNetworkDefFree(info->def);
     VIR_FREE(info->inxml);
     VIR_FREE(info->outxml);
+    VIR_FREE(info->outconf);
+    VIR_FREE(info->outhostsfile);
     VIR_FREE(info);
 }
 
@@ -104,37 +111,165 @@ testCompareXMLToXMLFiles(const void *data)
 }
 
 
+static int
+testCompareXMLToConfFiles(const void *data)
+{
+    testInfo *info = (void *) data;
+    char *confactual = NULL;
+    g_autofree char *hostsfileactual = NULL;
+    int ret = -1;
+    virNetworkDef *def = NULL;
+    virNetworkObj *obj = NULL;
+    g_autofree char *pidfile = NULL;
+    g_autoptr(dnsmasqContext) dctx = NULL;
+    bool compareFailed = false;
+
+    if (!(obj = virNetworkObjNew()))
+        goto fail;
+
+    if (!(def = g_steal_pointer(&info->def))) {
+        /* Previous test wasn't executed. */
+        if (!(def = virNetworkDefParse(NULL, info->inxml, info->xmlopt, false)))
+            goto fail;
+
+        if (networkValidateTests(def) < 0) {
+            virNetworkDefFree(def);
+            goto fail;
+        }
+    }
+
+    virNetworkObjSetDef(obj, def);
+
+    if (!networkNeedsDnsmasq(def)) {
+        ret = EXIT_AM_SKIP;
+        goto fail;
+    }
+
+    dctx = dnsmasqContextNew(def->name, "/var/lib/libvirt/dnsmasq");
+
+    if (dctx == NULL)
+        goto fail;
+
+    if (networkDnsmasqConfContents(obj, pidfile, &confactual,
+                                   &hostsfileactual, dctx, info->caps) < 0)
+        goto fail;
+
+    /* Any changes to this function ^^ should be reflected here too. */
+#ifndef __linux__
+    {
+        char * tmp;
+
+        if (!(tmp = virStringReplace(confactual,
+                                     "except-interface=lo0\n",
+                                     "except-interface=lo\n")))
+            goto fail;
+        VIR_FREE(confactual);
+        confactual = g_steal_pointer(&tmp);
+    }
+#endif
+
+    if (virTestCompareToFile(confactual, info->outconf) < 0)
+        compareFailed = true;
+
+    if (hostsfileactual) {
+        if (virTestCompareToFile(hostsfileactual, info->outhostsfile) < 0) {
+            compareFailed = true;
+        }
+    } else {
+        if (virFileExists(info->outhostsfile)) {
+            VIR_TEST_DEBUG("%s: hostsfile exists but the configuration did not specify any host",
+                           info->outhostsfile);
+            compareFailed = true;
+        }
+    }
+
+    if (compareFailed)
+        goto fail;
+
+    ret = 0;
+
+ fail:
+    VIR_FREE(confactual);
+    virNetworkObjEndAPI(&obj);
+    return ret;
+}
+
+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 void
 testRun(const char *name,
         int *ret,
         virNetworkXMLOption *xmlopt,
+        dnsmasqCaps *caps,
         testCompareNetXML2XMLResult expectResult,
         unsigned int flags)
 {
     g_autofree char *name_xml2xml = g_strdup_printf("Network XML-2-XML %s", name);
+    g_autofree char *name_xml2conf = g_strdup_printf("Network XML-2-Conf %s", name);
     g_autoptr(testInfo) info = g_new0(testInfo, 1);
 
     info->name = name;
     info->flags = flags;
     info->expectResult = expectResult;
     info->xmlopt = xmlopt;
+    info->caps = caps;
     info->inxml = g_strdup_printf("%s/networkxml2xmlin/%s.xml", abs_srcdir, name);
     info->outxml = g_strdup_printf("%s/networkxml2xmlout/%s.xml", abs_srcdir, name);
+    /* Temporarily use conf files from networkxml2confdata/ */
+    info->outconf = g_strdup_printf("%s/networkxml2confdata/%s.conf", abs_srcdir, name);
+    info->outhostsfile = g_strdup_printf("%s/networkxml2confdata/%s.hostsfile", abs_srcdir, name);
 
     virTestRunLog(ret, name_xml2xml, testCompareXMLToXMLFiles, info);
+
+    if (expectResult == TEST_COMPARE_NET_XML2XML_RESULT_SUCCESS)
+        virTestRunLog(ret, name_xml2conf, testCompareXMLToConfFiles, info);
 }
 
 static int
 mymain(void)
 {
     g_autoptr(virNetworkXMLOption) xmlopt = NULL;
+    g_autoptr(dnsmasqCaps) caps = NULL;
     int ret = 0;
 
     if (!(xmlopt = networkDnsmasqCreateXMLConf()))
         return -1;
 
+    if (!(caps = buildCaps()))
+        return -1;
+
 #define DO_TEST_FULL(name, flags, expectResult) \
-    testRun(name, &ret, xmlopt, expectResult, flags)
+    testRun(name, &ret, xmlopt, caps, expectResult, flags)
 #define DO_TEST(name) \
     DO_TEST_FULL(name, 0, TEST_COMPARE_NET_XML2XML_RESULT_SUCCESS)
 #define DO_TEST_FLAGS(name, flags) \
@@ -202,4 +337,5 @@ mymain(void)
 
 VIR_TEST_MAIN_PRELOAD(mymain,
                       VIR_TEST_MOCK("virpci"),
+                      VIR_TEST_MOCK("virdnsmasq"),
                       VIR_TEST_MOCK("virrandom"))