xref: /qemu/tests/qemu-iotests/testenv.py (revision c64430d2386d9968342a8e1ae00ed34ff0b98bbb)
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
28*c64430d2SPaolo Bonzinifrom typing import List, Dict, Any, Optional, ContextManager
292e5a2f57SVladimir Sementsov-Ogievskiy
302e5a2f57SVladimir Sementsov-Ogievskiy
312e5a2f57SVladimir Sementsov-Ogievskiydef isxfile(path: str) -> bool:
322e5a2f57SVladimir Sementsov-Ogievskiy    return os.path.isfile(path) and os.access(path, os.X_OK)
332e5a2f57SVladimir Sementsov-Ogievskiy
342e5a2f57SVladimir Sementsov-Ogievskiy
352e5a2f57SVladimir Sementsov-Ogievskiydef get_default_machine(qemu_prog: str) -> str:
362e5a2f57SVladimir Sementsov-Ogievskiy    outp = subprocess.run([qemu_prog, '-machine', 'help'], check=True,
372e5a2f57SVladimir Sementsov-Ogievskiy                          universal_newlines=True,
382e5a2f57SVladimir Sementsov-Ogievskiy                          stdout=subprocess.PIPE).stdout
392e5a2f57SVladimir Sementsov-Ogievskiy
402e5a2f57SVladimir Sementsov-Ogievskiy    machines = outp.split('\n')
412e5a2f57SVladimir Sementsov-Ogievskiy    try:
422e5a2f57SVladimir Sementsov-Ogievskiy        default_machine = next(m for m in machines if m.endswith(' (default)'))
432e5a2f57SVladimir Sementsov-Ogievskiy    except StopIteration:
442e5a2f57SVladimir Sementsov-Ogievskiy        return ''
452e5a2f57SVladimir Sementsov-Ogievskiy    default_machine = default_machine.split(' ', 1)[0]
462e5a2f57SVladimir Sementsov-Ogievskiy
472e5a2f57SVladimir Sementsov-Ogievskiy    alias_suf = ' (alias of {})'.format(default_machine)
482e5a2f57SVladimir Sementsov-Ogievskiy    alias = next((m for m in machines if m.endswith(alias_suf)), None)
492e5a2f57SVladimir Sementsov-Ogievskiy    if alias is not None:
502e5a2f57SVladimir Sementsov-Ogievskiy        default_machine = alias.split(' ', 1)[0]
512e5a2f57SVladimir Sementsov-Ogievskiy
522e5a2f57SVladimir Sementsov-Ogievskiy    return default_machine
532e5a2f57SVladimir Sementsov-Ogievskiy
542e5a2f57SVladimir Sementsov-Ogievskiy
552e5a2f57SVladimir Sementsov-Ogievskiyclass TestEnv(ContextManager['TestEnv']):
562e5a2f57SVladimir Sementsov-Ogievskiy    """
572e5a2f57SVladimir Sementsov-Ogievskiy    Manage system environment for running tests
582e5a2f57SVladimir Sementsov-Ogievskiy
592e5a2f57SVladimir Sementsov-Ogievskiy    The following variables are supported/provided. They are represented by
602e5a2f57SVladimir Sementsov-Ogievskiy    lower-cased TestEnv attributes.
612e5a2f57SVladimir Sementsov-Ogievskiy    """
622e5a2f57SVladimir Sementsov-Ogievskiy
632e5a2f57SVladimir Sementsov-Ogievskiy    # We store environment variables as instance attributes, and there are a
642e5a2f57SVladimir Sementsov-Ogievskiy    # lot of them. Silence pylint:
652e5a2f57SVladimir Sementsov-Ogievskiy    # pylint: disable=too-many-instance-attributes
662e5a2f57SVladimir Sementsov-Ogievskiy
672e5a2f57SVladimir Sementsov-Ogievskiy    env_variables = ['PYTHONPATH', 'TEST_DIR', 'SOCK_DIR', 'SAMPLE_IMG_DIR',
682e5a2f57SVladimir Sementsov-Ogievskiy                     'OUTPUT_DIR', 'PYTHON', 'QEMU_PROG', 'QEMU_IMG_PROG',
692e5a2f57SVladimir Sementsov-Ogievskiy                     'QEMU_IO_PROG', 'QEMU_NBD_PROG', 'QSD_PROG',
702e5a2f57SVladimir Sementsov-Ogievskiy                     'SOCKET_SCM_HELPER', 'QEMU_OPTIONS', 'QEMU_IMG_OPTIONS',
712e5a2f57SVladimir Sementsov-Ogievskiy                     'QEMU_IO_OPTIONS', 'QEMU_IO_OPTIONS_NO_FMT',
722e5a2f57SVladimir Sementsov-Ogievskiy                     'QEMU_NBD_OPTIONS', 'IMGOPTS', 'IMGFMT', 'IMGPROTO',
732e5a2f57SVladimir Sementsov-Ogievskiy                     'AIOMODE', 'CACHEMODE', 'VALGRIND_QEMU',
742e5a2f57SVladimir Sementsov-Ogievskiy                     'CACHEMODE_IS_DEFAULT', 'IMGFMT_GENERIC', 'IMGOPTSSYNTAX',
752e5a2f57SVladimir Sementsov-Ogievskiy                     'IMGKEYSECRET', 'QEMU_DEFAULT_MACHINE', 'MALLOC_PERTURB_']
762e5a2f57SVladimir Sementsov-Ogievskiy
77*c64430d2SPaolo Bonzini    def prepare_subprocess(self, args: List[str]) -> Dict[str, str]:
78*c64430d2SPaolo Bonzini        if self.debug:
79*c64430d2SPaolo Bonzini            args.append('-d')
80*c64430d2SPaolo Bonzini
81*c64430d2SPaolo Bonzini        with open(args[0], encoding="utf-8") as f:
82*c64430d2SPaolo Bonzini            try:
83*c64430d2SPaolo Bonzini                if f.readline().rstrip() == '#!/usr/bin/env python3':
84*c64430d2SPaolo Bonzini                    args.insert(0, self.python)
85*c64430d2SPaolo Bonzini            except UnicodeDecodeError:  # binary test? for future.
86*c64430d2SPaolo Bonzini                pass
87*c64430d2SPaolo Bonzini
88*c64430d2SPaolo Bonzini        os_env = os.environ.copy()
89*c64430d2SPaolo Bonzini        os_env.update(self.get_env())
90*c64430d2SPaolo Bonzini        return os_env
91*c64430d2SPaolo Bonzini
922e5a2f57SVladimir Sementsov-Ogievskiy    def get_env(self) -> Dict[str, str]:
932e5a2f57SVladimir Sementsov-Ogievskiy        env = {}
942e5a2f57SVladimir Sementsov-Ogievskiy        for v in self.env_variables:
952e5a2f57SVladimir Sementsov-Ogievskiy            val = getattr(self, v.lower(), None)
962e5a2f57SVladimir Sementsov-Ogievskiy            if val is not None:
972e5a2f57SVladimir Sementsov-Ogievskiy                env[v] = val
982e5a2f57SVladimir Sementsov-Ogievskiy
992e5a2f57SVladimir Sementsov-Ogievskiy        return env
1002e5a2f57SVladimir Sementsov-Ogievskiy
1012e5a2f57SVladimir Sementsov-Ogievskiy    def init_directories(self) -> None:
1022e5a2f57SVladimir Sementsov-Ogievskiy        """Init directory variables:
1032e5a2f57SVladimir Sementsov-Ogievskiy             PYTHONPATH
1042e5a2f57SVladimir Sementsov-Ogievskiy             TEST_DIR
1052e5a2f57SVladimir Sementsov-Ogievskiy             SOCK_DIR
1062e5a2f57SVladimir Sementsov-Ogievskiy             SAMPLE_IMG_DIR
1072e5a2f57SVladimir Sementsov-Ogievskiy             OUTPUT_DIR
1082e5a2f57SVladimir Sementsov-Ogievskiy        """
1092e5a2f57SVladimir Sementsov-Ogievskiy        self.pythonpath = os.getenv('PYTHONPATH')
1102e5a2f57SVladimir Sementsov-Ogievskiy        if self.pythonpath:
1112e5a2f57SVladimir Sementsov-Ogievskiy            self.pythonpath = self.source_iotests + os.pathsep + \
1122e5a2f57SVladimir Sementsov-Ogievskiy                self.pythonpath
1132e5a2f57SVladimir Sementsov-Ogievskiy        else:
1142e5a2f57SVladimir Sementsov-Ogievskiy            self.pythonpath = self.source_iotests
1152e5a2f57SVladimir Sementsov-Ogievskiy
1162e5a2f57SVladimir Sementsov-Ogievskiy        self.test_dir = os.getenv('TEST_DIR',
1172e5a2f57SVladimir Sementsov-Ogievskiy                                  os.path.join(os.getcwd(), 'scratch'))
1182e5a2f57SVladimir Sementsov-Ogievskiy        Path(self.test_dir).mkdir(parents=True, exist_ok=True)
1192e5a2f57SVladimir Sementsov-Ogievskiy
1202e5a2f57SVladimir Sementsov-Ogievskiy        try:
1212e5a2f57SVladimir Sementsov-Ogievskiy            self.sock_dir = os.environ['SOCK_DIR']
1222e5a2f57SVladimir Sementsov-Ogievskiy            self.tmp_sock_dir = False
1232e5a2f57SVladimir Sementsov-Ogievskiy            Path(self.test_dir).mkdir(parents=True, exist_ok=True)
1242e5a2f57SVladimir Sementsov-Ogievskiy        except KeyError:
1252e5a2f57SVladimir Sementsov-Ogievskiy            self.sock_dir = tempfile.mkdtemp()
1262e5a2f57SVladimir Sementsov-Ogievskiy            self.tmp_sock_dir = True
1272e5a2f57SVladimir Sementsov-Ogievskiy
1282e5a2f57SVladimir Sementsov-Ogievskiy        self.sample_img_dir = os.getenv('SAMPLE_IMG_DIR',
1292e5a2f57SVladimir Sementsov-Ogievskiy                                        os.path.join(self.source_iotests,
1302e5a2f57SVladimir Sementsov-Ogievskiy                                                     'sample_images'))
1312e5a2f57SVladimir Sementsov-Ogievskiy
1322e5a2f57SVladimir Sementsov-Ogievskiy        self.output_dir = os.getcwd()  # OUTPUT_DIR
1332e5a2f57SVladimir Sementsov-Ogievskiy
1342e5a2f57SVladimir Sementsov-Ogievskiy    def init_binaries(self) -> None:
1352e5a2f57SVladimir Sementsov-Ogievskiy        """Init binary path variables:
1362e5a2f57SVladimir Sementsov-Ogievskiy             PYTHON (for bash tests)
1372e5a2f57SVladimir Sementsov-Ogievskiy             QEMU_PROG, QEMU_IMG_PROG, QEMU_IO_PROG, QEMU_NBD_PROG, QSD_PROG
1382e5a2f57SVladimir Sementsov-Ogievskiy             SOCKET_SCM_HELPER
1392e5a2f57SVladimir Sementsov-Ogievskiy        """
1402e5a2f57SVladimir Sementsov-Ogievskiy        self.python = sys.executable
1412e5a2f57SVladimir Sementsov-Ogievskiy
1422e5a2f57SVladimir Sementsov-Ogievskiy        def root(*names: str) -> str:
1432e5a2f57SVladimir Sementsov-Ogievskiy            return os.path.join(self.build_root, *names)
1442e5a2f57SVladimir Sementsov-Ogievskiy
1452e5a2f57SVladimir Sementsov-Ogievskiy        arch = os.uname().machine
1462e5a2f57SVladimir Sementsov-Ogievskiy        if 'ppc64' in arch:
1472e5a2f57SVladimir Sementsov-Ogievskiy            arch = 'ppc64'
1482e5a2f57SVladimir Sementsov-Ogievskiy
1492e5a2f57SVladimir Sementsov-Ogievskiy        self.qemu_prog = os.getenv('QEMU_PROG', root(f'qemu-system-{arch}'))
1502e5a2f57SVladimir Sementsov-Ogievskiy        if not os.path.exists(self.qemu_prog):
1512e5a2f57SVladimir Sementsov-Ogievskiy            pattern = root('qemu-system-*')
1522e5a2f57SVladimir Sementsov-Ogievskiy            try:
153ca502ca6SKevin Wolf                progs = sorted(glob.iglob(pattern))
1542e5a2f57SVladimir Sementsov-Ogievskiy                self.qemu_prog = next(p for p in progs if isxfile(p))
1552e5a2f57SVladimir Sementsov-Ogievskiy            except StopIteration:
1562e5a2f57SVladimir Sementsov-Ogievskiy                sys.exit("Not found any Qemu executable binary by pattern "
1572e5a2f57SVladimir Sementsov-Ogievskiy                         f"'{pattern}'")
1582e5a2f57SVladimir Sementsov-Ogievskiy
1592e5a2f57SVladimir Sementsov-Ogievskiy        self.qemu_img_prog = os.getenv('QEMU_IMG_PROG', root('qemu-img'))
1602e5a2f57SVladimir Sementsov-Ogievskiy        self.qemu_io_prog = os.getenv('QEMU_IO_PROG', root('qemu-io'))
1612e5a2f57SVladimir Sementsov-Ogievskiy        self.qemu_nbd_prog = os.getenv('QEMU_NBD_PROG', root('qemu-nbd'))
1622e5a2f57SVladimir Sementsov-Ogievskiy        self.qsd_prog = os.getenv('QSD_PROG', root('storage-daemon',
1632e5a2f57SVladimir Sementsov-Ogievskiy                                                   'qemu-storage-daemon'))
1642e5a2f57SVladimir Sementsov-Ogievskiy
1652e5a2f57SVladimir Sementsov-Ogievskiy        for b in [self.qemu_img_prog, self.qemu_io_prog, self.qemu_nbd_prog,
1662e5a2f57SVladimir Sementsov-Ogievskiy                  self.qemu_prog, self.qsd_prog]:
1672e5a2f57SVladimir Sementsov-Ogievskiy            if not os.path.exists(b):
1682e5a2f57SVladimir Sementsov-Ogievskiy                sys.exit('No such file: ' + b)
1692e5a2f57SVladimir Sementsov-Ogievskiy            if not isxfile(b):
1702e5a2f57SVladimir Sementsov-Ogievskiy                sys.exit('Not executable: ' + b)
1712e5a2f57SVladimir Sementsov-Ogievskiy
1722e5a2f57SVladimir Sementsov-Ogievskiy        helper_path = os.path.join(self.build_iotests, 'socket_scm_helper')
1732e5a2f57SVladimir Sementsov-Ogievskiy        if isxfile(helper_path):
1742e5a2f57SVladimir Sementsov-Ogievskiy            self.socket_scm_helper = helper_path  # SOCKET_SCM_HELPER
1752e5a2f57SVladimir Sementsov-Ogievskiy
1762e5a2f57SVladimir Sementsov-Ogievskiy    def __init__(self, imgfmt: str, imgproto: str, aiomode: str,
1772e5a2f57SVladimir Sementsov-Ogievskiy                 cachemode: Optional[str] = None,
1782e5a2f57SVladimir Sementsov-Ogievskiy                 imgopts: Optional[str] = None,
1792e5a2f57SVladimir Sementsov-Ogievskiy                 misalign: bool = False,
1802e5a2f57SVladimir Sementsov-Ogievskiy                 debug: bool = False,
1812e5a2f57SVladimir Sementsov-Ogievskiy                 valgrind: bool = False) -> None:
1822e5a2f57SVladimir Sementsov-Ogievskiy        self.imgfmt = imgfmt
1832e5a2f57SVladimir Sementsov-Ogievskiy        self.imgproto = imgproto
1842e5a2f57SVladimir Sementsov-Ogievskiy        self.aiomode = aiomode
1852e5a2f57SVladimir Sementsov-Ogievskiy        self.imgopts = imgopts
1862e5a2f57SVladimir Sementsov-Ogievskiy        self.misalign = misalign
1872e5a2f57SVladimir Sementsov-Ogievskiy        self.debug = debug
1882e5a2f57SVladimir Sementsov-Ogievskiy
1892e5a2f57SVladimir Sementsov-Ogievskiy        if valgrind:
1902e5a2f57SVladimir Sementsov-Ogievskiy            self.valgrind_qemu = 'y'
1912e5a2f57SVladimir Sementsov-Ogievskiy
1922e5a2f57SVladimir Sementsov-Ogievskiy        if cachemode is None:
1932e5a2f57SVladimir Sementsov-Ogievskiy            self.cachemode_is_default = 'true'
1942e5a2f57SVladimir Sementsov-Ogievskiy            self.cachemode = 'writeback'
1952e5a2f57SVladimir Sementsov-Ogievskiy        else:
1962e5a2f57SVladimir Sementsov-Ogievskiy            self.cachemode_is_default = 'false'
1972e5a2f57SVladimir Sementsov-Ogievskiy            self.cachemode = cachemode
1982e5a2f57SVladimir Sementsov-Ogievskiy
1992e5a2f57SVladimir Sementsov-Ogievskiy        # Initialize generic paths: build_root, build_iotests, source_iotests,
2002e5a2f57SVladimir Sementsov-Ogievskiy        # which are needed to initialize some environment variables. They are
2012e5a2f57SVladimir Sementsov-Ogievskiy        # used by init_*() functions as well.
2022e5a2f57SVladimir Sementsov-Ogievskiy
2032e5a2f57SVladimir Sementsov-Ogievskiy        if os.path.islink(sys.argv[0]):
2042e5a2f57SVladimir Sementsov-Ogievskiy            # called from the build tree
2052e5a2f57SVladimir Sementsov-Ogievskiy            self.source_iotests = os.path.dirname(os.readlink(sys.argv[0]))
2062e5a2f57SVladimir Sementsov-Ogievskiy            self.build_iotests = os.path.dirname(os.path.abspath(sys.argv[0]))
2072e5a2f57SVladimir Sementsov-Ogievskiy        else:
2082e5a2f57SVladimir Sementsov-Ogievskiy            # called from the source tree
2092e5a2f57SVladimir Sementsov-Ogievskiy            self.source_iotests = os.getcwd()
2102e5a2f57SVladimir Sementsov-Ogievskiy            self.build_iotests = self.source_iotests
2112e5a2f57SVladimir Sementsov-Ogievskiy
2122e5a2f57SVladimir Sementsov-Ogievskiy        self.build_root = os.path.join(self.build_iotests, '..', '..')
2132e5a2f57SVladimir Sementsov-Ogievskiy
2142e5a2f57SVladimir Sementsov-Ogievskiy        self.init_directories()
2152e5a2f57SVladimir Sementsov-Ogievskiy        self.init_binaries()
2162e5a2f57SVladimir Sementsov-Ogievskiy
2172e5a2f57SVladimir Sementsov-Ogievskiy        self.malloc_perturb_ = os.getenv('MALLOC_PERTURB_',
2182e5a2f57SVladimir Sementsov-Ogievskiy                                         str(random.randrange(1, 255)))
2192e5a2f57SVladimir Sementsov-Ogievskiy
2202e5a2f57SVladimir Sementsov-Ogievskiy        # QEMU_OPTIONS
2212e5a2f57SVladimir Sementsov-Ogievskiy        self.qemu_options = '-nodefaults -display none -accel qtest'
2222e5a2f57SVladimir Sementsov-Ogievskiy        machine_map = (
2232e5a2f57SVladimir Sementsov-Ogievskiy            ('arm', 'virt'),
2242e5a2f57SVladimir Sementsov-Ogievskiy            ('aarch64', 'virt'),
2252e5a2f57SVladimir Sementsov-Ogievskiy            ('avr', 'mega2560'),
2267033f1fdSLaurent Vivier            ('m68k', 'virt'),
2272e5a2f57SVladimir Sementsov-Ogievskiy            ('rx', 'gdbsim-r5f562n8'),
2282e5a2f57SVladimir Sementsov-Ogievskiy            ('tricore', 'tricore_testboard')
2292e5a2f57SVladimir Sementsov-Ogievskiy        )
2302e5a2f57SVladimir Sementsov-Ogievskiy        for suffix, machine in machine_map:
2312e5a2f57SVladimir Sementsov-Ogievskiy            if self.qemu_prog.endswith(f'qemu-system-{suffix}'):
2322e5a2f57SVladimir Sementsov-Ogievskiy                self.qemu_options += f' -machine {machine}'
2332e5a2f57SVladimir Sementsov-Ogievskiy
2342e5a2f57SVladimir Sementsov-Ogievskiy        # QEMU_DEFAULT_MACHINE
2352e5a2f57SVladimir Sementsov-Ogievskiy        self.qemu_default_machine = get_default_machine(self.qemu_prog)
2362e5a2f57SVladimir Sementsov-Ogievskiy
2372e5a2f57SVladimir Sementsov-Ogievskiy        self.qemu_img_options = os.getenv('QEMU_IMG_OPTIONS')
2382e5a2f57SVladimir Sementsov-Ogievskiy        self.qemu_nbd_options = os.getenv('QEMU_NBD_OPTIONS')
2392e5a2f57SVladimir Sementsov-Ogievskiy
2402e5a2f57SVladimir Sementsov-Ogievskiy        is_generic = self.imgfmt not in ['bochs', 'cloop', 'dmg']
2412e5a2f57SVladimir Sementsov-Ogievskiy        self.imgfmt_generic = 'true' if is_generic else 'false'
2422e5a2f57SVladimir Sementsov-Ogievskiy
2432e5a2f57SVladimir Sementsov-Ogievskiy        self.qemu_io_options = f'--cache {self.cachemode} --aio {self.aiomode}'
2442e5a2f57SVladimir Sementsov-Ogievskiy        if self.misalign:
2452e5a2f57SVladimir Sementsov-Ogievskiy            self.qemu_io_options += ' --misalign'
2462e5a2f57SVladimir Sementsov-Ogievskiy
2472e5a2f57SVladimir Sementsov-Ogievskiy        self.qemu_io_options_no_fmt = self.qemu_io_options
2482e5a2f57SVladimir Sementsov-Ogievskiy
2492e5a2f57SVladimir Sementsov-Ogievskiy        if self.imgfmt == 'luks':
2502e5a2f57SVladimir Sementsov-Ogievskiy            self.imgoptssyntax = 'true'
2512e5a2f57SVladimir Sementsov-Ogievskiy            self.imgkeysecret = '123456'
2522e5a2f57SVladimir Sementsov-Ogievskiy            if not self.imgopts:
2532e5a2f57SVladimir Sementsov-Ogievskiy                self.imgopts = 'iter-time=10'
2542e5a2f57SVladimir Sementsov-Ogievskiy            elif 'iter-time=' not in self.imgopts:
2552e5a2f57SVladimir Sementsov-Ogievskiy                self.imgopts += ',iter-time=10'
2562e5a2f57SVladimir Sementsov-Ogievskiy        else:
2572e5a2f57SVladimir Sementsov-Ogievskiy            self.imgoptssyntax = 'false'
2582e5a2f57SVladimir Sementsov-Ogievskiy            self.qemu_io_options += ' -f ' + self.imgfmt
2592e5a2f57SVladimir Sementsov-Ogievskiy
2602e5a2f57SVladimir Sementsov-Ogievskiy        if self.imgfmt == 'vmdk':
2612e5a2f57SVladimir Sementsov-Ogievskiy            if not self.imgopts:
2622e5a2f57SVladimir Sementsov-Ogievskiy                self.imgopts = 'zeroed_grain=on'
2632e5a2f57SVladimir Sementsov-Ogievskiy            elif 'zeroed_grain=' not in self.imgopts:
2642e5a2f57SVladimir Sementsov-Ogievskiy                self.imgopts += ',zeroed_grain=on'
2652e5a2f57SVladimir Sementsov-Ogievskiy
2662e5a2f57SVladimir Sementsov-Ogievskiy    def close(self) -> None:
2672e5a2f57SVladimir Sementsov-Ogievskiy        if self.tmp_sock_dir:
2682e5a2f57SVladimir Sementsov-Ogievskiy            shutil.rmtree(self.sock_dir)
2692e5a2f57SVladimir Sementsov-Ogievskiy
2702e5a2f57SVladimir Sementsov-Ogievskiy    def __enter__(self) -> 'TestEnv':
2712e5a2f57SVladimir Sementsov-Ogievskiy        return self
2722e5a2f57SVladimir Sementsov-Ogievskiy
2732e5a2f57SVladimir Sementsov-Ogievskiy    def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
2742e5a2f57SVladimir Sementsov-Ogievskiy        self.close()
2752e5a2f57SVladimir Sementsov-Ogievskiy
2762e5a2f57SVladimir Sementsov-Ogievskiy    def print_env(self) -> None:
2772e5a2f57SVladimir Sementsov-Ogievskiy        template = """\
2782e5a2f57SVladimir Sementsov-OgievskiyQEMU          -- "{QEMU_PROG}" {QEMU_OPTIONS}
2792e5a2f57SVladimir Sementsov-OgievskiyQEMU_IMG      -- "{QEMU_IMG_PROG}" {QEMU_IMG_OPTIONS}
2802e5a2f57SVladimir Sementsov-OgievskiyQEMU_IO       -- "{QEMU_IO_PROG}" {QEMU_IO_OPTIONS}
2812e5a2f57SVladimir Sementsov-OgievskiyQEMU_NBD      -- "{QEMU_NBD_PROG}" {QEMU_NBD_OPTIONS}
2822e5a2f57SVladimir Sementsov-OgievskiyIMGFMT        -- {IMGFMT}{imgopts}
2832e5a2f57SVladimir Sementsov-OgievskiyIMGPROTO      -- {IMGPROTO}
2842e5a2f57SVladimir Sementsov-OgievskiyPLATFORM      -- {platform}
2852e5a2f57SVladimir Sementsov-OgievskiyTEST_DIR      -- {TEST_DIR}
2862e5a2f57SVladimir Sementsov-OgievskiySOCK_DIR      -- {SOCK_DIR}
2872e5a2f57SVladimir Sementsov-OgievskiySOCKET_SCM_HELPER -- {SOCKET_SCM_HELPER}"""
2882e5a2f57SVladimir Sementsov-Ogievskiy
2892e5a2f57SVladimir Sementsov-Ogievskiy        args = collections.defaultdict(str, self.get_env())
2902e5a2f57SVladimir Sementsov-Ogievskiy
2912e5a2f57SVladimir Sementsov-Ogievskiy        if 'IMGOPTS' in args:
2922e5a2f57SVladimir Sementsov-Ogievskiy            args['imgopts'] = f" ({args['IMGOPTS']})"
2932e5a2f57SVladimir Sementsov-Ogievskiy
2942e5a2f57SVladimir Sementsov-Ogievskiy        u = os.uname()
2952e5a2f57SVladimir Sementsov-Ogievskiy        args['platform'] = f'{u.sysname}/{u.machine} {u.nodename} {u.release}'
2962e5a2f57SVladimir Sementsov-Ogievskiy
2972e5a2f57SVladimir Sementsov-Ogievskiy        print(template.format_map(args))
298