1#!/usr/bin/env python3 2# 3# Configure environment and run group of tests in it. 4# 5# Copyright (c) 2020-2021 Virtuozzo International GmbH 6# 7# This program is free software; you can redistribute it and/or 8# modify it under the terms of the GNU General Public License as 9# published by the Free Software Foundation. 10# 11# This program is distributed in the hope that it would be useful, 12# but WITHOUT ANY WARRANTY; without even the implied warranty of 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14# GNU General Public License for more details. 15# 16# You should have received a copy of the GNU General Public License 17# along with this program. If not, see <http://www.gnu.org/licenses/>. 18 19import os 20import sys 21import argparse 22import shutil 23from pathlib import Path 24 25from findtests import TestFinder 26from testenv import TestEnv 27from testrunner import TestRunner 28 29 30def make_argparser() -> argparse.ArgumentParser: 31 p = argparse.ArgumentParser(description="Test run options") 32 33 p.add_argument('-n', '--dry-run', action='store_true', 34 help='show me, do not run tests') 35 p.add_argument('-makecheck', action='store_true', 36 help='pretty print output for make check') 37 38 p.add_argument('-d', dest='debug', action='store_true', help='debug') 39 p.add_argument('-gdb', action='store_true', 40 help="start gdbserver with $GDB_OPTIONS options \ 41 ('localhost:12345' if $GDB_OPTIONS is empty)") 42 p.add_argument('-valgrind', action='store_true', 43 help='use valgrind, sets VALGRIND_QEMU environment ' 44 'variable') 45 46 p.add_argument('-misalign', action='store_true', 47 help='misalign memory allocations') 48 p.add_argument('--color', choices=['on', 'off', 'auto'], 49 default='auto', help="use terminal colors. The default " 50 "'auto' value means use colors if terminal stdout detected") 51 52 g_env = p.add_argument_group('test environment options') 53 mg = g_env.add_mutually_exclusive_group() 54 # We don't set default for cachemode, as we need to distinguish default 55 # from user input later. 56 mg.add_argument('-nocache', dest='cachemode', action='store_const', 57 const='none', help='set cache mode "none" (O_DIRECT), ' 58 'sets CACHEMODE environment variable') 59 mg.add_argument('-c', dest='cachemode', 60 help='sets CACHEMODE environment variable') 61 62 g_env.add_argument('-i', dest='aiomode', default='threads', 63 help='sets AIOMODE environment variable') 64 65 p.set_defaults(imgfmt='raw', imgproto='file') 66 67 format_list = ['raw', 'bochs', 'cloop', 'parallels', 'qcow', 'qcow2', 68 'qed', 'vdi', 'vpc', 'vhdx', 'vmdk', 'luks', 'dmg'] 69 g_fmt = p.add_argument_group( 70 ' image format options', 71 'The following options set the IMGFMT environment variable. ' 72 'At most one choice is allowed, default is "raw"') 73 mg = g_fmt.add_mutually_exclusive_group() 74 for fmt in format_list: 75 mg.add_argument('-' + fmt, dest='imgfmt', action='store_const', 76 const=fmt, help=f'test {fmt}') 77 78 protocol_list = ['file', 'rbd', 'nbd', 'ssh', 'nfs', 'fuse'] 79 g_prt = p.add_argument_group( 80 ' image protocol options', 81 'The following options set the IMGPROTO environment variable. ' 82 'At most one choice is allowed, default is "file"') 83 mg = g_prt.add_mutually_exclusive_group() 84 for prt in protocol_list: 85 mg.add_argument('-' + prt, dest='imgproto', action='store_const', 86 const=prt, help=f'test {prt}') 87 88 g_bash = p.add_argument_group('bash tests options', 89 'The following options are ignored by ' 90 'python tests.') 91 # TODO: make support for the following options in iotests.py 92 g_bash.add_argument('-o', dest='imgopts', 93 help='options to pass to qemu-img create/convert, ' 94 'sets IMGOPTS environment variable') 95 96 g_sel = p.add_argument_group('test selecting options', 97 'The following options specify test set ' 98 'to run.') 99 g_sel.add_argument('-g', '--groups', metavar='group1,...', 100 help='include tests from these groups') 101 g_sel.add_argument('-x', '--exclude-groups', metavar='group1,...', 102 help='exclude tests from these groups') 103 g_sel.add_argument('--start-from', metavar='TEST', 104 help='Start from specified test: make sorted sequence ' 105 'of tests as usual and then drop tests from the first ' 106 'one to TEST (not inclusive). This may be used to ' 107 'rerun failed ./check command, starting from the ' 108 'middle of the process.') 109 g_sel.add_argument('tests', metavar='TEST_FILES', nargs='*', 110 help='tests to run, or "--" followed by a command') 111 112 return p 113 114 115if __name__ == '__main__': 116 args = make_argparser().parse_args() 117 118 env = TestEnv(imgfmt=args.imgfmt, imgproto=args.imgproto, 119 aiomode=args.aiomode, cachemode=args.cachemode, 120 imgopts=args.imgopts, misalign=args.misalign, 121 debug=args.debug, valgrind=args.valgrind, 122 gdb=args.gdb) 123 124 if len(sys.argv) > 1 and sys.argv[-len(args.tests)-1] == '--': 125 if not args.tests: 126 sys.exit("missing command after '--'") 127 cmd = args.tests 128 env.print_env() 129 exec_pathstr = shutil.which(cmd[0]) 130 if exec_pathstr is None: 131 sys.exit('command not found: ' + cmd[0]) 132 exec_path = Path(exec_pathstr).resolve() 133 cmd[0] = str(exec_path) 134 full_env = env.prepare_subprocess(cmd) 135 os.chdir(exec_path.parent) 136 os.execve(cmd[0], cmd, full_env) 137 138 testfinder = TestFinder(test_dir=env.source_iotests) 139 140 groups = args.groups.split(',') if args.groups else None 141 x_groups = args.exclude_groups.split(',') if args.exclude_groups else None 142 143 group_local = os.path.join(env.source_iotests, 'group.local') 144 if os.path.isfile(group_local): 145 try: 146 testfinder.add_group_file(group_local) 147 except ValueError as e: 148 sys.exit(f"Failed to parse group file '{group_local}': {e}") 149 150 try: 151 tests = testfinder.find_tests(groups=groups, exclude_groups=x_groups, 152 tests=args.tests, 153 start_from=args.start_from) 154 if not tests: 155 raise ValueError('No tests selected') 156 except ValueError as e: 157 sys.exit(e) 158 159 if args.dry_run: 160 print('\n'.join(tests)) 161 else: 162 with TestRunner(env, makecheck=args.makecheck, 163 color=args.color) as tr: 164 paths = [os.path.join(env.source_iotests, t) for t in tests] 165 ok = tr.run_tests(paths) 166 if not ok: 167 sys.exit(1) 168