migration/rdma: Plug memory leaks in qemu_rdma_registration_stop()
[qemu/armbru.git] / tests / acceptance / avocado_qemu / __init__.py
blob77d1c1d9ff1daf735ded0e66cf1a77ba05f99131
1 # Test class and utilities for functional tests
3 # Copyright (c) 2018 Red Hat, Inc.
5 # Author:
6 # Cleber Rosa <crosa@redhat.com>
8 # This work is licensed under the terms of the GNU GPL, version 2 or
9 # later. See the COPYING file in the top-level directory.
11 import logging
12 import os
13 import sys
14 import uuid
15 import tempfile
17 import avocado
19 #: The QEMU build root directory. It may also be the source directory
20 #: if building from the source dir, but it's safer to use BUILD_DIR for
21 #: that purpose. Be aware that if this code is moved outside of a source
22 #: and build tree, it will not be accurate.
23 BUILD_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
25 if os.path.islink(os.path.dirname(os.path.dirname(__file__))):
26 # The link to the acceptance tests dir in the source code directory
27 lnk = os.path.dirname(os.path.dirname(__file__))
28 #: The QEMU root source directory
29 SOURCE_DIR = os.path.dirname(os.path.dirname(os.readlink(lnk)))
30 else:
31 SOURCE_DIR = BUILD_DIR
33 sys.path.append(os.path.join(SOURCE_DIR, 'python'))
35 from qemu.machine import QEMUMachine
37 def is_readable_executable_file(path):
38 return os.path.isfile(path) and os.access(path, os.R_OK | os.X_OK)
41 def pick_default_qemu_bin(arch=None):
42 """
43 Picks the path of a QEMU binary, starting either in the current working
44 directory or in the source tree root directory.
46 :param arch: the arch to use when looking for a QEMU binary (the target
47 will match the arch given). If None (the default), arch
48 will be the current host system arch (as given by
49 :func:`os.uname`).
50 :type arch: str
51 :returns: the path to the default QEMU binary or None if one could not
52 be found
53 :rtype: str or None
54 """
55 if arch is None:
56 arch = os.uname()[4]
57 # qemu binary path does not match arch for powerpc, handle it
58 if 'ppc64le' in arch:
59 arch = 'ppc64'
60 qemu_bin_relative_path = os.path.join("%s-softmmu" % arch,
61 "qemu-system-%s" % arch)
62 if is_readable_executable_file(qemu_bin_relative_path):
63 return qemu_bin_relative_path
65 qemu_bin_from_bld_dir_path = os.path.join(BUILD_DIR,
66 qemu_bin_relative_path)
67 if is_readable_executable_file(qemu_bin_from_bld_dir_path):
68 return qemu_bin_from_bld_dir_path
71 def _console_interaction(test, success_message, failure_message,
72 send_string, keep_sending=False, vm=None):
73 assert not keep_sending or send_string
74 if vm is None:
75 vm = test.vm
76 console = vm.console_socket.makefile()
77 console_logger = logging.getLogger('console')
78 while True:
79 if send_string:
80 vm.console_socket.sendall(send_string.encode())
81 if not keep_sending:
82 send_string = None # send only once
83 msg = console.readline().strip()
84 if not msg:
85 continue
86 console_logger.debug(msg)
87 if success_message in msg:
88 break
89 if failure_message and failure_message in msg:
90 console.close()
91 fail = 'Failure message found in console: %s' % failure_message
92 test.fail(fail)
94 def interrupt_interactive_console_until_pattern(test, success_message,
95 failure_message=None,
96 interrupt_string='\r'):
97 """
98 Keep sending a string to interrupt a console prompt, while logging the
99 console output. Typical use case is to break a boot loader prompt, such:
101 Press a key within 5 seconds to interrupt boot process.
107 Booting default image...
109 :param test: an Avocado test containing a VM that will have its console
110 read and probed for a success or failure message
111 :type test: :class:`avocado_qemu.Test`
112 :param success_message: if this message appears, test succeeds
113 :param failure_message: if this message appears, test fails
114 :param interrupt_string: a string to send to the console before trying
115 to read a new line
117 _console_interaction(test, success_message, failure_message,
118 interrupt_string, True)
120 def wait_for_console_pattern(test, success_message, failure_message=None,
121 vm=None):
123 Waits for messages to appear on the console, while logging the content
125 :param test: an Avocado test containing a VM that will have its console
126 read and probed for a success or failure message
127 :type test: :class:`avocado_qemu.Test`
128 :param success_message: if this message appears, test succeeds
129 :param failure_message: if this message appears, test fails
131 _console_interaction(test, success_message, failure_message, None, vm=vm)
133 def exec_command_and_wait_for_pattern(test, command,
134 success_message, failure_message=None):
136 Send a command to a console (appending CRLF characters), then wait
137 for success_message to appear on the console, while logging the.
138 content. Mark the test as failed if failure_message is found instead.
140 :param test: an Avocado test containing a VM that will have its console
141 read and probed for a success or failure message
142 :type test: :class:`avocado_qemu.Test`
143 :param command: the command to send
144 :param success_message: if this message appears, test succeeds
145 :param failure_message: if this message appears, test fails
147 _console_interaction(test, success_message, failure_message, command + '\r')
149 class Test(avocado.Test):
150 def _get_unique_tag_val(self, tag_name):
152 Gets a tag value, if unique for a key
154 vals = self.tags.get(tag_name, [])
155 if len(vals) == 1:
156 return vals.pop()
157 return None
159 def setUp(self):
160 self._vms = {}
162 self.arch = self.params.get('arch',
163 default=self._get_unique_tag_val('arch'))
165 self.machine = self.params.get('machine',
166 default=self._get_unique_tag_val('machine'))
168 default_qemu_bin = pick_default_qemu_bin(arch=self.arch)
169 self.qemu_bin = self.params.get('qemu_bin',
170 default=default_qemu_bin)
171 if self.qemu_bin is None:
172 self.cancel("No QEMU binary defined or found in the build tree")
174 def _new_vm(self, *args):
175 vm = QEMUMachine(self.qemu_bin, sock_dir=tempfile.mkdtemp())
176 if args:
177 vm.add_args(*args)
178 return vm
180 @property
181 def vm(self):
182 return self.get_vm(name='default')
184 def get_vm(self, *args, name=None):
185 if not name:
186 name = str(uuid.uuid4())
187 if self._vms.get(name) is None:
188 self._vms[name] = self._new_vm(*args)
189 if self.machine is not None:
190 self._vms[name].set_machine(self.machine)
191 return self._vms[name]
193 def tearDown(self):
194 for vm in self._vms.values():
195 vm.shutdown()