Re: [PATCHv2 5/5] Introduce virschematest

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

 



On Tue, Jun 07, 2016 at 20:07:32 +0200, Ján Tomko wrote:
> 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(-)
>  create mode 100644 tests/virschematest.c

[...]

> 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 \

domainschematest was removed too so that line needs to be deleted

> -	domainsnapshotschematest \
>  	domainsnapshotxml2xmlin \
>  	domainsnapshotxml2xmlout \
>  	fchostdata \

> 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 @@

[...]

> +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;

At least this needs to be initialized as it's possible to jump to
cleanup if virAsprintf of xml_path fails in the first iteration.

> +    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;

This may jump to cleanup before, ...

> +
> +    va_start(args, schema);

calling va_start ...

> +    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);

... and call va_end.

> +    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);

You can hide the 'NULL' sentinel inside the macro after __VA_ARGS__

ACK with the problems fixed.

--
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]