qtest.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. import errno
  14. import socket
  15. import string
  16. import os
  17. import subprocess
  18. import qmp.qmp
  19. import qemu
  20. class QEMUQtestProtocol(object):
  21. def __init__(self, address, server=False):
  22. """
  23. 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
  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. def _get_sock(self):
  38. if isinstance(self._address, tuple):
  39. family = socket.AF_INET
  40. else:
  41. family = socket.AF_UNIX
  42. return socket.socket(family, socket.SOCK_STREAM)
  43. def connect(self):
  44. """
  45. Connect to the qtest socket.
  46. @raise socket.error on socket connection errors
  47. """
  48. self._sock.connect(self._address)
  49. def accept(self):
  50. """
  51. Await connection from QEMU.
  52. @raise socket.error on socket connection errors
  53. """
  54. self._sock, _ = self._sock.accept()
  55. def cmd(self, qtest_cmd):
  56. """
  57. Send a qtest command on the wire.
  58. @param qtest_cmd: qtest command text to be sent
  59. """
  60. self._sock.sendall(qtest_cmd + "\n")
  61. def close(self):
  62. self._sock.close()
  63. def settimeout(self, timeout):
  64. self._sock.settimeout(timeout)
  65. class QEMUQtestMachine(qemu.QEMUMachine):
  66. '''A QEMU VM'''
  67. def __init__(self, binary, args=[], name=None, test_dir="/var/tmp",
  68. socket_scm_helper=None):
  69. if name is None:
  70. name = "qemu-%d" % os.getpid()
  71. super(QEMUQtestMachine, self).__init__(binary, args, name=name, test_dir=test_dir,
  72. socket_scm_helper=socket_scm_helper)
  73. self._qtest_path = os.path.join(test_dir, name + "-qtest.sock")
  74. def _base_args(self):
  75. args = super(QEMUQtestMachine, self)._base_args()
  76. args.extend(['-qtest', 'unix:path=' + self._qtest_path,
  77. '-machine', 'accel=qtest'])
  78. return args
  79. def _pre_launch(self):
  80. super(QEMUQtestMachine, self)._pre_launch()
  81. self._qtest = QEMUQtestProtocol(self._qtest_path, server=True)
  82. def _post_launch(self):
  83. super(QEMUQtestMachine, self)._post_launch()
  84. self._qtest.accept()
  85. def _post_shutdown(self):
  86. super(QEMUQtestMachine, self)._post_shutdown()
  87. self._remove_if_exists(self._qtest_path)
  88. def qtest(self, cmd):
  89. '''Send a qtest command to guest'''
  90. return self._qtest.cmd(cmd)