Message ID | 20170901180340.30009-28-eblake@redhat.com |
---|---|
State | New |
Headers | show |
Series | Preliminary libqtest cleanups | expand |
On 01.09.2017 20:03, Eric Blake wrote: > Several callers were formatting a string into a temporary > variable before calling qtest_init(); factor that into the > common code. > > Signed-off-by: Eric Blake <eblake@redhat.com> > --- > tests/libqtest.h | 7 ++++--- > tests/libqtest.c | 13 ++++++++++-- > tests/bios-tables-test.c | 7 +------ > tests/postcopy-test.c | 4 ++-- > tests/vhost-user-test.c | 51 ++++++++++++++---------------------------------- > 5 files changed, 33 insertions(+), 49 deletions(-) > > diff --git a/tests/libqtest.h b/tests/libqtest.h > index 3ae570927a..d338de3e22 100644 > --- a/tests/libqtest.h > +++ b/tests/libqtest.h > @@ -25,11 +25,12 @@ extern QTestState *global_qtest; > > /** > * qtest_init: > - * @extra_args: other arguments to pass to QEMU. > + * @extra_args...: other arguments to pass to QEMU, formatted as if by > + * sprintf(). > * > * Returns: #QTestState instance. > */ > -QTestState *qtest_init(const char *extra_args); > +QTestState *qtest_init(const char *extra_args, ...) GCC_FMT_ATTR(1, 2); Maybe the parameter should be called "fmt" or "format" now instead? (similar to printf() and friends) > /** > * qtest_init_without_qmp_handshake: > @@ -518,7 +519,7 @@ void qtest_add_abrt_handler(GHookFunc fn, const void *data); > */ > static inline QTestState *qtest_start(const char *args) > { > - global_qtest = qtest_init(args); > + global_qtest = qtest_init("%s", args); That's a little bit sad that there are some spots which need an explicit "%s" now ... maybe it would be nicer to add a new function called qtest_initf() instead and keep the old one (without the "f") as it is? > return global_qtest; > } > > diff --git a/tests/libqtest.c b/tests/libqtest.c > index b6dd26e54a..18facbf130 100644 > --- a/tests/libqtest.c > +++ b/tests/libqtest.c > @@ -347,9 +347,18 @@ QTestState *qtest_init_without_qmp_handshake(const char *extra_args) > return s; > } > > -QTestState *qtest_init(const char *extra_args) > +QTestState *qtest_init(const char *extra_args, ...) > { > - QTestState *s = qtest_init_without_qmp_handshake(extra_args); > + va_list ap; > + QTestState *s; > + char *cmd; > + > + va_start(ap, extra_args); > + cmd = g_strdup_vprintf(extra_args, ap); > + va_end(ap); > + > + s = qtest_init_without_qmp_handshake(cmd); > + g_free(cmd); > > /* Read the QMP greeting and then do the handshake */ > qtest_qmp_discard_response(s, ""); > diff --git a/tests/bios-tables-test.c b/tests/bios-tables-test.c > index 976792f2c5..f8a107499f 100644 > --- a/tests/bios-tables-test.c > +++ b/tests/bios-tables-test.c > @@ -623,18 +623,14 @@ static void test_smbios_structs(test_data *data) > > static void test_acpi_one(const char *params, test_data *data) > { > - char *args; > - > /* Disable kernel irqchip to be able to override apic irq0. */ > - args = g_strdup_printf("-machine %s,accel=%s,kernel-irqchip=off " > + data->qts = qtest_init("-machine %s,accel=%s,kernel-irqchip=off " > "-net none -display none %s " > "-drive id=hd0,if=none,file=%s,format=raw " > "-device ide-hd,drive=hd0 ", > data->machine, "kvm:tcg", > params ? params : "", disk); > > - data->qts = qtest_init(args); > - > boot_sector_test(data->qts); > > test_acpi_rsdp_address(data); > @@ -657,7 +653,6 @@ static void test_acpi_one(const char *params, test_data *data) > test_smbios_structs(data); > > qtest_quit(data->qts); > - g_free(args); > } > > static uint8_t base_required_struct_types[] = { > diff --git a/tests/postcopy-test.c b/tests/postcopy-test.c > index 0e5fe20a83..20cfb280f6 100644 > --- a/tests/postcopy-test.c > +++ b/tests/postcopy-test.c > @@ -403,10 +403,10 @@ static void test_migrate(void) > > g_free(bootpath); > > - from = qtest_init(cmd_src); > + from = qtest_init("%s", cmd_src); > g_free(cmd_src); > > - to = qtest_init(cmd_dst); > + to = qtest_init("%s", cmd_dst); > g_free(cmd_dst); > > assert(!global_qtest); Thomas
On 09/05/2017 07:46 AM, Thomas Huth wrote: > On 01.09.2017 20:03, Eric Blake wrote: >> Several callers were formatting a string into a temporary >> variable before calling qtest_init(); factor that into the >> common code. >> >> Signed-off-by: Eric Blake <eblake@redhat.com> >> --- >> -QTestState *qtest_init(const char *extra_args); >> +QTestState *qtest_init(const char *extra_args, ...) GCC_FMT_ATTR(1, 2); > > Maybe the parameter should be called "fmt" or "format" now instead? > (similar to printf() and friends) Sure. > >> /** >> * qtest_init_without_qmp_handshake: >> @@ -518,7 +519,7 @@ void qtest_add_abrt_handler(GHookFunc fn, const void *data); >> */ >> static inline QTestState *qtest_start(const char *args) >> { >> - global_qtest = qtest_init(args); >> + global_qtest = qtest_init("%s", args); > > That's a little bit sad that there are some spots which need an explicit > "%s" now ... maybe it would be nicer to add a new function called > qtest_initf() instead and keep the old one (without the "f") as it is? Indeed, and looking back over my series, I see we could also benefit from a va_list form. Also, since you already questioned me on the churn from qtest_start() => qtest_init() => qtest_start(), I can make less churn in the next spin by converging on qtest_start() from the get-go. So I will go with qtest_start() (plain string), qtest_startf() (formatted like sprintf), and qtest_vstartf() (formatted like vsprintf).
On 07.09.2017 20:00, Eric Blake wrote: > On 09/05/2017 07:46 AM, Thomas Huth wrote: >> On 01.09.2017 20:03, Eric Blake wrote: >>> Several callers were formatting a string into a temporary >>> variable before calling qtest_init(); factor that into the >>> common code. >>> >>> Signed-off-by: Eric Blake <eblake@redhat.com> >>> --- > >>> -QTestState *qtest_init(const char *extra_args); >>> +QTestState *qtest_init(const char *extra_args, ...) GCC_FMT_ATTR(1, 2); >> >> Maybe the parameter should be called "fmt" or "format" now instead? >> (similar to printf() and friends) > > Sure. > >> >>> /** >>> * qtest_init_without_qmp_handshake: >>> @@ -518,7 +519,7 @@ void qtest_add_abrt_handler(GHookFunc fn, const void *data); >>> */ >>> static inline QTestState *qtest_start(const char *args) >>> { >>> - global_qtest = qtest_init(args); >>> + global_qtest = qtest_init("%s", args); >> >> That's a little bit sad that there are some spots which need an explicit >> "%s" now ... maybe it would be nicer to add a new function called >> qtest_initf() instead and keep the old one (without the "f") as it is? > > Indeed, and looking back over my series, I see we could also benefit > from a va_list form. > > Also, since you already questioned me on the churn from qtest_start() => > qtest_init() => qtest_start(), I can make less churn in the next spin by > converging on qtest_start() from the get-go. > > So I will go with qtest_start() (plain string), qtest_startf() > (formatted like sprintf), and qtest_vstartf() (formatted like vsprintf). Thanks, that sounds like a better way to me, too! Thomas
diff --git a/tests/libqtest.h b/tests/libqtest.h index 3ae570927a..d338de3e22 100644 --- a/tests/libqtest.h +++ b/tests/libqtest.h @@ -25,11 +25,12 @@ extern QTestState *global_qtest; /** * qtest_init: - * @extra_args: other arguments to pass to QEMU. + * @extra_args...: other arguments to pass to QEMU, formatted as if by + * sprintf(). * * Returns: #QTestState instance. */ -QTestState *qtest_init(const char *extra_args); +QTestState *qtest_init(const char *extra_args, ...) GCC_FMT_ATTR(1, 2); /** * qtest_init_without_qmp_handshake: @@ -518,7 +519,7 @@ void qtest_add_abrt_handler(GHookFunc fn, const void *data); */ static inline QTestState *qtest_start(const char *args) { - global_qtest = qtest_init(args); + global_qtest = qtest_init("%s", args); return global_qtest; } diff --git a/tests/libqtest.c b/tests/libqtest.c index b6dd26e54a..18facbf130 100644 --- a/tests/libqtest.c +++ b/tests/libqtest.c @@ -347,9 +347,18 @@ QTestState *qtest_init_without_qmp_handshake(const char *extra_args) return s; } -QTestState *qtest_init(const char *extra_args) +QTestState *qtest_init(const char *extra_args, ...) { - QTestState *s = qtest_init_without_qmp_handshake(extra_args); + va_list ap; + QTestState *s; + char *cmd; + + va_start(ap, extra_args); + cmd = g_strdup_vprintf(extra_args, ap); + va_end(ap); + + s = qtest_init_without_qmp_handshake(cmd); + g_free(cmd); /* Read the QMP greeting and then do the handshake */ qtest_qmp_discard_response(s, ""); diff --git a/tests/bios-tables-test.c b/tests/bios-tables-test.c index 976792f2c5..f8a107499f 100644 --- a/tests/bios-tables-test.c +++ b/tests/bios-tables-test.c @@ -623,18 +623,14 @@ static void test_smbios_structs(test_data *data) static void test_acpi_one(const char *params, test_data *data) { - char *args; - /* Disable kernel irqchip to be able to override apic irq0. */ - args = g_strdup_printf("-machine %s,accel=%s,kernel-irqchip=off " + data->qts = qtest_init("-machine %s,accel=%s,kernel-irqchip=off " "-net none -display none %s " "-drive id=hd0,if=none,file=%s,format=raw " "-device ide-hd,drive=hd0 ", data->machine, "kvm:tcg", params ? params : "", disk); - data->qts = qtest_init(args); - boot_sector_test(data->qts); test_acpi_rsdp_address(data); @@ -657,7 +653,6 @@ static void test_acpi_one(const char *params, test_data *data) test_smbios_structs(data); qtest_quit(data->qts); - g_free(args); } static uint8_t base_required_struct_types[] = { diff --git a/tests/postcopy-test.c b/tests/postcopy-test.c index 0e5fe20a83..20cfb280f6 100644 --- a/tests/postcopy-test.c +++ b/tests/postcopy-test.c @@ -403,10 +403,10 @@ static void test_migrate(void) g_free(bootpath); - from = qtest_init(cmd_src); + from = qtest_init("%s", cmd_src); g_free(cmd_src); - to = qtest_init(cmd_dst); + to = qtest_init("%s", cmd_dst); g_free(cmd_dst); assert(!global_qtest); diff --git a/tests/vhost-user-test.c b/tests/vhost-user-test.c index 1e0c50d12a..0dbed5a36a 100644 --- a/tests/vhost-user-test.c +++ b/tests/vhost-user-test.c @@ -482,12 +482,12 @@ static inline void test_server_connect(TestServer *server) } #define GET_QEMU_CMD(s) \ - g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \ - (s)->socket_path, "", (s)->chr_name) + QEMU_CMD, 512, 512, (root), (s)->chr_name, \ + (s)->socket_path, "", (s)->chr_name #define GET_QEMU_CMDE(s, mem, chr_opts, extra, ...) \ - g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \ - (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_ARGS__) + QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \ + (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_ARGS__ static gboolean _test_server_free(TestServer *server) { @@ -637,7 +637,6 @@ static void test_migrate(void) TestServer *dest = test_server_new("dest"); char *uri = g_strdup_printf("%s%s", "unix:", dest->mig_path); GSource *source; - gchar *cmd; QDict *rsp; guint8 *log; guint64 size; @@ -645,18 +644,14 @@ static void test_migrate(void) test_server_listen(s); test_server_listen(dest); - cmd = GET_QEMU_CMDE(s, 2, "", ""); - s->qts = qtest_init(cmd); - g_free(cmd); + s->qts = qtest_init(GET_QEMU_CMDE(s, 2, "", "")); init_virtio_dev(s); wait_for_fds(s); size = get_log_size(s); g_assert_cmpint(size, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8)); - cmd = GET_QEMU_CMDE(dest, 2, "", " -incoming %s", uri); - dest->qts = qtest_init(cmd); - g_free(cmd); + dest->qts = qtest_init(GET_QEMU_CMDE(dest, 2, "", " -incoming %s", uri)); source = g_source_new(&test_migrate_source_funcs, sizeof(TestMigrateSource)); @@ -749,12 +744,9 @@ connect_thread(gpointer data) static void test_reconnect_subprocess(void) { TestServer *s = test_server_new("reconnect"); - char *cmd; g_thread_new("connect", connect_thread, s); - cmd = GET_QEMU_CMDE(s, 2, ",server", ""); - s->qts = qtest_init(cmd); - g_free(cmd); + s->qts = qtest_init(GET_QEMU_CMDE(s, 2, ",server", "")); init_virtio_dev(s); wait_for_fds(s); @@ -783,13 +775,10 @@ static void test_reconnect(void) static void test_connect_fail_subprocess(void) { TestServer *s = test_server_new("connect-fail"); - char *cmd; s->test_fail = true; g_thread_new("connect", connect_thread, s); - cmd = GET_QEMU_CMDE(s, 2, ",server", ""); - s->qts = qtest_init(cmd); - g_free(cmd); + s->qts = qtest_init(GET_QEMU_CMDE(s, 2, ",server", "")); init_virtio_dev(s); wait_for_fds(s); @@ -810,13 +799,10 @@ static void test_connect_fail(void) static void test_flags_mismatch_subprocess(void) { TestServer *s = test_server_new("flags-mismatch"); - char *cmd; s->test_flags = TEST_FLAGS_DISCONNECT; g_thread_new("connect", connect_thread, s); - cmd = GET_QEMU_CMDE(s, 2, ",server", ""); - s->qts = qtest_init(cmd); - g_free(cmd); + s->qts = qtest_init(GET_QEMU_CMDE(s, 2, ",server", "")); init_virtio_dev(s); wait_for_fds(s); @@ -875,19 +861,16 @@ static void test_multiqueue(void) QPCIBus *bus; QVirtQueuePCI *vq[queues * 2]; QGuestAllocator *alloc; - char *cmd; int i; s->queues = queues; test_server_listen(s); - 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, - s->socket_path, "", s->chr_name, - queues, queues * 2 + 2); - s->qts = qtest_init(cmd); - g_free(cmd); + s->qts = qtest_init(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, + s->socket_path, "", s->chr_name, + queues, queues * 2 + 2); bus = qpci_init_pc(s->qts, NULL); dev = virtio_net_pci_init(bus, PCI_SLOT); @@ -917,7 +900,6 @@ int main(int argc, char **argv) { TestServer *server = NULL; const char *hugefs; - char *qemu_cmd = NULL; int ret; char template[] = "/tmp/vhost-test-XXXXXX"; GMainLoop *loop; @@ -949,10 +931,7 @@ int main(int argc, char **argv) /* run the main loop thread so the chardev may operate */ thread = g_thread_new(NULL, thread_function, loop); - qemu_cmd = GET_QEMU_CMD(server); - - server->qts = qtest_init(qemu_cmd); - g_free(qemu_cmd); + server->qts = qtest_init(GET_QEMU_CMD(server)); init_virtio_dev(server); qtest_add_data_func("/vhost-user/read-guest-mem", server, read_guest_mem);
Several callers were formatting a string into a temporary variable before calling qtest_init(); factor that into the common code. Signed-off-by: Eric Blake <eblake@redhat.com> --- tests/libqtest.h | 7 ++++--- tests/libqtest.c | 13 ++++++++++-- tests/bios-tables-test.c | 7 +------ tests/postcopy-test.c | 4 ++-- tests/vhost-user-test.c | 51 ++++++++++++++---------------------------------- 5 files changed, 33 insertions(+), 49 deletions(-)