[PATCHv2 5/5] Introduce virschematest

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Instead of calling xmllint via a shell script, use our virXMLValidator
API to do it directly via libxml.
---
 .gitignore                     |   1 -
 tests/Makefile.am              |  28 ++----
 tests/capabilityschematest     |   9 --
 tests/domaincapsschematest     |  10 ---
 tests/domainschematest         |  14 ---
 tests/domainsnapshotschematest |   9 --
 tests/interfaceschematest      |   9 --
 tests/networkschematest        |   9 --
 tests/nodedevschematest        |   9 --
 tests/nwfilterschematest       |   9 --
 tests/schematestutils.sh       |  47 ----------
 tests/secretschematest         |   9 --
 tests/storagepoolschematest    |   9 --
 tests/storagevolschematest     |   9 --
 tests/virschematest.c          | 190 +++++++++++++++++++++++++++++++++++++++++
 15 files changed, 196 insertions(+), 175 deletions(-)
 delete mode 100755 tests/capabilityschematest
 delete mode 100755 tests/domaincapsschematest
 delete mode 100755 tests/domainschematest
 delete mode 100755 tests/domainsnapshotschematest
 delete mode 100755 tests/interfaceschematest
 delete mode 100755 tests/networkschematest
 delete mode 100755 tests/nodedevschematest
 delete mode 100755 tests/nwfilterschematest
 delete mode 100644 tests/schematestutils.sh
 delete mode 100755 tests/secretschematest
 delete mode 100755 tests/storagepoolschematest
 delete mode 100755 tests/storagevolschematest
 create mode 100644 tests/virschematest.c

