mirror of https://gitee.com/openkylin/qemu.git
tests: add qmp_assert_error_class()
This helper will simplify a bunch of code checking for QMP errors and can be shared by various tests. Note that test-qga does check for error description as well, so don't replace the code there for now. Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com> Reviewed-by: Thomas Huth <thuth@redhat.com> Signed-off-by: Thomas Huth <thuth@redhat.com>
This commit is contained in:
parent
b8e1f74b0a
commit
ebb4d82d88
|
@ -67,7 +67,6 @@ static void test_after_failed_device_add(void)
|
||||||
{
|
{
|
||||||
char driver[32];
|
char driver[32];
|
||||||
QDict *response;
|
QDict *response;
|
||||||
QDict *error;
|
|
||||||
|
|
||||||
snprintf(driver, sizeof(driver), "virtio-blk-%s",
|
snprintf(driver, sizeof(driver), "virtio-blk-%s",
|
||||||
qvirtio_get_dev_type());
|
qvirtio_get_dev_type());
|
||||||
|
@ -83,9 +82,7 @@ static void test_after_failed_device_add(void)
|
||||||
" 'drive': 'drive0'"
|
" 'drive': 'drive0'"
|
||||||
"}}", driver);
|
"}}", driver);
|
||||||
g_assert(response);
|
g_assert(response);
|
||||||
error = qdict_get_qdict(response, "error");
|
qmp_assert_error_class(response, "GenericError");
|
||||||
g_assert_cmpstr(qdict_get_try_str(error, "class"), ==, "GenericError");
|
|
||||||
qobject_unref(response);
|
|
||||||
|
|
||||||
/* Delete the drive */
|
/* Delete the drive */
|
||||||
drive_del();
|
drive_del();
|
||||||
|
|
|
@ -1194,3 +1194,14 @@ bool qmp_rsp_is_err(QDict *rsp)
|
||||||
qobject_unref(rsp);
|
qobject_unref(rsp);
|
||||||
return !!error;
|
return !!error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void qmp_assert_error_class(QDict *rsp, const char *class)
|
||||||
|
{
|
||||||
|
QDict *error = qdict_get_qdict(rsp, "error");
|
||||||
|
|
||||||
|
g_assert_cmpstr(qdict_get_try_str(error, "class"), ==, class);
|
||||||
|
g_assert_nonnull(qdict_get_try_str(error, "desc"));
|
||||||
|
g_assert(!qdict_haskey(rsp, "return"));
|
||||||
|
|
||||||
|
qobject_unref(rsp);
|
||||||
|
}
|
||||||
|
|
|
@ -1004,4 +1004,13 @@ void qtest_qmp_device_del(const char *id);
|
||||||
*/
|
*/
|
||||||
bool qmp_rsp_is_err(QDict *rsp);
|
bool qmp_rsp_is_err(QDict *rsp);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* qmp_assert_error_class:
|
||||||
|
* @rsp: QMP response to check for error
|
||||||
|
* @class: an error class
|
||||||
|
*
|
||||||
|
* Assert the response has the given error class and discard @rsp.
|
||||||
|
*/
|
||||||
|
void qmp_assert_error_class(QDict *rsp, const char *class);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -21,15 +21,6 @@
|
||||||
|
|
||||||
const char common_args[] = "-nodefaults -machine none";
|
const char common_args[] = "-nodefaults -machine none";
|
||||||
|
|
||||||
static const char *get_error_class(QDict *resp)
|
|
||||||
{
|
|
||||||
QDict *error = qdict_get_qdict(resp, "error");
|
|
||||||
const char *desc = qdict_get_try_str(error, "desc");
|
|
||||||
|
|
||||||
g_assert(desc);
|
|
||||||
return error ? qdict_get_try_str(error, "class") : NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_version(QObject *version)
|
static void test_version(QObject *version)
|
||||||
{
|
{
|
||||||
Visitor *v;
|
Visitor *v;
|
||||||
|
@ -42,15 +33,12 @@ static void test_version(QObject *version)
|
||||||
visit_free(v);
|
visit_free(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool recovered(QTestState *qts)
|
static void assert_recovered(QTestState *qts)
|
||||||
{
|
{
|
||||||
QDict *resp;
|
QDict *resp;
|
||||||
bool ret;
|
|
||||||
|
|
||||||
resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd' }");
|
resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd' }");
|
||||||
ret = !strcmp(get_error_class(resp), "CommandNotFound");
|
qmp_assert_error_class(resp, "CommandNotFound");
|
||||||
qobject_unref(resp);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_malformed(QTestState *qts)
|
static void test_malformed(QTestState *qts)
|
||||||
|
@ -60,73 +48,61 @@ static void test_malformed(QTestState *qts)
|
||||||
/* syntax error */
|
/* syntax error */
|
||||||
qtest_qmp_send_raw(qts, "{]\n");
|
qtest_qmp_send_raw(qts, "{]\n");
|
||||||
resp = qtest_qmp_receive(qts);
|
resp = qtest_qmp_receive(qts);
|
||||||
g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
|
qmp_assert_error_class(resp, "GenericError");
|
||||||
qobject_unref(resp);
|
assert_recovered(qts);
|
||||||
g_assert(recovered(qts));
|
|
||||||
|
|
||||||
/* lexical error: impossible byte outside string */
|
/* lexical error: impossible byte outside string */
|
||||||
qtest_qmp_send_raw(qts, "{\xFF");
|
qtest_qmp_send_raw(qts, "{\xFF");
|
||||||
resp = qtest_qmp_receive(qts);
|
resp = qtest_qmp_receive(qts);
|
||||||
g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
|
qmp_assert_error_class(resp, "GenericError");
|
||||||
qobject_unref(resp);
|
assert_recovered(qts);
|
||||||
g_assert(recovered(qts));
|
|
||||||
|
|
||||||
/* lexical error: funny control character outside string */
|
/* lexical error: funny control character outside string */
|
||||||
qtest_qmp_send_raw(qts, "{\x01");
|
qtest_qmp_send_raw(qts, "{\x01");
|
||||||
resp = qtest_qmp_receive(qts);
|
resp = qtest_qmp_receive(qts);
|
||||||
g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
|
qmp_assert_error_class(resp, "GenericError");
|
||||||
qobject_unref(resp);
|
assert_recovered(qts);
|
||||||
g_assert(recovered(qts));
|
|
||||||
|
|
||||||
/* lexical error: impossible byte in string */
|
/* lexical error: impossible byte in string */
|
||||||
qtest_qmp_send_raw(qts, "{'bad \xFF");
|
qtest_qmp_send_raw(qts, "{'bad \xFF");
|
||||||
resp = qtest_qmp_receive(qts);
|
resp = qtest_qmp_receive(qts);
|
||||||
g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
|
qmp_assert_error_class(resp, "GenericError");
|
||||||
qobject_unref(resp);
|
assert_recovered(qts);
|
||||||
g_assert(recovered(qts));
|
|
||||||
|
|
||||||
/* lexical error: control character in string */
|
/* lexical error: control character in string */
|
||||||
qtest_qmp_send_raw(qts, "{'execute': 'nonexistent', 'id':'\n");
|
qtest_qmp_send_raw(qts, "{'execute': 'nonexistent', 'id':'\n");
|
||||||
resp = qtest_qmp_receive(qts);
|
resp = qtest_qmp_receive(qts);
|
||||||
g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
|
qmp_assert_error_class(resp, "GenericError");
|
||||||
qobject_unref(resp);
|
assert_recovered(qts);
|
||||||
g_assert(recovered(qts));
|
|
||||||
|
|
||||||
/* lexical error: interpolation */
|
/* lexical error: interpolation */
|
||||||
qtest_qmp_send_raw(qts, "%%p\n");
|
qtest_qmp_send_raw(qts, "%%p\n");
|
||||||
/* two errors, one for "%", one for "p" */
|
/* two errors, one for "%", one for "p" */
|
||||||
resp = qtest_qmp_receive(qts);
|
resp = qtest_qmp_receive(qts);
|
||||||
g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
|
qmp_assert_error_class(resp, "GenericError");
|
||||||
qobject_unref(resp);
|
|
||||||
resp = qtest_qmp_receive(qts);
|
resp = qtest_qmp_receive(qts);
|
||||||
g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
|
qmp_assert_error_class(resp, "GenericError");
|
||||||
qobject_unref(resp);
|
assert_recovered(qts);
|
||||||
g_assert(recovered(qts));
|
|
||||||
|
|
||||||
/* Not even a dictionary */
|
/* Not even a dictionary */
|
||||||
resp = qtest_qmp(qts, "null");
|
resp = qtest_qmp(qts, "null");
|
||||||
g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
|
qmp_assert_error_class(resp, "GenericError");
|
||||||
qobject_unref(resp);
|
|
||||||
|
|
||||||
/* No "execute" key */
|
/* No "execute" key */
|
||||||
resp = qtest_qmp(qts, "{}");
|
resp = qtest_qmp(qts, "{}");
|
||||||
g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
|
qmp_assert_error_class(resp, "GenericError");
|
||||||
qobject_unref(resp);
|
|
||||||
|
|
||||||
/* "execute" isn't a string */
|
/* "execute" isn't a string */
|
||||||
resp = qtest_qmp(qts, "{ 'execute': true }");
|
resp = qtest_qmp(qts, "{ 'execute': true }");
|
||||||
g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
|
qmp_assert_error_class(resp, "GenericError");
|
||||||
qobject_unref(resp);
|
|
||||||
|
|
||||||
/* "arguments" isn't a dictionary */
|
/* "arguments" isn't a dictionary */
|
||||||
resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'arguments': [] }");
|
resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'arguments': [] }");
|
||||||
g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
|
qmp_assert_error_class(resp, "GenericError");
|
||||||
qobject_unref(resp);
|
|
||||||
|
|
||||||
/* extra key */
|
/* extra key */
|
||||||
resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'extra': true }");
|
resp = qtest_qmp(qts, "{ 'execute': 'no-such-cmd', 'extra': true }");
|
||||||
g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
|
qmp_assert_error_class(resp, "GenericError");
|
||||||
qobject_unref(resp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_qmp_protocol(void)
|
static void test_qmp_protocol(void)
|
||||||
|
@ -148,8 +124,7 @@ static void test_qmp_protocol(void)
|
||||||
|
|
||||||
/* Test valid command before handshake */
|
/* Test valid command before handshake */
|
||||||
resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
|
resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
|
||||||
g_assert_cmpstr(get_error_class(resp), ==, "CommandNotFound");
|
qmp_assert_error_class(resp, "CommandNotFound");
|
||||||
qobject_unref(resp);
|
|
||||||
|
|
||||||
/* Test malformed commands before handshake */
|
/* Test malformed commands before handshake */
|
||||||
test_malformed(qts);
|
test_malformed(qts);
|
||||||
|
@ -162,8 +137,7 @@ static void test_qmp_protocol(void)
|
||||||
|
|
||||||
/* Test repeated handshake */
|
/* Test repeated handshake */
|
||||||
resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
|
resp = qtest_qmp(qts, "{ 'execute': 'qmp_capabilities' }");
|
||||||
g_assert_cmpstr(get_error_class(resp), ==, "CommandNotFound");
|
qmp_assert_error_class(resp, "CommandNotFound");
|
||||||
qobject_unref(resp);
|
|
||||||
|
|
||||||
/* Test valid command */
|
/* Test valid command */
|
||||||
resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
|
resp = qtest_qmp(qts, "{ 'execute': 'query-version' }");
|
||||||
|
@ -182,9 +156,8 @@ static void test_qmp_protocol(void)
|
||||||
|
|
||||||
/* Test command failure with 'id' */
|
/* Test command failure with 'id' */
|
||||||
resp = qtest_qmp(qts, "{ 'execute': 'human-monitor-command', 'id': 2 }");
|
resp = qtest_qmp(qts, "{ 'execute': 'human-monitor-command', 'id': 2 }");
|
||||||
g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
|
|
||||||
g_assert_cmpint(qdict_get_int(resp, "id"), ==, 2);
|
g_assert_cmpint(qdict_get_int(resp, "id"), ==, 2);
|
||||||
qobject_unref(resp);
|
qmp_assert_error_class(resp, "GenericError");
|
||||||
|
|
||||||
qtest_quit(qts);
|
qtest_quit(qts);
|
||||||
}
|
}
|
||||||
|
|
|
@ -244,17 +244,12 @@ static void test_qga_invalid_id(gconstpointer fix)
|
||||||
static void test_qga_invalid_oob(gconstpointer fix)
|
static void test_qga_invalid_oob(gconstpointer fix)
|
||||||
{
|
{
|
||||||
const TestFixture *fixture = fix;
|
const TestFixture *fixture = fix;
|
||||||
QDict *ret, *error;
|
QDict *ret;
|
||||||
const char *class;
|
|
||||||
|
|
||||||
ret = qmp_fd(fixture->fd, "{'exec-oob': 'guest-ping'}");
|
ret = qmp_fd(fixture->fd, "{'exec-oob': 'guest-ping'}");
|
||||||
g_assert_nonnull(ret);
|
g_assert_nonnull(ret);
|
||||||
|
|
||||||
error = qdict_get_qdict(ret, "error");
|
qmp_assert_error_class(ret, "GenericError");
|
||||||
class = qdict_get_try_str(error, "class");
|
|
||||||
g_assert_cmpstr(class, ==, "GenericError");
|
|
||||||
|
|
||||||
qobject_unref(ret);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_qga_invalid_args(gconstpointer fix)
|
static void test_qga_invalid_args(gconstpointer fix)
|
||||||
|
|
Loading…
Reference in New Issue