qtest.py 3.1 KB

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