test virtio-blk hotplug
v2 cover test virtio-blk hotplug
It's worth to add a hotplug test to qtest, but without cooperation of guest OS, new devices can't be initialized by guest, and hot-unplug doesn't work.
However, the new test can cover some part of code of hotplug/unplug.
I will write another subtest to test hotplug with pci support.
V2: move qmp_exec_hmp_cmd() to libqtest.c
excape hmp cmd (stefanha)
use qmp_exec_hmp_cmd() in blockdev-test
Amos Kong (4):
virtio-blk-test.c: change pci_nop() to virtblk_init()
qtest: introduce qmp_exec_hmp_cmd() virtio-blk-test.c: add hotplug subtest
qtest: use qmp_exec_hmp_cmd() in blockdev-test
tests/blockdev-test.c | 23 ++-------------------- tests/libqtest.c | 16 ++++++++++++++++ tests/libqtest.h | 8 ++++++++ tests/virtio-blk-test.c | 51 ++++++++++++++++++++++++++++++++++++++++++------- 4 files changed, 70 insertions(+), 28 deletions(-)
Signature · 1 line
1.9.3
PATCH v2 1/4 virtio-blk-test.c: change pci_nop() to virtblk_init()
I want to add a new subtest in virtio-blk-test.c, it will start guest without network. The original pci_init() did nothing, but it's good to reserve a very simple initialization testing.
--- tests/virtio-blk-test.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
tests/virtio-blk-test.c +6 -7
@@ -12,9 +12,12 @@#include "libqtest.h" #include "qemu/osdep.h" -/* Tests only initialization so far. TODO: Replace with functional tests */ -static void pci_nop (void ) +/* Tests only initialization */ +static void virtblk_init (void ) { +qtest_start ("-drive id=drv0,if=none,file=/dev/null " +"-device virtio-blk-pci,drive=drv0" ); +qtest_end (); }int main (int argc,char **argv)
@@ -22,13 +25,9 @@ int main(int argc, char **argv)int ret;g_test_init (&argc, &argv,NULL ); -qtest_add_func ("/virtio/blk/pci/nop" , pci_nop); +qtest_add_func ("/virtio/blk/pci/init" , virtblk_init); -qtest_start ("-drive id=drv0,if=none,file=/dev/null " -"-device virtio-blk-pci,drive=drv0" ); ret =g_test_run (); -qtest_end (); -return ret; }
Signature · 1 line
1.9.3
PATCH v2 2/4 qtest: introduce qmp_exec_hmp_cmd()
In reply to Amos Kong--- tests/libqtest.c | 16 ++++++++++++++++ tests/libqtest.h | 8 ++++++++ 2 files changed, 24 insertions(+)
tests/libqtest.c +16 -0
@@ -646,3 +646,19 @@ void qmp_discard_response(const char *fmt, ...)qtest_qmpv_discard_response (global_qtest, fmt, ap);va_end (ap); } + +void qmp_exec_hmp_cmd (const char *cmd,const char *expected_ret) +{ +QDict *response; +const char *response_return; + + response =qmp ("{\"execute\": \"human-monitor-command\"," +" \"arguments\": {" +" \"command-line\": \"%s\"" +"}}" ,g_strescape (cmd,NULL )); +g_assert (response); + response_return =qdict_get_try_str (response,"return" ); +g_assert (response_return); + g_assert_cmpstr(response_return, ==, expected_ret); +QDECREF (response); +}
tests/libqtest.h +8 -0
@@ -375,6 +375,14 @@ QDict *qmp(const char *fmt, ...);void qmp_discard_response (const char *fmt, ...);/** +* qmp_exec_hmp_cmd: +* @fmt...: HMP command to execute +* +* Executes HMP command by 'human-monitor-command'. +*/ +void qmp_exec_hmp_cmd (const char *cmd,const char *expected_ret); + +/** * qmp_receive: * * Reads a QMP message from QEMU and returns the response.
Signature · 1 line
1.9.3
Re: [Qemu-devel] [PATCH v2 2/4] qtest: introduce qmp_exec_hmp_cmd()
Quoted 9 lines
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Amos Kong
Sent: Thursday, June 05, 2014 1:45 PM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]
Subject: [Qemu-devel] [PATCH v2 2/4] qtest: introduceqmp_exec_hmp_cmd()
Quoted 6 lines
Signed-off-by Amos Kong <[email protected]>--- tests/libqtest.c | 16 ++++++++++++++++ tests/libqtest.h | 8 ++++++++ 2 files changed, 24 insertions(+)
Nice job :)
I will use it for usb host adapters hotplug/unplug qtests.
Best regards,
-Gonglei
Re: [Qemu-devel] [PATCH v2 2/4] qtest: introduce qmp_exec_hmp_cmd()
In reply to Amos KongQuoted 14 lines · Amos Kong
Signed-off-by Amos Kong <[email protected]>--- tests/libqtest.c | 16 ++++++++++++++++ tests/libqtest.h | 8 ++++++++ 2 files changed, 24 insertions(+)tests/libqtest.c +0
-0@@ -646,3 +646,19 @@ void qmp_discard_response(const char *fmt, ...)qtest_qmpv_discard_response (global_qtest, fmt, ap);va_end (ap); }
tests/libqtest.c void qmp_discard_response(const char *fmt, ...) · line 658
Quoted 10 lines
tests/libqtest.c +10
-0+ +void qmp_exec_hmp_cmd (const char *cmd,const char *expected_ret) +{ +QDict *response; +const char *response_return; + + response =qmp ("{\"execute\": \"human-monitor-command\"," +" \"arguments\": {" +" \"command-line\": \"%s\"" +"}}" ,g_strescape (cmd,NULL ));
You need to free the g_strescape() return value after using it.
Quoted 11 lines
tests/libqtest.c +6
-0+g_assert (response); + response_return =qdict_get_try_str (response,"return" ); +g_assert (response_return); + g_assert_cmpstr(response_return, ==, expected_ret); +QDECREF (response); +}tests/libqtest.h +0
-0
tests/libqtest.h QDict *qmp(const char *fmt, ...); · line 379
Quoted 5 lines
tests/libqtest.h +2
-0@@ -375,6 +375,14 @@ QDict *qmp(const char *fmt, ...);void qmp_discard_response (const char *fmt, ...);/** +* qmp_exec_hmp_cmd: +* @fmt...: HMP command to execute
'fmt...' doesn't exist. Please document cmd and expected_ret.
Quoted 12 lines
tests/libqtest.h +6
-0+* +* Executes HMP command by 'human-monitor-command'. +*/ +void qmp_exec_hmp_cmd (const char *cmd,const char *expected_ret); + +/** * qmp_receive: * * Reads a QMP message from QEMU and returns the response. Signature · 1 line
1.9.3
PATCH v2 3/4 virtio-blk-test.c: add hotplug subtest
In reply to Amos KongThis patch adds a new subtest, it hotplugs 29 * 8 = 232 virtio-blk devices to guest, and try to hot-unplug them.
Note: the hot-unplug can't work without cooperation of guest OS.
--- tests/virtio-blk-test.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
tests/virtio-blk-test.c +38 -0
@@ -7,11 +7,48 @@* See the COPYING file in the top-level directory. */ +#include <stdio.h> #include <glib.h> #include <string.h> #include "libqtest.h" #include "qemu/osdep.h" +static void test_blk_hotplug (void ) +{ +char addr[6 ]; +char cmd[100 ]; +int i, j; + +/* start with no network/block device, slots 3~0x1f are free */ +qtest_start ("-net none" ); + +for (i =3 ; i <=0x1f ; i++) { +for (j =7 ; j >=0 ; j--) { +sprintf (addr,"%x.%x" , i, j); +sprintf (cmd,"drive_add 0 if=none,file=/dev/null,id=drv-%s" , addr); +qmp_exec_hmp_cmd (cmd,"OK\r\n" ); + +sprintf (cmd,"device_add virtio-blk-pci,id=dev-%s,drive=drv-%s," +"addr=0x%s,multifunction=on" , addr, addr, addr); +qmp_exec_hmp_cmd (cmd,"" ); + } + } + +/* hot-unplug doesn't work without cooperation of guest OS */ +for (i =3 ; i <=0x1f ; i++) { +for (j =7 ; j >=0 ; j--) { +sprintf (addr,"%x.%x" , i, j); +sprintf (cmd,"drive_del drv-%s" , addr); +qmp_exec_hmp_cmd (cmd,"" ); + +sprintf (cmd,"device_del dev-%s" , addr); +qmp_exec_hmp_cmd (cmd,"" ); + } + } + +qtest_end (); +} +/* Tests only initialization */ static void virtblk_init (void ) {
@@ -26,6 +63,7 @@ int main(int argc, char **argv)g_test_init (&argc, &argv,NULL );qtest_add_func ("/virtio/blk/pci/init" , virtblk_init); +qtest_add_func ("/virtio/blk/pci/hotplug" , test_blk_hotplug); ret =g_test_run ();
Signature · 1 line
1.9.3
Re: [Qemu-devel] [PATCH v2 3/4] virtio-blk-test.c: add hotplug subtest
Quoted 46 lines · Amos Kong
This patch adds a new subtest, it hotplugs 29 * 8 = 232 virtio-blk devices to guest, and try to hot-unplug them.
Note: the hot-unplug can't work without cooperation of guest OS.
Signed-off-by Amos Kong <[email protected]>--- tests/virtio-blk-test.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)tests/virtio-blk-test.c +23
-0@@ -7,11 +7,48 @@* See the COPYING file in the top-level directory. */ +#include <stdio.h> #include <glib.h> #include <string.h> #include "libqtest.h" #include "qemu/osdep.h" +static void test_blk_hotplug (void ) +{ +char addr[6 ]; +char cmd[100 ]; +int i, j; + +/* start with no network/block device, slots 3~0x1f are free */ +qtest_start ("-net none" ); + +for (i =3 ; i <=0x1f ; i++) { +for (j =7 ; j >=0 ; j--) { +sprintf (addr,"%x.%x" , i, j); +sprintf (cmd,"drive_add 0 if=none,file=/dev/null,id=drv-%s" , addr); +qmp_exec_hmp_cmd (cmd,"OK\r\n" ); + +sprintf (cmd,"device_add virtio-blk-pci,id=dev-%s,drive=drv-%s," +"addr=0x%s,multifunction=on" , addr, addr, addr); +qmp_exec_hmp_cmd (cmd,"" ); + } + } + +/* hot-unplug doesn't work without cooperation of guest OS */
tests/virtio-blk-test.c line 47
Quoted 10 lines
tests/virtio-blk-test.c +10
-0+for (i =3 ; i <=0x1f ; i++) { +for (j =7 ; j >=0 ; j--) { +sprintf (addr,"%x.%x" , i, j); +sprintf (cmd,"drive_del drv-%s" , addr); +qmp_exec_hmp_cmd (cmd,"" ); + +sprintf (cmd,"device_del dev-%s" , addr); +qmp_exec_hmp_cmd (cmd,"" ); + } + }
All of this sprintf() usage makes me wonder about:qmp_exec_hmp_cmd(const char *expected_ret, const char *fmt, ...);
This way callers don't need to manage formatting buffers themselves.
Besides that:
PATCH v2 4/4 qtest: use qmp_exec_hmp_cmd() in blockdev-test
In reply to Amos Kong--- tests/blockdev-test.c | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-)
tests/blockdev-test.c +2 -21
@@ -16,35 +16,16 @@static void test_drive_add_empty (void ) { -QDict *response; -const char *response_return; -/* Start with an empty drive */ qtest_start ("-drive if=none,id=drive0" );/* Delete the drive */ - response =qmp ("{\"execute\": \"human-monitor-command\"," -" \"arguments\": {" -" \"command-line\": \"drive_del drive0\"" -"}}" ); -g_assert (response); - response_return =qdict_get_try_str (response,"return" ); -g_assert (response_return); -g_assert (strcmp (response_return,"" ) ==0 ); -QDECREF (response); +qmp_exec_hmp_cmd ("drive_del drive0" ,"" );/* Ensure re-adding the drive works - there should be no duplicate ID error * because the old drive must be gone. */ - response =qmp ("{\"execute\": \"human-monitor-command\"," -" \"arguments\": {" -" \"command-line\": \"drive_add 0 if=none,id=drive0\"" -"}}" ); -g_assert (response); - response_return =qdict_get_try_str (response,"return" ); -g_assert (response_return); -g_assert (strcmp (response_return,"OK\r\n" ) ==0 ); -QDECREF (response); +qmp_exec_hmp_cmd ("drive_add 0 if=none,id=drive0" ,"OK\r\n" );qtest_end (); }
Signature · 1 line
1.9.3
Re: [Qemu-devel] [PATCH v2 4/4] qtest: use qmp_exec_hmp_cmd() in blockdev-test
Quoted 4 lines · Amos Kong
Signed-off-by Amos Kong <[email protected]>--- tests/blockdev-test.c | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-)