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