blob: e03cb45d954b5184f5dba9473be627c62f28beee [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
Roger Meier40cc2322014-06-11 11:09:14 +020039
James E. King III9bea32f2018-03-16 16:07:42 -040040# 3.3 introduced subprocess timeouts on waiting for child
41req_version = (3, 3)
42cur_version = sys.version_info
43assert (cur_version >= req_version), "Python 3.3 or later is required for proper operation."
44
45
Nobuaki Sukegawabd165302016-01-19 11:10:07 +090046ROOT_DIR = os.path.dirname(os.path.realpath(os.path.dirname(__file__)))
47TEST_DIR_RELATIVE = 'test'
Yuxuan 'fishy' Wang716835f2025-05-28 15:44:53 -070048TEST_DIR = os.path.join(ROOT_DIR, TEST_DIR_RELATIVE)
49FEATURE_DIR_RELATIVE = os.path.join(TEST_DIR_RELATIVE, 'features')
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090050CONFIG_FILE = 'tests.json'
cdwijayarathnad1041652014-08-15 23:42:20 +053051
Roger Meier40cc2322014-06-11 11:09:14 +020052
James E. King III6f8c99e2018-03-24 16:32:02 -040053def run_cross_tests(server_match, client_match, jobs, skip_known_failures, only_known_failures, retry_count, regex):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090054 logger = multiprocessing.get_logger()
55 logger.debug('Collecting tests')
Yuxuan 'fishy' Wang716835f2025-05-28 15:44:53 -070056 with open(os.path.join(TEST_DIR, CONFIG_FILE), 'r') as fp:
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090057 j = json.load(fp)
Nobuaki Sukegawa144bbef2016-02-11 13:15:40 +090058 tests = crossrunner.collect_cross_tests(j, server_match, client_match, regex)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090059 if not tests:
60 print('No test found that matches the criteria', file=sys.stderr)
61 print(' servers: %s' % server_match, file=sys.stderr)
62 print(' clients: %s' % client_match, file=sys.stderr)
63 return False
James E. King III6f8c99e2018-03-24 16:32:02 -040064 if only_known_failures:
65 logger.debug('Only running known failures')
66 known = crossrunner.load_known_failures(TEST_DIR)
67 tests = list(filter(lambda t: crossrunner.test_name(**t) in known, tests))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090068 if skip_known_failures:
69 logger.debug('Skipping known failures')
70 known = crossrunner.load_known_failures(TEST_DIR)
71 tests = list(filter(lambda t: crossrunner.test_name(**t) not in known, tests))
Roger Meier41ad4342015-03-24 22:30:40 +010072
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090073 dispatcher = crossrunner.TestDispatcher(TEST_DIR, ROOT_DIR, TEST_DIR_RELATIVE, jobs)
74 logger.debug('Executing %d tests' % len(tests))
75 try:
76 for r in [dispatcher.dispatch(test, retry_count) for test in tests]:
77 r.wait()
78 logger.debug('Waiting for completion')
79 return dispatcher.wait()
80 except (KeyboardInterrupt, SystemExit):
81 logger.debug('Interrupted, shutting down')
82 dispatcher.terminate()
83 return False
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090084
85
James E. King III6f8c99e2018-03-24 16:32:02 -040086def run_feature_tests(server_match, feature_match, jobs, skip_known_failures, only_known_failures, retry_count, regex):
Yuxuan 'fishy' Wang716835f2025-05-28 15:44:53 -070087 basedir = os.path.join(ROOT_DIR, FEATURE_DIR_RELATIVE)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090088 logger = multiprocessing.get_logger()
89 logger.debug('Collecting tests')
Yuxuan 'fishy' Wang716835f2025-05-28 15:44:53 -070090 with open(os.path.join(TEST_DIR, CONFIG_FILE), 'r') as fp:
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090091 j = json.load(fp)
Yuxuan 'fishy' Wang716835f2025-05-28 15:44:53 -070092 with open(os.path.join(basedir, CONFIG_FILE), 'r') as fp:
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090093 j2 = json.load(fp)
Nobuaki Sukegawa144bbef2016-02-11 13:15:40 +090094 tests = crossrunner.collect_feature_tests(j, j2, server_match, feature_match, regex)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090095 if not tests:
96 print('No test found that matches the criteria', file=sys.stderr)
97 print(' servers: %s' % server_match, file=sys.stderr)
98 print(' features: %s' % feature_match, file=sys.stderr)
99 return False
James E. King III6f8c99e2018-03-24 16:32:02 -0400100 if only_known_failures:
101 logger.debug('Only running known failures')
102 known = crossrunner.load_known_failures(basedir)
103 tests = list(filter(lambda t: crossrunner.test_name(**t) in known, tests))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900104 if skip_known_failures:
105 logger.debug('Skipping known failures')
106 known = crossrunner.load_known_failures(basedir)
107 tests = list(filter(lambda t: crossrunner.test_name(**t) not in known, tests))
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +0900108
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900109 dispatcher = crossrunner.TestDispatcher(TEST_DIR, ROOT_DIR, FEATURE_DIR_RELATIVE, jobs)
110 logger.debug('Executing %d tests' % len(tests))
111 try:
112 for r in [dispatcher.dispatch(test, retry_count) for test in tests]:
113 r.wait()
114 logger.debug('Waiting for completion')
115 return dispatcher.wait()
116 except (KeyboardInterrupt, SystemExit):
117 logger.debug('Interrupted, shutting down')
118 dispatcher.terminate()
119 return False
Roger Meier41ad4342015-03-24 22:30:40 +0100120
121
Jiayu Liuab83ffc2022-05-10 01:56:30 +0800122def default_concurrency():
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900123 try:
124 return int(os.environ.get('THRIFT_CROSSTEST_CONCURRENCY'))
125 except (TypeError, ValueError):
126 # Since much time is spent sleeping, use many threads
127 return int(multiprocessing.cpu_count() * 1.25) + 1
Roger Meier41ad4342015-03-24 22:30:40 +0100128
129
130def main(argv):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900131 parser = argparse.ArgumentParser()
132 parser.add_argument('--server', default='', nargs='*',
133 help='list of servers to test')
134 parser.add_argument('--client', default='', nargs='*',
135 help='list of clients to test')
136 parser.add_argument('-F', '--features', nargs='*', default=None,
137 help='run server feature tests instead of cross language tests')
Nobuaki Sukegawa144bbef2016-02-11 13:15:40 +0900138 parser.add_argument('-R', '--regex', help='test name pattern to run')
James E. King III6f8c99e2018-03-24 16:32:02 -0400139 parser.add_argument('-o', '--only-known_failures', action='store_true', dest='only_known_failures',
140 help='only execute tests that are known to fail')
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900141 parser.add_argument('-s', '--skip-known-failures', action='store_true', dest='skip_known_failures',
142 help='do not execute tests that are known to fail')
143 parser.add_argument('-r', '--retry-count', type=int,
144 default=0, help='maximum retry on failure')
145 parser.add_argument('-j', '--jobs', type=int,
Jiayu Liuab83ffc2022-05-10 01:56:30 +0800146 default=default_concurrency(),
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900147 help='number of concurrent test executions')
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +0900148
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900149 g = parser.add_argument_group(title='Advanced')
150 g.add_argument('-v', '--verbose', action='store_const',
151 dest='log_level', const=logging.DEBUG, default=logging.WARNING,
152 help='show debug output for test runner')
153 g.add_argument('-P', '--print-expected-failures', choices=['merge', 'overwrite'],
154 dest='print_failures',
155 help="generate expected failures based on last result and print to stdout")
156 g.add_argument('-U', '--update-expected-failures', choices=['merge', 'overwrite'],
157 dest='update_failures',
158 help="generate expected failures based on last result and save to default file location")
159 options = parser.parse_args(argv)
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +0900160
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900161 logger = multiprocessing.log_to_stderr()
162 logger.setLevel(options.log_level)
Roger Meier41ad4342015-03-24 22:30:40 +0100163
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900164 if options.features is not None and options.client:
165 print('Cannot specify both --features and --client ', file=sys.stderr)
166 return 1
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900167
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900168 # Allow multiple args separated with ',' for backward compatibility
169 server_match = list(chain(*[x.split(',') for x in options.server]))
170 client_match = list(chain(*[x.split(',') for x in options.client]))
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +0900171
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900172 if options.update_failures or options.print_failures:
Yuxuan 'fishy' Wang716835f2025-05-28 15:44:53 -0700173 dire = os.path.join(ROOT_DIR, FEATURE_DIR_RELATIVE) if options.features is not None else TEST_DIR
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900174 res = crossrunner.generate_known_failures(
175 dire, options.update_failures == 'overwrite',
176 options.update_failures, options.print_failures)
177 elif options.features is not None:
178 features = options.features or ['.*']
James E. King III9bea32f2018-03-16 16:07:42 -0400179 res = run_feature_tests(server_match, features, options.jobs,
James E. King III6f8c99e2018-03-24 16:32:02 -0400180 options.skip_known_failures, options.only_known_failures,
181 options.retry_count, options.regex)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900182 else:
James E. King III9bea32f2018-03-16 16:07:42 -0400183 res = run_cross_tests(server_match, client_match, options.jobs,
James E. King III6f8c99e2018-03-24 16:32:02 -0400184 options.skip_known_failures, options.only_known_failures,
185 options.retry_count, options.regex)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900186 return 0 if res else 1
Roger Meiere8bafb62014-08-01 23:39:32 +0200187
James E. King, III0ad20bd2017-09-30 15:44:16 -0700188
Roger Meier41ad4342015-03-24 22:30:40 +0100189if __name__ == '__main__':
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900190 sys.exit(main(sys.argv[1:]))