3 # Configure environment and run group of tests in it.
5 # Copyright (c) 2020-2021 Virtuozzo International GmbH
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.
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.
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/>.
23 from pathlib import Path
25 from findtests import TestFinder
26 from testenv import TestEnv
27 from testrunner import TestRunner
30 def make_argparser() -> argparse.ArgumentParser:
31 p = argparse.ArgumentParser(description="Test run options")
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')
38 p.add_argument('-d', dest='debug', action='store_true', help='debug')
39 p.add_argument('-misalign', action='store_true',
40 help='misalign memory allocations')
41 p.add_argument('--color', choices=['on', 'off', 'auto'],
42 default='auto', help="use terminal colors. The default "
43 "'auto' value means use colors if terminal stdout detected")
45 g_env = p.add_argument_group('test environment options')
46 mg = g_env.add_mutually_exclusive_group()
47 # We don't set default for cachemode, as we need to distinguish default
48 # from user input later.
49 mg.add_argument('-nocache', dest='cachemode', action='store_const',
50 const='none', help='set cache mode "none" (O_DIRECT), '
51 'sets CACHEMODE environment variable')
52 mg.add_argument('-c', dest='cachemode',
53 help='sets CACHEMODE environment variable')
55 g_env.add_argument('-i', dest='aiomode', default='threads',
56 help='sets AIOMODE environment variable')
58 p.set_defaults(imgfmt='raw', imgproto='file')
60 format_list = ['raw', 'bochs', 'cloop', 'parallels', 'qcow', 'qcow2',
61 'qed', 'vdi', 'vpc', 'vhdx', 'vmdk', 'luks', 'dmg']
62 g_fmt = p.add_argument_group(
63 ' image format options',
64 'The following options set the IMGFMT environment variable. '
65 'At most one choice is allowed, default is "raw"')
66 mg = g_fmt.add_mutually_exclusive_group()
67 for fmt in format_list:
68 mg.add_argument('-' + fmt, dest='imgfmt', action='store_const',
69 const=fmt, help=f'test {fmt}')
71 protocol_list = ['file', 'rbd', 'nbd', 'ssh', 'nfs', 'fuse']
72 g_prt = p.add_argument_group(
73 ' image protocol options',
74 'The following options set the IMGPROTO environment variable. '
75 'At most one choice is allowed, default is "file"')
76 mg = g_prt.add_mutually_exclusive_group()
77 for prt in protocol_list:
78 mg.add_argument('-' + prt, dest='imgproto', action='store_const',
79 const=prt, help=f'test {prt}')
81 g_bash = p.add_argument_group('bash tests options',
82 'The following options are ignored by '
84 # TODO: make support for the following options in iotests.py
85 g_bash.add_argument('-o', dest='imgopts',
86 help='options to pass to qemu-img create/convert, '
87 'sets IMGOPTS environment variable')
88 g_bash.add_argument('-valgrind', action='store_true',
89 help='use valgrind, sets VALGRIND_QEMU environment '
92 g_sel = p.add_argument_group('test selecting options',
93 'The following options specify test set '
95 g_sel.add_argument('-g', '--groups', metavar='group1,...',
96 help='include tests from these groups')
97 g_sel.add_argument('-x', '--exclude-groups', metavar='group1,...',
98 help='exclude tests from these groups')
99 g_sel.add_argument('--start-from', metavar='TEST',
100 help='Start from specified test: make sorted sequence '
101 'of tests as usual and then drop tests from the first '
102 'one to TEST (not inclusive). This may be used to '
103 'rerun failed ./check command, starting from the '
104 'middle of the process.')
105 g_sel.add_argument('tests', metavar='TEST_FILES', nargs='*',
106 help='tests to run, or "--" followed by a command')
111 if __name__ == '__main__':
112 args = make_argparser().parse_args()
114 env = TestEnv(imgfmt=args.imgfmt, imgproto=args.imgproto,
115 aiomode=args.aiomode, cachemode=args.cachemode,
116 imgopts=args.imgopts, misalign=args.misalign,
117 debug=args.debug, valgrind=args.valgrind)
119 if len(sys.argv) > 1 and sys.argv[-len(args.tests)-1] == '--':
121 sys.exit("missing command after '--'")
124 exec_pathstr = shutil.which(cmd[0])
125 if exec_pathstr is None:
126 sys.exit('command not found: ' + cmd[0])
127 exec_path = Path(exec_pathstr).resolve()
128 cmd[0] = str(exec_path)
129 full_env = env.prepare_subprocess(cmd)
130 os.chdir(exec_path.parent)
131 os.execve(cmd[0], cmd, full_env)
133 testfinder = TestFinder(test_dir=env.source_iotests)
135 groups = args.groups.split(',') if args.groups else None
136 x_groups = args.exclude_groups.split(',') if args.exclude_groups else None
138 group_local = os.path.join(env.source_iotests, 'group.local')
139 if os.path.isfile(group_local):
141 testfinder.add_group_file(group_local)
142 except ValueError as e:
143 sys.exit(f"Failed to parse group file '{group_local}': {e}")
146 tests = testfinder.find_tests(groups=groups, exclude_groups=x_groups,
148 start_from=args.start_from)
150 raise ValueError('No tests selected')
151 except ValueError as e:
155 print('\n'.join(tests))
157 with TestRunner(env, makecheck=args.makecheck,
158 color=args.color) as tr:
159 paths = [os.path.join(env.source_iotests, t) for t in tests]
160 ok = tr.run_tests(paths)