Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
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 | |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 21 | # 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 Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 28 | |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 29 | from __future__ import print_function |
| 30 | from itertools import chain |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 31 | import json |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 32 | import logging |
| 33 | import multiprocessing |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 34 | import argparse |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 35 | import os |
| 36 | import sys |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 37 | |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 38 | import crossrunner |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 39 | from crossrunner.compat import path_join |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 40 | |
Nobuaki Sukegawa | bd16530 | 2016-01-19 11:10:07 +0900 | [diff] [blame] | 41 | ROOT_DIR = os.path.dirname(os.path.realpath(os.path.dirname(__file__))) |
| 42 | TEST_DIR_RELATIVE = 'test' |
| 43 | TEST_DIR = path_join(ROOT_DIR, TEST_DIR_RELATIVE) |
| 44 | FEATURE_DIR_RELATIVE = path_join(TEST_DIR_RELATIVE, 'features') |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 45 | CONFIG_FILE = 'tests.json' |
cdwijayarathna | d104165 | 2014-08-15 23:42:20 +0530 | [diff] [blame] | 46 | |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 47 | |
Nobuaki Sukegawa | 144bbef | 2016-02-11 13:15:40 +0900 | [diff] [blame] | 48 | 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] | 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) |
Nobuaki Sukegawa | 144bbef | 2016-02-11 13:15:40 +0900 | [diff] [blame] | 53 | tests = crossrunner.collect_cross_tests(j, server_match, client_match, regex) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 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 |
| 59 | if skip_known_failures: |
| 60 | logger.debug('Skipping known failures') |
| 61 | known = crossrunner.load_known_failures(TEST_DIR) |
| 62 | 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] | 63 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 64 | dispatcher = crossrunner.TestDispatcher(TEST_DIR, ROOT_DIR, TEST_DIR_RELATIVE, jobs) |
| 65 | logger.debug('Executing %d tests' % len(tests)) |
| 66 | try: |
| 67 | for r in [dispatcher.dispatch(test, retry_count) 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 |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 75 | |
| 76 | |
Nobuaki Sukegawa | 144bbef | 2016-02-11 13:15:40 +0900 | [diff] [blame] | 77 | 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] | 78 | basedir = path_join(ROOT_DIR, FEATURE_DIR_RELATIVE) |
| 79 | 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) |
Nobuaki Sukegawa | 144bbef | 2016-02-11 13:15:40 +0900 | [diff] [blame] | 85 | tests = crossrunner.collect_feature_tests(j, j2, server_match, feature_match, regex) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 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)) |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 95 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 96 | dispatcher = crossrunner.TestDispatcher(TEST_DIR, ROOT_DIR, FEATURE_DIR_RELATIVE, jobs) |
| 97 | logger.debug('Executing %d tests' % len(tests)) |
| 98 | try: |
| 99 | for r in [dispatcher.dispatch(test, retry_count) 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 |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 107 | |
| 108 | |
| 109 | def default_concurrenty(): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 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 |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 115 | |
| 116 | |
| 117 | def main(argv): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 118 | 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('-F', '--features', nargs='*', default=None, |
| 124 | help='run server feature tests instead of cross language tests') |
Nobuaki Sukegawa | 144bbef | 2016-02-11 13:15:40 +0900 | [diff] [blame] | 125 | parser.add_argument('-R', '--regex', help='test name pattern to run') |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 126 | parser.add_argument('-s', '--skip-known-failures', action='store_true', dest='skip_known_failures', |
| 127 | help='do not execute tests that are known to fail') |
| 128 | parser.add_argument('-r', '--retry-count', type=int, |
| 129 | default=0, help='maximum retry on failure') |
| 130 | parser.add_argument('-j', '--jobs', type=int, |
| 131 | default=default_concurrenty(), |
| 132 | help='number of concurrent test executions') |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 133 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 134 | g = parser.add_argument_group(title='Advanced') |
| 135 | g.add_argument('-v', '--verbose', action='store_const', |
| 136 | dest='log_level', const=logging.DEBUG, default=logging.WARNING, |
| 137 | help='show debug output for test runner') |
| 138 | g.add_argument('-P', '--print-expected-failures', choices=['merge', 'overwrite'], |
| 139 | dest='print_failures', |
| 140 | help="generate expected failures based on last result and print to stdout") |
| 141 | g.add_argument('-U', '--update-expected-failures', choices=['merge', 'overwrite'], |
| 142 | dest='update_failures', |
| 143 | help="generate expected failures based on last result and save to default file location") |
| 144 | options = parser.parse_args(argv) |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 145 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 146 | logger = multiprocessing.log_to_stderr() |
| 147 | logger.setLevel(options.log_level) |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 148 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 149 | if options.features is not None and options.client: |
| 150 | print('Cannot specify both --features and --client ', file=sys.stderr) |
| 151 | return 1 |
Nobuaki Sukegawa | 8565061 | 2016-01-08 03:26:44 +0900 | [diff] [blame] | 152 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 153 | # Allow multiple args separated with ',' for backward compatibility |
| 154 | server_match = list(chain(*[x.split(',') for x in options.server])) |
| 155 | client_match = list(chain(*[x.split(',') for x in options.client])) |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 156 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 157 | if options.update_failures or options.print_failures: |
| 158 | dire = path_join(ROOT_DIR, FEATURE_DIR_RELATIVE) if options.features is not None else TEST_DIR |
| 159 | res = crossrunner.generate_known_failures( |
| 160 | dire, options.update_failures == 'overwrite', |
| 161 | options.update_failures, options.print_failures) |
| 162 | elif options.features is not None: |
| 163 | features = options.features or ['.*'] |
Nobuaki Sukegawa | 144bbef | 2016-02-11 13:15:40 +0900 | [diff] [blame] | 164 | res = run_feature_tests(server_match, features, options.jobs, options.skip_known_failures, options.retry_count, options.regex) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 165 | else: |
Nobuaki Sukegawa | 144bbef | 2016-02-11 13:15:40 +0900 | [diff] [blame] | 166 | res = run_cross_tests(server_match, client_match, options.jobs, options.skip_known_failures, options.retry_count, options.regex) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 167 | return 0 if res else 1 |
Roger Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 168 | |
Roger Meier | 41ad434 | 2015-03-24 22:30:40 +0100 | [diff] [blame] | 169 | if __name__ == '__main__': |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 170 | sys.exit(main(sys.argv[1:])) |