xref: /qemu/tests/qemu-iotests/testenv.py (revision 1a8fcca03fd6ff5acec8ca97fc20766df74f2ccb)
12e5a2f57SVladimir Sementsov-Ogievskiy# TestEnv class to manage test environment variables.
22e5a2f57SVladimir Sementsov-Ogievskiy#
32e5a2f57SVladimir Sementsov-Ogievskiy# Copyright (c) 2020-2021 Virtuozzo International GmbH
42e5a2f57SVladimir Sementsov-Ogievskiy#
52e5a2f57SVladimir Sementsov-Ogievskiy# This program is free software; you can redistribute it and/or modify
62e5a2f57SVladimir Sementsov-Ogievskiy# it under the terms of the GNU General Public License as published by
72e5a2f57SVladimir Sementsov-Ogievskiy# the Free Software Foundation; either version 2 of the License, or
82e5a2f57SVladimir Sementsov-Ogievskiy# (at your option) any later version.
92e5a2f57SVladimir Sementsov-Ogievskiy#
102e5a2f57SVladimir Sementsov-Ogievskiy# This program is distributed in the hope that it will be useful,
112e5a2f57SVladimir Sementsov-Ogievskiy# but WITHOUT ANY WARRANTY; without even the implied warranty of
122e5a2f57SVladimir Sementsov-Ogievskiy# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
132e5a2f57SVladimir Sementsov-Ogievskiy# GNU General Public License for more details.
142e5a2f57SVladimir Sementsov-Ogievskiy#
152e5a2f57SVladimir Sementsov-Ogievskiy# You should have received a copy of the GNU General Public License
162e5a2f57SVladimir Sementsov-Ogievskiy# along with this program.  If not, see <http://www.gnu.org/licenses/>.
172e5a2f57SVladimir Sementsov-Ogievskiy#
182e5a2f57SVladimir Sementsov-Ogievskiy
192e5a2f57SVladimir Sementsov-Ogievskiyimport os
202e5a2f57SVladimir Sementsov-Ogievskiyimport sys
212e5a2f57SVladimir Sementsov-Ogievskiyimport tempfile
222e5a2f57SVladimir Sementsov-Ogievskiyfrom pathlib import Path
232e5a2f57SVladimir Sementsov-Ogievskiyimport shutil
242e5a2f57SVladimir Sementsov-Ogievskiyimport collections
252e5a2f57SVladimir Sementsov-Ogievskiyimport random
262e5a2f57SVladimir Sementsov-Ogievskiyimport subprocess
272e5a2f57SVladimir Sementsov-Ogievskiyimport glob
28c64430d2SPaolo Bonzinifrom typing import List, Dict, Any, Optional, ContextManager
292e5a2f57SVladimir Sementsov-Ogievskiy
30cfb9b0b7SEmanuele Giuseppe EspositoDEF_GDB_OPTIONS = 'localhost:12345'
312e5a2f57SVladimir Sementsov-Ogievskiy
322e5a2f57SVladimir Sementsov-Ogievskiydef isxfile(path: str) -> bool:
332e5a2f57SVladimir Sementsov-Ogievskiy    return os.path.isfile(path) and os.access(path, os.X_OK)
342e5a2f57SVladimir Sementsov-Ogievskiy
352e5a2f57SVladimir Sementsov-Ogievskiy
362e5a2f57SVladimir Sementsov-Ogievskiydef get_default_machine(qemu_prog: str) -> str:
372e5a2f57SVladimir Sementsov-Ogievskiy    outp = subprocess.run([qemu_prog, '-machine', 'help'], check=True,
382e5a2f57SVladimir Sementsov-Ogievskiy                          universal_newlines=True,
392e5a2f57SVladimir Sementsov-Ogievskiy                          stdout=subprocess.PIPE).stdout
402e5a2f57SVladimir Sementsov-Ogievskiy
412e5a2f57SVladimir Sementsov-Ogievskiy    machines = outp.split('\n')
422e5a2f57SVladimir Sementsov-Ogievskiy    try:
432e5a2f57SVladimir Sementsov-Ogievskiy        default_machine = next(m for m in machines if m.endswith(' (default)'))
442e5a2f57SVladimir Sementsov-Ogievskiy    except StopIteration:
452e5a2f57SVladimir Sementsov-Ogievskiy        return ''
462e5a2f57SVladimir Sementsov-Ogievskiy    default_machine = default_machine.split(' ', 1)[0]
472e5a2f57SVladimir Sementsov-Ogievskiy
482e5a2f57SVladimir Sementsov-Ogievskiy    alias_suf = ' (alias of {})'.format(default_machine)
492e5a2f57SVladimir Sementsov-Ogievskiy    alias = next((m for m in machines if m.endswith(alias_suf)), None)
502e5a2f57SVladimir Sementsov-Ogievskiy    if alias is not None:
512e5a2f57SVladimir Sementsov-Ogievskiy        default_machine = alias.split(' ', 1)[0]
522e5a2f57SVladimir Sementsov-Ogievskiy
532e5a2f57SVladimir Sementsov-Ogievskiy    return default_machine
542e5a2f57SVladimir Sementsov-Ogievskiy
552e5a2f57SVladimir Sementsov-Ogievskiy
562e5a2f57SVladimir Sementsov-Ogievskiyclass TestEnv(ContextManager['TestEnv']):
572e5a2f57SVladimir Sementsov-Ogievskiy    """
582e5a2f57SVladimir Sementsov-Ogievskiy    Manage system environment for running tests
592e5a2f57SVladimir Sementsov-Ogievskiy
602e5a2f57SVladimir Sementsov-Ogievskiy    The following variables are supported/provided. They are represented by
612e5a2f57SVladimir Sementsov-Ogievskiy    lower-cased TestEnv attributes.
622e5a2f57SVladimir Sementsov-Ogievskiy    """
632e5a2f57SVladimir Sementsov-Ogievskiy
642e5a2f57SVladimir Sementsov-Ogievskiy    # We store environment variables as instance attributes, and there are a
652e5a2f57SVladimir Sementsov-Ogievskiy    # lot of them. Silence pylint:
662e5a2f57SVladimir Sementsov-Ogievskiy    # pylint: disable=too-many-instance-attributes
672e5a2f57SVladimir Sementsov-Ogievskiy
682e5a2f57SVladimir Sementsov-Ogievskiy    env_variables = ['PYTHONPATH', 'TEST_DIR', 'SOCK_DIR', 'SAMPLE_IMG_DIR',
69*1a8fcca0SHanna Reitz                     'PYTHON', 'QEMU_PROG', 'QEMU_IMG_PROG',
702e5a2f57SVladimir Sementsov-Ogievskiy                     'QEMU_IO_PROG', 'QEMU_NBD_PROG', 'QSD_PROG',
71c163c723SJohn Snow                     'QEMU_OPTIONS', 'QEMU_IMG_OPTIONS',
722e5a2f57SVladimir Sementsov-Ogievskiy                     'QEMU_IO_OPTIONS', 'QEMU_IO_OPTIONS_NO_FMT',
732e5a2f57SVladimir Sementsov-Ogievskiy                     'QEMU_NBD_OPTIONS', 'IMGOPTS', 'IMGFMT', 'IMGPROTO',
742e5a2f57SVladimir Sementsov-Ogievskiy                     'AIOMODE', 'CACHEMODE', 'VALGRIND_QEMU',
752e5a2f57SVladimir Sementsov-Ogievskiy                     'CACHEMODE_IS_DEFAULT', 'IMGFMT_GENERIC', 'IMGOPTSSYNTAX',
76cfb9b0b7SEmanuele Giuseppe Esposito                     'IMGKEYSECRET', 'QEMU_DEFAULT_MACHINE', 'MALLOC_PERTURB_',
77eb7a91d0SEmanuele Giuseppe Esposito                     'GDB_OPTIONS', 'PRINT_QEMU']
782e5a2f57SVladimir Sementsov-Ogievskiy
79c64430d2SPaolo Bonzini    def prepare_subprocess(self, args: List[str]) -> Dict[str, str]:
80c64430d2SPaolo Bonzini        if self.debug:
81c64430d2SPaolo Bonzini            args.append('-d')
82c64430d2SPaolo Bonzini
83c64430d2SPaolo Bonzini        with open(args[0], encoding="utf-8") as f:
84c64430d2SPaolo Bonzini            try:
85c64430d2SPaolo Bonzini                if f.readline().rstrip() == '#!/usr/bin/env python3':
86c64430d2SPaolo Bonzini                    args.insert(0, self.python)
87c64430d2SPaolo Bonzini            except UnicodeDecodeError:  # binary test? for future.
88c64430d2SPaolo Bonzini                pass
89c64430d2SPaolo Bonzini
90c64430d2SPaolo Bonzini        os_env = os.environ.copy()
91c64430d2SPaolo Bonzini        os_env.update(self.get_env())
92c64430d2SPaolo Bonzini        return os_env
93c64430d2SPaolo Bonzini
942e5a2f57SVladimir Sementsov-Ogievskiy    def get_env(self) -> Dict[str, str]:
952e5a2f57SVladimir Sementsov-Ogievskiy        env = {}
962e5a2f57SVladimir Sementsov-Ogievskiy        for v in self.env_variables:
972e5a2f57SVladimir Sementsov-Ogievskiy            val = getattr(self, v.lower(), None)
982e5a2f57SVladimir Sementsov-Ogievskiy            if val is not None:
992e5a2f57SVladimir Sementsov-Ogievskiy                env[v] = val
1002e5a2f57SVladimir Sementsov-Ogievskiy
1012e5a2f57SVladimir Sementsov-Ogievskiy        return env
1022e5a2f57SVladimir Sementsov-Ogievskiy
1032e5a2f57SVladimir Sementsov-Ogievskiy    def init_directories(self) -> None:
1042e5a2f57SVladimir Sementsov-Ogievskiy        """Init directory variables:
1052e5a2f57SVladimir Sementsov-Ogievskiy             PYTHONPATH
1062e5a2f57SVladimir Sementsov-Ogievskiy             TEST_DIR
1072e5a2f57SVladimir Sementsov-Ogievskiy             SOCK_DIR
1082e5a2f57SVladimir Sementsov-Ogievskiy             SAMPLE_IMG_DIR
1092e5a2f57SVladimir Sementsov-Ogievskiy        """
110af6d4c56SJohn Snow
111af6d4c56SJohn Snow        # Path where qemu goodies live in this source tree.
112af6d4c56SJohn Snow        qemu_srctree_path = Path(__file__, '../../../python').resolve()
113af6d4c56SJohn Snow
114af6d4c56SJohn Snow        self.pythonpath = os.pathsep.join(filter(None, (
115af6d4c56SJohn Snow            self.source_iotests,
116af6d4c56SJohn Snow            str(qemu_srctree_path),
117af6d4c56SJohn Snow            os.getenv('PYTHONPATH'),
118af6d4c56SJohn Snow        )))
1192e5a2f57SVladimir Sementsov-Ogievskiy
1202e5a2f57SVladimir Sementsov-Ogievskiy        self.test_dir = os.getenv('TEST_DIR',
1212e5a2f57SVladimir Sementsov-Ogievskiy                                  os.path.join(os.getcwd(), 'scratch'))
1222e5a2f57SVladimir Sementsov-Ogievskiy        Path(self.test_dir).mkdir(parents=True, exist_ok=True)
1232e5a2f57SVladimir Sementsov-Ogievskiy
1242e5a2f57SVladimir Sementsov-Ogievskiy        try:
1252e5a2f57SVladimir Sementsov-Ogievskiy            self.sock_dir = os.environ['SOCK_DIR']
1262e5a2f57SVladimir Sementsov-Ogievskiy            self.tmp_sock_dir = False
127c3d479aaSPaolo Bonzini            Path(self.sock_dir).mkdir(parents=True, exist_ok=True)
1282e5a2f57SVladimir Sementsov-Ogievskiy        except KeyError:
1292e5a2f57SVladimir Sementsov-Ogievskiy            self.sock_dir = tempfile.mkdtemp()
1302e5a2f57SVladimir Sementsov-Ogievskiy            self.tmp_sock_dir = True
1312e5a2f57SVladimir Sementsov-Ogievskiy
1322e5a2f57SVladimir Sementsov-Ogievskiy        self.sample_img_dir = os.getenv('SAMPLE_IMG_DIR',
1332e5a2f57SVladimir Sementsov-Ogievskiy                                        os.path.join(self.source_iotests,
1342e5a2f57SVladimir Sementsov-Ogievskiy                                                     'sample_images'))
1352e5a2f57SVladimir Sementsov-Ogievskiy
1362e5a2f57SVladimir Sementsov-Ogievskiy    def init_binaries(self) -> None:
1372e5a2f57SVladimir Sementsov-Ogievskiy        """Init binary path variables:
1382e5a2f57SVladimir Sementsov-Ogievskiy             PYTHON (for bash tests)
1392e5a2f57SVladimir Sementsov-Ogievskiy             QEMU_PROG, QEMU_IMG_PROG, QEMU_IO_PROG, QEMU_NBD_PROG, QSD_PROG
1402e5a2f57SVladimir Sementsov-Ogievskiy        """
1412e5a2f57SVladimir Sementsov-Ogievskiy        self.python = sys.executable
1422e5a2f57SVladimir Sementsov-Ogievskiy
1432e5a2f57SVladimir Sementsov-Ogievskiy        def root(*names: str) -> str:
1442e5a2f57SVladimir Sementsov-Ogievskiy            return os.path.join(self.build_root, *names)
1452e5a2f57SVladimir Sementsov-Ogievskiy
1462e5a2f57SVladimir Sementsov-Ogievskiy        arch = os.uname().machine
1472e5a2f57SVladimir Sementsov-Ogievskiy        if 'ppc64' in arch:
1482e5a2f57SVladimir Sementsov-Ogievskiy            arch = 'ppc64'
1492e5a2f57SVladimir Sementsov-Ogievskiy
1502e5a2f57SVladimir Sementsov-Ogievskiy        self.qemu_prog = os.getenv('QEMU_PROG', root(f'qemu-system-{arch}'))
1512e5a2f57SVladimir Sementsov-Ogievskiy        if not os.path.exists(self.qemu_prog):
1522e5a2f57SVladimir Sementsov-Ogievskiy            pattern = root('qemu-system-*')
1532e5a2f57SVladimir Sementsov-Ogievskiy            try:
154ca502ca6SKevin Wolf                progs = sorted(glob.iglob(pattern))
1552e5a2f57SVladimir Sementsov-Ogievskiy                self.qemu_prog = next(p for p in progs if isxfile(p))
1562e5a2f57SVladimir Sementsov-Ogievskiy            except StopIteration:
1572e5a2f57SVladimir Sementsov-Ogievskiy                sys.exit("Not found any Qemu executable binary by pattern "
1582e5a2f57SVladimir Sementsov-Ogievskiy                         f"'{pattern}'")
1592e5a2f57SVladimir Sementsov-Ogievskiy
1602e5a2f57SVladimir Sementsov-Ogievskiy        self.qemu_img_prog = os.getenv('QEMU_IMG_PROG', root('qemu-img'))
1612e5a2f57SVladimir Sementsov-Ogievskiy        self.qemu_io_prog = os.getenv('QEMU_IO_PROG', root('qemu-io'))
1622e5a2f57SVladimir Sementsov-Ogievskiy        self.qemu_nbd_prog = os.getenv('QEMU_NBD_PROG', root('qemu-nbd'))
1632e5a2f57SVladimir Sementsov-Ogievskiy        self.qsd_prog = os.getenv('QSD_PROG', root('storage-daemon',
1642e5a2f57SVladimir Sementsov-Ogievskiy                                                   'qemu-storage-daemon'))
1652e5a2f57SVladimir Sementsov-Ogievskiy
1662e5a2f57SVladimir Sementsov-Ogievskiy        for b in [self.qemu_img_prog, self.qemu_io_prog, self.qemu_nbd_prog,
1672e5a2f57SVladimir Sementsov-Ogievskiy                  self.qemu_prog, self.qsd_prog]:
1682e5a2f57SVladimir Sementsov-Ogievskiy            if not os.path.exists(b):
1692e5a2f57SVladimir Sementsov-Ogievskiy                sys.exit('No such file: ' + b)
1702e5a2f57SVladimir Sementsov-Ogievskiy            if not isxfile(b):
1712e5a2f57SVladimir Sementsov-Ogievskiy                sys.exit('Not executable: ' + b)
1722e5a2f57SVladimir Sementsov-Ogievskiy
1732e5a2f57SVladimir Sementsov-Ogievskiy    def __init__(self, imgfmt: str, imgproto: str, aiomode: str,
1742e5a2f57SVladimir Sementsov-Ogievskiy                 cachemode: Optional[str] = None,
1752e5a2f57SVladimir Sementsov-Ogievskiy                 imgopts: Optional[str] = None,
1762e5a2f57SVladimir Sementsov-Ogievskiy                 misalign: bool = False,
1772e5a2f57SVladimir Sementsov-Ogievskiy                 debug: bool = False,
178cfb9b0b7SEmanuele Giuseppe Esposito                 valgrind: bool = False,
179eb7a91d0SEmanuele Giuseppe Esposito                 gdb: bool = False,
180eb7a91d0SEmanuele Giuseppe Esposito                 qprint: bool = False) -> None:
1812e5a2f57SVladimir Sementsov-Ogievskiy        self.imgfmt = imgfmt
1822e5a2f57SVladimir Sementsov-Ogievskiy        self.imgproto = imgproto
1832e5a2f57SVladimir Sementsov-Ogievskiy        self.aiomode = aiomode
1842e5a2f57SVladimir Sementsov-Ogievskiy        self.imgopts = imgopts
1852e5a2f57SVladimir Sementsov-Ogievskiy        self.misalign = misalign
1862e5a2f57SVladimir Sementsov-Ogievskiy        self.debug = debug
1872e5a2f57SVladimir Sementsov-Ogievskiy
188eb7a91d0SEmanuele Giuseppe Esposito        if qprint:
189eb7a91d0SEmanuele Giuseppe Esposito            self.print_qemu = 'y'
190eb7a91d0SEmanuele Giuseppe Esposito
191cfb9b0b7SEmanuele Giuseppe Esposito        if gdb:
192cfb9b0b7SEmanuele Giuseppe Esposito            self.gdb_options = os.getenv('GDB_OPTIONS', DEF_GDB_OPTIONS)
193cfb9b0b7SEmanuele Giuseppe Esposito            if not self.gdb_options:
194cfb9b0b7SEmanuele Giuseppe Esposito                # cover the case 'export GDB_OPTIONS='
195cfb9b0b7SEmanuele Giuseppe Esposito                self.gdb_options = DEF_GDB_OPTIONS
196cfb9b0b7SEmanuele Giuseppe Esposito        elif 'GDB_OPTIONS' in os.environ:
197cfb9b0b7SEmanuele Giuseppe Esposito            # to not propagate it in prepare_subprocess()
198cfb9b0b7SEmanuele Giuseppe Esposito            del os.environ['GDB_OPTIONS']
199cfb9b0b7SEmanuele Giuseppe Esposito
2002e5a2f57SVladimir Sementsov-Ogievskiy        if valgrind:
2012e5a2f57SVladimir Sementsov-Ogievskiy            self.valgrind_qemu = 'y'
2022e5a2f57SVladimir Sementsov-Ogievskiy
2032e5a2f57SVladimir Sementsov-Ogievskiy        if cachemode is None:
2042e5a2f57SVladimir Sementsov-Ogievskiy            self.cachemode_is_default = 'true'
2052e5a2f57SVladimir Sementsov-Ogievskiy            self.cachemode = 'writeback'
2062e5a2f57SVladimir Sementsov-Ogievskiy        else:
2072e5a2f57SVladimir Sementsov-Ogievskiy            self.cachemode_is_default = 'false'
2082e5a2f57SVladimir Sementsov-Ogievskiy            self.cachemode = cachemode
2092e5a2f57SVladimir Sementsov-Ogievskiy
2102e5a2f57SVladimir Sementsov-Ogievskiy        # Initialize generic paths: build_root, build_iotests, source_iotests,
2112e5a2f57SVladimir Sementsov-Ogievskiy        # which are needed to initialize some environment variables. They are
2122e5a2f57SVladimir Sementsov-Ogievskiy        # used by init_*() functions as well.
2132e5a2f57SVladimir Sementsov-Ogievskiy
2142e5a2f57SVladimir Sementsov-Ogievskiy        if os.path.islink(sys.argv[0]):
2152e5a2f57SVladimir Sementsov-Ogievskiy            # called from the build tree
2162e5a2f57SVladimir Sementsov-Ogievskiy            self.source_iotests = os.path.dirname(os.readlink(sys.argv[0]))
2172e5a2f57SVladimir Sementsov-Ogievskiy            self.build_iotests = os.path.dirname(os.path.abspath(sys.argv[0]))
2182e5a2f57SVladimir Sementsov-Ogievskiy        else:
2192e5a2f57SVladimir Sementsov-Ogievskiy            # called from the source tree
2202e5a2f57SVladimir Sementsov-Ogievskiy            self.source_iotests = os.getcwd()
2212e5a2f57SVladimir Sementsov-Ogievskiy            self.build_iotests = self.source_iotests
2222e5a2f57SVladimir Sementsov-Ogievskiy
2232e5a2f57SVladimir Sementsov-Ogievskiy        self.build_root = os.path.join(self.build_iotests, '..', '..')
2242e5a2f57SVladimir Sementsov-Ogievskiy
2252e5a2f57SVladimir Sementsov-Ogievskiy        self.init_directories()
2262e5a2f57SVladimir Sementsov-Ogievskiy        self.init_binaries()
2272e5a2f57SVladimir Sementsov-Ogievskiy
2282e5a2f57SVladimir Sementsov-Ogievskiy        self.malloc_perturb_ = os.getenv('MALLOC_PERTURB_',
2292e5a2f57SVladimir Sementsov-Ogievskiy                                         str(random.randrange(1, 255)))
2302e5a2f57SVladimir Sementsov-Ogievskiy
2312e5a2f57SVladimir Sementsov-Ogievskiy        # QEMU_OPTIONS
2322e5a2f57SVladimir Sementsov-Ogievskiy        self.qemu_options = '-nodefaults -display none -accel qtest'
2332e5a2f57SVladimir Sementsov-Ogievskiy        machine_map = (
2342e5a2f57SVladimir Sementsov-Ogievskiy            ('arm', 'virt'),
2352e5a2f57SVladimir Sementsov-Ogievskiy            ('aarch64', 'virt'),
2362e5a2f57SVladimir Sementsov-Ogievskiy            ('avr', 'mega2560'),
2377033f1fdSLaurent Vivier            ('m68k', 'virt'),
2382e5a2f57SVladimir Sementsov-Ogievskiy            ('rx', 'gdbsim-r5f562n8'),
2392e5a2f57SVladimir Sementsov-Ogievskiy            ('tricore', 'tricore_testboard')
2402e5a2f57SVladimir Sementsov-Ogievskiy        )
2412e5a2f57SVladimir Sementsov-Ogievskiy        for suffix, machine in machine_map:
2422e5a2f57SVladimir Sementsov-Ogievskiy            if self.qemu_prog.endswith(f'qemu-system-{suffix}'):
2432e5a2f57SVladimir Sementsov-Ogievskiy                self.qemu_options += f' -machine {machine}'
2442e5a2f57SVladimir Sementsov-Ogievskiy
2452e5a2f57SVladimir Sementsov-Ogievskiy        # QEMU_DEFAULT_MACHINE
2462e5a2f57SVladimir Sementsov-Ogievskiy        self.qemu_default_machine = get_default_machine(self.qemu_prog)
2472e5a2f57SVladimir Sementsov-Ogievskiy
2482e5a2f57SVladimir Sementsov-Ogievskiy        self.qemu_img_options = os.getenv('QEMU_IMG_OPTIONS')
2492e5a2f57SVladimir Sementsov-Ogievskiy        self.qemu_nbd_options = os.getenv('QEMU_NBD_OPTIONS')
2502e5a2f57SVladimir Sementsov-Ogievskiy
2512e5a2f57SVladimir Sementsov-Ogievskiy        is_generic = self.imgfmt not in ['bochs', 'cloop', 'dmg']
2522e5a2f57SVladimir Sementsov-Ogievskiy        self.imgfmt_generic = 'true' if is_generic else 'false'
2532e5a2f57SVladimir Sementsov-Ogievskiy
2542e5a2f57SVladimir Sementsov-Ogievskiy        self.qemu_io_options = f'--cache {self.cachemode} --aio {self.aiomode}'
2552e5a2f57SVladimir Sementsov-Ogievskiy        if self.misalign:
2562e5a2f57SVladimir Sementsov-Ogievskiy            self.qemu_io_options += ' --misalign'
2572e5a2f57SVladimir Sementsov-Ogievskiy
2582e5a2f57SVladimir Sementsov-Ogievskiy        self.qemu_io_options_no_fmt = self.qemu_io_options
2592e5a2f57SVladimir Sementsov-Ogievskiy
2602e5a2f57SVladimir Sementsov-Ogievskiy        if self.imgfmt == 'luks':
2612e5a2f57SVladimir Sementsov-Ogievskiy            self.imgoptssyntax = 'true'
2622e5a2f57SVladimir Sementsov-Ogievskiy            self.imgkeysecret = '123456'
2632e5a2f57SVladimir Sementsov-Ogievskiy            if not self.imgopts:
2642e5a2f57SVladimir Sementsov-Ogievskiy                self.imgopts = 'iter-time=10'
2652e5a2f57SVladimir Sementsov-Ogievskiy            elif 'iter-time=' not in self.imgopts:
2662e5a2f57SVladimir Sementsov-Ogievskiy                self.imgopts += ',iter-time=10'
2672e5a2f57SVladimir Sementsov-Ogievskiy        else:
2682e5a2f57SVladimir Sementsov-Ogievskiy            self.imgoptssyntax = 'false'
2692e5a2f57SVladimir Sementsov-Ogievskiy            self.qemu_io_options += ' -f ' + self.imgfmt
2702e5a2f57SVladimir Sementsov-Ogievskiy
2712e5a2f57SVladimir Sementsov-Ogievskiy        if self.imgfmt == 'vmdk':
2722e5a2f57SVladimir Sementsov-Ogievskiy            if not self.imgopts:
2732e5a2f57SVladimir Sementsov-Ogievskiy                self.imgopts = 'zeroed_grain=on'
2742e5a2f57SVladimir Sementsov-Ogievskiy            elif 'zeroed_grain=' not in self.imgopts:
2752e5a2f57SVladimir Sementsov-Ogievskiy                self.imgopts += ',zeroed_grain=on'
2762e5a2f57SVladimir Sementsov-Ogievskiy
2772e5a2f57SVladimir Sementsov-Ogievskiy    def close(self) -> None:
2782e5a2f57SVladimir Sementsov-Ogievskiy        if self.tmp_sock_dir:
2792e5a2f57SVladimir Sementsov-Ogievskiy            shutil.rmtree(self.sock_dir)
2802e5a2f57SVladimir Sementsov-Ogievskiy
2812e5a2f57SVladimir Sementsov-Ogievskiy    def __enter__(self) -> 'TestEnv':
2822e5a2f57SVladimir Sementsov-Ogievskiy        return self
2832e5a2f57SVladimir Sementsov-Ogievskiy
2842e5a2f57SVladimir Sementsov-Ogievskiy    def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
2852e5a2f57SVladimir Sementsov-Ogievskiy        self.close()
2862e5a2f57SVladimir Sementsov-Ogievskiy
287d316859fSPaolo Bonzini    def print_env(self, prefix: str = '') -> None:
2882e5a2f57SVladimir Sementsov-Ogievskiy        template = """\
289d316859fSPaolo Bonzini{prefix}QEMU          -- "{QEMU_PROG}" {QEMU_OPTIONS}
290d316859fSPaolo Bonzini{prefix}QEMU_IMG      -- "{QEMU_IMG_PROG}" {QEMU_IMG_OPTIONS}
291d316859fSPaolo Bonzini{prefix}QEMU_IO       -- "{QEMU_IO_PROG}" {QEMU_IO_OPTIONS}
292d316859fSPaolo Bonzini{prefix}QEMU_NBD      -- "{QEMU_NBD_PROG}" {QEMU_NBD_OPTIONS}
293d316859fSPaolo Bonzini{prefix}IMGFMT        -- {IMGFMT}{imgopts}
294d316859fSPaolo Bonzini{prefix}IMGPROTO      -- {IMGPROTO}
295d316859fSPaolo Bonzini{prefix}PLATFORM      -- {platform}
296d316859fSPaolo Bonzini{prefix}TEST_DIR      -- {TEST_DIR}
297d316859fSPaolo Bonzini{prefix}SOCK_DIR      -- {SOCK_DIR}
298d316859fSPaolo Bonzini{prefix}GDB_OPTIONS   -- {GDB_OPTIONS}
299d316859fSPaolo Bonzini{prefix}VALGRIND_QEMU -- {VALGRIND_QEMU}
300d316859fSPaolo Bonzini{prefix}PRINT_QEMU_OUTPUT -- {PRINT_QEMU}
301d316859fSPaolo Bonzini{prefix}"""
3022e5a2f57SVladimir Sementsov-Ogievskiy
3032e5a2f57SVladimir Sementsov-Ogievskiy        args = collections.defaultdict(str, self.get_env())
3042e5a2f57SVladimir Sementsov-Ogievskiy
3052e5a2f57SVladimir Sementsov-Ogievskiy        if 'IMGOPTS' in args:
3062e5a2f57SVladimir Sementsov-Ogievskiy            args['imgopts'] = f" ({args['IMGOPTS']})"
3072e5a2f57SVladimir Sementsov-Ogievskiy
3082e5a2f57SVladimir Sementsov-Ogievskiy        u = os.uname()
3092e5a2f57SVladimir Sementsov-Ogievskiy        args['platform'] = f'{u.sysname}/{u.machine} {u.nodename} {u.release}'
310d316859fSPaolo Bonzini        args['prefix'] = prefix
3112e5a2f57SVladimir Sementsov-Ogievskiy        print(template.format_map(args))
312