xref: /openbmc/qemu/tests/qemu-iotests/check (revision f101c9fe)
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('-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")
44
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')
54
55    g_env.add_argument('-i', dest='aiomode', default='threads',
56                       help='sets AIOMODE environment variable')
57
58    p.set_defaults(imgfmt='raw', imgproto='file')
59
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}')
70
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}')
80
81    g_bash = p.add_argument_group('bash tests options',
82                                  'The following options are ignored by '
83                                  'python tests.')
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 '
90                        'variable')
91
92    g_sel = p.add_argument_group('test selecting options',
93                                 'The following options specify test set '
94                                 'to run.')
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')
107
108    return p
109
110
111if __name__ == '__main__':
112    args = make_argparser().parse_args()
113
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)
118
119    if len(sys.argv) > 1 and sys.argv[-len(args.tests)-1] == '--':
120        if not args.tests:
121            sys.exit("missing command after '--'")
122        cmd = args.tests
123        env.print_env()
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)
132
133    testfinder = TestFinder(test_dir=env.source_iotests)
134
135    groups = args.groups.split(',') if args.groups else None
136    x_groups = args.exclude_groups.split(',') if args.exclude_groups else None
137
138    group_local = os.path.join(env.source_iotests, 'group.local')
139    if os.path.isfile(group_local):
140        try:
141            testfinder.add_group_file(group_local)
142        except ValueError as e:
143            sys.exit(f"Failed to parse group file '{group_local}': {e}")
144
145    try:
146        tests = testfinder.find_tests(groups=groups, exclude_groups=x_groups,
147                                      tests=args.tests,
148                                      start_from=args.start_from)
149        if not tests:
150            raise ValueError('No tests selected')
151    except ValueError as e:
152        sys.exit(e)
153
154    if args.dry_run:
155        print('\n'.join(tests))
156    else:
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)
161            if not ok:
162                sys.exit(1)
163