xref: /openbmc/qemu/tests/qemu-iotests/check (revision c4fa97c7f216fc80b09a5d32be847ff8d502cba6)
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
24import warnings
25
26from findtests import TestFinder
27from testenv import TestEnv
28from testrunner import TestRunner
29
30def get_default_path(follow_link=False):
31    """
32    Try to automagically figure out the path we are running from.
33    """
34    # called from the build tree?
35    if os.path.islink(sys.argv[0]):
36        if follow_link:
37            return os.path.dirname(os.readlink(sys.argv[0]))
38        else:
39            return os.path.dirname(os.path.abspath(sys.argv[0]))
40    else:  # or source tree?
41        return os.getcwd()
42
43def make_argparser() -> argparse.ArgumentParser:
44    p = argparse.ArgumentParser(
45        description="Test run options",
46        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
47
48    p.add_argument('-n', '--dry-run', action='store_true',
49                   help='show me, do not run tests')
50    p.add_argument('-j', dest='jobs', type=int, default=1,
51                   help='run tests in multiple parallel jobs')
52
53    p.add_argument('-d', dest='debug', action='store_true', help='debug')
54    p.add_argument('-p', dest='print', action='store_true',
55                   help='redirects qemu\'s stdout and stderr to '
56                        'the test output')
57    p.add_argument('-gdb', action='store_true',
58                   help="start gdbserver with $GDB_OPTIONS options "
59                        "('localhost:12345' if $GDB_OPTIONS is empty)")
60    p.add_argument('-valgrind', action='store_true',
61                   help='use valgrind, sets VALGRIND_QEMU environment '
62                        'variable')
63
64    p.add_argument('-misalign', action='store_true',
65                   help='misalign memory allocations')
66    p.add_argument('--color', choices=['on', 'off', 'auto'],
67                   default='auto', help="use terminal colors. The default "
68                   "'auto' value means use colors if terminal stdout detected")
69    p.add_argument('-tap', action='store_true',
70                   help='produce TAP output')
71
72    g_env = p.add_argument_group('test environment options')
73    mg = g_env.add_mutually_exclusive_group()
74    # We don't set default for cachemode, as we need to distinguish default
75    # from user input later.
76    mg.add_argument('-nocache', dest='cachemode', action='store_const',
77                    const='none', help='set cache mode "none" (O_DIRECT), '
78                    'sets CACHEMODE environment variable')
79    mg.add_argument('-c', dest='cachemode',
80                    help='sets CACHEMODE environment variable')
81
82    g_env.add_argument('-i', dest='aiomode', default='threads',
83                       help='sets AIOMODE environment variable')
84
85    p.set_defaults(imgfmt='raw', imgproto='file')
86
87    format_list = ['raw', 'bochs', 'cloop', 'parallels', 'qcow', 'qcow2',
88                   'qed', 'vdi', 'vpc', 'vhdx', 'vmdk', 'luks', 'dmg', 'vvfat']
89    g_fmt = p.add_argument_group(
90        '  image format options',
91        'The following options set the IMGFMT environment variable. '
92        'At most one choice is allowed, default is "raw"')
93    mg = g_fmt.add_mutually_exclusive_group()
94    for fmt in format_list:
95        mg.add_argument('-' + fmt, dest='imgfmt', action='store_const',
96                        const=fmt, help=f'test {fmt}')
97
98    protocol_list = ['file', 'rbd', 'nbd', 'ssh', 'nfs', 'fuse']
99    g_prt = p.add_argument_group(
100        '  image protocol options',
101        'The following options set the IMGPROTO environment variable. '
102        'At most one choice is allowed, default is "file"')
103    mg = g_prt.add_mutually_exclusive_group()
104    for prt in protocol_list:
105        mg.add_argument('-' + prt, dest='imgproto', action='store_const',
106                        const=prt, help=f'test {prt}')
107
108    g_bash = p.add_argument_group('bash tests options',
109                                  'The following options are ignored by '
110                                  'python tests.')
111    # TODO: make support for the following options in iotests.py
112    g_bash.add_argument('-o', dest='imgopts',
113                        help='options to pass to qemu-img create/convert, '
114                        'sets IMGOPTS environment variable')
115
116    g_sel = p.add_argument_group('test selecting options',
117                                 'The following options specify test set '
118                                 'to run.')
119    g_sel.add_argument('-g', '--groups', metavar='group1,...',
120                       help='include tests from these groups')
121    g_sel.add_argument('-x', '--exclude-groups', metavar='group1,...',
122                       help='exclude tests from these groups')
123    g_sel.add_argument('--start-from', metavar='TEST',
124                       help='Start from specified test: make sorted sequence '
125                       'of tests as usual and then drop tests from the first '
126                       'one to TEST (not inclusive). This may be used to '
127                       'rerun failed ./check command, starting from the '
128                       'middle of the process.')
129    g_sel.add_argument('tests', metavar='TEST_FILES', nargs='*',
130                       help='tests to run, or "--" followed by a command')
131    g_sel.add_argument('--build-dir', default=get_default_path(),
132                       help='Path to iotests build directory')
133    g_sel.add_argument('--source-dir',
134                       default=get_default_path(follow_link=True),
135                       help='Path to iotests build directory')
136
137    return p
138
139
140if __name__ == '__main__':
141    warnings.simplefilter("default")
142    os.environ["PYTHONWARNINGS"] = "default"
143
144    args = make_argparser().parse_args()
145
146    env = TestEnv(source_dir=args.source_dir,
147                  build_dir=args.build_dir,
148                  imgfmt=args.imgfmt, imgproto=args.imgproto,
149                  aiomode=args.aiomode, cachemode=args.cachemode,
150                  imgopts=args.imgopts, misalign=args.misalign,
151                  debug=args.debug, valgrind=args.valgrind,
152                  gdb=args.gdb, qprint=args.print,
153                  dry_run=args.dry_run)
154
155    if len(sys.argv) > 1 and sys.argv[-len(args.tests)-1] == '--':
156        if not args.tests:
157            sys.exit("missing command after '--'")
158        cmd = args.tests
159        env.print_env()
160        exec_pathstr = shutil.which(cmd[0])
161        if exec_pathstr is None:
162            sys.exit('command not found: ' + cmd[0])
163        exec_path = Path(exec_pathstr).resolve()
164        cmd[0] = str(exec_path)
165        full_env = env.prepare_subprocess(cmd)
166        os.chdir(exec_path.parent)
167        os.execve(cmd[0], cmd, full_env)
168
169    testfinder = TestFinder(test_dir=env.source_iotests)
170
171    groups = args.groups.split(',') if args.groups else None
172    x_groups = args.exclude_groups.split(',') if args.exclude_groups else None
173
174    group_local = os.path.join(env.source_iotests, 'group.local')
175    if os.path.isfile(group_local):
176        try:
177            testfinder.add_group_file(group_local)
178        except ValueError as e:
179            sys.exit(f"Failed to parse group file '{group_local}': {e}")
180
181    try:
182        tests = testfinder.find_tests(groups=groups, exclude_groups=x_groups,
183                                      tests=args.tests,
184                                      start_from=args.start_from)
185        if not tests:
186            raise ValueError('No tests selected')
187    except ValueError as e:
188        sys.exit(str(e))
189
190    if args.dry_run:
191        with env:
192            print('\n'.join([os.path.basename(t) for t in tests]))
193    else:
194        with TestRunner(env, tap=args.tap,
195                        color=args.color) as tr:
196            paths = [os.path.join(env.source_iotests, t) for t in tests]
197            ok = tr.run_tests(paths, args.jobs)
198            if not ok:
199                sys.exit(1)
200