libqtest: Add a generic function to run a callback function for every machine

Some tests need to run single tests for every available machine of the
current QEMU binary. To avoid code duplication, let's extract this
code that deals with 'query-machines' into a separate function.

Signed-off-by: Thomas Huth <thuth@redhat.com>
Message-Id: <1490860207-8302-3-git-send-email-thuth@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
This commit is contained in:
Thomas Huth 2017-03-30 09:50:06 +02:00 committed by Dr. David Alan Gilbert
parent 6bb87be893
commit 02ef6e878f
4 changed files with 80 additions and 89 deletions

View File

@ -946,3 +946,33 @@ bool qtest_big_endian(QTestState *s)
{
return s->big_endian;
}
void qtest_cb_for_every_machine(void (*cb)(const char *machine))
{
QDict *response, *minfo;
QList *list;
const QListEntry *p;
QObject *qobj;
QString *qstr;
const char *mname;
qtest_start("-machine none");
response = qmp("{ 'execute': 'query-machines' }");
g_assert(response);
list = qdict_get_qlist(response, "return");
g_assert(list);
for (p = qlist_first(list); p; p = qlist_next(p)) {
minfo = qobject_to_qdict(qlist_entry_obj(p));
g_assert(minfo);
qobj = qdict_get(minfo, "name");
g_assert(qobj);
qstr = qobject_to_qstring(qobj);
g_assert(qstr);
mname = qstring_get_str(qstr);
cb(mname);
}
qtest_end();
QDECREF(response);
}

View File

@ -919,4 +919,12 @@ void qmp_fd_send(int fd, const char *fmt, ...);
QDict *qmp_fdv(int fd, const char *fmt, va_list ap);
QDict *qmp_fd(int fd, const char *fmt, ...);
/**
* qtest_cb_for_every_machine:
* @cb: Pointer to the callback function
*
* Call a callback function for every name of all available machines.
*/
void qtest_cb_for_every_machine(void (*cb)(const char *machine));
#endif

View File

@ -79,33 +79,13 @@ static void test_data_free(gpointer data)
g_free(pc);
}
static void add_pc_test_cases(void)
static void add_pc_test_case(const char *mname)
{
QDict *response, *minfo;
QList *list;
const QListEntry *p;
QObject *qobj;
QString *qstr;
const char *mname;
char *path;
PCTestData *data;
qtest_start("-machine none");
response = qmp("{ 'execute': 'query-machines' }");
g_assert(response);
list = qdict_get_qlist(response, "return");
g_assert(list);
for (p = qlist_first(list); p; p = qlist_next(p)) {
minfo = qobject_to_qdict(qlist_entry_obj(p));
g_assert(minfo);
qobj = qdict_get(minfo, "name");
g_assert(qobj);
qstr = qobject_to_qstring(qobj);
g_assert(qstr);
mname = qstring_get_str(qstr);
if (!g_str_has_prefix(mname, "pc-")) {
continue;
return;
}
data = g_malloc(sizeof(PCTestData));
data->machine = g_strdup(mname);
@ -140,9 +120,6 @@ static void add_pc_test_cases(void)
g_free(path);
}
}
QDECREF(response);
qtest_end();
}
int main(int argc, char **argv)
{
@ -151,7 +128,7 @@ int main(int argc, char **argv)
g_test_init(&argc, &argv, NULL);
if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
add_pc_test_cases();
qtest_cb_for_every_machine(add_pc_test_case);
}
return g_test_run();

View File

@ -107,30 +107,10 @@ static void test_machine(gconstpointer data)
g_free((void *)machine);
}
static void add_machine_test_cases(void)
static void add_machine_test_case(const char *mname)
{
const char *arch = qtest_get_arch();
QDict *response, *minfo;
QList *list;
const QListEntry *p;
QObject *qobj;
QString *qstr;
const char *mname;
qtest_start("-machine none");
response = qmp("{ 'execute': 'query-machines' }");
g_assert(response);
list = qdict_get_qlist(response, "return");
g_assert(list);
for (p = qlist_first(list); p; p = qlist_next(p)) {
minfo = qobject_to_qdict(qlist_entry_obj(p));
g_assert(minfo);
qobj = qdict_get(minfo, "name");
g_assert(qobj);
qstr = qobject_to_qstring(qobj);
g_assert(qstr);
mname = qstring_get_str(qstr);
if (!is_blacklisted(arch, mname)) {
char *path = g_strdup_printf("qom/%s", mname);
qtest_add_data_func(path, g_strdup(mname), test_machine);
@ -138,15 +118,11 @@ static void add_machine_test_cases(void)
}
}
qtest_end();
QDECREF(response);
}
int main(int argc, char **argv)
{
g_test_init(&argc, &argv, NULL);
add_machine_test_cases();
qtest_cb_for_every_machine(add_machine_test_case);
return g_test_run();
}