From: Michal Privoznik <[email protected]>

Signed-off-by: Michal Privoznik <[email protected]>
---
 src/network/bridge_driver.c                   |   2 +-
 .../bandwidth-network.conf                    |  16 +++
 .../nat-network-dns-srv-records.conf          |  20 +++
 .../nat-network-dns-srv-records.hostsfile     |   2 +
 .../nat-network-explicit-flood.conf           |  18 +++
 .../nat-network-explicit-flood.hostsfile      |   2 +
 .../nat-network-forward-nat-address.conf      |  18 +++
 .../nat-network-forward-nat-address.hostsfile |   2 +
 .../nat-network-forward-nat-ipv6.conf         |  12 ++
 .../nat-network-forward-nat-no-address.conf   |  18 +++
 ...t-network-forward-nat-no-address.hostsfile |   2 +
 tests/networkxml2confdata/passthrough-pf.conf |  11 ++
 tests/networkxml2confdata/set-mtu.conf        |  20 +++
 tests/networkxml2xmltest.c                    | 136 +++++++++++++++++-
 14 files changed, 277 insertions(+), 2 deletions(-)
 create mode 100644 tests/networkxml2confdata/bandwidth-network.conf
 create mode 100644 tests/networkxml2confdata/nat-network-dns-srv-records.conf
 create mode 100644 
tests/networkxml2confdata/nat-network-dns-srv-records.hostsfile
 create mode 100644 tests/networkxml2confdata/nat-network-explicit-flood.conf
 create mode 100644 
tests/networkxml2confdata/nat-network-explicit-flood.hostsfile
 create mode 100644 
tests/networkxml2confdata/nat-network-forward-nat-address.conf
 create mode 100644 
tests/networkxml2confdata/nat-network-forward-nat-address.hostsfile
 create mode 100644 tests/networkxml2confdata/nat-network-forward-nat-ipv6.conf
 create mode 100644 
tests/networkxml2confdata/nat-network-forward-nat-no-address.conf
 create mode 100644 
tests/networkxml2confdata/nat-network-forward-nat-no-address.hostsfile
 create mode 100644 tests/networkxml2confdata/passthrough-pf.conf
 create mode 100644 tests/networkxml2confdata/set-mtu.conf

diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c
index 9636984390..104c298683 100644
--- a/src/network/bridge_driver.c
+++ b/src/network/bridge_driver.c
@@ -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
index 0000000000..d45b23ad7a
--- /dev/null
+++ b/tests/networkxml2confdata/bandwidth-network.conf
@@ -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
index 0000000000..b85eb85af3
--- /dev/null
+++ b/tests/networkxml2confdata/nat-network-dns-srv-records.conf
@@ -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
index 0000000000..deb3f00ac1
--- /dev/null
+++ b/tests/networkxml2confdata/nat-network-dns-srv-records.hostsfile
@@ -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
index 0000000000..873a360acc
--- /dev/null
+++ b/tests/networkxml2confdata/nat-network-explicit-flood.conf
@@ -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
index 0000000000..deb3f00ac1
--- /dev/null
+++ b/tests/networkxml2confdata/nat-network-explicit-flood.hostsfile
@@ -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
index 0000000000..873a360acc
--- /dev/null
+++ b/tests/networkxml2confdata/nat-network-forward-nat-address.conf
@@ -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
index 0000000000..deb3f00ac1
--- /dev/null
+++ b/tests/networkxml2confdata/nat-network-forward-nat-address.hostsfile
@@ -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
index 0000000000..3627bbaf1d
--- /dev/null
+++ b/tests/networkxml2confdata/nat-network-forward-nat-ipv6.conf
@@ -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
index 0000000000..873a360acc
--- /dev/null
+++ b/tests/networkxml2confdata/nat-network-forward-nat-no-address.conf
@@ -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
index 0000000000..deb3f00ac1
--- /dev/null
+++ b/tests/networkxml2confdata/nat-network-forward-nat-no-address.hostsfile
@@ -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
index 0000000000..1957dc8011
--- /dev/null
+++ b/tests/networkxml2confdata/passthrough-pf.conf
@@ -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
index 0000000000..011a288a5b
--- /dev/null
+++ b/tests/networkxml2confdata/set-mtu.conf
@@ -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
diff --git a/tests/networkxml2xmltest.c b/tests/networkxml2xmltest.c
index a8dacfe0f3..1d897c1fe4 100644
--- a/tests/networkxml2xmltest.c
+++ b/tests/networkxml2xmltest.c
@@ -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,163 @@ 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))
+        return EXIT_AM_SKIP;
+
+    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 +335,5 @@ mymain(void)
 
 VIR_TEST_MAIN_PRELOAD(mymain,
                       VIR_TEST_MOCK("virpci"),
+                      VIR_TEST_MOCK("virdnsmasq"),
                       VIR_TEST_MOCK("virrandom"))
-- 
2.52.0

Reply via email to