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

Peter Krempa posted 4 patches 8 years, 10 months ago
[libvirt] [PATCH 1/4] tests: schema: Add possibility to validate individual files
Posted by Peter Krempa 8 years, 10 months ago
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

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 1/4] tests: schema: Add possibility to validate individual files
Posted by Andrea Bolognani 8 years, 10 months ago
On Tue, 2017-03-28 at 13:58 +0200, Peter Krempa wrote:
[...]
> @@ -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)
> +
> +

Only one empty line here, please.


Now that you've introduced DO_TEST_FILE(), I think it would
make sense to rename DO_TEST() to DO_TEST_DIRS() for clarity.

ACK whether you feel the same or not.

-- 
Andrea Bolognani / Red Hat / Virtualization

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 1/4] tests: schema: Add possibility to validate individual files
Posted by Martin Kletzander 8 years, 10 months ago
On Tue, Mar 28, 2017 at 01:58:56PM +0200, Peter Krempa wrote:
>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)                         \

testSchemaFile does not prepend abs_srcdir to the path (like
testSchemaDirs does), so after you add the test case, the check fails in
vpath builds (I just checked it).  I would suggest just a wrapper around
testSchemaFile that prepends that path or just use it when calling the
macro.

>+                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
>
>--
>libvir-list mailing list
>libvir-list@redhat.com
>https://www.redhat.com/mailman/listinfo/libvir-list
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list