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