]> git.proxmox.com Git - mirror_qemu.git/blame - scripts/qtest.py
9pfs: clear migration blocker at session reset
[mirror_qemu.git] / scripts / qtest.py
CommitLineData
a628daa4
FZ
1# QEMU qtest library
2#
3# Copyright (C) 2015 Red Hat Inc.
4#
5# Authors:
6# Fam Zheng <famz@redhat.com>
7#
8# This work is licensed under the terms of the GNU GPL, version 2. See
9# the COPYING file in the top-level directory.
10#
11# Based on qmp.py.
12#
13
14import errno
15import socket
66613974
DB
16import string
17import os
18import subprocess
19import qmp.qmp
20import qemu
a628daa4
FZ
21
22class QEMUQtestProtocol(object):
23 def __init__(self, address, server=False):
24 """
25 Create a QEMUQtestProtocol object.
26
27 @param address: QEMU address, can be either a unix socket path (string)
28 or a tuple in the form ( address, port ) for a TCP
29 connection
30 @param server: server mode, listens on the socket (bool)
31 @raise socket.error on socket connection errors
32 @note No connection is established, this is done by the connect() or
33 accept() methods
34 """
35 self._address = address
36 self._sock = self._get_sock()
37 if server:
38 self._sock.bind(self._address)
39 self._sock.listen(1)
40
41 def _get_sock(self):
42 if isinstance(self._address, tuple):
43 family = socket.AF_INET
44 else:
45 family = socket.AF_UNIX
46 return socket.socket(family, socket.SOCK_STREAM)
47
48 def connect(self):
49 """
50 Connect to the qtest socket.
51
52 @raise socket.error on socket connection errors
53 """
54 self._sock.connect(self._address)
55
56 def accept(self):
57 """
58 Await connection from QEMU.
59
60 @raise socket.error on socket connection errors
61 """
62 self._sock, _ = self._sock.accept()
63
64 def cmd(self, qtest_cmd):
65 """
66 Send a qtest command on the wire.
67
68 @param qtest_cmd: qtest command text to be sent
69 """
70 self._sock.sendall(qtest_cmd + "\n")
71
72 def close(self):
73 self._sock.close()
74
75 def settimeout(self, timeout):
76 self._sock.settimeout(timeout)
66613974
DB
77
78
79class QEMUQtestMachine(qemu.QEMUMachine):
80 '''A QEMU VM'''
81
4c44b4a4
DB
82 def __init__(self, binary, args=[], name=None, test_dir="/var/tmp",
83 socket_scm_helper=None):
84 if name is None:
85 name = "qemu-%d" % os.getpid()
86 super(QEMUQtestMachine, self).__init__(binary, args, name=name, test_dir=test_dir,
87 socket_scm_helper=socket_scm_helper)
66613974
DB
88 self._qtest_path = os.path.join(test_dir, name + "-qtest.sock")
89
90 def _base_args(self):
4c44b4a4
DB
91 args = super(QEMUQtestMachine, self)._base_args()
92 args.extend(['-qtest', 'unix:path=' + self._qtest_path,
93 '-machine', 'accel=qtest'])
66613974
DB
94 return args
95
96 def _pre_launch(self):
4c44b4a4 97 super(QEMUQtestMachine, self)._pre_launch()
66613974
DB
98 self._qtest = QEMUQtestProtocol(self._qtest_path, server=True)
99
100 def _post_launch(self):
4c44b4a4 101 super(QEMUQtestMachine, self)._post_launch()
66613974
DB
102 self._qtest.accept()
103
104 def _post_shutdown(self):
4c44b4a4 105 super(QEMUQtestMachine, self)._post_shutdown()
66613974
DB
106 self._remove_if_exists(self._qtest_path)
107
108 def qtest(self, cmd):
109 '''Send a qtest command to guest'''
110 return self._qtest.cmd(cmd)