]> git.proxmox.com Git - mirror_qemu.git/blob - tests/acceptance/avocado_qemu/__init__.py
Merge remote-tracking branch 'remotes/rth/tags/pull-tcg-20190903' into staging
[mirror_qemu.git] / tests / acceptance / avocado_qemu / __init__.py
1 # Test class and utilities for functional tests
2 #
3 # Copyright (c) 2018 Red Hat, Inc.
4 #
5 # Author:
6 # Cleber Rosa <crosa@redhat.com>
7 #
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.
10
11 import os
12 import sys
13 import uuid
14
15 import avocado
16
17 SRC_ROOT_DIR = os.path.join(os.path.dirname(__file__), '..', '..', '..')
18 sys.path.append(os.path.join(SRC_ROOT_DIR, 'python'))
19
20 from qemu.machine import QEMUMachine
21
22 def is_readable_executable_file(path):
23 return os.path.isfile(path) and os.access(path, os.R_OK | os.X_OK)
24
25
26 def pick_default_qemu_bin(arch=None):
27 """
28 Picks the path of a QEMU binary, starting either in the current working
29 directory or in the source tree root directory.
30
31 :param arch: the arch to use when looking for a QEMU binary (the target
32 will match the arch given). If None (the default), arch
33 will be the current host system arch (as given by
34 :func:`os.uname`).
35 :type arch: str
36 :returns: the path to the default QEMU binary or None if one could not
37 be found
38 :rtype: str or None
39 """
40 if arch is None:
41 arch = os.uname()[4]
42 # qemu binary path does not match arch for powerpc, handle it
43 if 'ppc64le' in arch:
44 arch = 'ppc64'
45 qemu_bin_relative_path = os.path.join("%s-softmmu" % arch,
46 "qemu-system-%s" % arch)
47 if is_readable_executable_file(qemu_bin_relative_path):
48 return qemu_bin_relative_path
49
50 qemu_bin_from_src_dir_path = os.path.join(SRC_ROOT_DIR,
51 qemu_bin_relative_path)
52 if is_readable_executable_file(qemu_bin_from_src_dir_path):
53 return qemu_bin_from_src_dir_path
54
55
56 class Test(avocado.Test):
57 def setUp(self):
58 self._vms = {}
59 arches = self.tags.get('arch', [])
60 if len(arches) == 1:
61 arch = arches.pop()
62 else:
63 arch = None
64 self.arch = self.params.get('arch', default=arch)
65 default_qemu_bin = pick_default_qemu_bin(arch=self.arch)
66 self.qemu_bin = self.params.get('qemu_bin',
67 default=default_qemu_bin)
68 if self.qemu_bin is None:
69 self.cancel("No QEMU binary defined or found in the source tree")
70
71 def _new_vm(self, *args):
72 vm = QEMUMachine(self.qemu_bin)
73 if args:
74 vm.add_args(*args)
75 return vm
76
77 @property
78 def vm(self):
79 return self.get_vm(name='default')
80
81 def get_vm(self, *args, name=None):
82 if not name:
83 name = str(uuid.uuid4())
84 if self._vms.get(name) is None:
85 self._vms[name] = self._new_vm(*args)
86 return self._vms[name]
87
88 def tearDown(self):
89 for vm in self._vms.values():
90 vm.shutdown()