Skip to content

Commit

Permalink
vhost-user-test: create a temporary directory per TestServer
Browse files Browse the repository at this point in the history
This makes the tests more independent, and also the source and destination
TestServers in the migration test.

Reviewed-by: Marc-André Lureau <[email protected]>
Signed-off-by: Paolo Bonzini <[email protected]>
Message-Id: <[email protected]>
Message-Id: <[email protected]>
Reviewed-by: Michael S. Tsirkin <[email protected]>
Signed-off-by: Michael S. Tsirkin <[email protected]>
  • Loading branch information
bonzini authored and mstsirkin committed Feb 21, 2019
1 parent 9ee1bb8 commit 4d3f50e
Showing 1 changed file with 35 additions and 42 deletions.
77 changes: 35 additions & 42 deletions tests/vhost-user-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ typedef struct TestServer {
gchar *socket_path;
gchar *mig_path;
gchar *chr_name;
const gchar *mem_path;
gchar *tmpfs;
CharBackend chr;
int fds_num;
int fds[VHOST_MEMORY_MAX_NREGIONS];
Expand All @@ -163,17 +165,14 @@ static TestServer *test_server_new(const gchar *name);
static void test_server_free(TestServer *server);
static void test_server_listen(TestServer *server);

static const char *tmpfs;
static const char *root;

enum test_memfd {
TEST_MEMFD_AUTO,
TEST_MEMFD_YES,
TEST_MEMFD_NO,
};

static char *get_qemu_cmd(TestServer *s,
int mem, enum test_memfd memfd, const char *mem_path,
int mem, enum test_memfd memfd,
const char *chr_opts, const char *extra)
{
if (memfd == TEST_MEMFD_AUTO && qemu_memfd_check(0)) {
Expand All @@ -188,7 +187,7 @@ static char *get_qemu_cmd(TestServer *s,
} else {
return g_strdup_printf(QEMU_CMD_MEM QEMU_CMD_CHR
QEMU_CMD_NETDEV QEMU_CMD_NET "%s", mem, mem,
mem_path, s->chr_name, s->socket_path,
s->mem_path, s->chr_name, s->socket_path,
chr_opts, s->chr_name, extra);
}
}
Expand Down Expand Up @@ -507,13 +506,23 @@ static const char *init_hugepagefs(void)
static TestServer *test_server_new(const gchar *name)
{
TestServer *server = g_new0(TestServer, 1);
char template[] = "/tmp/vhost-test-XXXXXX";
const char *tmpfs;

server->context = g_main_context_new();
server->loop = g_main_loop_new(server->context, FALSE);

/* run the main loop thread so the chardev may operate */
server->thread = g_thread_new(NULL, thread_function, server->loop);

tmpfs = mkdtemp(template);
if (!tmpfs) {
g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
}
g_assert(tmpfs);

server->tmpfs = g_strdup(tmpfs);
server->mem_path = init_hugepagefs() ? : server->tmpfs;
server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name);
server->mig_path = g_strdup_printf("%s/%s.mig", tmpfs, name);
server->chr_name = g_strdup_printf("chr-%s", name);
Expand Down Expand Up @@ -559,7 +568,7 @@ static void test_server_listen(TestServer *server)

static void test_server_free(TestServer *server)
{
int i;
int i, ret;

/* finish the helper thread and dispatch pending sources */
g_main_loop_quit(server->loop);
Expand All @@ -568,6 +577,18 @@ static void test_server_free(TestServer *server)
g_main_context_iteration(NULL, TRUE);
}

unlink(server->socket_path);
g_free(server->socket_path);

unlink(server->mig_path);
g_free(server->mig_path);

ret = rmdir(server->tmpfs);
if (ret != 0) {
g_test_message("unable to rmdir: path (%s): %s",
server->tmpfs, strerror(errno));
}

qemu_chr_fe_deinit(&server->chr, true);

for (i = 0; i < server->fds_num; i++) {
Expand All @@ -578,12 +599,6 @@ static void test_server_free(TestServer *server)
close(server->log_fd);
}

unlink(server->socket_path);
g_free(server->socket_path);

unlink(server->mig_path);
g_free(server->mig_path);

g_free(server->chr_name);
g_assert(server->bus);
qpci_free_pc(server->bus);
Expand Down Expand Up @@ -691,7 +706,7 @@ static void test_read_guest_mem(const void *arg)
"read-guest-memfd" : "read-guest-mem");
test_server_listen(server);

qemu_cmd = get_qemu_cmd(server, 512, memfd, root, "", "");
qemu_cmd = get_qemu_cmd(server, 512, memfd, "", "");

s = qtest_start(qemu_cmd);
g_free(qemu_cmd);
Expand Down Expand Up @@ -726,7 +741,7 @@ static void test_migrate(void)
test_server_listen(s);
test_server_listen(dest);

cmd = get_qemu_cmd(s, 2, TEST_MEMFD_AUTO, root, "", "");
cmd = get_qemu_cmd(s, 2, TEST_MEMFD_AUTO, "", "");
from = qtest_start(cmd);
g_free(cmd);

Expand All @@ -739,7 +754,7 @@ static void test_migrate(void)
g_assert_cmpint(size, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8));

