1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2024-12-23 21:34:54 +03:00
libvirt/tests/xmconfigtest.c

258 lines
6.7 KiB
C
Raw Normal View History

/*
* xmconfigtest.c: Test backend for xm_internal config file handling
*
* Copyright (C) 2007, 2010-2011, 2014 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*
* Author: Daniel P. Berrange <berrange@redhat.com>
*
*/
#include <config.h>
#include <stdio.h>
#include <string.h>
2008-04-18 19:28:33 +04:00
#include <unistd.h>
#include "internal.h"
#include "datatypes.h"
#include "xen/xen_driver.h"
#include "xen/xm_internal.h"
#include "xenconfig/xen_xm.h"
#include "testutils.h"
#include "testutilsxen.h"
2012-12-12 22:06:53 +04:00
#include "viralloc.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
static virCapsPtr caps;
static virDomainXMLOptionPtr xmlopt;
static int
testCompareParseXML(const char *xmcfg, const char *xml)
{
char *gotxmcfgData = NULL;
virConfPtr conf = NULL;
int ret = -1;
virConnectPtr conn = NULL;
int wrote = 4096;
struct _xenUnifiedPrivate priv;
virDomainDefPtr def = NULL;
if (VIR_ALLOC_N(gotxmcfgData, wrote) < 0)
goto fail;
2008-11-21 15:16:08 +03:00
conn = virGetConnect();
if (!conn) goto fail;
/* Many puppies died to bring you this code. */
priv.caps = caps;
conn->privateData = &priv;
2015-04-23 18:10:15 +03:00
if (!(def = virDomainDefParseFile(xml, caps, xmlopt,
VIR_DOMAIN_DEF_PARSE_INACTIVE)))
goto fail;
if (!virDomainDefCheckABIStability(def, def)) {
fprintf(stderr, "ABI stability check failed on %s", xml);
goto fail;
}
if (!(conf = xenFormatXM(conn, def)))
goto fail;
if (virConfWriteMem(gotxmcfgData, &wrote, conf) < 0)
goto fail;
gotxmcfgData[wrote] = '\0';
if (virtTestCompareToFile(gotxmcfgData, xmcfg) < 0)
goto fail;
ret = 0;
fail:
VIR_FREE(gotxmcfgData);
if (conf)
virConfFree(conf);
virDomainDefFree(def);
virObjectUnref(conn);
return ret;
}
static int
testCompareFormatXML(const char *xmcfg, const char *xml)
{
char *xmcfgData = NULL;
char *gotxml = NULL;
virConfPtr conf = NULL;
int ret = -1;
virConnectPtr conn;
struct _xenUnifiedPrivate priv;
virDomainDefPtr def = NULL;
2008-11-21 15:16:08 +03:00
conn = virGetConnect();
if (!conn) goto fail;
if (virtTestLoadFile(xmcfg, &xmcfgData) < 0)
goto fail;
/* Many puppies died to bring you this code. */
priv.caps = caps;
conn->privateData = &priv;
if (!(conf = virConfReadMem(xmcfgData, strlen(xmcfgData), 0)))
goto fail;
if (!(def = xenParseXM(conf, caps, xmlopt)))
goto fail;
if (!(gotxml = virDomainDefFormat(def, caps, VIR_DOMAIN_DEF_FORMAT_SECURE)))
goto fail;
if (virtTestCompareToFile(gotxml, xml) < 0)
goto fail;
ret = 0;
fail:
if (conf)
virConfFree(conf);
VIR_FREE(xmcfgData);
VIR_FREE(gotxml);
virDomainDefFree(def);
virObjectUnref(conn);
return ret;
}
2008-04-18 19:28:33 +04:00
struct testInfo {
const char *name;
int mode;
};
2007-07-19 01:08:22 +04:00
static int
testCompareHelper(const void *data)
{
int result = -1;
2008-04-18 19:28:33 +04:00
const struct testInfo *info = data;
char *xml = NULL;
char *cfg = NULL;
char *cfgout = NULL;
if (virAsprintf(&xml, "%s/xmconfigdata/test-%s.xml",
abs_srcdir, info->name) < 0 ||
virAsprintf(&cfg, "%s/xmconfigdata/test-%s.cfg",
abs_srcdir, info->name) < 0)
goto cleanup;
2008-04-18 19:28:33 +04:00
if (info->mode == 0)
result = testCompareParseXML(cfg, xml);
2008-04-18 19:28:33 +04:00
else
result = testCompareFormatXML(cfg, xml);
cleanup:
VIR_FREE(xml);
VIR_FREE(cfg);
VIR_FREE(cfgout);
return result;
2007-07-19 01:08:22 +04:00
}
static int
tests: simplify common setup A few of the tests were missing basic sanity checks, while most of them were doing copy-and-paste initialization (in fact, some of them pasted the argc > 1 check more than once!). It's much nicer to do things in one common place, and minimizes the size of the next patch that fixes getcwd usage. * tests/testutils.h (EXIT_AM_HARDFAIL): New define. (progname, abs_srcdir): Define for all tests. (VIRT_TEST_MAIN): Change callback signature. * tests/testutils.c (virtTestMain): Do more common init. * tests/commandtest.c (mymain): Simplify. * tests/cputest.c (mymain): Likewise. * tests/esxutilstest.c (mymain): Likewise. * tests/eventtest.c (mymain): Likewise. * tests/hashtest.c (mymain): Likewise. * tests/networkxml2xmltest.c (mymain): Likewise. * tests/nodedevxml2xmltest.c (myname): Likewise. * tests/nodeinfotest.c (mymain): Likewise. * tests/nwfilterxml2xmltest.c (mymain): Likewise. * tests/qemuargv2xmltest.c (mymain): Likewise. * tests/qemuhelptest.c (mymain): Likewise. * tests/qemuxml2argvtest.c (mymain): Likewise. * tests/qemuxml2xmltest.c (mymain): Likewise. * tests/qparamtest.c (mymain): Likewise. * tests/sexpr2xmltest.c (mymain): Likewise. * tests/sockettest.c (mymain): Likewise. * tests/statstest.c (mymain): Likewise. * tests/storagepoolxml2xmltest.c (mymain): Likewise. * tests/storagevolxml2xmltest.c (mymain): Likewise. * tests/virbuftest.c (mymain): Likewise. * tests/virshtest.c (mymain): Likewise. * tests/vmx2xmltest.c (mymain): Likewise. * tests/xencapstest.c (mymain): Likewise. * tests/xmconfigtest.c (mymain): Likewise. * tests/xml2sexprtest.c (mymain): Likewise. * tests/xml2vmxtest.c (mymain): Likewise.
2011-04-29 20:21:20 +04:00
mymain(void)
{
int ret = 0;
2008-04-18 19:28:33 +04:00
if (!(caps = testXenCapsInit()))
return EXIT_FAILURE;
2008-04-18 19:28:33 +04:00
if (!(xmlopt = xenDomainXMLConfInit()))
return EXIT_FAILURE;
#define DO_TEST_PARSE(name) \
2008-04-18 19:28:33 +04:00
do { \
struct testInfo info0 = { name, 0 }; \
2008-04-18 19:28:33 +04:00
if (virtTestRun("Xen XM-2-XML Parse " name, \
testCompareHelper, &info0) < 0) \
2008-04-18 19:28:33 +04:00
ret = -1; \
} while (0)
#define DO_TEST_FORMAT(name) \
do { \
struct testInfo info1 = { name, 1 }; \
2008-04-18 19:28:33 +04:00
if (virtTestRun("Xen XM-2-XML Format " name, \
testCompareHelper, &info1) < 0) \
2008-04-18 19:28:33 +04:00
ret = -1; \
} while (0)
#define DO_TEST(name) \
do { \
DO_TEST_PARSE(name); \
DO_TEST_FORMAT(name); \
} while (0)
DO_TEST("paravirt-new-pvfb");
DO_TEST("paravirt-new-pvfb-vncdisplay");
DO_TEST("paravirt-net-e1000");
DO_TEST("paravirt-net-vifname");
DO_TEST("paravirt-vcpu");
DO_TEST("paravirt-maxvcpus");
DO_TEST("fullvirt-new-cdrom");
DO_TEST("fullvirt-utc");
DO_TEST("fullvirt-localtime");
DO_TEST("fullvirt-usbtablet");
DO_TEST("fullvirt-usbmouse");
DO_TEST("fullvirt-serial-file");
DO_TEST("fullvirt-serial-dev-2-ports");
DO_TEST("fullvirt-serial-dev-2nd-port");
DO_TEST("fullvirt-serial-null");
DO_TEST("fullvirt-serial-pipe");
DO_TEST("fullvirt-serial-pty");
DO_TEST("fullvirt-serial-stdio");
DO_TEST("fullvirt-serial-tcp");
DO_TEST("fullvirt-serial-tcp-telnet");
DO_TEST("fullvirt-serial-udp");
DO_TEST("fullvirt-serial-unix");
DO_TEST("fullvirt-force-hpet");
DO_TEST("fullvirt-force-nohpet");
DO_TEST("fullvirt-parallel-tcp");
DO_TEST("fullvirt-sound");
DO_TEST("fullvirt-net-netfront");
DO_TEST_FORMAT("fullvirt-default-feature");
DO_TEST("escape-paths");
DO_TEST("no-source-cdrom");
DO_TEST("pci-devs");
virObjectUnref(caps);
virObjectUnref(xmlopt);
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
VIRT_TEST_MAIN(mymain)