qga: test: Add tests for merged flag

This commit adds a test to ensure `merged` functions as expected.
We also add a negative test to ensure we haven't regressed previous
functionality.

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
Signed-off-by: Konstantin Kostiuk <kkostiuk@redhat.com>
This commit is contained in:
Daniel Xu 2023-03-22 18:19:28 -06:00 committed by Konstantin Kostiuk
parent 810f677ab8
commit c7d74f2724

View File

@ -755,32 +755,16 @@ static void test_qga_fsfreeze_status(gconstpointer fix)
g_assert_cmpstr(status, ==, "thawed"); g_assert_cmpstr(status, ==, "thawed");
} }
static void test_qga_guest_exec(gconstpointer fix) static QDict *wait_for_guest_exec_completion(int fd, int64_t pid)
{ {
const TestFixture *fixture = fix; QDict *ret = NULL;
g_autoptr(QDict) ret = NULL; int64_t now;
QDict *val;
const gchar *out;
g_autofree guchar *decoded = NULL;
int64_t pid, now, exitcode;
gsize len;
bool exited; bool exited;
QDict *val;
/* exec 'echo foo bar' */
ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {"
" 'path': '/bin/echo', 'arg': [ '-n', '\" test_str \"' ],"
" 'capture-output': true } }");
g_assert_nonnull(ret);
qmp_assert_no_error(ret);
val = qdict_get_qdict(ret, "return");
pid = qdict_get_int(val, "pid");
g_assert_cmpint(pid, >, 0);
qobject_unref(ret);
/* wait for completion */
now = g_get_monotonic_time(); now = g_get_monotonic_time();
do { do {
ret = qmp_fd(fixture->fd, ret = qmp_fd(fd,
"{'execute': 'guest-exec-status'," "{'execute': 'guest-exec-status',"
" 'arguments': { 'pid': %" PRId64 " } }", pid); " 'arguments': { 'pid': %" PRId64 " } }", pid);
g_assert_nonnull(ret); g_assert_nonnull(ret);
@ -793,7 +777,34 @@ static void test_qga_guest_exec(gconstpointer fix)
g_get_monotonic_time() < now + 5 * G_TIME_SPAN_SECOND); g_get_monotonic_time() < now + 5 * G_TIME_SPAN_SECOND);
g_assert(exited); g_assert(exited);
return ret;
}
static void test_qga_guest_exec(gconstpointer fix)
{
const TestFixture *fixture = fix;
g_autoptr(QDict) ret = NULL;
QDict *val;
const gchar *out;
g_autofree guchar *decoded = NULL;
int64_t pid, exitcode;
gsize len;
/* exec 'echo foo bar' */
ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {"
" 'path': '/bin/echo', 'arg': [ '-n', '\" test_str \"' ],"
" 'capture-output': true } }");
g_assert_nonnull(ret);
qmp_assert_no_error(ret);
val = qdict_get_qdict(ret, "return");
pid = qdict_get_int(val, "pid");
g_assert_cmpint(pid, >, 0);
qobject_unref(ret);
ret = wait_for_guest_exec_completion(fixture->fd, pid);
/* check stdout */ /* check stdout */
val = qdict_get_qdict(ret, "return");
exitcode = qdict_get_int(val, "exitcode"); exitcode = qdict_get_int(val, "exitcode");
g_assert_cmpint(exitcode, ==, 0); g_assert_cmpint(exitcode, ==, 0);
out = qdict_get_str(val, "out-data"); out = qdict_get_str(val, "out-data");
@ -802,6 +813,115 @@ static void test_qga_guest_exec(gconstpointer fix)
g_assert_cmpstr((char *)decoded, ==, "\" test_str \""); g_assert_cmpstr((char *)decoded, ==, "\" test_str \"");
} }
#if defined(G_OS_WIN32)
static void test_qga_guest_exec_separated(gconstpointer fix)
{
}
static void test_qga_guest_exec_merged(gconstpointer fix)
{
const TestFixture *fixture = fix;
g_autoptr(QDict) ret = NULL;
QDict *val;
const gchar *class, *desc;
g_autofree guchar *decoded = NULL;
/* exec 'echo foo bar' */
ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {"
" 'path': 'echo',"
" 'arg': [ 'execution never reaches here' ],"
" 'capture-output': 'merged' } }");
g_assert_nonnull(ret);
val = qdict_get_qdict(ret, "error");
g_assert_nonnull(val);
class = qdict_get_str(val, "class");
desc = qdict_get_str(val, "desc");
g_assert_cmpstr(class, ==, "GenericError");
g_assert_cmpint(strlen(desc), >, 0);
}
#else
static void test_qga_guest_exec_separated(gconstpointer fix)
{
const TestFixture *fixture = fix;
g_autoptr(QDict) ret = NULL;
QDict *val;
const gchar *out, *err;
g_autofree guchar *out_decoded = NULL;
g_autofree guchar *err_decoded = NULL;
int64_t pid, exitcode;
gsize len;
/* exec 'echo foo bar' */
ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {"
" 'path': '/bin/bash',"
" 'arg': [ '-c', 'for i in $(seq 4); do if (( $i %% 2 )); then echo stdout; else echo stderr 1>&2; fi; done;' ],"
" 'capture-output': 'separated' } }");
g_assert_nonnull(ret);
qmp_assert_no_error(ret);
val = qdict_get_qdict(ret, "return");
pid = qdict_get_int(val, "pid");
g_assert_cmpint(pid, >, 0);
qobject_unref(ret);
ret = wait_for_guest_exec_completion(fixture->fd, pid);
val = qdict_get_qdict(ret, "return");
exitcode = qdict_get_int(val, "exitcode");
g_assert_cmpint(exitcode, ==, 0);
/* check stdout */
out = qdict_get_str(val, "out-data");
out_decoded = g_base64_decode(out, &len);
g_assert_cmpint(len, ==, 14);
g_assert_cmpstr((char *)out_decoded, ==, "stdout\nstdout\n");
/* check stderr */
err = qdict_get_try_str(val, "err-data");
err_decoded = g_base64_decode(err, &len);
g_assert_cmpint(len, ==, 14);
g_assert_cmpstr((char *)err_decoded, ==, "stderr\nstderr\n");
}
static void test_qga_guest_exec_merged(gconstpointer fix)
{
const TestFixture *fixture = fix;
g_autoptr(QDict) ret = NULL;
QDict *val;
const gchar *out, *err;
g_autofree guchar *decoded = NULL;
int64_t pid, exitcode;
gsize len;
/* exec 'echo foo bar' */
ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {"
" 'path': '/bin/bash',"
" 'arg': [ '-c', 'for i in $(seq 4); do if (( $i %% 2 )); then echo stdout; else echo stderr 1>&2; fi; done;' ],"
" 'capture-output': 'merged' } }");
g_assert_nonnull(ret);
qmp_assert_no_error(ret);
val = qdict_get_qdict(ret, "return");
pid = qdict_get_int(val, "pid");
g_assert_cmpint(pid, >, 0);
qobject_unref(ret);
ret = wait_for_guest_exec_completion(fixture->fd, pid);
val = qdict_get_qdict(ret, "return");
exitcode = qdict_get_int(val, "exitcode");
g_assert_cmpint(exitcode, ==, 0);
/* check stdout */
out = qdict_get_str(val, "out-data");
decoded = g_base64_decode(out, &len);
g_assert_cmpint(len, ==, 28);
g_assert_cmpstr((char *)decoded, ==, "stdout\nstderr\nstdout\nstderr\n");
/* check stderr */
err = qdict_get_try_str(val, "err-data");
g_assert_null(err);
}
#endif
static void test_qga_guest_exec_invalid(gconstpointer fix) static void test_qga_guest_exec_invalid(gconstpointer fix)
{ {
const TestFixture *fixture = fix; const TestFixture *fixture = fix;
@ -972,6 +1092,10 @@ int main(int argc, char **argv)
g_test_add_data_func("/qga/blockedrpcs", NULL, test_qga_blockedrpcs); g_test_add_data_func("/qga/blockedrpcs", NULL, test_qga_blockedrpcs);
g_test_add_data_func("/qga/config", NULL, test_qga_config); g_test_add_data_func("/qga/config", NULL, test_qga_config);
g_test_add_data_func("/qga/guest-exec", &fix, test_qga_guest_exec); g_test_add_data_func("/qga/guest-exec", &fix, test_qga_guest_exec);
g_test_add_data_func("/qga/guest-exec-separated", &fix,
test_qga_guest_exec_separated);
g_test_add_data_func("/qga/guest-exec-merged", &fix,
test_qga_guest_exec_merged);
g_test_add_data_func("/qga/guest-exec-invalid", &fix, g_test_add_data_func("/qga/guest-exec-invalid", &fix,
test_qga_guest_exec_invalid); test_qga_guest_exec_invalid);
g_test_add_data_func("/qga/guest-get-osinfo", &fix, g_test_add_data_func("/qga/guest-get-osinfo", &fix,