1 # Test class and utilities for functional tests
3 # Copyright (c) 2018 Red Hat, Inc.
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.
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
)))
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):
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
51 :returns: the path to the default QEMU binary or None if one could not
57 # qemu binary path does not match arch for powerpc, handle it
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
76 console
= vm
.console_socket
.makefile()
77 console_logger
= logging
.getLogger('console')
80 vm
.console_socket
.sendall(send_string
.encode())
82 send_string
= None # send only once
83 msg
= console
.readline().strip()
86 console_logger
.debug(msg
)
87 if success_message
in msg
:
89 if failure_message
and failure_message
in msg
:
91 fail
= 'Failure message found in console: %s' % failure_message
94 def interrupt_interactive_console_until_pattern(test
, success_message
,
96 interrupt_string
='\r'):
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
117 _console_interaction(test
, success_message
, failure_message
,
118 interrupt_string
, True)
120 def wait_for_console_pattern(test
, success_message
, failure_message
=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
, [])
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())
182 return self
.get_vm(name
='default')
184 def get_vm(self
, *args
, name
=None):
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
]
194 for vm
in self
._vms
.values():