4 * Copyright IBM, Corp. 2012
5 * Copyright Red Hat, Inc. 2012
6 * Copyright SUSE LINUX Products GmbH 2013
9 * Anthony Liguori <aliguori@us.ibm.com>
10 * Paolo Bonzini <pbonzini@redhat.com>
11 * Andreas Färber <afaerber@suse.de>
13 * This work is licensed under the terms of the GNU GPL, version 2 or later.
14 * See the COPYING file in the top-level directory.
17 #include "qemu/osdep.h"
19 #include <sys/socket.h>
24 #include "qemu/cutils.h"
25 #include "qapi/error.h"
26 #include "qapi/qmp/json-parser.h"
27 #include "qapi/qmp/json-streamer.h"
28 #include "qapi/qmp/qdict.h"
29 #include "qapi/qmp/qjson.h"
30 #include "qapi/qmp/qlist.h"
31 #include "qapi/qmp/qstring.h"
34 #define SOCKET_TIMEOUT 50
36 QTestState
*global_qtest
;
42 bool irq_level
[MAX_IRQ
];
44 pid_t qemu_pid
; /* our child QEMU process */
48 static GHookList abrt_hooks
;
49 static struct sigaction sigact_old
;
51 #define g_assert_no_errno(ret) do { \
52 g_assert_cmpint(ret, !=, -1); \
55 static int qtest_query_target_endianness(QTestState
*s
);
57 static int init_socket(const char *socket_path
)
59 struct sockaddr_un addr
;
63 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
64 g_assert_no_errno(sock
);
66 addr
.sun_family
= AF_UNIX
;
67 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socket_path
);
68 qemu_set_cloexec(sock
);
71 ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
));
72 } while (ret
== -1 && errno
== EINTR
);
73 g_assert_no_errno(ret
);
74 ret
= listen(sock
, 1);
75 g_assert_no_errno(ret
);
80 static int socket_accept(int sock
)
82 struct sockaddr_un addr
;
85 struct timeval timeout
= { .tv_sec
= SOCKET_TIMEOUT
,
88 setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, (void *)&timeout
,
92 addrlen
= sizeof(addr
);
93 ret
= accept(sock
, (struct sockaddr
*)&addr
, &addrlen
);
94 } while (ret
== -1 && errno
== EINTR
);
96 fprintf(stderr
, "%s failed: %s\n", __func__
, strerror(errno
));
103 static void kill_qemu(QTestState
*s
)
105 if (s
->qemu_pid
!= -1) {
106 kill(s
->qemu_pid
, SIGTERM
);
107 waitpid(s
->qemu_pid
, NULL
, 0);
111 static void kill_qemu_hook_func(void *s
)
116 static void sigabrt_handler(int signo
)
118 g_hook_list_invoke(&abrt_hooks
, FALSE
);
121 static void setup_sigabrt_handler(void)
123 struct sigaction sigact
;
125 /* Catch SIGABRT to clean up on g_assert() failure */
126 sigact
= (struct sigaction
){
127 .sa_handler
= sigabrt_handler
,
128 .sa_flags
= SA_RESETHAND
,
130 sigemptyset(&sigact
.sa_mask
);
131 sigaction(SIGABRT
, &sigact
, &sigact_old
);
134 static void cleanup_sigabrt_handler(void)
136 sigaction(SIGABRT
, &sigact_old
, NULL
);
139 void qtest_add_abrt_handler(GHookFunc fn
, const void *data
)
143 /* Only install SIGABRT handler once */
144 if (!abrt_hooks
.is_setup
) {
145 g_hook_list_init(&abrt_hooks
, sizeof(GHook
));
147 setup_sigabrt_handler();
149 hook
= g_hook_alloc(&abrt_hooks
);
151 hook
->data
= (void *)data
;
153 g_hook_prepend(&abrt_hooks
, hook
);
156 static const char *qtest_qemu_binary(void)
158 const char *qemu_bin
;
160 qemu_bin
= getenv("QTEST_QEMU_BINARY");
162 fprintf(stderr
, "Environment variable QTEST_QEMU_BINARY required\n");
169 QTestState
*qtest_init_without_qmp_handshake(const char *extra_args
)
172 int sock
, qmpsock
, i
;
174 gchar
*qmp_socket_path
;
176 const char *qemu_binary
= qtest_qemu_binary();
178 s
= g_new(QTestState
, 1);
180 socket_path
= g_strdup_printf("/tmp/qtest-%d.sock", getpid());
181 qmp_socket_path
= g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
183 /* It's possible that if an earlier test run crashed it might
184 * have left a stale unix socket lying around. Delete any
185 * stale old socket to avoid spurious test failures with
186 * tests/libqtest.c:70:init_socket: assertion failed (ret != -1): (-1 != -1)
189 unlink(qmp_socket_path
);
191 sock
= init_socket(socket_path
);
192 qmpsock
= init_socket(qmp_socket_path
);
194 qtest_add_abrt_handler(kill_qemu_hook_func
, s
);
196 s
->qemu_pid
= fork();
197 if (s
->qemu_pid
== 0) {
198 setenv("QEMU_AUDIO_DRV", "none", true);
199 command
= g_strdup_printf("exec %s "
200 "-qtest unix:%s,nowait "
202 "-qmp unix:%s,nowait "
203 "-machine accel=qtest "
205 "%s", qemu_binary
, socket_path
,
206 getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
209 execlp("/bin/sh", "sh", "-c", command
, NULL
);
213 s
->fd
= socket_accept(sock
);
215 s
->qmp_fd
= socket_accept(qmpsock
);
218 unlink(qmp_socket_path
);
220 g_free(qmp_socket_path
);
222 g_assert(s
->fd
>= 0 && s
->qmp_fd
>= 0);
224 s
->rx
= g_string_new("");
225 for (i
= 0; i
< MAX_IRQ
; i
++) {
226 s
->irq_level
[i
] = false;
229 if (getenv("QTEST_STOP")) {
230 kill(s
->qemu_pid
, SIGSTOP
);
233 /* ask endianness of the target */
235 s
->big_endian
= qtest_query_target_endianness(s
);
240 QTestState
*qtest_init(const char *extra_args
)
242 QTestState
*s
= qtest_init_without_qmp_handshake(extra_args
);
244 /* Read the QMP greeting and then do the handshake */
245 qtest_qmp_discard_response(s
, "");
246 qtest_qmp_discard_response(s
, "{ 'execute': 'qmp_capabilities' }");
251 QTestState
*qtest_vstartf(const char *fmt
, va_list ap
)
253 char *args
= g_strdup_vprintf(fmt
, ap
);
256 s
= qtest_start(args
);
262 QTestState
*qtest_startf(const char *fmt
, ...)
268 s
= qtest_vstartf(fmt
, ap
);
273 void qtest_quit(QTestState
*s
)
275 g_hook_destroy_link(&abrt_hooks
, g_hook_find_data(&abrt_hooks
, TRUE
, s
));
277 /* Uninstall SIGABRT handler on last instance */
278 cleanup_sigabrt_handler();
283 g_string_free(s
->rx
, true);
287 static void socket_send(int fd
, const char *buf
, size_t size
)
292 while (offset
< size
) {
295 len
= write(fd
, buf
+ offset
, size
- offset
);
296 if (len
== -1 && errno
== EINTR
) {
300 g_assert_no_errno(len
);
301 g_assert_cmpint(len
, >, 0);
307 static void socket_sendf(int fd
, const char *fmt
, va_list ap
)
309 gchar
*str
= g_strdup_vprintf(fmt
, ap
);
310 size_t size
= strlen(str
);
312 socket_send(fd
, str
, size
);
316 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState
*s
, const char *fmt
, ...)
321 socket_sendf(s
->fd
, fmt
, ap
);
325 static GString
*qtest_recv_line(QTestState
*s
)
331 while ((eol
= strchr(s
->rx
->str
, '\n')) == NULL
) {
335 len
= read(s
->fd
, buffer
, sizeof(buffer
));
336 if (len
== -1 && errno
== EINTR
) {
340 if (len
== -1 || len
== 0) {
341 fprintf(stderr
, "Broken pipe\n");
345 g_string_append_len(s
->rx
, buffer
, len
);
348 offset
= eol
- s
->rx
->str
;
349 line
= g_string_new_len(s
->rx
->str
, offset
);
350 g_string_erase(s
->rx
, 0, offset
+ 1);
355 static gchar
**qtest_rsp(QTestState
*s
, int expected_args
)
362 line
= qtest_recv_line(s
);
363 words
= g_strsplit(line
->str
, " ", 0);
364 g_string_free(line
, TRUE
);
366 if (strcmp(words
[0], "IRQ") == 0) {
370 g_assert(words
[1] != NULL
);
371 g_assert(words
[2] != NULL
);
373 ret
= qemu_strtol(words
[2], NULL
, 0, &irq
);
375 g_assert_cmpint(irq
, >=, 0);
376 g_assert_cmpint(irq
, <, MAX_IRQ
);
378 if (strcmp(words
[1], "raise") == 0) {
379 s
->irq_level
[irq
] = true;
381 s
->irq_level
[irq
] = false;
388 g_assert(words
[0] != NULL
);
389 g_assert_cmpstr(words
[0], ==, "OK");
392 for (i
= 0; i
< expected_args
; i
++) {
393 g_assert(words
[i
] != NULL
);
402 static int qtest_query_target_endianness(QTestState
*s
)
407 qtest_sendf(s
, "endianness\n");
408 args
= qtest_rsp(s
, 1);
409 g_assert(strcmp(args
[1], "big") == 0 || strcmp(args
[1], "little") == 0);
410 big_endian
= strcmp(args
[1], "big") == 0;
417 JSONMessageParser parser
;
421 static void qmp_response(JSONMessageParser
*parser
, GQueue
*tokens
)
423 QMPResponseParser
*qmp
= container_of(parser
, QMPResponseParser
, parser
);
426 obj
= json_parser_parse(tokens
, NULL
);
428 fprintf(stderr
, "QMP JSON response parsing failed\n");
432 g_assert(!qmp
->response
);
433 qmp
->response
= qobject_to(QDict
, obj
);
434 g_assert(qmp
->response
);
437 QDict
*qmp_fd_receive(int fd
)
439 QMPResponseParser qmp
;
440 bool log
= getenv("QTEST_LOG") != NULL
;
443 json_message_parser_init(&qmp
.parser
, qmp_response
);
444 while (!qmp
.response
) {
448 len
= read(fd
, &c
, 1);
449 if (len
== -1 && errno
== EINTR
) {
453 if (len
== -1 || len
== 0) {
454 fprintf(stderr
, "Broken pipe\n");
459 len
= write(2, &c
, 1);
461 json_message_parser_feed(&qmp
.parser
, &c
, 1);
463 json_message_parser_destroy(&qmp
.parser
);
468 QDict
*qtest_qmp_receive(QTestState
*s
)
470 return qmp_fd_receive(s
->qmp_fd
);
474 * Allow users to send a message without waiting for the reply,
475 * in the case that they choose to discard all replies up until
476 * a particular EVENT is received.
478 void qmp_fd_sendv(int fd
, const char *fmt
, va_list ap
)
483 /* qobject_from_jsonv() silently eats leading 0xff as invalid
484 * JSON, but we want to test sending them over the wire to force
486 if (*fmt
== '\377') {
487 socket_send(fd
, fmt
, 1);
491 /* Going through qobject ensures we escape strings properly.
492 * This seemingly unnecessary copy is required in case va_list
495 va_copy(ap_copy
, ap
);
496 qobj
= qobject_from_jsonv(fmt
, &ap_copy
, &error_abort
);
499 /* No need to send anything for an empty QObject. */
501 int log
= getenv("QTEST_LOG") != NULL
;
502 QString
*qstr
= qobject_to_json(qobj
);
506 * BUG: QMP doesn't react to input until it sees a newline, an
507 * object, or an array. Work-around: give it a newline.
509 qstring_append_chr(qstr
, '\n');
510 str
= qstring_get_str(qstr
);
513 fprintf(stderr
, "%s", str
);
515 /* Send QMP request */
516 socket_send(fd
, str
, qstring_get_length(qstr
));
519 qobject_decref(qobj
);
523 void qtest_async_qmpv(QTestState
*s
, const char *fmt
, va_list ap
)
525 qmp_fd_sendv(s
->qmp_fd
, fmt
, ap
);
528 QDict
*qmp_fdv(int fd
, const char *fmt
, va_list ap
)
530 qmp_fd_sendv(fd
, fmt
, ap
);
532 return qmp_fd_receive(fd
);
535 QDict
*qtest_qmpv(QTestState
*s
, const char *fmt
, va_list ap
)
537 qtest_async_qmpv(s
, fmt
, ap
);
540 return qtest_qmp_receive(s
);
543 QDict
*qmp_fd(int fd
, const char *fmt
, ...)
549 response
= qmp_fdv(fd
, fmt
, ap
);
554 void qmp_fd_send(int fd
, const char *fmt
, ...)
559 qmp_fd_sendv(fd
, fmt
, ap
);
563 QDict
*qtest_qmp(QTestState
*s
, const char *fmt
, ...)
569 response
= qtest_qmpv(s
, fmt
, ap
);
574 void qtest_async_qmp(QTestState
*s
, const char *fmt
, ...)
579 qtest_async_qmpv(s
, fmt
, ap
);
583 void qtest_qmpv_discard_response(QTestState
*s
, const char *fmt
, va_list ap
)
585 QDict
*response
= qtest_qmpv(s
, fmt
, ap
);
589 void qtest_qmp_discard_response(QTestState
*s
, const char *fmt
, ...)
595 response
= qtest_qmpv(s
, fmt
, ap
);
600 QDict
*qtest_qmp_eventwait_ref(QTestState
*s
, const char *event
)
605 response
= qtest_qmp_receive(s
);
606 if ((qdict_haskey(response
, "event")) &&
607 (strcmp(qdict_get_str(response
, "event"), event
) == 0)) {
614 void qtest_qmp_eventwait(QTestState
*s
, const char *event
)
618 response
= qtest_qmp_eventwait_ref(s
, event
);
622 char *qtest_hmpv(QTestState
*s
, const char *fmt
, va_list ap
)
628 cmd
= g_strdup_vprintf(fmt
, ap
);
629 resp
= qtest_qmp(s
, "{'execute': 'human-monitor-command',"
630 " 'arguments': {'command-line': %s}}",
632 ret
= g_strdup(qdict_get_try_str(resp
, "return"));
633 while (ret
== NULL
&& qdict_get_try_str(resp
, "event")) {
634 /* Ignore asynchronous QMP events */
636 resp
= qtest_qmp_receive(s
);
637 ret
= g_strdup(qdict_get_try_str(resp
, "return"));
645 char *qtest_hmp(QTestState
*s
, const char *fmt
, ...)
651 ret
= qtest_hmpv(s
, fmt
, ap
);
656 const char *qtest_get_arch(void)
658 const char *qemu
= qtest_qemu_binary();
659 const char *end
= strrchr(qemu
, '/');
661 return end
+ strlen("/qemu-system-");
664 bool qtest_get_irq(QTestState
*s
, int num
)
666 /* dummy operation in order to make sure irq is up to date */
669 return s
->irq_level
[num
];
672 static int64_t qtest_clock_rsp(QTestState
*s
)
676 words
= qtest_rsp(s
, 2);
677 clock
= g_ascii_strtoll(words
[1], NULL
, 0);
682 int64_t qtest_clock_step_next(QTestState
*s
)
684 qtest_sendf(s
, "clock_step\n");
685 return qtest_clock_rsp(s
);
688 int64_t qtest_clock_step(QTestState
*s
, int64_t step
)
690 qtest_sendf(s
, "clock_step %"PRIi64
"\n", step
);
691 return qtest_clock_rsp(s
);
694 int64_t qtest_clock_set(QTestState
*s
, int64_t val
)
696 qtest_sendf(s
, "clock_set %"PRIi64
"\n", val
);
697 return qtest_clock_rsp(s
);
700 void qtest_irq_intercept_out(QTestState
*s
, const char *qom_path
)
702 qtest_sendf(s
, "irq_intercept_out %s\n", qom_path
);
706 void qtest_irq_intercept_in(QTestState
*s
, const char *qom_path
)
708 qtest_sendf(s
, "irq_intercept_in %s\n", qom_path
);
712 static void qtest_out(QTestState
*s
, const char *cmd
, uint16_t addr
, uint32_t value
)
714 qtest_sendf(s
, "%s 0x%x 0x%x\n", cmd
, addr
, value
);
718 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
)
720 qtest_out(s
, "outb", addr
, value
);
723 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
)
725 qtest_out(s
, "outw", addr
, value
);
728 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
)
730 qtest_out(s
, "outl", addr
, value
);
733 static uint32_t qtest_in(QTestState
*s
, const char *cmd
, uint16_t addr
)
739 qtest_sendf(s
, "%s 0x%x\n", cmd
, addr
);
740 args
= qtest_rsp(s
, 2);
741 ret
= qemu_strtoul(args
[1], NULL
, 0, &value
);
742 g_assert(!ret
&& value
<= UINT32_MAX
);
748 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
)
750 return qtest_in(s
, "inb", addr
);
753 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
)
755 return qtest_in(s
, "inw", addr
);
758 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
)
760 return qtest_in(s
, "inl", addr
);
763 static void qtest_write(QTestState
*s
, const char *cmd
, uint64_t addr
,
766 qtest_sendf(s
, "%s 0x%" PRIx64
" 0x%" PRIx64
"\n", cmd
, addr
, value
);
770 void qtest_writeb(QTestState
*s
, uint64_t addr
, uint8_t value
)
772 qtest_write(s
, "writeb", addr
, value
);
775 void qtest_writew(QTestState
*s
, uint64_t addr
, uint16_t value
)
777 qtest_write(s
, "writew", addr
, value
);
780 void qtest_writel(QTestState
*s
, uint64_t addr
, uint32_t value
)
782 qtest_write(s
, "writel", addr
, value
);
785 void qtest_writeq(QTestState
*s
, uint64_t addr
, uint64_t value
)
787 qtest_write(s
, "writeq", addr
, value
);
790 static uint64_t qtest_read(QTestState
*s
, const char *cmd
, uint64_t addr
)
796 qtest_sendf(s
, "%s 0x%" PRIx64
"\n", cmd
, addr
);
797 args
= qtest_rsp(s
, 2);
798 ret
= qemu_strtou64(args
[1], NULL
, 0, &value
);
805 uint8_t qtest_readb(QTestState
*s
, uint64_t addr
)
807 return qtest_read(s
, "readb", addr
);
810 uint16_t qtest_readw(QTestState
*s
, uint64_t addr
)
812 return qtest_read(s
, "readw", addr
);
815 uint32_t qtest_readl(QTestState
*s
, uint64_t addr
)
817 return qtest_read(s
, "readl", addr
);
820 uint64_t qtest_readq(QTestState
*s
, uint64_t addr
)
822 return qtest_read(s
, "readq", addr
);
825 static int hex2nib(char ch
)
827 if (ch
>= '0' && ch
<= '9') {
829 } else if (ch
>= 'a' && ch
<= 'f') {
830 return 10 + (ch
- 'a');
831 } else if (ch
>= 'A' && ch
<= 'F') {
832 return 10 + (ch
- 'a');
838 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
848 qtest_sendf(s
, "read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
849 args
= qtest_rsp(s
, 2);
851 for (i
= 0; i
< size
; i
++) {
852 ptr
[i
] = hex2nib(args
[1][2 + (i
* 2)]) << 4;
853 ptr
[i
] |= hex2nib(args
[1][2 + (i
* 2) + 1]);
859 uint64_t qtest_rtas_call(QTestState
*s
, const char *name
,
860 uint32_t nargs
, uint64_t args
,
861 uint32_t nret
, uint64_t ret
)
863 qtest_sendf(s
, "rtas %s %u 0x%"PRIx64
" %u 0x%"PRIx64
"\n",
864 name
, nargs
, args
, nret
, ret
);
869 void qtest_add_func(const char *str
, void (*fn
)(void))
871 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
872 g_test_add_func(path
, fn
);
876 void qtest_add_data_func_full(const char *str
, void *data
,
877 void (*fn
)(const void *),
878 GDestroyNotify data_free_func
)
880 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
881 g_test_add_data_func_full(path
, data
, fn
, data_free_func
);
885 void qtest_add_data_func(const char *str
, const void *data
,
886 void (*fn
)(const void *))
888 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
889 g_test_add_data_func(path
, data
, fn
);
893 void qtest_bufwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
897 bdata
= g_base64_encode(data
, size
);
898 qtest_sendf(s
, "b64write 0x%" PRIx64
" 0x%zx ", addr
, size
);
899 socket_send(s
->fd
, bdata
, strlen(bdata
));
900 socket_send(s
->fd
, "\n", 1);
905 void qtest_bufread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
910 qtest_sendf(s
, "b64read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
911 args
= qtest_rsp(s
, 2);
913 g_base64_decode_inplace(args
[1], &len
);
915 fprintf(stderr
, "bufread: asked for %zu bytes but decoded %zu\n",
917 len
= MIN(len
, size
);
920 memcpy(data
, args
[1], len
);
924 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
926 const uint8_t *ptr
= data
;
934 enc
= g_malloc(2 * size
+ 1);
936 for (i
= 0; i
< size
; i
++) {
937 sprintf(&enc
[i
* 2], "%02x", ptr
[i
]);
940 qtest_sendf(s
, "write 0x%" PRIx64
" 0x%zx 0x%s\n", addr
, size
, enc
);
945 void qtest_memset(QTestState
*s
, uint64_t addr
, uint8_t pattern
, size_t size
)
947 qtest_sendf(s
, "memset 0x%" PRIx64
" 0x%zx 0x%02x\n", addr
, size
, pattern
);
951 QDict
*qmp(const char *fmt
, ...)
957 response
= qtest_qmpv(global_qtest
, fmt
, ap
);
962 void qmp_async(const char *fmt
, ...)
967 qtest_async_qmpv(global_qtest
, fmt
, ap
);
971 void qmp_discard_response(const char *fmt
, ...)
976 qtest_qmpv_discard_response(global_qtest
, fmt
, ap
);
979 char *hmp(const char *fmt
, ...)
985 ret
= qtest_hmpv(global_qtest
, fmt
, ap
);
990 bool qtest_big_endian(QTestState
*s
)
992 return s
->big_endian
;
995 void qtest_cb_for_every_machine(void (*cb
)(const char *machine
))
997 QDict
*response
, *minfo
;
1004 qtest_start("-machine none");
1005 response
= qmp("{ 'execute': 'query-machines' }");
1007 list
= qdict_get_qlist(response
, "return");
1010 for (p
= qlist_first(list
); p
; p
= qlist_next(p
)) {
1011 minfo
= qobject_to(QDict
, qlist_entry_obj(p
));
1013 qobj
= qdict_get(minfo
, "name");
1015 qstr
= qobject_to(QString
, qobj
);
1017 mname
= qstring_get_str(qstr
);
1026 * Generic hot-plugging test via the device_add QMP command.
1028 void qtest_qmp_device_add(const char *driver
, const char *id
, const char *fmt
,
1032 char *cmd
, *opts
= NULL
;
1037 opts
= g_strdup_vprintf(fmt
, va
);
1041 cmd
= g_strdup_printf("{'execute': 'device_add',"
1042 " 'arguments': { 'driver': '%s', 'id': '%s'%s%s }}",
1043 driver
, id
, opts
? ", " : "", opts
? opts
: "");
1046 response
= qmp(cmd
);
1049 g_assert(!qdict_haskey(response
, "event")); /* We don't expect any events */
1050 g_assert(!qdict_haskey(response
, "error"));
1055 * Generic hot-unplugging test via the device_del QMP command.
1056 * Device deletion will get one response and one event. For example:
1058 * {'execute': 'device_del','arguments': { 'id': 'scsi-hd'}}
1060 * will get this one:
1062 * {"timestamp": {"seconds": 1505289667, "microseconds": 569862},
1063 * "event": "DEVICE_DELETED", "data": {"device": "scsi-hd",
1064 * "path": "/machine/peripheral/scsi-hd"}}
1070 * But the order of arrival may vary - so we've got to detect both.
1072 void qtest_qmp_device_del(const char *id
)
1074 QDict
*response1
, *response2
, *event
= NULL
;
1077 cmd
= g_strdup_printf("{'execute': 'device_del',"
1078 " 'arguments': { 'id': '%s' }}", id
);
1079 response1
= qmp(cmd
);
1081 g_assert(response1
);
1082 g_assert(!qdict_haskey(response1
, "error"));
1084 response2
= qmp("");
1085 g_assert(response2
);
1086 g_assert(!qdict_haskey(response2
, "error"));
1088 if (qdict_haskey(response1
, "event")) {
1090 } else if (qdict_haskey(response2
, "event")) {
1094 g_assert_cmpstr(qdict_get_str(event
, "event"), ==, "DEVICE_DELETED");