blob: df4c72e7607a477f0408f9bf612e973a204d782f [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
Nobuaki Sukegawabd165302016-01-19 11:10:07 +090041ROOT_DIR = os.path.dirname(os.path.realpath(os.path.dirname(__file__)))
42TEST_DIR_RELATIVE = 'test'
43TEST_DIR = path_join(ROOT_DIR, TEST_DIR_RELATIVE)
44FEATURE_DIR_RELATIVE = path_join(TEST_DIR_RELATIVE, 'features')
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090045CONFIG_FILE = 'tests.json'
cdwijayarathnad1041652014-08-15 23:42:20 +053046
Roger Meier40cc2322014-06-11 11:09:14 +020047
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090048def run_cross_tests(server_match, client_match, jobs, skip_known_failures):
49 logger = multiprocessing.get_logger()
50 logger.debug('Collecting tests')
51 with open(path_join(TEST_DIR, CONFIG_FILE), 'r') as fp:
52 j = json.load(fp)
53 tests = crossrunner.collect_cross_tests(j, server_match, client_match)
54 if not tests:
55 print('No test found that matches the criteria', file=sys.stderr)
56 print(' servers: %s' % server_match, file=sys.stderr)
57 print(' clients: %s' % client_match, file=sys.stderr)
58 return False
Roger Meier41ad4342015-03-24 22:30:40 +010059 if skip_known_failures:
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090060 logger.debug('Skipping known failures')
Roger Meier41ad4342015-03-24 22:30:40 +010061 known = crossrunner.load_known_failures(TEST_DIR)
62 tests = list(filter(lambda t: crossrunner.test_name(**t) not in known, tests))
63
Nobuaki Sukegawabd165302016-01-19 11:10:07 +090064 dispatcher = crossrunner.TestDispatcher(TEST_DIR, ROOT_DIR, TEST_DIR_RELATIVE, jobs)
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090065 logger.debug('Executing %d tests' % len(tests))
66 try:
67 for r in [dispatcher.dispatch(test) for test in tests]:
68 r.wait()
69 logger.debug('Waiting for completion')
70 return dispatcher.wait()
71 except (KeyboardInterrupt, SystemExit):
72 logger.debug('Interrupted, shutting down')
73 dispatcher.terminate()
74 return False
75
76
77def run_feature_tests(server_match, feature_match, jobs, skip_known_failures):
Nobuaki Sukegawabd165302016-01-19 11:10:07 +090078 basedir = path_join(ROOT_DIR, FEATURE_DIR_RELATIVE)
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090079 logger = multiprocessing.get_logger()
80 logger.debug('Collecting tests')
81 with open(path_join(TEST_DIR, CONFIG_FILE), 'r') as fp:
82 j = json.load(fp)
83 with open(path_join(basedir, CONFIG_FILE), 'r') as fp:
84 j2 = json.load(fp)
85 tests = crossrunner.collect_feature_tests(j, j2, server_match, feature_match)
86 if not tests:
87 print('No test found that matches the criteria', file=sys.stderr)
88 print(' servers: %s' % server_match, file=sys.stderr)
89 print(' features: %s' % feature_match, file=sys.stderr)
90 return False
91 if skip_known_failures:
92 logger.debug('Skipping known failures')
93 known = crossrunner.load_known_failures(basedir)
94 tests = list(filter(lambda t: crossrunner.test_name(**t) not in known, tests))
95
Nobuaki Sukegawabd165302016-01-19 11:10:07 +090096 dispatcher = crossrunner.TestDispatcher(TEST_DIR, ROOT_DIR, FEATURE_DIR_RELATIVE, jobs)
Roger Meier41ad4342015-03-24 22:30:40 +010097 logger.debug('Executing %d tests' % len(tests))
98 try:
99 for r in [dispatcher.dispatch(test) for test in tests]:
100 r.wait()
101 logger.debug('Waiting for completion')
102 return dispatcher.wait()
103 except (KeyboardInterrupt, SystemExit):
104 logger.debug('Interrupted, shutting down')
105 dispatcher.terminate()
106 return False
107
108
109def default_concurrenty():
110 try:
111 return int(os.environ.get('THRIFT_CROSSTEST_CONCURRENCY'))
112 except (TypeError, ValueError):
113 # Since much time is spent sleeping, use many threads
114 return int(multiprocessing.cpu_count() * 1.25) + 1
115
116
117def main(argv):
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +0900118 parser = argparse.ArgumentParser()
119 parser.add_argument('--server', default='', nargs='*',
120 help='list of servers to test')
121 parser.add_argument('--client', default='', nargs='*',
122 help='list of clients to test')
123 parser.add_argument('-s', '--skip-known-failures', action='store_true', dest='skip_known_failures',
124 help='do not execute tests that are known to fail')
125 parser.add_argument('-j', '--jobs', type=int,
126 default=default_concurrenty(),
127 help='number of concurrent test executions')
128 parser.add_argument('-F', '--features', nargs='*', default=None,
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900129 help='run server feature tests instead of cross language tests')
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +0900130
131 g = parser.add_argument_group(title='Advanced')
132 g.add_argument('-v', '--verbose', action='store_const',
133 dest='log_level', const=logging.DEBUG, default=logging.WARNING,
134 help='show debug output for test runner')
135 g.add_argument('-P', '--print-expected-failures', choices=['merge', 'overwrite'],
136 dest='print_failures',
137 help="generate expected failures based on last result and print to stdout")
138 g.add_argument('-U', '--update-expected-failures', choices=['merge', 'overwrite'],
139 dest='update_failures',
140 help="generate expected failures based on last result and save to default file location")
141 options = parser.parse_args(argv)
142
Roger Meier41ad4342015-03-24 22:30:40 +0100143 logger = multiprocessing.log_to_stderr()
Roger Meier41ad4342015-03-24 22:30:40 +0100144 logger.setLevel(options.log_level)
145
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900146 if options.features is not None and options.client:
147 print('Cannot specify both --features and --client ', file=sys.stderr)
148 return 1
149
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +0900150 # Allow multiple args separated with ',' for backward compatibility
151 server_match = list(chain(*[x.split(',') for x in options.server]))
152 client_match = list(chain(*[x.split(',') for x in options.client]))
153
154 if options.update_failures or options.print_failures:
Nobuaki Sukegawabd165302016-01-19 11:10:07 +0900155 dire = path_join(ROOT_DIR, FEATURE_DIR_RELATIVE) if options.features is not None else TEST_DIR
Roger Meier41ad4342015-03-24 22:30:40 +0100156 res = crossrunner.generate_known_failures(
Nobuaki Sukegawa85650612016-01-08 03:26:44 +0900157 dire, options.update_failures == 'overwrite',
Roger Meier41ad4342015-03-24 22:30:40 +0100158 options.update_failures, options.print_failures)
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +0900159 elif options.features is not None:
160 features = options.features or ['.*']
161 res = run_feature_tests(server_match, features, options.jobs, options.skip_known_failures)
Roger Meiere8bafb62014-08-01 23:39:32 +0200162 else:
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +0900163 res = run_cross_tests(server_match, client_match, options.jobs, options.skip_known_failures)
Roger Meier41ad4342015-03-24 22:30:40 +0100164 return 0 if res else 1
Roger Meiere8bafb62014-08-01 23:39:32 +0200165
Roger Meier41ad4342015-03-24 22:30:40 +0100166if __name__ == '__main__':
167 sys.exit(main(sys.argv[1:]))