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