xref: /openbmc/qemu/tests/qemu-iotests/check (revision 9b45a025)
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', 'sheepdog', 'nbd', 'ssh', 'nfs',
69                     'fuse']
70    g_prt = p.add_argument_group(
71        '  image protocol options',
72        'The following options set the IMGPROTO environment variable. '
73        'At most one choice is allowed, default is "file"')
74    mg = g_prt.add_mutually_exclusive_group()
75    for prt in protocol_list:
76        mg.add_argument('-' + prt, dest='imgproto', action='store_const',
77                        const=prt, help=f'test {prt}')
78
79    g_bash = p.add_argument_group('bash tests options',
80                                  'The following options are ignored by '
81                                  'python tests.')
82    # TODO: make support for the following options in iotests.py
83    g_bash.add_argument('-o', dest='imgopts',
84                        help='options to pass to qemu-img create/convert, '
85                        'sets IMGOPTS environment variable')
86    g_bash.add_argument('-valgrind', action='store_true',
87                        help='use valgrind, sets VALGRIND_QEMU environment '
88                        'variable')
89
90    g_sel = p.add_argument_group('test selecting options',
91                                 'The following options specify test set '
92                                 'to run.')
93    g_sel.add_argument('-g', '--groups', metavar='group1,...',
94                       help='include tests from these groups')
95    g_sel.add_argument('-x', '--exclude-groups', metavar='group1,...',
96                       help='exclude tests from these groups')
97    g_sel.add_argument('--start-from', metavar='TEST',
98                       help='Start from specified test: make sorted sequence '
99                       'of tests as usual and then drop tests from the first '
100                       'one to TEST (not inclusive). This may be used to '
101                       'rerun failed ./check command, starting from the '
102                       'middle of the process.')
103    g_sel.add_argument('tests', metavar='TEST_FILES', nargs='*',
104                       help='tests to run')
105
106    return p
107
108
109if __name__ == '__main__':
110    args = make_argparser().parse_args()
111
112    env = TestEnv(imgfmt=args.imgfmt, imgproto=args.imgproto,
113                  aiomode=args.aiomode, cachemode=args.cachemode,
114                  imgopts=args.imgopts, misalign=args.misalign,
115                  debug=args.debug, valgrind=args.valgrind)
116
117    testfinder = TestFinder(test_dir=env.source_iotests)
118
119    groups = args.groups.split(',') if args.groups else None
120    x_groups = args.exclude_groups.split(',') if args.exclude_groups else None
121
122    group_local = os.path.join(env.source_iotests, 'group.local')
123    if os.path.isfile(group_local):
124        try:
125            testfinder.add_group_file(group_local)
126        except ValueError as e:
127            sys.exit(f"Failed to parse group file '{group_local}': {e}")
128
129    try:
130        tests = testfinder.find_tests(groups=groups, exclude_groups=x_groups,
131                                      tests=args.tests,
132                                      start_from=args.start_from)
133        if not tests:
134            raise ValueError('No tests selected')
135    except ValueError as e:
136        sys.exit(e)
137
138    if args.dry_run:
139        print('\n'.join(tests))
140    else:
141        with TestRunner(env, makecheck=args.makecheck,
142                        color=args.color) as tr:
143            paths = [os.path.join(env.source_iotests, t) for t in tests]
144            ok = tr.run_tests(paths)
145            if not ok:
146                sys.exit(1)
147