diff --git a/.gitignore b/.gitignore
index ee89787..7fd9963 100644
--- a/.gitignore
+++ b/.gitignore
@@ -162,7 +162,6 @@
 /tests/*test
 /tests/commandhelper
 /tests/qemucapsprobe
-!/tests/*schematest
 !/tests/virt-aa-helper-test
 /tests/objectlocking
 /tests/objectlocking-files.txt
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 0c4ad3c..3840457 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -88,15 +88,12 @@ EXTRA_DIST =		\
 	bhyvexml2argvdata \
 	bhyvexml2xmloutdata \
 	capabilityschemadata \
-	capabilityschematest \
 	commanddata \
 	cputestdata \
 	domaincapsschemadata \
-	domaincapsschematest \
 	domainconfdata \
 	domainschemadata \
 	domainschematest \
-	domainsnapshotschematest \
 	domainsnapshotxml2xmlin \
 	domainsnapshotxml2xmlout \
 	fchostdata \
@@ -106,7 +103,6 @@ EXTRA_DIST =		\
 	lxcconf2xmldata \
 	lxcxml2xmldata \
 	lxcxml2xmloutdata \
-	networkschematest \
 	networkxml2confdata \
 	networkxml2firewalldata \
 	networkxml2xmlin \
@@ -114,10 +110,8 @@ EXTRA_DIST =		\
 	networkxml2xmlupdatein \
 	networkxml2xmlupdateout \
 	nodedevschemadata \
-	nodedevschematest \
 	nodeinfodata     \
 	nssdata \
-	nwfilterschematest \
 	nwfilterxml2firewalldata \
 	nwfilterxml2xmlin \
 	nwfilterxml2xmlout \
@@ -131,17 +125,14 @@ EXTRA_DIST =		\
 	qemumonitorjsondata \
 	qemuxml2argvdata \
 	qemuxml2xmloutdata \
-	schematestutils.sh \
 	secretxml2xmlin \
 	securityselinuxhelperdata \
 	securityselinuxlabeldata \
 	sexpr2xmldata \
 	storagepoolschemadata \
-	storagepoolschematest \
 	storagepoolxml2xmlin \
 	storagepoolxml2xmlout \
 	storagevolschemadata \
-	storagevolschematest \
 	storagevolxml2argvdata \
 	storagevolxml2xmlin \
 	storagevolxml2xmlout \
@@ -190,6 +181,7 @@ test_programs = virshtest sockettest \
 	virlockspacetest \
 	virlogtest \
 	virrotatingfiletest \
+	virschematest \
 	virstringtest \
 	virportallocatortest \
 	sysinfotest \
@@ -365,19 +357,7 @@ test_programs += virusbtest \
 	$(NULL)
 endif WITH_LINUX
 
-test_scripts = \
-	capabilityschematest \
-	interfaceschematest \
-	networkschematest \
-	storagepoolschematest \
-	storagevolschematest \
-	domaincapsschematest \
-	domainschematest \
-	nodedevschematest \
-	nwfilterschematest \
-	domainsnapshotschematest \
-	secretschematest
-
+test_scripts =
 libvirtd_test_scripts =		\
 	libvirtd-fail			\
 	libvirtd-pool			\
@@ -1019,6 +999,10 @@ virtimetest_SOURCES = \
 	virtimetest.c testutils.h testutils.c
 virtimetest_LDADD = $(LDADDS)
 
+virschematest_SOURCES = \
+	virschematest.c testutils.h testutils.c
+virschematest_LDADD = $(LDADDS)
+
 virstringtest_SOURCES = \
 	virstringtest.c testutils.h testutils.c
 virstringtest_LDADD = $(LDADDS)
diff --git a/tests/capabilityschematest b/tests/capabilityschematest
deleted file mode 100755
index 458212e..0000000
--- a/tests/capabilityschematest
+++ /dev/null
@@ -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"
diff --git a/tests/domaincapsschematest b/tests/domaincapsschematest
deleted file mode 100755
index 3b2021f..0000000
--- a/tests/domaincapsschematest
+++ /dev/null
@@ -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"
diff --git a/tests/domainschematest b/tests/domainschematest
deleted file mode 100755
index c059c98..0000000
--- a/tests/domainschematest
+++ /dev/null
@@ -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"
diff --git a/tests/domainsnapshotschematest b/tests/domainsnapshotschematest
deleted file mode 100755
index 33b539a..0000000
--- a/tests/domainsnapshotschematest
+++ /dev/null
@@ -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"
diff --git a/tests/interfaceschematest b/tests/interfaceschematest
deleted file mode 100755
index 239b749..0000000
--- a/tests/interfaceschematest
+++ /dev/null
@@ -1,9 +0,0 @@
-#!/bin/sh
-
-. "$(dirname $0)/test-lib.sh"
-. $abs_srcdir/schematestutils.sh
-
-DIRS="interfaceschemadata"
-SCHEMA="interface.rng"
-
-check_schema "$DIRS" "$SCHEMA"
diff --git a/tests/networkschematest b/tests/networkschematest
deleted file mode 100755
index adbc7f4..0000000
--- a/tests/networkschematest
+++ /dev/null
@@ -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"
diff --git a/tests/nodedevschematest b/tests/nodedevschematest
deleted file mode 100755
index 1d85371..0000000
--- a/tests/nodedevschematest
+++ /dev/null
@@ -1,9 +0,0 @@
-#!/bin/sh
-
-. "$(dirname $0)/test-lib.sh"
-. $abs_srcdir/schematestutils.sh
-
-DIRS="nodedevschemadata"
-SCHEMA="nodedev.rng"
-
-check_schema "$DIRS" "$SCHEMA"
diff --git a/tests/nwfilterschematest b/tests/nwfilterschematest
deleted file mode 100755
index 408034c..0000000
--- a/tests/nwfilterschematest
+++ /dev/null
@@ -1,9 +0,0 @@
-#!/bin/sh
-
-. "$(dirname $0)/test-lib.sh"
-. $abs_srcdir/schematestutils.sh
-
-DIRS="nwfilterxml2xmlout"
-SCHEMA="nwfilter.rng"
-
-check_schema "$DIRS" "$SCHEMA"
diff --git a/tests/schematestutils.sh b/tests/schematestutils.sh
deleted file mode 100644
index e07e9b9..0000000
--- a/tests/schematestutils.sh
+++ /dev/null
@@ -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
-
-}
diff --git a/tests/secretschematest b/tests/secretschematest
deleted file mode 100755
index 9c29021..0000000
--- a/tests/secretschematest
+++ /dev/null
@@ -1,9 +0,0 @@
-#!/bin/sh
-
-. "$(dirname $0)/test-lib.sh"
-. $abs_srcdir/schematestutils.sh
-
-DIRS="secretxml2xmlin"
-SCHEMA="secret.rng"
-
-check_schema "$DIRS" "$SCHEMA"
diff --git a/tests/storagepoolschematest b/tests/storagepoolschematest
deleted file mode 100755
index ebbf4d1..0000000
--- a/tests/storagepoolschematest
+++ /dev/null
@@ -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"
diff --git a/tests/storagevolschematest b/tests/storagevolschematest
deleted file mode 100755
index d3ba3a8..0000000
--- a/tests/storagevolschematest
+++ /dev/null
@@ -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"
diff --git a/tests/virschematest.c b/tests/virschematest.c
new file mode 100644
index 0000000..11d5375
--- /dev/null
+++ b/tests/virschematest.c
@@ -0,0 +1,190 @@
+/*
+ * 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@xxxxxxxxxx>
+ */
+
+#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;
+    char *xml_path;
+    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;
+
+    if (virAsprintf(&schema_path, "%s/docs/schemas/%s", abs_topsrcdir, schema) < 0)
+        return -1;
+
+    if (!(validator = virXMLValidatorInit(schema_path)))
+        goto cleanup;
+
+    va_start(args, schema);
+    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__) < 0)    \
+            ret = -1;                                   \
+    } while (0)                                         \
+
+    DO_TEST("capability.rng", "capabilityschemadata", "xencapsdata", NULL);
+    DO_TEST("domain.rng", "domainschemadata", "qemuargv2xmldata",
+            "qemuxml2argvdata", "sexpr2xmldata", "xmconfigdata",
+            "xml2sexprdata", "qemuxml2xmloutdata", "lxcxml2xmldata",
+            "lxcxml2xmloutdata", "bhyvexml2argvdata", "genericxml2xmlindata",
+            "genericxml2xmloutdata", "xlconfigdata", NULL);
+    DO_TEST("domaincaps.rng", "domaincapsschemadata", NULL);
+    DO_TEST("domainsnapshot.rng", "domainsnapshotxml2xmlin",
+            "domainsnapshotxml2xmlout", NULL);
+    DO_TEST("interface.rng", "interfaceschemadata", NULL);
+    DO_TEST("network.rng", "../src/network", "networkxml2xmlin",
+            "networkxml2xmlout", NULL);
+    DO_TEST("nodedev.rng", "nodedevschemadata", NULL);
+    DO_TEST("nwfilter.rng", "nwfilterxml2xmlout", NULL);
+    DO_TEST("secret.rng", "secretxml2xmlin", NULL);
+    DO_TEST("storagepool.rng", "storagepoolxml2xmlin", "storagepoolxml2xmlout",
+            "storagepoolschemadata", NULL);
+    DO_TEST("storagevol.rng", "storagevolxml2xmlin", "storagevolxml2xmlout",
+            "storagevolschemadata", NULL);
+
+    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
+}
+
+VIRT_TEST_MAIN(mymain)
-- 
2.7.3

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]