mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-25 01:34:11 +03:00
9523b3c320
networkSaveDnsmasqHostsfile was added in8fa9c22142
(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 in89ae9849f7
(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. Then9d4e2845d4
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.
129 lines
2.8 KiB
C
129 lines
2.8 KiB
C
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
#include <fcntl.h>
|
|
|
|
#include "internal.h"
|
|
#include "testutils.h"
|
|
#include "network_conf.h"
|
|
#include "command.h"
|
|
#include "memory.h"
|
|
#include "network/bridge_driver.h"
|
|
|
|
static int testCompareXMLToArgvFiles(const char *inxml, const char *outargv) {
|
|
char *inXmlData = NULL;
|
|
char *outArgvData = NULL;
|
|
char *actual = NULL;
|
|
int ret = -1;
|
|
virNetworkDefPtr dev = NULL;
|
|
virNetworkObjPtr obj = NULL;
|
|
virCommandPtr cmd = NULL;
|
|
char *pidfile = NULL;
|
|
dnsmasqContext *dctx = NULL;
|
|
|
|
if (virtTestLoadFile(inxml, &inXmlData) < 0)
|
|
goto fail;
|
|
|
|
if (virtTestLoadFile(outargv, &outArgvData) < 0)
|
|
goto fail;
|
|
|
|
if (!(dev = virNetworkDefParseString(inXmlData)))
|
|
goto fail;
|
|
|
|
if (VIR_ALLOC(obj) < 0)
|
|
goto fail;
|
|
|
|
obj->def = dev;
|
|
dctx = dnsmasqContextNew(dev->name, "/var/lib/libvirt/dnsmasq");
|
|
|
|
if (dctx == NULL)
|
|
goto fail;
|
|
|
|
if (networkBuildDhcpDaemonCommandLine(obj, &cmd, pidfile, dctx) < 0)
|
|
goto fail;
|
|
|
|
if (!(actual = virCommandToString(cmd)))
|
|
goto fail;
|
|
|
|
if (STRNEQ(outArgvData, actual)) {
|
|
virtTestDifference(stderr, outArgvData, actual);
|
|
goto fail;
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
fail:
|
|
free(inXmlData);
|
|
free(outArgvData);
|
|
free(actual);
|
|
VIR_FREE(pidfile);
|
|
virCommandFree(cmd);
|
|
virNetworkObjFree(obj);
|
|
dnsmasqContextFree(dctx);
|
|
return ret;
|
|
}
|
|
|
|
static int
|
|
testCompareXMLToArgvHelper(const void *data)
|
|
{
|
|
int result = -1;
|
|
char *inxml = NULL;
|
|
char *outxml = NULL;
|
|
|
|
if (virAsprintf(&inxml, "%s/networkxml2argvdata/%s.xml",
|
|
abs_srcdir, (const char*)data) < 0 ||
|
|
virAsprintf(&outxml, "%s/networkxml2argvdata/%s.argv",
|
|
abs_srcdir, (const char*)data) < 0) {
|
|
goto cleanup;
|
|
}
|
|
|
|
result = testCompareXMLToArgvFiles(inxml, outxml);
|
|
|
|
cleanup:
|
|
free(inxml);
|
|
free(outxml);
|
|
|
|
return result;
|
|
}
|
|
|
|
static char *
|
|
testDnsmasqLeaseFileName(const char *netname)
|
|
{
|
|
char *leasefile;
|
|
|
|
virAsprintf(&leasefile, "/var/lib/libvirt/dnsmasq/%s.leases",
|
|
netname);
|
|
|
|
return leasefile;
|
|
}
|
|
|
|
static int
|
|
mymain(void)
|
|
{
|
|
int ret = 0;
|
|
|
|
networkDnsmasqLeaseFileName = testDnsmasqLeaseFileName;
|
|
|
|
#define DO_TEST(name) \
|
|
if (virtTestRun("Network XML-2-Argv " name, \
|
|
1, testCompareXMLToArgvHelper, (name)) < 0) \
|
|
ret = -1
|
|
|
|
DO_TEST("isolated-network");
|
|
DO_TEST("routed-network");
|
|
DO_TEST("nat-network");
|
|
DO_TEST("netboot-network");
|
|
DO_TEST("netboot-proxy-network");
|
|
DO_TEST("nat-network-dns-txt-record");
|
|
DO_TEST("nat-network-dns-hosts");
|
|
|
|
return (ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);
|
|
}
|
|
|
|
VIRT_TEST_MAIN(mymain)
|