[libvirt] [PATCH 1/4] tests: schema: Add possibility to validate individual files

Peter Krempa pkrempa at redhat.com
Tue Mar 28 11:58:56 UTC 2017


Sometimes it may be desired to validate individual files against a
schema. Refactor the data structures to unify them and introduce a new
macro DO_TEST_FILE(schema, xmlfile) which will test the XML file against
the given schema file.
---
 tests/virschematest.c | 34 +++++++++++++++++++++++++++-------
 1 file changed, 27 insertions(+), 7 deletions(-)

diff --git a/tests/virschematest.c b/tests/virschematest.c
index faf66d642..beefabc96 100644
--- a/tests/virschematest.c
+++ b/tests/virschematest.c
@@ -35,6 +35,7 @@ VIR_LOG_INIT("tests.schematest");

 struct testSchemaData {
     virXMLValidatorPtr validator;
+    const char *schema;
     const char *xml_path;
 };

@@ -140,15 +141,10 @@ testSchemaDirs(const char *schema, virXMLValidatorPtr validator, ...)
 }


-struct testSchemaFileData {
-    virXMLValidatorPtr validator;
-    const char *schema;
-};
-
 static int
 testSchemaGrammar(const void *opaque)
 {
-    struct testSchemaFileData *data = (struct testSchemaFileData *) opaque;
+    struct testSchemaData *data = (struct testSchemaData *) opaque;
     char *schema_path;
     int ret = -1;

@@ -171,7 +167,7 @@ static int
 mymain(void)
 {
     int ret = 0;
-    struct testSchemaFileData data;
+    struct testSchemaData data;

     memset(&data, 0, sizeof(data));

@@ -196,6 +192,30 @@ mymain(void)
         }                                                                      \
     } while (0)

+#define DO_TEST_FILE(sch, xmlfile)                                             \
+    do {                                                                       \
+        data.schema = sch;                                                     \
+        data.xml_path = xmlfile;                                               \
+        if (virTestRun("test schema grammar file: " sch,                       \
+                       testSchemaGrammar, &data) == 0) {                       \
+            /* initialize the validator even if the schema test                \
+             * was skipped because of VIR_TEST_RANGE */                        \
+            if (!data.validator && testSchemaGrammar(&data) < 0) {             \
+                ret = -1;                                                      \
+                break;                                                         \
+            }                                                                  \
+            if (virTestRun("Checking " xmlfile " against " sch,                \
+                           testSchemaFile, &data) < 0)                         \
+                ret = -1;                                                      \
+                                                                               \
+            virXMLValidatorFree(data.validator);                               \
+            data.validator = NULL;                                             \
+        } else {                                                               \
+            ret = -1;                                                          \
+        }                                                                      \
+    } while (0)
+
+
     DO_TEST("capability.rng", "capabilityschemadata", "xencapsdata");
     DO_TEST("domain.rng", "domainschemadata", "qemuargv2xmldata",
             "qemuxml2argvdata", "sexpr2xmldata", "xmconfigdata",
-- 
2.12.1




More information about the libvir-list mailing list