blob: 20d76f46f78b37ed3fdf380b13993d98254c0084 [file] [log] [blame]
Roger Meier40cc2322014-06-11 11:09:14 +02001#!/usr/bin/env python
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
Roger Meier41ad4342015-03-24 22:30:40 +010021# Apache Thrift - integration test suite
22#
23# tests different server-client, protocol and transport combinations
24#
25# This script supports python 2.7 and later.
26# python 3.x is recommended for better stability.
27#
Roger Meier41ad4342015-03-24 22:30:40 +010028
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090029from __future__ import print_function
30from 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
Roger Meier41ad4342015-03-24 22:30:40 +010041TEST_DIR = os.path.realpath(os.path.dirname(__file__))
Nobuaki Sukegawa85650612016-01-08 03:26:44 +090042FEATURE_DIR = path_join(TEST_DIR, 'features')
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090043CONFIG_FILE = 'tests.json'
cdwijayarathnad1041652014-08-15 23:42:20 +053044
Roger Meier40cc2322014-06-11 11:09:14 +020045
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090046def run_tests(collect_func, basedir, server_match, client_match, jobs, skip):
Roger Meier41ad4342015-03-24 22:30:40 +010047 logger = multiprocessing.get_logger()
48 logger.debug('Collecting tests')
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090049 with open(path_join(basedir, CONFIG_FILE), 'r') as fp:
Roger Meier41ad4342015-03-24 22:30:40 +010050 j = json.load(fp)
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090051 tests = collect_func(j, server_match, client_match)
52 if not tests:
53 print('No test found that matches the criteria', file=sys.stderr)
54 # print(' servers: %s' % server_match, file=sys.stderr)
55 # print(' clients: %s' % client_match, file=sys.stderr)
56 return False
57 if skip:
58 logger.debug('Skipping known failures')
59 known = crossrunner.load_known_failures(basedir)
60 tests = list(filter(lambda t: crossrunner.test_name(**t) not in known, tests))
61
62 dispatcher = crossrunner.TestDispatcher(TEST_DIR, basedir, jobs)
63 logger.debug('Executing %d tests' % len(tests))
64 try:
65 for r in [dispatcher.dispatch(test) for test in tests]:
66 r.wait()
67 logger.debug('Waiting for completion')
68 return dispatcher.wait()
69 except (KeyboardInterrupt, SystemExit):
70 logger.debug('Interrupted, shutting down')
71 dispatcher.terminate()
72 return False
73
74
75def run_cross_tests(server_match, client_match, jobs, skip_known_failures):
76 logger = multiprocessing.get_logger()
77 logger.debug('Collecting tests')
78 with open(path_join(TEST_DIR, CONFIG_FILE), 'r') as fp:
79 j = json.load(fp)
80 tests = crossrunner.collect_cross_tests(j, server_match, client_match)
81 if not tests:
82 print('No test found that matches the criteria', file=sys.stderr)
83 print(' servers: %s' % server_match, file=sys.stderr)
84 print(' clients: %s' % client_match, file=sys.stderr)
85 return False
Roger Meier41ad4342015-03-24 22:30:40 +010086 if skip_known_failures:
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090087 logger.debug('Skipping known failures')
Roger Meier41ad4342015-03-24 22:30:40 +010088 known = crossrunner.load_known_failures(TEST_DIR)
89 tests = list(filter(lambda t: crossrunner.test_name(**t) not in known, tests))
90
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090091 dispatcher = crossrunner.TestDispatcher(TEST_DIR, TEST_DIR, jobs)
92 logger.debug('Executing %d tests' % len(tests))
93 try:
94 for r in [dispatcher.dispatch(test) for test in tests]:
95 r.wait()
96 logger.debug('Waiting for completion')
97 return dispatcher.wait()
98 except (KeyboardInterrupt, SystemExit):
99 logger.debug('Interrupted, shutting down')
100 dispatcher.terminate()
101 return False
102
103
104def run_feature_tests(server_match, feature_match, jobs, skip_known_failures):
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900105 basedir = FEATURE_DIR
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +0900106 logger = multiprocessing.get_logger()
107 logger.debug('Collecting tests')
108 with open(path_join(TEST_DIR, CONFIG_FILE), 'r') as fp:
109 j = json.load(fp)
110 with open(path_join(basedir, CONFIG_FILE), 'r') as fp:
111 j2 = json.load(fp)
112 tests = crossrunner.collect_feature_tests(j, j2, server_match, feature_match)
113 if not tests:
114 print('No test found that matches the criteria', file=sys.stderr)
115 print(' servers: %s' % server_match, file=sys.stderr)
116 print(' features: %s' % feature_match, file=sys.stderr)
117 return False
118 if skip_known_failures:
119 logger.debug('Skipping known failures')
120 known = crossrunner.load_known_failures(basedir)
121 tests = list(filter(lambda t: crossrunner.test_name(**t) not in known, tests))
122
123 dispatcher = crossrunner.TestDispatcher(TEST_DIR, basedir, jobs)
Roger Meier41ad4342015-03-24 22:30:40 +0100124 logger.debug('Executing %d tests' % len(tests))
125 try:
126 for r in [dispatcher.dispatch(test) for test in tests]:
127 r.wait()
128 logger.debug('Waiting for completion')
129 return dispatcher.wait()
130 except (KeyboardInterrupt, SystemExit):
131 logger.debug('Interrupted, shutting down')
132 dispatcher.terminate()
133 return False
134
135
136def default_concurrenty():
137 try:
138 return int(os.environ.get('THRIFT_CROSSTEST_CONCURRENCY'))
139 except (TypeError, ValueError):
140 # Since much time is spent sleeping, use many threads
141 return int(multiprocessing.cpu_count() * 1.25) + 1
142
143
144def main(argv):
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +0900145 parser = argparse.ArgumentParser()
146 parser.add_argument('--server', default='', nargs='*',
147 help='list of servers to test')
148 parser.add_argument('--client', default='', nargs='*',
149 help='list of clients to test')
150 parser.add_argument('-s', '--skip-known-failures', action='store_true', dest='skip_known_failures',
151 help='do not execute tests that are known to fail')
152 parser.add_argument('-j', '--jobs', type=int,
153 default=default_concurrenty(),
154 help='number of concurrent test executions')
155 parser.add_argument('-F', '--features', nargs='*', default=None,
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900156 help='run server feature tests instead of cross language tests')
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +0900157
158 g = parser.add_argument_group(title='Advanced')
159 g.add_argument('-v', '--verbose', action='store_const',
160 dest='log_level', const=logging.DEBUG, default=logging.WARNING,
161 help='show debug output for test runner')
162 g.add_argument('-P', '--print-expected-failures', choices=['merge', 'overwrite'],
163 dest='print_failures',
164 help="generate expected failures based on last result and print to stdout")
165 g.add_argument('-U', '--update-expected-failures', choices=['merge', 'overwrite'],
166 dest='update_failures',
167 help="generate expected failures based on last result and save to default file location")
168 options = parser.parse_args(argv)
169
Roger Meier41ad4342015-03-24 22:30:40 +0100170 logger = multiprocessing.log_to_stderr()
Roger Meier41ad4342015-03-24 22:30:40 +0100171 logger.setLevel(options.log_level)
172
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900173 if options.features is not None and options.client:
174 print('Cannot specify both --features and --client ', file=sys.stderr)
175 return 1
176
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +0900177 # Allow multiple args separated with ',' for backward compatibility
178 server_match = list(chain(*[x.split(',') for x in options.server]))
179 client_match = list(chain(*[x.split(',') for x in options.client]))
180
181 if options.update_failures or options.print_failures:
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900182 dire = FEATURE_DIR if options.features is not None else TEST_DIR
Roger Meier41ad4342015-03-24 22:30:40 +0100183 res = crossrunner.generate_known_failures(
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900184 dire, options.update_failures == 'overwrite',
Roger Meier41ad4342015-03-24 22:30:40 +0100185 options.update_failures, options.print_failures)
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +0900186 elif options.features is not None:
187 features = options.features or ['.*']
188 res = run_feature_tests(server_match, features, options.jobs, options.skip_known_failures)
Roger Meiere8bafb62014-08-01 23:39:32 +0200189 else:
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +0900190 res = run_cross_tests(server_match, client_match, options.jobs, options.skip_known_failures)
Roger Meier41ad4342015-03-24 22:30:40 +0100191 return 0 if res else 1
Roger Meiere8bafb62014-08-01 23:39:32 +0200192
Roger Meier41ad4342015-03-24 22:30:40 +0100193if __name__ == '__main__':
194 sys.exit(main(sys.argv[1:]))