xref: /openbmc/qemu/tests/qemu-iotests/check (revision 6d00c6f982562222adbd0613966285792125abe5)
1f203080bSVladimir Sementsov-Ogievskiy#!/usr/bin/env python3
26bf19c94SChristoph Hellwig#
3f203080bSVladimir Sementsov-Ogievskiy# Configure environment and run group of tests in it.
4f203080bSVladimir Sementsov-Ogievskiy#
5f203080bSVladimir Sementsov-Ogievskiy# Copyright (c) 2020-2021 Virtuozzo International GmbH
66bf19c94SChristoph Hellwig#
76bf19c94SChristoph Hellwig# This program is free software; you can redistribute it and/or
86bf19c94SChristoph Hellwig# modify it under the terms of the GNU General Public License as
96bf19c94SChristoph Hellwig# published by the Free Software Foundation.
106bf19c94SChristoph Hellwig#
116bf19c94SChristoph Hellwig# This program is distributed in the hope that it would be useful,
126bf19c94SChristoph Hellwig# but WITHOUT ANY WARRANTY; without even the implied warranty of
136bf19c94SChristoph Hellwig# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
146bf19c94SChristoph Hellwig# GNU General Public License for more details.
156bf19c94SChristoph Hellwig#
166bf19c94SChristoph Hellwig# You should have received a copy of the GNU General Public License
17e8c212d6SChristoph Hellwig# along with this program.  If not, see <http://www.gnu.org/licenses/>.
186bf19c94SChristoph Hellwig
19f203080bSVladimir Sementsov-Ogievskiyimport os
20f203080bSVladimir Sementsov-Ogievskiyimport sys
21f203080bSVladimir Sementsov-Ogievskiyimport argparse
22480b75eeSPaolo Bonziniimport shutil
23480b75eeSPaolo Bonzinifrom pathlib import Path
24480b75eeSPaolo Bonzini
25f203080bSVladimir Sementsov-Ogievskiyfrom findtests import TestFinder
26f203080bSVladimir Sementsov-Ogievskiyfrom testenv import TestEnv
27f203080bSVladimir Sementsov-Ogievskiyfrom testrunner import TestRunner
286bf19c94SChristoph Hellwig
290c8076b0SDaniel P. Berrangédef get_default_path(follow_link=False):
300c8076b0SDaniel P. Berrangé    """
310c8076b0SDaniel P. Berrangé    Try to automagically figure out the path we are running from.
320c8076b0SDaniel P. Berrangé    """
330c8076b0SDaniel P. Berrangé    # called from the build tree?
340c8076b0SDaniel P. Berrangé    if os.path.islink(sys.argv[0]):
350c8076b0SDaniel P. Berrangé        if follow_link:
360c8076b0SDaniel P. Berrangé            return os.path.dirname(os.readlink(sys.argv[0]))
370c8076b0SDaniel P. Berrangé        else:
380c8076b0SDaniel P. Berrangé            return os.path.dirname(os.path.abspath(sys.argv[0]))
390c8076b0SDaniel P. Berrangé    else:  # or source tree?
400c8076b0SDaniel P. Berrangé        return os.getcwd()
41e8f8624dSMax Reitz
42f203080bSVladimir Sementsov-Ogievskiydef make_argparser() -> argparse.ArgumentParser:
430c8076b0SDaniel P. Berrangé    p = argparse.ArgumentParser(
440c8076b0SDaniel P. Berrangé        description="Test run options",
450c8076b0SDaniel P. Berrangé        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
46e8f8624dSMax Reitz
47f203080bSVladimir Sementsov-Ogievskiy    p.add_argument('-n', '--dry-run', action='store_true',
48f203080bSVladimir Sementsov-Ogievskiy                   help='show me, do not run tests')
49722f87dfSVladimir Sementsov-Ogievskiy    p.add_argument('-j', dest='jobs', type=int, default=1,
50722f87dfSVladimir Sementsov-Ogievskiy                   help='run tests in multiple parallel jobs')
51e8f8624dSMax Reitz
52f203080bSVladimir Sementsov-Ogievskiy    p.add_argument('-d', dest='debug', action='store_true', help='debug')
53eb7a91d0SEmanuele Giuseppe Esposito    p.add_argument('-p', dest='print', action='store_true',
5487e4d4a2SEmanuele Giuseppe Esposito                   help='redirects qemu\'s stdout and stderr to '
5587e4d4a2SEmanuele Giuseppe Esposito                        'the test output')
56cfb9b0b7SEmanuele Giuseppe Esposito    p.add_argument('-gdb', action='store_true',
5787e4d4a2SEmanuele Giuseppe Esposito                   help="start gdbserver with $GDB_OPTIONS options "
5887e4d4a2SEmanuele Giuseppe Esposito                        "('localhost:12345' if $GDB_OPTIONS is empty)")
59a9b4c6bbSEmanuele Giuseppe Esposito    p.add_argument('-valgrind', action='store_true',
60a9b4c6bbSEmanuele Giuseppe Esposito                   help='use valgrind, sets VALGRIND_QEMU environment '
61a9b4c6bbSEmanuele Giuseppe Esposito                        'variable')
62a9b4c6bbSEmanuele Giuseppe Esposito
63f203080bSVladimir Sementsov-Ogievskiy    p.add_argument('-misalign', action='store_true',
64f203080bSVladimir Sementsov-Ogievskiy                   help='misalign memory allocations')
65f203080bSVladimir Sementsov-Ogievskiy    p.add_argument('--color', choices=['on', 'off', 'auto'],
66f203080bSVladimir Sementsov-Ogievskiy                   default='auto', help="use terminal colors. The default "
67f203080bSVladimir Sementsov-Ogievskiy                   "'auto' value means use colors if terminal stdout detected")
68d316859fSPaolo Bonzini    p.add_argument('-tap', action='store_true',
69d316859fSPaolo Bonzini                   help='produce TAP output')
707fed1a49SMax Reitz
71f203080bSVladimir Sementsov-Ogievskiy    g_env = p.add_argument_group('test environment options')
72f203080bSVladimir Sementsov-Ogievskiy    mg = g_env.add_mutually_exclusive_group()
73f203080bSVladimir Sementsov-Ogievskiy    # We don't set default for cachemode, as we need to distinguish default
74f203080bSVladimir Sementsov-Ogievskiy    # from user input later.
75f203080bSVladimir Sementsov-Ogievskiy    mg.add_argument('-nocache', dest='cachemode', action='store_const',
76f203080bSVladimir Sementsov-Ogievskiy                    const='none', help='set cache mode "none" (O_DIRECT), '
77f203080bSVladimir Sementsov-Ogievskiy                    'sets CACHEMODE environment variable')
78f203080bSVladimir Sementsov-Ogievskiy    mg.add_argument('-c', dest='cachemode',
79f203080bSVladimir Sementsov-Ogievskiy                    help='sets CACHEMODE environment variable')
806bf19c94SChristoph Hellwig
81f203080bSVladimir Sementsov-Ogievskiy    g_env.add_argument('-i', dest='aiomode', default='threads',
82f203080bSVladimir Sementsov-Ogievskiy                       help='sets AIOMODE environment variable')
8309d653e6SPaolo Bonzini
84f203080bSVladimir Sementsov-Ogievskiy    p.set_defaults(imgfmt='raw', imgproto='file')
8509d653e6SPaolo Bonzini
86f203080bSVladimir Sementsov-Ogievskiy    format_list = ['raw', 'bochs', 'cloop', 'parallels', 'qcow', 'qcow2',
87*c8f60bfbSAmjad Alsharafi                   'qed', 'vdi', 'vpc', 'vhdx', 'vmdk', 'luks', 'dmg', 'vvfat']
88f203080bSVladimir Sementsov-Ogievskiy    g_fmt = p.add_argument_group(
89f203080bSVladimir Sementsov-Ogievskiy        '  image format options',
90f203080bSVladimir Sementsov-Ogievskiy        'The following options set the IMGFMT environment variable. '
91f203080bSVladimir Sementsov-Ogievskiy        'At most one choice is allowed, default is "raw"')
92f203080bSVladimir Sementsov-Ogievskiy    mg = g_fmt.add_mutually_exclusive_group()
93f203080bSVladimir Sementsov-Ogievskiy    for fmt in format_list:
94f203080bSVladimir Sementsov-Ogievskiy        mg.add_argument('-' + fmt, dest='imgfmt', action='store_const',
95f203080bSVladimir Sementsov-Ogievskiy                        const=fmt, help=f'test {fmt}')
9670ff5b07SAlex Bennée
9709ec8517SMarkus Armbruster    protocol_list = ['file', 'rbd', 'nbd', 'ssh', 'nfs', 'fuse']
98f203080bSVladimir Sementsov-Ogievskiy    g_prt = p.add_argument_group(
99f203080bSVladimir Sementsov-Ogievskiy        '  image protocol options',
100f203080bSVladimir Sementsov-Ogievskiy        'The following options set the IMGPROTO environment variable. '
101f203080bSVladimir Sementsov-Ogievskiy        'At most one choice is allowed, default is "file"')
102f203080bSVladimir Sementsov-Ogievskiy    mg = g_prt.add_mutually_exclusive_group()
103f203080bSVladimir Sementsov-Ogievskiy    for prt in protocol_list:
104f203080bSVladimir Sementsov-Ogievskiy        mg.add_argument('-' + prt, dest='imgproto', action='store_const',
105f203080bSVladimir Sementsov-Ogievskiy                        const=prt, help=f'test {prt}')
10670ff5b07SAlex Bennée
107f203080bSVladimir Sementsov-Ogievskiy    g_bash = p.add_argument_group('bash tests options',
108f203080bSVladimir Sementsov-Ogievskiy                                  'The following options are ignored by '
109f203080bSVladimir Sementsov-Ogievskiy                                  'python tests.')
110f203080bSVladimir Sementsov-Ogievskiy    # TODO: make support for the following options in iotests.py
111f203080bSVladimir Sementsov-Ogievskiy    g_bash.add_argument('-o', dest='imgopts',
112f203080bSVladimir Sementsov-Ogievskiy                        help='options to pass to qemu-img create/convert, '
113f203080bSVladimir Sementsov-Ogievskiy                        'sets IMGOPTS environment variable')
11409d653e6SPaolo Bonzini
115f203080bSVladimir Sementsov-Ogievskiy    g_sel = p.add_argument_group('test selecting options',
116f203080bSVladimir Sementsov-Ogievskiy                                 'The following options specify test set '
117f203080bSVladimir Sementsov-Ogievskiy                                 'to run.')
118f203080bSVladimir Sementsov-Ogievskiy    g_sel.add_argument('-g', '--groups', metavar='group1,...',
119f203080bSVladimir Sementsov-Ogievskiy                       help='include tests from these groups')
120f203080bSVladimir Sementsov-Ogievskiy    g_sel.add_argument('-x', '--exclude-groups', metavar='group1,...',
121f203080bSVladimir Sementsov-Ogievskiy                       help='exclude tests from these groups')
122f203080bSVladimir Sementsov-Ogievskiy    g_sel.add_argument('--start-from', metavar='TEST',
123f203080bSVladimir Sementsov-Ogievskiy                       help='Start from specified test: make sorted sequence '
124f203080bSVladimir Sementsov-Ogievskiy                       'of tests as usual and then drop tests from the first '
125f203080bSVladimir Sementsov-Ogievskiy                       'one to TEST (not inclusive). This may be used to '
126f203080bSVladimir Sementsov-Ogievskiy                       'rerun failed ./check command, starting from the '
127f203080bSVladimir Sementsov-Ogievskiy                       'middle of the process.')
128f203080bSVladimir Sementsov-Ogievskiy    g_sel.add_argument('tests', metavar='TEST_FILES', nargs='*',
129480b75eeSPaolo Bonzini                       help='tests to run, or "--" followed by a command')
1300c8076b0SDaniel P. Berrangé    g_sel.add_argument('--build-dir', default=get_default_path(),
1310c8076b0SDaniel P. Berrangé                       help='Path to iotests build directory')
1320c8076b0SDaniel P. Berrangé    g_sel.add_argument('--source-dir',
1330c8076b0SDaniel P. Berrangé                       default=get_default_path(follow_link=True),
1340c8076b0SDaniel P. Berrangé                       help='Path to iotests build directory')
13509d653e6SPaolo Bonzini
136f203080bSVladimir Sementsov-Ogievskiy    return p
13709d653e6SPaolo Bonzini
13809d653e6SPaolo Bonzini
139f203080bSVladimir Sementsov-Ogievskiyif __name__ == '__main__':
140f203080bSVladimir Sementsov-Ogievskiy    args = make_argparser().parse_args()
14109d653e6SPaolo Bonzini
1420c8076b0SDaniel P. Berrangé    env = TestEnv(source_dir=args.source_dir,
1430c8076b0SDaniel P. Berrangé                  build_dir=args.build_dir,
1440c8076b0SDaniel P. Berrangé                  imgfmt=args.imgfmt, imgproto=args.imgproto,
145f203080bSVladimir Sementsov-Ogievskiy                  aiomode=args.aiomode, cachemode=args.cachemode,
146f203080bSVladimir Sementsov-Ogievskiy                  imgopts=args.imgopts, misalign=args.misalign,
147cfb9b0b7SEmanuele Giuseppe Esposito                  debug=args.debug, valgrind=args.valgrind,
148a9e21786SDaniel P. Berrangé                  gdb=args.gdb, qprint=args.print,
149a9e21786SDaniel P. Berrangé                  dry_run=args.dry_run)
15009d653e6SPaolo Bonzini
151480b75eeSPaolo Bonzini    if len(sys.argv) > 1 and sys.argv[-len(args.tests)-1] == '--':
152480b75eeSPaolo Bonzini        if not args.tests:
153480b75eeSPaolo Bonzini            sys.exit("missing command after '--'")
154480b75eeSPaolo Bonzini        cmd = args.tests
155480b75eeSPaolo Bonzini        env.print_env()
156480b75eeSPaolo Bonzini        exec_pathstr = shutil.which(cmd[0])
157480b75eeSPaolo Bonzini        if exec_pathstr is None:
158480b75eeSPaolo Bonzini            sys.exit('command not found: ' + cmd[0])
159480b75eeSPaolo Bonzini        exec_path = Path(exec_pathstr).resolve()
160480b75eeSPaolo Bonzini        cmd[0] = str(exec_path)
161480b75eeSPaolo Bonzini        full_env = env.prepare_subprocess(cmd)
162480b75eeSPaolo Bonzini        os.chdir(exec_path.parent)
163480b75eeSPaolo Bonzini        os.execve(cmd[0], cmd, full_env)
164480b75eeSPaolo Bonzini
165f203080bSVladimir Sementsov-Ogievskiy    testfinder = TestFinder(test_dir=env.source_iotests)
1668803714bSEric Blake
167f203080bSVladimir Sementsov-Ogievskiy    groups = args.groups.split(',') if args.groups else None
168f203080bSVladimir Sementsov-Ogievskiy    x_groups = args.exclude_groups.split(',') if args.exclude_groups else None
16909d653e6SPaolo Bonzini
170f203080bSVladimir Sementsov-Ogievskiy    group_local = os.path.join(env.source_iotests, 'group.local')
171f203080bSVladimir Sementsov-Ogievskiy    if os.path.isfile(group_local):
172f203080bSVladimir Sementsov-Ogievskiy        try:
173f203080bSVladimir Sementsov-Ogievskiy            testfinder.add_group_file(group_local)
174f203080bSVladimir Sementsov-Ogievskiy        except ValueError as e:
175f203080bSVladimir Sementsov-Ogievskiy            sys.exit(f"Failed to parse group file '{group_local}': {e}")
17609d653e6SPaolo Bonzini
177f203080bSVladimir Sementsov-Ogievskiy    try:
178f203080bSVladimir Sementsov-Ogievskiy        tests = testfinder.find_tests(groups=groups, exclude_groups=x_groups,
179f203080bSVladimir Sementsov-Ogievskiy                                      tests=args.tests,
180f203080bSVladimir Sementsov-Ogievskiy                                      start_from=args.start_from)
181f203080bSVladimir Sementsov-Ogievskiy        if not tests:
182f203080bSVladimir Sementsov-Ogievskiy            raise ValueError('No tests selected')
183f203080bSVladimir Sementsov-Ogievskiy    except ValueError as e:
1845bcf18b0SJohn Snow        sys.exit(str(e))
18509d653e6SPaolo Bonzini
186f203080bSVladimir Sementsov-Ogievskiy    if args.dry_run:
187c645bac4SDaniel P. Berrangé        with env:
188663755b0SDaniel P. Berrangé            print('\n'.join([os.path.basename(t) for t in tests]))
189f203080bSVladimir Sementsov-Ogievskiy    else:
190d316859fSPaolo Bonzini        with TestRunner(env, tap=args.tap,
191f203080bSVladimir Sementsov-Ogievskiy                        color=args.color) as tr:
1923ae50942SVladimir Sementsov-Ogievskiy            paths = [os.path.join(env.source_iotests, t) for t in tests]
193722f87dfSVladimir Sementsov-Ogievskiy            ok = tr.run_tests(paths, args.jobs)
1943ae50942SVladimir Sementsov-Ogievskiy            if not ok:
1953ae50942SVladimir Sementsov-Ogievskiy                sys.exit(1)
196