libvirt/tests/networkxml2conftest.c

159 lines
4.1 KiB
C
Raw Normal View History

#include <config.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include "internal.h"
#include "testutils.h"
#include "network_conf.h"
#include "vircommand.h"
2012-12-12 18:06:53 +00:00
#include "viralloc.h"
#include "network/bridge_driver.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
static int
testCompareXMLToConfFiles(const char *inxml, const char *outconf, dnsmasqCapsPtr caps)
{
char *actual = NULL;
int ret = -1;
virNetworkDefPtr def = NULL;
virNetworkObjPtr obj = NULL;
virCommandPtr cmd = NULL;
char *pidfile = NULL;
network: Fix dnsmasq hostsfile creation logic and related tests networkSaveDnsmasqHostsfile was added in 8fa9c2214247 (Apr 2010). It has a force flag. If the dnsmasq hostsfile already exists force needs to be true to overwrite it. networkBuildDnsmasqArgv sets force to false, networkDefine sets it to true. This results in the hostsfile being written only in networkDefine in the common case. If no error occurred networkSaveDnsmasqHostsfile returns true and networkBuildDnsmasqArgv adds the --dhcp-hostsfile to the dnsmasq command line. networkSaveDnsmasqHostsfile was changed in 89ae9849f744 (24 Jun 2011) to return a new dnsmasqContext instead of reusing one. This change broke the logic of the force flag as now networkSaveDnsmasqHostsfile returns NULL on error, but the early return -- if force was not set and the hostsfile exists -- returns 0. This turned the early return in an error case and networkBuildDnsmasqArgv didn't add the --dhcp-hostsfile option anymore if the hostsfile already exists. It did because networkDefine created the hostsfile already. Then 9d4e2845d498 fixed the return 0 case in networkSaveDnsmasqHostsfile but didn't apply the force option correctly to the new addnhosts file. Now force doesn't control an early return anymore, but influences the handling of the hostsfile context creation and dnsmasqSave is always called now. This commit also added test cases that reveal several problems. First, the tests now calls functions that try to write the dnsmasq config files to disk. If someone runs this tests as root this might overwrite actively used dnsmasq config files, this is a no-go. Also the tests depend on configure --localstatedir, this needs to be fixed as well, because it makes the tests fail when localstatedir is different from /var. This patch does several things to fix this: 1) Move dnsmasqContext creation and saving out of networkBuildDnsmasqArgv to the caller to separate the command line generation from the config file writing. This makes the command line generation testable without the risk of interfering with system files, because the tests just don't call dnsmasqSave. 2) This refactoring of networkSaveDnsmasqHostsfile makes the force flag useless as the saving happens somewhere else now. This fixes the wrong usage of the force flag in combination with then newly added addnhosts file by removing the force flag. 3) Adapt the wrong test cases to the correct behavior, by adding the missing --dhcp-hostsfile option. Both affected tests contain DHCP host elements but missed the necessary --dhcp-hostsfile option. 4) Rename networkSaveDnsmasqHostsfile to networkBuildDnsmasqHostsfile, because it doesn't save the dnsmasqContext anymore. 5) Move all directory creations in dnsmasq context handling code from the *New functions to dnsmasqSave to avoid directory creations in system paths in the test cases. 6) Now that networkBuildDnsmasqArgv doesn't create the dnsmasqContext anymore the test case can create one with the localstatedir that is expected by the tests instead of the configure --localstatedir given one.
2011-06-28 11:07:59 +00:00
dnsmasqContext *dctx = NULL;
virNetworkXMLOptionPtr xmlopt = NULL;
if (!(xmlopt = networkDnsmasqCreateXMLConf()))
goto fail;
if (!(def = virNetworkDefParseFile(inxml, xmlopt)))
goto fail;
if (!(obj = virNetworkObjNew()))
goto fail;
virNetworkObjSetDef(obj, def);
dctx = dnsmasqContextNew(def->name, "/var/lib/libvirt/dnsmasq");
network: Fix dnsmasq hostsfile creation logic and related tests networkSaveDnsmasqHostsfile was added in 8fa9c2214247 (Apr 2010). It has a force flag. If the dnsmasq hostsfile already exists force needs to be true to overwrite it. networkBuildDnsmasqArgv sets force to false, networkDefine sets it to true. This results in the hostsfile being written only in networkDefine in the common case. If no error occurred networkSaveDnsmasqHostsfile returns true and networkBuildDnsmasqArgv adds the --dhcp-hostsfile to the dnsmasq command line. networkSaveDnsmasqHostsfile was changed in 89ae9849f744 (24 Jun 2011) to return a new dnsmasqContext instead of reusing one. This change broke the logic of the force flag as now networkSaveDnsmasqHostsfile returns NULL on error, but the early return -- if force was not set and the hostsfile exists -- returns 0. This turned the early return in an error case and networkBuildDnsmasqArgv didn't add the --dhcp-hostsfile option anymore if the hostsfile already exists. It did because networkDefine created the hostsfile already. Then 9d4e2845d498 fixed the return 0 case in networkSaveDnsmasqHostsfile but didn't apply the force option correctly to the new addnhosts file. Now force doesn't control an early return anymore, but influences the handling of the hostsfile context creation and dnsmasqSave is always called now. This commit also added test cases that reveal several problems. First, the tests now calls functions that try to write the dnsmasq config files to disk. If someone runs this tests as root this might overwrite actively used dnsmasq config files, this is a no-go. Also the tests depend on configure --localstatedir, this needs to be fixed as well, because it makes the tests fail when localstatedir is different from /var. This patch does several things to fix this: 1) Move dnsmasqContext creation and saving out of networkBuildDnsmasqArgv to the caller to separate the command line generation from the config file writing. This makes the command line generation testable without the risk of interfering with system files, because the tests just don't call dnsmasqSave. 2) This refactoring of networkSaveDnsmasqHostsfile makes the force flag useless as the saving happens somewhere else now. This fixes the wrong usage of the force flag in combination with then newly added addnhosts file by removing the force flag. 3) Adapt the wrong test cases to the correct behavior, by adding the missing --dhcp-hostsfile option. Both affected tests contain DHCP host elements but missed the necessary --dhcp-hostsfile option. 4) Rename networkSaveDnsmasqHostsfile to networkBuildDnsmasqHostsfile, because it doesn't save the dnsmasqContext anymore. 5) Move all directory creations in dnsmasq context handling code from the *New functions to dnsmasqSave to avoid directory creations in system paths in the test cases. 6) Now that networkBuildDnsmasqArgv doesn't create the dnsmasqContext anymore the test case can create one with the localstatedir that is expected by the tests instead of the configure --localstatedir given one.
2011-06-28 11:07:59 +00:00
if (dctx == NULL)
goto fail;
if (networkDnsmasqConfContents(obj, pidfile, &actual, dctx, caps) < 0)
goto fail;
/* Any changes to this function ^^ should be reflected here too. */
#ifndef __linux__
char * tmp;
if (!(tmp = virStringReplace(actual,
"except-interface=lo0\n",
"except-interface=lo\n")))
goto fail;
VIR_FREE(actual);
VIR_STEAL_PTR(actual, tmp);
#endif
if (virTestCompareToFile(actual, outconf) < 0)
goto fail;
ret = 0;
fail:
VIR_FREE(actual);
VIR_FREE(pidfile);
virCommandFree(cmd);
virObjectUnref(xmlopt);
virNetworkObjEndAPI(&obj);
network: Fix dnsmasq hostsfile creation logic and related tests networkSaveDnsmasqHostsfile was added in 8fa9c2214247 (Apr 2010). It has a force flag. If the dnsmasq hostsfile already exists force needs to be true to overwrite it. networkBuildDnsmasqArgv sets force to false, networkDefine sets it to true. This results in the hostsfile being written only in networkDefine in the common case. If no error occurred networkSaveDnsmasqHostsfile returns true and networkBuildDnsmasqArgv adds the --dhcp-hostsfile to the dnsmasq command line. networkSaveDnsmasqHostsfile was changed in 89ae9849f744 (24 Jun 2011) to return a new dnsmasqContext instead of reusing one. This change broke the logic of the force flag as now networkSaveDnsmasqHostsfile returns NULL on error, but the early return -- if force was not set and the hostsfile exists -- returns 0. This turned the early return in an error case and networkBuildDnsmasqArgv didn't add the --dhcp-hostsfile option anymore if the hostsfile already exists. It did because networkDefine created the hostsfile already. Then 9d4e2845d498 fixed the return 0 case in networkSaveDnsmasqHostsfile but didn't apply the force option correctly to the new addnhosts file. Now force doesn't control an early return anymore, but influences the handling of the hostsfile context creation and dnsmasqSave is always called now. This commit also added test cases that reveal several problems. First, the tests now calls functions that try to write the dnsmasq config files to disk. If someone runs this tests as root this might overwrite actively used dnsmasq config files, this is a no-go. Also the tests depend on configure --localstatedir, this needs to be fixed as well, because it makes the tests fail when localstatedir is different from /var. This patch does several things to fix this: 1) Move dnsmasqContext creation and saving out of networkBuildDnsmasqArgv to the caller to separate the command line generation from the config file writing. This makes the command line generation testable without the risk of interfering with system files, because the tests just don't call dnsmasqSave. 2) This refactoring of networkSaveDnsmasqHostsfile makes the force flag useless as the saving happens somewhere else now. This fixes the wrong usage of the force flag in combination with then newly added addnhosts file by removing the force flag. 3) Adapt the wrong test cases to the correct behavior, by adding the missing --dhcp-hostsfile option. Both affected tests contain DHCP host elements but missed the necessary --dhcp-hostsfile option. 4) Rename networkSaveDnsmasqHostsfile to networkBuildDnsmasqHostsfile, because it doesn't save the dnsmasqContext anymore. 5) Move all directory creations in dnsmasq context handling code from the *New functions to dnsmasqSave to avoid directory creations in system paths in the test cases. 6) Now that networkBuildDnsmasqArgv doesn't create the dnsmasqContext anymore the test case can create one with the localstatedir that is expected by the tests instead of the configure --localstatedir given one.
2011-06-28 11:07:59 +00:00
dnsmasqContextFree(dctx);
return ret;
}
typedef struct {
const char *name;
dnsmasqCapsPtr caps;
} testInfo;
static int
testCompareXMLToConfHelper(const void *data)
{
int result = -1;
const testInfo *info = data;
char *inxml = NULL;
char *outconf = NULL;
if (virAsprintf(&inxml, "%s/networkxml2confdata/%s.xml",
abs_srcdir, info->name) < 0 ||
virAsprintf(&outconf, "%s/networkxml2confdata/%s.conf",
abs_srcdir, info->name) < 0) {
goto cleanup;
}
result = testCompareXMLToConfFiles(inxml, outconf, info->caps);
cleanup:
VIR_FREE(inxml);
VIR_FREE(outconf);
return result;
}
static int
mymain(void)
{
int ret = 0;
dnsmasqCapsPtr restricted
= dnsmasqCapsNewFromBuffer("Dnsmasq version 2.48", DNSMASQ);
dnsmasqCapsPtr full
= dnsmasqCapsNewFromBuffer("Dnsmasq version 2.63\n--bind-dynamic", DNSMASQ);
dnsmasqCapsPtr dhcpv6
= dnsmasqCapsNewFromBuffer("Dnsmasq version 2.64\n--bind-dynamic", DNSMASQ);
#define DO_TEST(xname, xcaps) \
do { \
static testInfo info; \
\
info.name = xname; \
info.caps = xcaps; \
if (virTestRun("Network XML-2-Conf " xname, \
testCompareXMLToConfHelper, &info) < 0) { \
ret = -1; \
} \
} while (0)
DO_TEST("isolated-network", restricted);
DO_TEST("netboot-network", restricted);
DO_TEST("netboot-proxy-network", restricted);
DO_TEST("nat-network-dns-srv-record-minimal", restricted);
DO_TEST("nat-network-name-with-quotes", restricted);
DO_TEST("routed-network", full);
DO_TEST("routed-network-no-dns", full);
DO_TEST("open-network", full);
DO_TEST("nat-network", dhcpv6);
DO_TEST("nat-network-dns-txt-record", full);
DO_TEST("nat-network-dns-srv-record", full);
DO_TEST("nat-network-dns-hosts", full);
DO_TEST("nat-network-dns-forward-plain", full);
DO_TEST("nat-network-dns-forwarders", full);
DO_TEST("nat-network-dns-forwarder-no-resolv", full);
DO_TEST("nat-network-dns-local-domain", full);
DO_TEST("nat-network-mtu", dhcpv6);
DO_TEST("dhcp6-network", dhcpv6);
DO_TEST("dhcp6-nat-network", dhcpv6);
DO_TEST("dhcp6host-routed-network", dhcpv6);
DO_TEST("ptr-domains-auto", dhcpv6);
DO_TEST("dnsmasq-options", dhcpv6);
virObjectUnref(dhcpv6);
virObjectUnref(full);
virObjectUnref(restricted);
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
VIR_TEST_MAIN(mymain)