tmp = g_strdup_printf(" -incoming %s", uri);
cmd = get_qemu_cmd(dest, 2, TEST_MEMFD_AUTO, root, "", tmp);
cmd = get_qemu_cmd(dest, 2, TEST_MEMFD_AUTO, "", tmp);
g_free(tmp);
to = qtest_init(cmd);
g_free(cmd);
Expand Down Expand Up @@ -850,7 +865,7 @@ static void test_reconnect_subprocess(void)
char *cmd;

g_thread_new("connect", connect_thread, s);
cmd = get_qemu_cmd(s, 2, TEST_MEMFD_AUTO, root, ",server", "");
cmd = get_qemu_cmd(s, 2, TEST_MEMFD_AUTO, ",server", "");
qtest_start(cmd);
g_free(cmd);

Expand Down Expand Up @@ -895,7 +910,7 @@ static void test_connect_fail_subprocess(void)

s->test_fail = true;
g_thread_new("connect", connect_thread, s);
cmd = get_qemu_cmd(s, 2, TEST_MEMFD_AUTO, root, ",server", "");
cmd = get_qemu_cmd(s, 2, TEST_MEMFD_AUTO, ",server", "");
qtest_start(cmd);
g_free(cmd);

Expand Down Expand Up @@ -928,7 +943,7 @@ static void test_flags_mismatch_subprocess(void)

s->test_flags = TEST_FLAGS_DISCONNECT;
g_thread_new("connect", connect_thread, s);
cmd = get_qemu_cmd(s, 2, TEST_MEMFD_AUTO, root, ",server", "");
cmd = get_qemu_cmd(s, 2, TEST_MEMFD_AUTO, ",server", "");
qtest_start(cmd);
g_free(cmd);

Expand Down Expand Up @@ -976,7 +991,7 @@ static void test_multiqueue(void)
cmd = g_strdup_printf(
QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=%d "
"-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
512, 512, root, s->chr_name,
512, 512, s->mem_path, s->chr_name,
s->socket_path, "", s->chr_name,
s->queues, s->queues * 2 + 2);
}
Expand All @@ -996,22 +1011,11 @@ static void test_multiqueue(void)

int main(int argc, char **argv)
{
int ret;
char template[] = "/tmp/vhost-test-XXXXXX";

g_test_init(&argc, &argv, NULL);

module_call_init(MODULE_INIT_QOM);
qemu_add_opts(&qemu_chardev_opts);

tmpfs = mkdtemp(template);
if (!tmpfs) {
g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno));
}
g_assert(tmpfs);

root = init_hugepagefs() ? : tmpfs;

if (qemu_memfd_check(0)) {
qtest_add_data_func("/vhost-user/read-guest-mem/memfd",
GINT_TO_POINTER(TEST_MEMFD_YES),
Expand All @@ -1035,16 +1039,5 @@ int main(int argc, char **argv)
qtest_add_func("/vhost-user/flags-mismatch", test_flags_mismatch);
}

ret = g_test_run();

/* cleanup */

ret = rmdir(tmpfs);
if (ret != 0) {
g_test_message("unable to rmdir: path (%s): %s\n",
tmpfs, strerror(errno));
}
g_assert_cmpint(ret, ==, 0);

return ret;
return g_test_run();
}

0 comments on commit 4d3f50e

Please sign in to comment.