tests/acceptance: look for target architecture in test tags first
[qemu/ar7.git] / tests / acceptance / avocado_qemu / __init__.py
blob2b236a1cf0f6403231f35deabc8b61adea19abdf
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(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.
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_bin_relative_path = os.path.join("%s-softmmu" % arch,
43 "qemu-system-%s" % arch)
44 if is_readable_executable_file(qemu_bin_relative_path):
45 return qemu_bin_relative_path
47 qemu_bin_from_src_dir_path = os.path.join(SRC_ROOT_DIR,
48 qemu_bin_relative_path)
49 if is_readable_executable_file(qemu_bin_from_src_dir_path):
50 return qemu_bin_from_src_dir_path
53 class Test(avocado.Test):
54 def setUp(self):
55 self._vms = {}
56 arches = self.tags.get('arch', [])
57 if len(arches) == 1:
58 arch = arches.pop()
59 else:
60 arch = None
61 self.arch = self.params.get('arch', default=arch)
62 default_qemu_bin = pick_default_qemu_bin(arch=self.arch)
63 self.qemu_bin = self.params.get('qemu_bin',
64 default=default_qemu_bin)
65 if self.qemu_bin is None:
66 self.cancel("No QEMU binary defined or found in the source tree")
68 def _new_vm(self, *args):
69 vm = QEMUMachine(self.qemu_bin)
70 if args:
71 vm.add_args(*args)
72 return vm
74 @property
75 def vm(self):
76 return self.get_vm(name='default')
78 def get_vm(self, *args, name=None):
79 if not name:
80 name = str(uuid.uuid4())
81 if self._vms.get(name) is None:
82 self._vms[name] = self._new_vm(*args)
83 return self._vms[name]
85 def tearDown(self):
86 for vm in self._vms.values():
87 vm.shutdown()