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