blob: a288c265ae1410090fb7e82b8ebe9b02b6a46e47 [file] [log] [blame]
James E. King III9bea32f2018-03-16 16:07:42 -04001#!/usr/bin/env python3
Roger Meier40cc2322014-06-11 11:09:14 +02002#
3# Licensed to the Apache Software Foundation (ASF) under one
4# or more contributor license agreements. See the NOTICE file
5# distributed with this work for additional information
6# regarding copyright ownership. The ASF licenses this file
7# to you under the Apache License, Version 2.0 (the
8# "License"); you may not use this file except in compliance
9# with the License. You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing,
14# software distributed under the License is distributed on an
15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16# KIND, either express or implied. See the License for the
17# specific language governing permissions and limitations
18# under the License.
19#
20
James E. King III9bea32f2018-03-16 16:07:42 -040021#
22# Apache Thrift - integration (cross) test suite
Roger Meier41ad4342015-03-24 22:30:40 +010023#
24# tests different server-client, protocol and transport combinations
25#
James E. King III9bea32f2018-03-16 16:07:42 -040026# This script requires python 3.x due to the improvements in
27# subprocess management that are needed for reliability.
Roger Meier41ad4342015-03-24 22:30:40 +010028#
Roger Meier41ad4342015-03-24 22:30:40 +010029
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090030from itertools import chain
Roger Meier40cc2322014-06-11 11:09:14 +020031import json
Roger Meier41ad4342015-03-24 22:30:40 +010032import logging
33import multiprocessing
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090034import argparse
Roger Meier41ad4342015-03-24 22:30:40 +010035import os
36import sys
Roger Meier40cc2322014-06-11 11:09:14 +020037
Roger Meier41ad4342015-03-24 22:30:40 +010038import crossrunner
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090039from crossrunner.compat import path_join
Roger Meier40cc2322014-06-11 11:09:14 +020040
James E. King III9bea32f2018-03-16 16:07:42 -040041# 3.3 introduced subprocess timeouts on waiting for child
42req_version = (3, 3)
43cur_version = sys.version_info
44assert (cur_version >= req_version), "Python 3.3 or later is required for proper operation."
45
46
Nobuaki Sukegawabd165302016-01-19 11:10:07 +090047ROOT_DIR = os.path.dirname(os.path.realpath(os.path.dirname(__file__)))
48TEST_DIR_RELATIVE = 'test'
49TEST_DIR = path_join(ROOT_DIR, TEST_DIR_RELATIVE)
50FEATURE_DIR_RELATIVE = path_join(TEST_DIR_RELATIVE, 'features')
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090051CONFIG_FILE = 'tests.json'
cdwijayarathnad1041652014-08-15 23:42:20 +053052
Roger Meier40cc2322014-06-11 11:09:14 +020053
James E. King III6f8c99e2018-03-24 16:32:02 -040054def run_cross_tests(server_match, client_match, jobs, skip_known_failures, only_known_failures, retry_count, regex):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090055 logger = multiprocessing.get_logger()
56 logger.debug('Collecting tests')
57 with open(path_join(TEST_DIR, CONFIG_FILE), 'r') as fp:
58 j = json.load(fp)
Nobuaki Sukegawa144bbef2016-02-11 13:15:40 +090059 tests = crossrunner.collect_cross_tests(j, server_match, client_match, regex)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090060 if not tests:
61 print('No test found that matches the criteria', file=sys.stderr)
62 print(' servers: %s' % server_match, file=sys.stderr)
63 print(' clients: %s' % client_match, file=sys.stderr)
64 return False
James E. King III6f8c99e2018-03-24 16:32:02 -040065 if only_known_failures:
66 logger.debug('Only running known failures')
67 known = crossrunner.load_known_failures(TEST_DIR)
68 tests = list(filter(lambda t: crossrunner.test_name(**t) in known, tests))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090069 if skip_known_failures:
70 logger.debug('Skipping known failures')
71 known = crossrunner.load_known_failures(TEST_DIR)
72 tests = list(filter(lambda t: crossrunner.test_name(**t) not in known, tests))
Roger Meier41ad4342015-03-24 22:30:40 +010073
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090074 dispatcher = crossrunner.TestDispatcher(TEST_DIR, ROOT_DIR, TEST_DIR_RELATIVE, jobs)
75 logger.debug('Executing %d tests' % len(tests))
76 try:
77 for r in [dispatcher.dispatch(test, retry_count) for test in tests]:
78 r.wait()
79 logger.debug('Waiting for completion')
80 return dispatcher.wait()
81 except (KeyboardInterrupt, SystemExit):
82 logger.debug('Interrupted, shutting down')
83 dispatcher.terminate()
84 return False
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090085
86
James E. King III6f8c99e2018-03-24 16:32:02 -040087def run_feature_tests(server_match, feature_match, jobs, skip_known_failures, only_known_failures, retry_count, regex):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090088 basedir = path_join(ROOT_DIR, FEATURE_DIR_RELATIVE)
89 logger = multiprocessing.get_logger()
90 logger.debug('Collecting tests')
91 with open(path_join(TEST_DIR, CONFIG_FILE), 'r') as fp:
92 j = json.load(fp)
93 with open(path_join(basedir, CONFIG_FILE), 'r') as fp:
94 j2 = json.load(fp)
Nobuaki Sukegawa144bbef2016-02-11 13:15:40 +090095 tests = crossrunner.collect_feature_tests(j, j2, server_match, feature_match, regex)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090096 if not tests:
97 print('No test found that matches the criteria', file=sys.stderr)
98 print(' servers: %s' % server_match, file=sys.stderr)
99 print(' features: %s' % feature_match, file=sys.stderr)
100 return False
James E. King III6f8c99e2018-03-24 16:32:02 -0400101 if only_known_failures:
102 logger.debug('Only running known failures')
103 known = crossrunner.load_known_failures(basedir)
104 tests = list(filter(lambda t: crossrunner.test_name(**t) in known, tests))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900105 if skip_known_failures:
106 logger.debug('Skipping known failures')
107 known = crossrunner.load_known_failures(basedir)
108 tests = list(filter(lambda t: crossrunner.test_name(**t) not in known, tests))
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +0900109
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900110 dispatcher = crossrunner.TestDispatcher(TEST_DIR, ROOT_DIR, FEATURE_DIR_RELATIVE, jobs)
111 logger.debug('Executing %d tests' % len(tests))
112 try:
113 for r in [dispatcher.dispatch(test, retry_count) for test in tests]:
114 r.wait()
115 logger.debug('Waiting for completion')
116 return dispatcher.wait()
117 except (KeyboardInterrupt, SystemExit):
118 logger.debug('Interrupted, shutting down')
119 dispatcher.terminate()
120 return False
Roger Meier41ad4342015-03-24 22:30:40 +0100121
122
Jiayu Liuab83ffc2022-05-10 01:56:30 +0800123def default_concurrency():
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900124 try:
125 return int(os.environ.get('THRIFT_CROSSTEST_CONCURRENCY'))
126 except (TypeError, ValueError):
127 # Since much time is spent sleeping, use many threads
128 return int(multiprocessing.cpu_count() * 1.25) + 1
Roger Meier41ad4342015-03-24 22:30:40 +0100129
130
131def main(argv):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900132 parser = argparse.ArgumentParser()
133 parser.add_argument('--server', default='', nargs='*',
134 help='list of servers to test')
135 parser.add_argument('--client', default='', nargs='*',
136 help='list of clients to test')
137 parser.add_argument('-F', '--features', nargs='*', default=None,
138 help='run server feature tests instead of cross language tests')
Nobuaki Sukegawa144bbef2016-02-11 13:15:40 +0900139 parser.add_argument('-R', '--regex', help='test name pattern to run')
James E. King III6f8c99e2018-03-24 16:32:02 -0400140 parser.add_argument('-o', '--only-known_failures', action='store_true', dest='only_known_failures',
141 help='only execute tests that are known to fail')
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900142 parser.add_argument('-s', '--skip-known-failures', action='store_true', dest='skip_known_failures',
143 help='do not execute tests that are known to fail')
144 parser.add_argument('-r', '--retry-count', type=int,
145 default=0, help='maximum retry on failure')
146 parser.add_argument('-j', '--jobs', type=int,
Jiayu Liuab83ffc2022-05-10 01:56:30 +0800147 default=default_concurrency(),
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900148 help='number of concurrent test executions')
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +0900149
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900150 g = parser.add_argument_group(title='Advanced')
151 g.add_argument('-v', '--verbose', action='store_const',
152 dest='log_level', const=logging.DEBUG, default=logging.WARNING,
153 help='show debug output for test runner')
154 g.add_argument('-P', '--print-expected-failures', choices=['merge', 'overwrite'],
155 dest='print_failures',
156 help="generate expected failures based on last result and print to stdout")
157 g.add_argument('-U', '--update-expected-failures', choices=['merge', 'overwrite'],
158 dest='update_failures',
159 help="generate expected failures based on last result and save to default file location")
160 options = parser.parse_args(argv)
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +0900161
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900162 logger = multiprocessing.log_to_stderr()
163 logger.setLevel(options.log_level)
Roger Meier41ad4342015-03-24 22:30:40 +0100164
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900165 if options.features is not None and options.client:
166 print('Cannot specify both --features and --client ', file=sys.stderr)
167 return 1
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900168
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900169 # Allow multiple args separated with ',' for backward compatibility
170 server_match = list(chain(*[x.split(',') for x in options.server]))
171 client_match = list(chain(*[x.split(',') for x in options.client]))
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +0900172
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900173 if options.update_failures or options.print_failures:
174 dire = path_join(ROOT_DIR, FEATURE_DIR_RELATIVE) if options.features is not None else TEST_DIR
175 res = crossrunner.generate_known_failures(
176 dire, options.update_failures == 'overwrite',
177 options.update_failures, options.print_failures)
178 elif options.features is not None:
179 features = options.features or ['.*']
James E. King III9bea32f2018-03-16 16:07:42 -0400180 res = run_feature_tests(server_match, features, options.jobs,
James E. King III6f8c99e2018-03-24 16:32:02 -0400181 options.skip_known_failures, options.only_known_failures,
182 options.retry_count, options.regex)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900183 else:
James E. King III9bea32f2018-03-16 16:07:42 -0400184 res = run_cross_tests(server_match, client_match, options.jobs,
James E. King III6f8c99e2018-03-24 16:32:02 -0400185 options.skip_known_failures, options.only_known_failures,
186 options.retry_count, options.regex)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900187 return 0 if res else 1
Roger Meiere8bafb62014-08-01 23:39:32 +0200188
James E. King, III0ad20bd2017-09-30 15:44:16 -0700189
Roger Meier41ad4342015-03-24 22:30:40 +0100190if __name__ == '__main__':
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900191 sys.exit(main(sys.argv[1:]))