mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-02-01 17:35:17 +00:00
Introduce virschematest
Instead of calling xmllint via a shell script, use our virXMLValidator API to do it directly via libxml.
This commit is contained in:
parent
4702687cfb
commit
cf0974fbca
1
.gitignore
vendored
1
.gitignore
vendored
@ -162,7 +162,6 @@
|
|||||||
/tests/*test
|
/tests/*test
|
||||||
/tests/commandhelper
|
/tests/commandhelper
|
||||||
/tests/qemucapsprobe
|
/tests/qemucapsprobe
|
||||||
!/tests/*schematest
|
|
||||||
!/tests/virt-aa-helper-test
|
!/tests/virt-aa-helper-test
|
||||||
/tests/objectlocking
|
/tests/objectlocking
|
||||||
/tests/objectlocking-files.txt
|
/tests/objectlocking-files.txt
|
||||||
|
@ -88,15 +88,11 @@ EXTRA_DIST = \
|
|||||||
bhyvexml2argvdata \
|
bhyvexml2argvdata \
|
||||||
bhyvexml2xmloutdata \
|
bhyvexml2xmloutdata \
|
||||||
capabilityschemadata \
|
capabilityschemadata \
|
||||||
capabilityschematest \
|
|
||||||
commanddata \
|
commanddata \
|
||||||
cputestdata \
|
cputestdata \
|
||||||
domaincapsschemadata \
|
domaincapsschemadata \
|
||||||
domaincapsschematest \
|
|
||||||
domainconfdata \
|
domainconfdata \
|
||||||
domainschemadata \
|
domainschemadata \
|
||||||
domainschematest \
|
|
||||||
domainsnapshotschematest \
|
|
||||||
domainsnapshotxml2xmlin \
|
domainsnapshotxml2xmlin \
|
||||||
domainsnapshotxml2xmlout \
|
domainsnapshotxml2xmlout \
|
||||||
fchostdata \
|
fchostdata \
|
||||||
@ -106,7 +102,6 @@ EXTRA_DIST = \
|
|||||||
lxcconf2xmldata \
|
lxcconf2xmldata \
|
||||||
lxcxml2xmldata \
|
lxcxml2xmldata \
|
||||||
lxcxml2xmloutdata \
|
lxcxml2xmloutdata \
|
||||||
networkschematest \
|
|
||||||
networkxml2confdata \
|
networkxml2confdata \
|
||||||
networkxml2firewalldata \
|
networkxml2firewalldata \
|
||||||
networkxml2xmlin \
|
networkxml2xmlin \
|
||||||
@ -114,10 +109,8 @@ EXTRA_DIST = \
|
|||||||
networkxml2xmlupdatein \
|
networkxml2xmlupdatein \
|
||||||
networkxml2xmlupdateout \
|
networkxml2xmlupdateout \
|
||||||
nodedevschemadata \
|
nodedevschemadata \
|
||||||
nodedevschematest \
|
|
||||||
nodeinfodata \
|
nodeinfodata \
|
||||||
nssdata \
|
nssdata \
|
||||||
nwfilterschematest \
|
|
||||||
nwfilterxml2firewalldata \
|
nwfilterxml2firewalldata \
|
||||||
nwfilterxml2xmlin \
|
nwfilterxml2xmlin \
|
||||||
nwfilterxml2xmlout \
|
nwfilterxml2xmlout \
|
||||||
@ -131,17 +124,14 @@ EXTRA_DIST = \
|
|||||||
qemumonitorjsondata \
|
qemumonitorjsondata \
|
||||||
qemuxml2argvdata \
|
qemuxml2argvdata \
|
||||||
qemuxml2xmloutdata \
|
qemuxml2xmloutdata \
|
||||||
schematestutils.sh \
|
|
||||||
secretxml2xmlin \
|
secretxml2xmlin \
|
||||||
securityselinuxhelperdata \
|
securityselinuxhelperdata \
|
||||||
securityselinuxlabeldata \
|
securityselinuxlabeldata \
|
||||||
sexpr2xmldata \
|
sexpr2xmldata \
|
||||||
storagepoolschemadata \
|
storagepoolschemadata \
|
||||||
storagepoolschematest \
|
|
||||||
storagepoolxml2xmlin \
|
storagepoolxml2xmlin \
|
||||||
storagepoolxml2xmlout \
|
storagepoolxml2xmlout \
|
||||||
storagevolschemadata \
|
storagevolschemadata \
|
||||||
storagevolschematest \
|
|
||||||
storagevolxml2argvdata \
|
storagevolxml2argvdata \
|
||||||
storagevolxml2xmlin \
|
storagevolxml2xmlin \
|
||||||
storagevolxml2xmlout \
|
storagevolxml2xmlout \
|
||||||
@ -190,6 +180,7 @@ test_programs = virshtest sockettest \
|
|||||||
virlockspacetest \
|
virlockspacetest \
|
||||||
virlogtest \
|
virlogtest \
|
||||||
virrotatingfiletest \
|
virrotatingfiletest \
|
||||||
|
virschematest \
|
||||||
virstringtest \
|
virstringtest \
|
||||||
virportallocatortest \
|
virportallocatortest \
|
||||||
sysinfotest \
|
sysinfotest \
|
||||||
@ -365,19 +356,7 @@ test_programs += virusbtest \
|
|||||||
$(NULL)
|
$(NULL)
|
||||||
endif WITH_LINUX
|
endif WITH_LINUX
|
||||||
|
|
||||||
test_scripts = \
|
test_scripts =
|
||||||
capabilityschematest \
|
|
||||||
interfaceschematest \
|
|
||||||
networkschematest \
|
|
||||||
storagepoolschematest \
|
|
||||||
storagevolschematest \
|
|
||||||
domaincapsschematest \
|
|
||||||
domainschematest \
|
|
||||||
nodedevschematest \
|
|
||||||
nwfilterschematest \
|
|
||||||
domainsnapshotschematest \
|
|
||||||
secretschematest
|
|
||||||
|
|
||||||
libvirtd_test_scripts = \
|
libvirtd_test_scripts = \
|
||||||
libvirtd-fail \
|
libvirtd-fail \
|
||||||
libvirtd-pool \
|
libvirtd-pool \
|
||||||
@ -1019,6 +998,10 @@ virtimetest_SOURCES = \
|
|||||||
virtimetest.c testutils.h testutils.c
|
virtimetest.c testutils.h testutils.c
|
||||||
virtimetest_LDADD = $(LDADDS)
|
virtimetest_LDADD = $(LDADDS)
|
||||||
|
|
||||||
|
virschematest_SOURCES = \
|
||||||
|
virschematest.c testutils.h testutils.c
|
||||||
|
virschematest_LDADD = $(LDADDS)
|
||||||
|
|
||||||
virstringtest_SOURCES = \
|
virstringtest_SOURCES = \
|
||||||
virstringtest.c testutils.h testutils.c
|
virstringtest.c testutils.h testutils.c
|
||||||
virstringtest_LDADD = $(LDADDS)
|
virstringtest_LDADD = $(LDADDS)
|
||||||
|
@ -1,9 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
. "$(dirname $0)/test-lib.sh"
|
|
||||||
. $abs_srcdir/schematestutils.sh
|
|
||||||
|
|
||||||
DIRS="capabilityschemadata xencapsdata"
|
|
||||||
SCHEMA="capability.rng"
|
|
||||||
|
|
||||||
check_schema "$DIRS" "$SCHEMA"
|
|
@ -1,10 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
. "$(dirname $0)/test-lib.sh"
|
|
||||||
. $abs_srcdir/schematestutils.sh
|
|
||||||
|
|
||||||
DIRS=""
|
|
||||||
DIRS="$DIRS domaincapsschemadata"
|
|
||||||
SCHEMA="domaincaps.rng"
|
|
||||||
|
|
||||||
check_schema "$DIRS" "$SCHEMA"
|
|
@ -1,14 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
. "$(dirname $0)/test-lib.sh"
|
|
||||||
. $abs_srcdir/schematestutils.sh
|
|
||||||
|
|
||||||
DIRS=""
|
|
||||||
DIRS="$DIRS domainschemadata qemuargv2xmldata qemuxml2argvdata sexpr2xmldata"
|
|
||||||
DIRS="$DIRS xmconfigdata xml2sexprdata qemuxml2xmloutdata"
|
|
||||||
DIRS="$DIRS lxcxml2xmldata lxcxml2xmloutdata"
|
|
||||||
DIRS="$DIRS bhyvexml2argvdata genericxml2xmlindata genericxml2xmloutdata"
|
|
||||||
DIRS="$DIRS xlconfigdata"
|
|
||||||
SCHEMA="domain.rng"
|
|
||||||
|
|
||||||
check_schema "$DIRS" "$SCHEMA"
|
|
@ -1,9 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
. "$(dirname $0)/test-lib.sh"
|
|
||||||
. $abs_srcdir/schematestutils.sh
|
|
||||||
|
|
||||||
DIRS="domainsnapshotxml2xmlin domainsnapshotxml2xmlout"
|
|
||||||
SCHEMA="domainsnapshot.rng"
|
|
||||||
|
|
||||||
check_schema "$DIRS" "$SCHEMA"
|
|
@ -1,9 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
. "$(dirname $0)/test-lib.sh"
|
|
||||||
. $abs_srcdir/schematestutils.sh
|
|
||||||
|
|
||||||
DIRS="interfaceschemadata"
|
|
||||||
SCHEMA="interface.rng"
|
|
||||||
|
|
||||||
check_schema "$DIRS" "$SCHEMA"
|
|
@ -1,9 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
. "$(dirname $0)/test-lib.sh"
|
|
||||||
. $abs_srcdir/schematestutils.sh
|
|
||||||
|
|
||||||
DIRS="../src/network networkxml2xmlin networkxml2xmlout"
|
|
||||||
SCHEMA="network.rng"
|
|
||||||
|
|
||||||
check_schema "$DIRS" "$SCHEMA"
|
|
@ -1,9 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
. "$(dirname $0)/test-lib.sh"
|
|
||||||
. $abs_srcdir/schematestutils.sh
|
|
||||||
|
|
||||||
DIRS="nodedevschemadata"
|
|
||||||
SCHEMA="nodedev.rng"
|
|
||||||
|
|
||||||
check_schema "$DIRS" "$SCHEMA"
|
|
@ -1,9 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
. "$(dirname $0)/test-lib.sh"
|
|
||||||
. $abs_srcdir/schematestutils.sh
|
|
||||||
|
|
||||||
DIRS="nwfilterxml2xmlout"
|
|
||||||
SCHEMA="nwfilter.rng"
|
|
||||||
|
|
||||||
check_schema "$DIRS" "$SCHEMA"
|
|
@ -1,47 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
(xmllint --version) >/dev/null 2>&1 || skip_test_ 'Missing xmllint'
|
|
||||||
|
|
||||||
check_schema () {
|
|
||||||
|
|
||||||
DIRS=$1
|
|
||||||
SCHEMA="$abs_top_srcdir/docs/schemas/$2"
|
|
||||||
|
|
||||||
test_intro $this_test
|
|
||||||
|
|
||||||
n=0
|
|
||||||
f=0
|
|
||||||
for dir in $DIRS
|
|
||||||
do
|
|
||||||
XML=`find $abs_srcdir/$dir -name '*.xml'` || exit 1
|
|
||||||
|
|
||||||
for xml in `echo "$XML" | sort`
|
|
||||||
do
|
|
||||||
n=`expr $n + 1`
|
|
||||||
cmd="xmllint --relaxng $SCHEMA --noout $xml"
|
|
||||||
result=`$cmd 2>&1`
|
|
||||||
ret=$?
|
|
||||||
|
|
||||||
# Alter ret if error was expected.
|
|
||||||
case $xml:$ret in
|
|
||||||
*-invalid.xml:[34]) ret=0 ;;
|
|
||||||
*-invalid.xml:0) ret=3 ;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
test_result $n $(basename $(dirname $xml))"/"$(basename $xml) $ret
|
|
||||||
if test "$verbose" = "1" && test $ret != 0 ; then
|
|
||||||
printf '%s\n' "$cmd" "$result"
|
|
||||||
fi
|
|
||||||
if test "$ret" != 0 ; then
|
|
||||||
f=`expr $f + 1`
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
done
|
|
||||||
|
|
||||||
test_final $n $f
|
|
||||||
|
|
||||||
ret=0
|
|
||||||
test $f != 0 && ret=255
|
|
||||||
exit $ret
|
|
||||||
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
. "$(dirname $0)/test-lib.sh"
|
|
||||||
. $abs_srcdir/schematestutils.sh
|
|
||||||
|
|
||||||
DIRS="secretxml2xmlin"
|
|
||||||
SCHEMA="secret.rng"
|
|
||||||
|
|
||||||
check_schema "$DIRS" "$SCHEMA"
|
|
@ -1,9 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
. "$(dirname $0)/test-lib.sh"
|
|
||||||
. $abs_srcdir/schematestutils.sh
|
|
||||||
|
|
||||||
DIRS="storagepoolxml2xmlin storagepoolxml2xmlout storagepoolschemadata"
|
|
||||||
SCHEMA="storagepool.rng"
|
|
||||||
|
|
||||||
check_schema "$DIRS" "$SCHEMA"
|
|
@ -1,9 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
. "$(dirname $0)/test-lib.sh"
|
|
||||||
. $abs_srcdir/schematestutils.sh
|
|
||||||
|
|
||||||
DIRS="storagevolxml2xmlin storagevolxml2xmlout storagevolschemadata"
|
|
||||||
SCHEMA="storagevol.rng"
|
|
||||||
|
|
||||||
check_schema "$DIRS" "$SCHEMA"
|
|
191
tests/virschematest.c
Normal file
191
tests/virschematest.c
Normal file
@ -0,0 +1,191 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 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: Ján Tomko <jtomko@redhat.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <config.h>
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "testutils.h"
|
||||||
|
|
||||||
|
#include "virerror.h"
|
||||||
|
#include "viralloc.h"
|
||||||
|
#include "virlog.h"
|
||||||
|
#include "virxml.h"
|
||||||
|
|
||||||
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
||||||
|
|
||||||
|
VIR_LOG_INIT("tests.schematest");
|
||||||
|
|
||||||
|
struct testSchemaData {
|
||||||
|
virXMLValidatorPtr validator;
|
||||||
|
const char *xml_path;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static int
|
||||||
|
testSchemaFile(const void *args)
|
||||||
|
{
|
||||||
|
const struct testSchemaData *data = args;
|
||||||
|
bool shouldFail = virFileHasSuffix(data->xml_path, "-invalid.xml");
|
||||||
|
xmlDocPtr xml = NULL;
|
||||||
|
int ret = -1;
|
||||||
|
|
||||||
|
if (!(xml = virXMLParseFile(data->xml_path)))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (virXMLValidatorValidate(data->validator, xml) < 0) {
|
||||||
|
if (!shouldFail)
|
||||||
|
goto cleanup;
|
||||||
|
} else {
|
||||||
|
if (shouldFail)
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = 0;
|
||||||
|
cleanup:
|
||||||
|
xmlFreeDoc(xml);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int
|
||||||
|
testSchemaDir(const char *schema,
|
||||||
|
virXMLValidatorPtr validator,
|
||||||
|
const char *dir_path)
|
||||||
|
{
|
||||||
|
DIR *dir = NULL;
|
||||||
|
struct dirent *ent;
|
||||||
|
int ret = 0;
|
||||||
|
int rc;
|
||||||
|
char *test_name = NULL;
|
||||||
|
char *xml_path = NULL;
|
||||||
|
struct testSchemaData data = {
|
||||||
|
.validator = validator,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!(dir = opendir(dir_path))) {
|
||||||
|
virReportSystemError(errno,
|
||||||
|
"Failed to opendir path '%s'",
|
||||||
|
dir_path);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while ((rc = virDirRead(dir, &ent, dir_path)) > 0) {
|
||||||
|
if (!virFileHasSuffix(ent->d_name, ".xml"))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (virAsprintf(&xml_path, "%s/%s", dir_path, ent->d_name) < 0)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
if (virAsprintf(&test_name, "Checking %s against %s",
|
||||||
|
ent->d_name, schema) < 0)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
data.xml_path = xml_path;
|
||||||
|
if (virtTestRun(test_name, testSchemaFile, &data) < 0)
|
||||||
|
ret = -1;
|
||||||
|
|
||||||
|
VIR_FREE(test_name);
|
||||||
|
VIR_FREE(xml_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rc < 0)
|
||||||
|
ret = -1;
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
VIR_FREE(test_name);
|
||||||
|
VIR_FREE(xml_path);
|
||||||
|
closedir(dir);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int
|
||||||
|
testSchemaDirs(const char *schema, ...)
|
||||||
|
{
|
||||||
|
virXMLValidatorPtr validator;
|
||||||
|
va_list args;
|
||||||
|
int ret = 0;
|
||||||
|
char *schema_path = NULL;
|
||||||
|
char *dir_path = NULL;
|
||||||
|
const char *dir;
|
||||||
|
|
||||||
|
va_start(args, schema);
|
||||||
|
|
||||||
|
if (virAsprintf(&schema_path, "%s/docs/schemas/%s", abs_topsrcdir, schema) < 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (!(validator = virXMLValidatorInit(schema_path)))
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
while ((dir = va_arg(args, char *))) {
|
||||||
|
if (virAsprintf(&dir_path, "%s/%s", abs_srcdir, dir) < 0) {
|
||||||
|
ret = -1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
if (testSchemaDir(schema, validator, dir) < 0)
|
||||||
|
ret = -1;
|
||||||
|
VIR_FREE(dir_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
virXMLValidatorFree(validator);
|
||||||
|
VIR_FREE(schema_path);
|
||||||
|
VIR_FREE(dir_path);
|
||||||
|
va_end(args);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int
|
||||||
|
mymain(void)
|
||||||
|
{
|
||||||
|
int ret = 0;
|
||||||
|
|
||||||
|
#define DO_TEST(schema, ...) \
|
||||||
|
do { \
|
||||||
|
if (testSchemaDirs(schema, __VA_ARGS__, NULL) < 0) \
|
||||||
|
ret = -1; \
|
||||||
|
} while (0) \
|
||||||
|
|
||||||
|
DO_TEST("capability.rng", "capabilityschemadata", "xencapsdata");
|
||||||
|
DO_TEST("domain.rng", "domainschemadata", "qemuargv2xmldata",
|
||||||
|
"qemuxml2argvdata", "sexpr2xmldata", "xmconfigdata",
|
||||||
|
"xml2sexprdata", "qemuxml2xmloutdata", "lxcxml2xmldata",
|
||||||
|
"lxcxml2xmloutdata", "bhyvexml2argvdata", "genericxml2xmlindata",
|
||||||
|
"genericxml2xmloutdata", "xlconfigdata");
|
||||||
|
DO_TEST("domaincaps.rng", "domaincapsschemadata");
|
||||||
|
DO_TEST("domainsnapshot.rng", "domainsnapshotxml2xmlin",
|
||||||
|
"domainsnapshotxml2xmlout");
|
||||||
|
DO_TEST("interface.rng", "interfaceschemadata");
|
||||||
|
DO_TEST("network.rng", "../src/network", "networkxml2xmlin",
|
||||||
|
"networkxml2xmlout");
|
||||||
|
DO_TEST("nodedev.rng", "nodedevschemadata");
|
||||||
|
DO_TEST("nwfilter.rng", "nwfilterxml2xmlout");
|
||||||
|
DO_TEST("secret.rng", "secretxml2xmlin");
|
||||||
|
DO_TEST("storagepool.rng", "storagepoolxml2xmlin", "storagepoolxml2xmlout",
|
||||||
|
"storagepoolschemadata");
|
||||||
|
DO_TEST("storagevol.rng", "storagevolxml2xmlin", "storagevolxml2xmlout",
|
||||||
|
"storagevolschemadata");
|
||||||
|
|
||||||
|
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
VIRT_TEST_MAIN(mymain)
|
Loading…
x
Reference in New Issue
Block a user