spapr_pci: Get rid of duplicate code for node name creation
[qemu/ar7.git] / tests / acceptance / avocado_qemu / __init__.py
bloba66ec72daa06c06e3d853e6f4160ae199b99fbbe
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 os
12 import sys
13 import uuid
15 import avocado
17 SRC_ROOT_DIR = os.path.join(os.path.dirname(__file__), '..', '..', '..')
18 sys.path.append(os.path.join(SRC_ROOT_DIR, 'python'))
20 from qemu import QEMUMachine
22 def is_readable_executable_file(path):
23 return os.path.isfile(path) and os.access(path, os.R_OK | os.X_OK)
26 def pick_default_qemu_bin():
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 arch = os.uname()[4]
32 qemu_bin_relative_path = os.path.join("%s-softmmu" % arch,
33 "qemu-system-%s" % arch)
34 if is_readable_executable_file(qemu_bin_relative_path):
35 return qemu_bin_relative_path
37 qemu_bin_from_src_dir_path = os.path.join(SRC_ROOT_DIR,
38 qemu_bin_relative_path)
39 if is_readable_executable_file(qemu_bin_from_src_dir_path):
40 return qemu_bin_from_src_dir_path
43 class Test(avocado.Test):
44 def setUp(self):
45 self._vms = {}
46 self.qemu_bin = self.params.get('qemu_bin',
47 default=pick_default_qemu_bin())
48 if self.qemu_bin is None:
49 self.cancel("No QEMU binary defined or found in the source tree")
51 def _new_vm(self, *args):
52 vm = QEMUMachine(self.qemu_bin)
53 if args:
54 vm.add_args(*args)
55 return vm
57 @property
58 def vm(self):
59 return self.get_vm(name='default')
61 def get_vm(self, *args, name=None):
62 if not name:
63 name = str(uuid.uuid4())
64 if self._vms.get(name) is None:
65 self._vms[name] = self._new_vm(*args)
66 return self._vms[name]
68 def tearDown(self):
69 for vm in self._vms.values():
70 vm.shutdown()