virschematest: Add possibility to have exceptions from the '-invalid' suffix

The exception is needed in qemuxml2xmltest which is in one instance
testing update from an invalid config to a valid one. Currently the
compliance with the test is achieved via a hack.

As further patches will be simpler without the hack present we need a
way to invert the expected output in specific cases.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2023-12-15 21:47:17 +01:00
parent 3a45455558
commit ad42a7e074
3 changed files with 31 additions and 10 deletions

View File

@ -1 +0,0 @@
disk-cdrom-empty-network-invalid-active.x86_64-latest.xml

View File

@ -33,6 +33,9 @@ struct testSchemaEntry {
/* if dirRegex is non-NULL the provided regular expression is used to match
* the file names in a directory (without path prefixed) and only matching
* files are validated */
const char **exceptions; /* optional NULL terminated list of filenames inside
directory where the expected validation result is
inverted */
const char *dirRegex;
const char *file;
};
@ -40,6 +43,7 @@ struct testSchemaEntry {
struct testSchemaData {
virXMLValidator *validator;
bool exception;
const char *xml_path;
};
@ -53,6 +57,9 @@ testSchemaValidateXML(const void *args)
bool shouldFail = !!strstr(data->xml_path, "-invalid.");
g_autoptr(xmlDoc) xml = NULL;
if (data->exception)
shouldFail = !shouldFail;
if (!(xml = virXMLParseFileCtxt(data->xml_path, NULL)))
return -1;
@ -66,12 +73,14 @@ testSchemaValidateXML(const void *args)
static int
testSchemaFile(const char *schema,
virXMLValidator *validator,
const char *path)
const char *path,
bool exception)
{
g_autofree char *test_name = NULL;
struct testSchemaData data = {
.validator = validator,
.xml_path = path,
.exception = exception,
};
test_name = g_strdup_printf("Checking %s against %s", path, schema);
@ -84,7 +93,7 @@ static int
testSchemaDir(const char *schema,
virXMLValidator *validator,
const char *dir_path,
const char *filterstr)
const struct testSchemaEntry *entry)
{
g_autoptr(DIR) dir = NULL;
struct dirent *ent;
@ -97,17 +106,18 @@ testSchemaDir(const char *schema,
return -1;
}
if (filterstr) {
if (entry->dirRegex) {
g_autoptr(GError) err = NULL;
if (!(filter = g_regex_new(filterstr, 0, 0, &err))) {
VIR_TEST_VERBOSE("\nfailed to compile regex '%s': %s", filterstr, err->message);
if (!(filter = g_regex_new(entry->dirRegex, 0, 0, &err))) {
VIR_TEST_VERBOSE("\nfailed to compile regex '%s': %s", entry->dirRegex, err->message);
return -1;
}
}
while ((rc = virDirRead(dir, &ent, dir_path)) > 0) {
g_autofree char *xml_path = NULL;
bool exception = false;
if (!virStringHasSuffix(ent->d_name, ".xml"))
continue;
@ -117,9 +127,12 @@ testSchemaDir(const char *schema,
!g_regex_match(filter, ent->d_name, 0, NULL))
continue;
if (entry->exceptions)
exception = g_strv_contains(entry->exceptions, ent->d_name);
xml_path = g_strdup_printf("%s/%s", dir_path, ent->d_name);
if (testSchemaFile(schema, validator, xml_path) < 0)
if (testSchemaFile(schema, validator, xml_path, exception) < 0)
ret = -1;
}
@ -190,14 +203,14 @@ testSchemaEntries(const char *schema,
if (entry->dir) {
g_autofree char *path = g_strdup_printf("%s/%s", abs_top_srcdir, entry->dir);
if (testSchemaDir(schema, validator, path, entry->dirRegex) < 0)
if (testSchemaDir(schema, validator, path, entry) < 0)
ret = -1;
}
if (entry->file) {
g_autofree char *path = g_strdup_printf("%s/%s", abs_top_srcdir, entry->file);
if (testSchemaFile(schema, validator, path) < 0)
if (testSchemaFile(schema, validator, path, false) < 0)
ret = -1;
}
}
@ -212,11 +225,20 @@ static const struct testSchemaEntry schemaCapability[] = {
{ .dir = "tests/qemucaps2xmloutdata" },
};
/* give exceptions for output files of invalid input XMLs */
static const char *exceptions_qemuxml2xmloutdata[] = {
"disk-cdrom-empty-network-invalid.x86_64-latest.xml",
NULL
};
static const struct testSchemaEntry schemaDomain[] = {
{ .dir = "tests/domainschemadata" },
{ .dir = "tests/qemuxml2argvdata" },
{ .dir = "tests/xmconfigdata" },
{ .dir = "tests/qemuxml2xmloutdata" },
{ .dir = "tests/qemuxml2xmloutdata",
.exceptions = exceptions_qemuxml2xmloutdata,
},
{ .dir = "tests/lxcxml2xmldata" },
{ .dir = "tests/lxcxml2xmloutdata" },
{ .dir = "tests/bhyvexml2argvdata" },