3 # Copyright (C) 2015 Red Hat Inc.
6 # Fam Zheng <famz@redhat.com>
8 # This work is licensed under the terms of the GNU GPL, version 2. See
9 # the COPYING file in the top-level directory.
19 class QEMUQtestProtocol(object):
20 def __init__(self
, address
, server
=False):
22 Create a QEMUQtestProtocol object.
24 @param address: QEMU address, can be either a unix socket path (string)
25 or a tuple in the form ( address, port ) for a TCP
27 @param server: server mode, listens on the socket (bool)
28 @raise socket.error on socket connection errors
29 @note No connection is established, this is done by the connect() or
32 self
._address
= address
33 self
._sock
= self
._get
_sock
()
35 self
._sock
.bind(self
._address
)
39 if isinstance(self
._address
, tuple):
40 family
= socket
.AF_INET
42 family
= socket
.AF_UNIX
43 return socket
.socket(family
, socket
.SOCK_STREAM
)
47 Connect to the qtest socket.
49 @raise socket.error on socket connection errors
51 self
._sock
.connect(self
._address
)
55 Await connection from QEMU.
57 @raise socket.error on socket connection errors
59 self
._sock
, _
= self
._sock
.accept()
61 def cmd(self
, qtest_cmd
):
63 Send a qtest command on the wire.
65 @param qtest_cmd: qtest command text to be sent
67 self
._sock
.sendall((qtest_cmd
+ "\n").encode('utf-8'))
72 def settimeout(self
, timeout
):
73 self
._sock
.settimeout(timeout
)
76 class QEMUQtestMachine(qemu
.QEMUMachine
):
79 def __init__(self
, binary
, args
=None, name
=None, test_dir
="/var/tmp",
80 socket_scm_helper
=None):
82 name
= "qemu-%d" % os
.getpid()
83 super(QEMUQtestMachine
,
84 self
).__init
__(binary
, args
, name
=name
, test_dir
=test_dir
,
85 socket_scm_helper
=socket_scm_helper
)
87 self
._qtest
_path
= os
.path
.join(test_dir
, name
+ "-qtest.sock")
90 args
= super(QEMUQtestMachine
, self
)._base
_args
()
91 args
.extend(['-qtest', 'unix:path=' + self
._qtest
_path
,
92 '-machine', 'accel=qtest'])
95 def _pre_launch(self
):
96 super(QEMUQtestMachine
, self
)._pre
_launch
()
97 self
._qtest
= QEMUQtestProtocol(self
._qtest
_path
, server
=True)
99 def _post_launch(self
):
100 super(QEMUQtestMachine
, self
)._post
_launch
()
103 def _post_shutdown(self
):
104 super(QEMUQtestMachine
, self
)._post
_shutdown
()
105 self
._remove
_if
_exists
(self
._qtest
_path
)
107 def qtest(self
, cmd
):
108 '''Send a qtest command to guest'''
109 return self
._qtest
.cmd(cmd
)