blob: adf1fe3f26fa7dd7492a42c2627d77c708b0472f [file] [log] [blame]
Fam Zhenga628daa2015-01-30 10:49:43 +08001# 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
Fam Zhenga628daa2015-01-30 10:49:43 +080014import socket
Daniel P. Berrange66613972016-07-20 14:23:10 +010015import os
Daniel P. Berrange66613972016-07-20 14:23:10 +010016import qemu
Fam Zhenga628daa2015-01-30 10:49:43 +080017
Lukáš Doktor4d934292017-08-18 16:26:13 +020018
Fam Zhenga628daa2015-01-30 10:49:43 +080019class QEMUQtestProtocol(object):
20 def __init__(self, address, server=False):
21 """
22 Create a QEMUQtestProtocol object.
23
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
26 connection
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
30 accept() methods
31 """
32 self._address = address
33 self._sock = self._get_sock()
34 if server:
35 self._sock.bind(self._address)
36 self._sock.listen(1)
37
38 def _get_sock(self):
39 if isinstance(self._address, tuple):
40 family = socket.AF_INET
41 else:
42 family = socket.AF_UNIX
43 return socket.socket(family, socket.SOCK_STREAM)
44
45 def connect(self):
46 """
47 Connect to the qtest socket.
48
49 @raise socket.error on socket connection errors
50 """
51 self._sock.connect(self._address)
52
53 def accept(self):
54 """
55 Await connection from QEMU.
56
57 @raise socket.error on socket connection errors
58 """
59 self._sock, _ = self._sock.accept()
60
61 def cmd(self, qtest_cmd):
62 """
63 Send a qtest command on the wire.
64
65 @param qtest_cmd: qtest command text to be sent
66 """
Max Reitz8eb5e672018-10-22 14:53:01 +010067 self._sock.sendall((qtest_cmd + "\n").encode('utf-8'))
Fam Zhenga628daa2015-01-30 10:49:43 +080068
69 def close(self):
70 self._sock.close()
71
72 def settimeout(self, timeout):
73 self._sock.settimeout(timeout)
Daniel P. Berrange66613972016-07-20 14:23:10 +010074
75
76class QEMUQtestMachine(qemu.QEMUMachine):
77 '''A QEMU VM'''
78
Lukáš Doktor2782fc52017-08-18 16:26:05 +020079 def __init__(self, binary, args=None, name=None, test_dir="/var/tmp",
Daniel P. Berrange4c44b4a2016-07-26 17:16:07 +010080 socket_scm_helper=None):
81 if name is None:
82 name = "qemu-%d" % os.getpid()
Lukáš Doktor4d934292017-08-18 16:26:13 +020083 super(QEMUQtestMachine,
84 self).__init__(binary, args, name=name, test_dir=test_dir,
85 socket_scm_helper=socket_scm_helper)
86 self._qtest = None
Daniel P. Berrange66613972016-07-20 14:23:10 +010087 self._qtest_path = os.path.join(test_dir, name + "-qtest.sock")
88
89 def _base_args(self):
Daniel P. Berrange4c44b4a2016-07-26 17:16:07 +010090 args = super(QEMUQtestMachine, self)._base_args()
91 args.extend(['-qtest', 'unix:path=' + self._qtest_path,
92 '-machine', 'accel=qtest'])
Daniel P. Berrange66613972016-07-20 14:23:10 +010093 return args
94
95 def _pre_launch(self):
Daniel P. Berrange4c44b4a2016-07-26 17:16:07 +010096 super(QEMUQtestMachine, self)._pre_launch()
Daniel P. Berrange66613972016-07-20 14:23:10 +010097 self._qtest = QEMUQtestProtocol(self._qtest_path, server=True)
98
99 def _post_launch(self):
Daniel P. Berrange4c44b4a2016-07-26 17:16:07 +0100100 super(QEMUQtestMachine, self)._post_launch()
Daniel P. Berrange66613972016-07-20 14:23:10 +0100101 self._qtest.accept()
102
103 def _post_shutdown(self):
Daniel P. Berrange4c44b4a2016-07-26 17:16:07 +0100104 super(QEMUQtestMachine, self)._post_shutdown()
Daniel P. Berrange66613972016-07-20 14:23:10 +0100105 self._remove_if_exists(self._qtest_path)
106
107 def qtest(self, cmd):
108 '''Send a qtest command to guest'''
109 return self._qtest.cmd(cmd)