testutilsqemuschema: Allow loading non-latest schema

Add testQEMUSchemaLoad and refactor internals so that we can load the
QMP schema from an arbitrary caps replies file.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Peter Krempa 2020-05-15 16:32:40 +02:00
parent 18e9806c64
commit b6246d9320
2 changed files with 45 additions and 22 deletions

View File

@ -607,6 +607,45 @@ testQEMUSchemaValidateCommand(const char *command,
} }
static virJSONValuePtr
testQEMUSchemaLoadReplies(const char *filename)
{
g_autofree char *caps = NULL;
char *schemaReply;
char *end;
g_autoptr(virJSONValue) reply = NULL;
virJSONValuePtr schema = NULL;
if (virTestLoadFile(filename, &caps) < 0)
return NULL;
if (!(schemaReply = strstr(caps, "\"execute\": \"query-qmp-schema\"")) ||
!(schemaReply = strstr(schemaReply, "\n\n")) ||
!(end = strstr(schemaReply + 2, "\n\n"))) {
VIR_TEST_VERBOSE("failed to find reply to 'query-qmp-schema' in '%s'",
filename);
return NULL;
}
schemaReply += 2;
*end = '\0';
if (!(reply = virJSONValueFromString(schemaReply))) {
VIR_TEST_VERBOSE("failed to parse 'query-qmp-schema' reply from '%s'",
filename);
return NULL;
}
if (!(schema = virJSONValueObjectStealArray(reply, "return"))) {
VIR_TEST_VERBOSE("missing qapi schema data in reply in '%s'",
filename);
return NULL;
}
return schema;
}
/** /**
* testQEMUSchemaGetLatest: * testQEMUSchemaGetLatest:
* *
@ -617,11 +656,6 @@ virJSONValuePtr
testQEMUSchemaGetLatest(const char *arch) testQEMUSchemaGetLatest(const char *arch)
{ {
g_autofree char *capsLatestFile = NULL; g_autofree char *capsLatestFile = NULL;
g_autofree char *capsLatest = NULL;
char *schemaReply;
char *end;
g_autoptr(virJSONValue) reply = NULL;
virJSONValuePtr schema = NULL;
if (!(capsLatestFile = testQemuGetLatestCapsForArch(arch, "replies"))) { if (!(capsLatestFile = testQemuGetLatestCapsForArch(arch, "replies"))) {
VIR_TEST_VERBOSE("failed to find latest caps replies"); VIR_TEST_VERBOSE("failed to find latest caps replies");
@ -630,33 +664,7 @@ testQEMUSchemaGetLatest(const char* arch)
VIR_TEST_DEBUG("replies file: '%s'", capsLatestFile); VIR_TEST_DEBUG("replies file: '%s'", capsLatestFile);
if (virTestLoadFile(capsLatestFile, &capsLatest) < 0) return testQEMUSchemaLoadReplies(capsLatestFile);
return NULL;
if (!(schemaReply = strstr(capsLatest, "\"execute\": \"query-qmp-schema\"")) ||
!(schemaReply = strstr(schemaReply, "\n\n")) ||
!(end = strstr(schemaReply + 2, "\n\n"))) {
VIR_TEST_VERBOSE("failed to find reply to 'query-qmp-schema' in '%s'",
capsLatestFile);
return NULL;
}
schemaReply += 2;
*end = '\0';
if (!(reply = virJSONValueFromString(schemaReply))) {
VIR_TEST_VERBOSE("failed to parse 'query-qmp-schema' reply from '%s'",
capsLatestFile);
return NULL;
}
if (!(schema = virJSONValueObjectStealArray(reply, "return"))) {
VIR_TEST_VERBOSE("missing qapi schema data in reply in '%s'",
capsLatestFile);
return NULL;
}
return schema;
} }
@ -670,3 +678,15 @@ testQEMUSchemaLoadLatest(const char *arch)
return virQEMUQAPISchemaConvert(schema); return virQEMUQAPISchemaConvert(schema);
} }
virHashTablePtr
testQEMUSchemaLoad(const char *filename)
{
virJSONValuePtr schema;
if (!(schema = testQEMUSchemaLoadReplies(filename)))
return NULL;
return virQEMUQAPISchemaConvert(schema);
}

View File

@ -42,3 +42,6 @@ testQEMUSchemaGetLatest(const char* arch);
virHashTablePtr virHashTablePtr
testQEMUSchemaLoadLatest(const char *arch); testQEMUSchemaLoadLatest(const char *arch);
virHashTablePtr
testQEMUSchemaLoad(const char *filename);