James E. King III | 9bea32f | 2018-03-16 16:07:42 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 2 | # |
| 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 III | 9bea32f | 2018-03-16 16:07:42 -0400 | [diff] [blame] | 21 | # |
| 22 | # Apache Thrift - integration (cross) test suite |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 23 | # |
| 24 | # tests different server-client, protocol and transport combinations |
| 25 | # |
James E. King III | 9bea32f | 2018-03-16 16:07:42 -0400 | [diff] [blame] | 26 | # This script requires python 3.x due to the improvements in |
| 27 | # subprocess management that are needed for reliability. |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 28 | # |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 29 | |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 30 | from __future__ import print_function |
| 31 | from itertools import chain |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 32 | import json |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 33 | import logging |
| 34 | import multiprocessing |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 35 | import argparse |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 36 | import os |
| 37 | import sys |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 38 | |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 39 | import crossrunner |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 40 | from crossrunner.compat import path_join |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 41 | |
James E. King III | 9bea32f | 2018-03-16 16:07:42 -0400 | [diff] [blame] | 42 | # 3.3 introduced subprocess timeouts on waiting for child |
| 43 | req_version = (3, 3) |
| 44 | cur_version = sys.version_info |
| 45 | assert (cur_version >= req_version), "Python 3.3 or later is required for proper operation." |
| 46 | |
| 47 | |
Nobuaki Sukegawa | bd16530 | 2016-01-19 11:10:07 +0900 | [diff] [blame] | 48 | ROOT_DIR = os.path.dirname(os.path.realpath(os.path.dirname(__file__))) |
| 49 | TEST_DIR_RELATIVE = 'test' |
| 50 | TEST_DIR = path_join(ROOT_DIR, TEST_DIR_RELATIVE) |
| 51 | FEATURE_DIR_RELATIVE = path_join(TEST_DIR_RELATIVE, 'features') |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 52 | CONFIG_FILE = 'tests.json' |
cdwijayarathna | d104165 | 2014-08-15 23:42:20 +0530 | [diff] [blame] | 53 | |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 54 | |
Nobuaki Sukegawa | 144bbef | 2016-02-11 13:15:40 +0900 | [diff] [blame] | 55 | def run_cross_tests(server_match, client_match, jobs, skip_known_failures, retry_count, regex): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 56 | logger = multiprocessing.get_logger() |
| 57 | logger.debug('Collecting tests') |
| 58 | with open(path_join(TEST_DIR, CONFIG_FILE), 'r') as fp: |
| 59 | j = json.load(fp) |
Nobuaki Sukegawa | 144bbef | 2016-02-11 13:15:40 +0900 | [diff] [blame] | 60 | tests = crossrunner.collect_cross_tests(j, server_match, client_match, regex) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 61 | if not tests: |
| 62 | print('No test found that matches the criteria', file=sys.stderr) |
| 63 | print(' servers: %s' % server_match, file=sys.stderr) |
| 64 | print(' clients: %s' % client_match, file=sys.stderr) |
| 65 | return False |
| 66 | if skip_known_failures: |
| 67 | logger.debug('Skipping known failures') |
| 68 | known = crossrunner.load_known_failures(TEST_DIR) |
| 69 | tests = list(filter(lambda t: crossrunner.test_name(**t) not in known, tests)) |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 70 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 71 | dispatcher = crossrunner.TestDispatcher(TEST_DIR, ROOT_DIR, TEST_DIR_RELATIVE, jobs) |
| 72 | logger.debug('Executing %d tests' % len(tests)) |
| 73 | try: |
| 74 | for r in [dispatcher.dispatch(test, retry_count) for test in tests]: |
| 75 | r.wait() |
| 76 | logger.debug('Waiting for completion') |
| 77 | return dispatcher.wait() |
| 78 | except (KeyboardInterrupt, SystemExit): |
| 79 | logger.debug('Interrupted, shutting down') |
| 80 | dispatcher.terminate() |
| 81 | return False |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 82 | |
| 83 | |
Nobuaki Sukegawa | 144bbef | 2016-02-11 13:15:40 +0900 | [diff] [blame] | 84 | def run_feature_tests(server_match, feature_match, jobs, skip_known_failures, retry_count, regex): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 85 | basedir = path_join(ROOT_DIR, FEATURE_DIR_RELATIVE) |
| 86 | logger = multiprocessing.get_logger() |
| 87 | logger.debug('Collecting tests') |
| 88 | with open(path_join(TEST_DIR, CONFIG_FILE), 'r') as fp: |
| 89 | j = json.load(fp) |
| 90 | with open(path_join(basedir, CONFIG_FILE), 'r') as fp: |
| 91 | j2 = json.load(fp) |
Nobuaki Sukegawa | 144bbef | 2016-02-11 13:15:40 +0900 | [diff] [blame] | 92 | tests = crossrunner.collect_feature_tests(j, j2, server_match, feature_match, regex) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 93 | if not tests: |
| 94 | print('No test found that matches the criteria', file=sys.stderr) |
| 95 | print(' servers: %s' % server_match, file=sys.stderr) |
| 96 | print(' features: %s' % feature_match, file=sys.stderr) |
| 97 | return False |
| 98 | if skip_known_failures: |
| 99 | logger.debug('Skipping known failures') |
| 100 | known = crossrunner.load_known_failures(basedir) |
| 101 | tests = list(filter(lambda t: crossrunner.test_name(**t) not in known, tests)) |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 102 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 103 | dispatcher = crossrunner.TestDispatcher(TEST_DIR, ROOT_DIR, FEATURE_DIR_RELATIVE, jobs) |
| 104 | logger.debug('Executing %d tests' % len(tests)) |
| 105 | try: |
| 106 | for r in [dispatcher.dispatch(test, retry_count) for test in tests]: |
| 107 | r.wait() |
| 108 | logger.debug('Waiting for completion') |
| 109 | return dispatcher.wait() |
| 110 | except (KeyboardInterrupt, SystemExit): |
| 111 | logger.debug('Interrupted, shutting down') |
| 112 | dispatcher.terminate() |
| 113 | return False |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 114 | |
| 115 | |
| 116 | def default_concurrenty(): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 117 | try: |
| 118 | return int(os.environ.get('THRIFT_CROSSTEST_CONCURRENCY')) |
| 119 | except (TypeError, ValueError): |
| 120 | # Since much time is spent sleeping, use many threads |
| 121 | return int(multiprocessing.cpu_count() * 1.25) + 1 |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 122 | |
| 123 | |
| 124 | def main(argv): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 125 | parser = argparse.ArgumentParser() |
| 126 | parser.add_argument('--server', default='', nargs='*', |
| 127 | help='list of servers to test') |
| 128 | parser.add_argument('--client', default='', nargs='*', |
| 129 | help='list of clients to test') |
| 130 | parser.add_argument('-F', '--features', nargs='*', default=None, |
| 131 | help='run server feature tests instead of cross language tests') |
Nobuaki Sukegawa | 144bbef | 2016-02-11 13:15:40 +0900 | [diff] [blame] | 132 | parser.add_argument('-R', '--regex', help='test name pattern to run') |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 133 | parser.add_argument('-s', '--skip-known-failures', action='store_true', dest='skip_known_failures', |
| 134 | help='do not execute tests that are known to fail') |
| 135 | parser.add_argument('-r', '--retry-count', type=int, |
| 136 | default=0, help='maximum retry on failure') |
| 137 | parser.add_argument('-j', '--jobs', type=int, |
| 138 | default=default_concurrenty(), |
| 139 | help='number of concurrent test executions') |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 140 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 141 | g = parser.add_argument_group(title='Advanced') |
| 142 | g.add_argument('-v', '--verbose', action='store_const', |
| 143 | dest='log_level', const=logging.DEBUG, default=logging.WARNING, |
| 144 | help='show debug output for test runner') |
| 145 | g.add_argument('-P', '--print-expected-failures', choices=['merge', 'overwrite'], |
| 146 | dest='print_failures', |
| 147 | help="generate expected failures based on last result and print to stdout") |
| 148 | g.add_argument('-U', '--update-expected-failures', choices=['merge', 'overwrite'], |
| 149 | dest='update_failures', |
| 150 | help="generate expected failures based on last result and save to default file location") |
| 151 | options = parser.parse_args(argv) |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 152 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 153 | logger = multiprocessing.log_to_stderr() |
| 154 | logger.setLevel(options.log_level) |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 155 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 156 | if options.features is not None and options.client: |
| 157 | print('Cannot specify both --features and --client ', file=sys.stderr) |
| 158 | return 1 |
Nobuaki Sukegawa | 8565061 | 2016-01-08 03:26:44 +0900 | [diff] [blame] | 159 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 160 | # Allow multiple args separated with ',' for backward compatibility |
| 161 | server_match = list(chain(*[x.split(',') for x in options.server])) |
| 162 | client_match = list(chain(*[x.split(',') for x in options.client])) |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 163 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 164 | if options.update_failures or options.print_failures: |
| 165 | dire = path_join(ROOT_DIR, FEATURE_DIR_RELATIVE) if options.features is not None else TEST_DIR |
| 166 | res = crossrunner.generate_known_failures( |
| 167 | dire, options.update_failures == 'overwrite', |
| 168 | options.update_failures, options.print_failures) |
| 169 | elif options.features is not None: |
| 170 | features = options.features or ['.*'] |
James E. King III | 9bea32f | 2018-03-16 16:07:42 -0400 | [diff] [blame] | 171 | res = run_feature_tests(server_match, features, options.jobs, |
| 172 | options.skip_known_failures, options.retry_count, options.regex) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 173 | else: |
James E. King III | 9bea32f | 2018-03-16 16:07:42 -0400 | [diff] [blame] | 174 | res = run_cross_tests(server_match, client_match, options.jobs, |
| 175 | options.skip_known_failures, options.retry_count, options.regex) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 176 | return 0 if res else 1 |
Roger Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 177 | |
James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame] | 178 | |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 179 | if __name__ == '__main__': |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 180 | sys.exit(main(sys.argv[1:])) |