Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 3 | # |
| 4 | # Licensed to the Apache Software Foundation (ASF) under one |
| 5 | # or more contributor license agreements. See the NOTICE file |
| 6 | # distributed with this work for additional information |
| 7 | # regarding copyright ownership. The ASF licenses this file |
| 8 | # to you under the Apache License, Version 2.0 (the |
| 9 | # "License"); you may not use this file except in compliance |
| 10 | # with the License. You may obtain a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, |
| 15 | # software distributed under the License is distributed on an |
| 16 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | # KIND, either express or implied. See the License for the |
| 18 | # specific language governing permissions and limitations |
| 19 | # under the License. |
| 20 | # |
| 21 | |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 22 | from __future__ import division |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 23 | from __future__ import print_function |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 24 | import copy |
| 25 | import glob |
| 26 | import os |
| 27 | import signal |
Roger Meier | 9b32853 | 2014-04-21 21:22:54 +0200 | [diff] [blame] | 28 | import socket |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 29 | import subprocess |
| 30 | import sys |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 31 | import time |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 32 | from optparse import OptionParser |
| 33 | |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 34 | SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__)) |
| 35 | ROOT_DIR = os.path.dirname(os.path.dirname(SCRIPT_DIR)) |
| 36 | DEFAULT_LIBDIR_GLOB = os.path.join(ROOT_DIR, 'lib', 'py', 'build', 'lib.*') |
| 37 | DEFAULT_LIBDIR_PY3 = os.path.join(ROOT_DIR, 'lib', 'py', 'build', 'lib') |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 38 | |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 39 | SCRIPTS = [ |
| 40 | 'TSimpleJSONProtocolTest.py', |
| 41 | 'SerializationTest.py', |
| 42 | 'TestEof.py', |
| 43 | 'TestSyntax.py', |
| 44 | 'TestSocket.py', |
| 45 | ] |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 46 | FRAMED = ["TNonblockingServer"] |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 47 | SKIP_ZLIB = ['TNonblockingServer', 'THttpServer'] |
| 48 | SKIP_SSL = ['TNonblockingServer', 'THttpServer'] |
Roger Meier | 6857b7f | 2015-09-16 19:53:07 +0200 | [diff] [blame] | 49 | EXTRA_DELAY = dict(TProcessPoolServer=5.5) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 50 | |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 51 | PROTOS = [ |
| 52 | 'accel', |
| 53 | 'binary', |
| 54 | 'compact', |
| 55 | 'json', |
| 56 | ] |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 57 | |
| 58 | SERVERS = [ |
| 59 | "TSimpleServer", |
| 60 | "TThreadedServer", |
| 61 | "TThreadPoolServer", |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 62 | "TProcessPoolServer", |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 63 | "TForkingServer", |
| 64 | "TNonblockingServer", |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 65 | "THttpServer", |
| 66 | ] |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 67 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 68 | |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 69 | def relfile(fname): |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 70 | return os.path.join(SCRIPT_DIR, fname) |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 71 | |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 72 | |
| 73 | def setup_pypath(dirs): |
| 74 | env = copy.copy(os.environ) |
| 75 | pypath = env.get('PYTHONPATH', None) |
| 76 | if pypath: |
| 77 | dirs.append(pypath) |
| 78 | env['PYTHONPATH'] = ':'.join(dirs) |
| 79 | return env |
| 80 | |
| 81 | |
| 82 | def runScriptTest(libdir, genpydir, script): |
| 83 | env = setup_pypath([libdir, genpydir]) |
| 84 | script_args = [sys.executable, relfile(script)] |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 85 | print('\nTesting script: %s\n----' % (' '.join(script_args))) |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 86 | ret = subprocess.call(script_args, env=env) |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 87 | if ret != 0: |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 88 | print('*** FAILED ***', file=sys.stderr) |
| 89 | print('LIBDIR: %s' % libdir, file=sys.stderr) |
| 90 | print('PY_GEN: %s' % genpydir, file=sys.stderr) |
| 91 | print('SCRIPT: %s' % script, file=sys.stderr) |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 92 | raise Exception("Script subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(script_args))) |
Roger Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 93 | |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 94 | |
| 95 | def runServiceTest(libdir, genpydir, server_class, proto, port, use_zlib, use_ssl, verbose): |
| 96 | env = setup_pypath([libdir, genpydir]) |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 97 | # Build command line arguments |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 98 | server_args = [sys.executable, relfile('TestServer.py')] |
| 99 | cli_args = [sys.executable, relfile('TestClient.py')] |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 100 | for which in (server_args, cli_args): |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 101 | which.append('--protocol=%s' % proto) # accel, binary, compact or json |
| 102 | which.append('--port=%d' % port) # default to 9090 |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 103 | if use_zlib: |
| 104 | which.append('--zlib') |
| 105 | if use_ssl: |
| 106 | which.append('--ssl') |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 107 | if verbose == 0: |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 108 | which.append('-q') |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 109 | if verbose == 2: |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 110 | which.append('-v') |
| 111 | # server-specific option to select server class |
| 112 | server_args.append(server_class) |
| 113 | # client-specific cmdline options |
| 114 | if server_class in FRAMED: |
Roger Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 115 | cli_args.append('--transport=framed') |
| 116 | else: |
| 117 | cli_args.append('--transport=buffered') |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 118 | if server_class == 'THttpServer': |
| 119 | cli_args.append('--http=/') |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 120 | if verbose > 0: |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 121 | print('Testing server %s: %s' % (server_class, ' '.join(server_args))) |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 122 | serverproc = subprocess.Popen(server_args, env=env) |
Roger Meier | 9b32853 | 2014-04-21 21:22:54 +0200 | [diff] [blame] | 123 | |
| 124 | def ensureServerAlive(): |
| 125 | if serverproc.poll() is not None: |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 126 | print(('FAIL: Server process (%s) failed with retcode %d') |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 127 | % (' '.join(server_args), serverproc.returncode)) |
Roger Meier | 9b32853 | 2014-04-21 21:22:54 +0200 | [diff] [blame] | 128 | raise Exception('Server subprocess %s died, args: %s' |
| 129 | % (server_class, ' '.join(server_args))) |
| 130 | |
| 131 | # Wait for the server to start accepting connections on the given port. |
| 132 | sock = socket.socket() |
| 133 | sleep_time = 0.1 # Seconds |
| 134 | max_attempts = 100 |
| 135 | try: |
| 136 | attempt = 0 |
| 137 | while sock.connect_ex(('127.0.0.1', port)) != 0: |
| 138 | attempt += 1 |
| 139 | if attempt >= max_attempts: |
| 140 | raise Exception("TestServer not ready on port %d after %.2f seconds" |
| 141 | % (port, sleep_time * attempt)) |
| 142 | ensureServerAlive() |
| 143 | time.sleep(sleep_time) |
| 144 | finally: |
| 145 | sock.close() |
| 146 | |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 147 | try: |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 148 | if verbose > 0: |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 149 | print('Testing client: %s' % (' '.join(cli_args))) |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 150 | ret = subprocess.call(cli_args, env=env) |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 151 | if ret != 0: |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 152 | print('*** FAILED ***', file=sys.stderr) |
| 153 | print('LIBDIR: %s' % libdir, file=sys.stderr) |
| 154 | print('PY_GEN: %s' % genpydir, file=sys.stderr) |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 155 | raise Exception("Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args))) |
| 156 | finally: |
| 157 | # check that server didn't die |
Roger Meier | 9b32853 | 2014-04-21 21:22:54 +0200 | [diff] [blame] | 158 | ensureServerAlive() |
| 159 | extra_sleep = EXTRA_DELAY.get(server_class, 0) |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 160 | if extra_sleep > 0 and verbose > 0: |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 161 | print('Giving %s (proto=%s,zlib=%s,ssl=%s) an extra %d seconds for child' |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 162 | 'processes to terminate via alarm' |
| 163 | % (server_class, proto, use_zlib, use_ssl, extra_sleep)) |
Roger Meier | 9b32853 | 2014-04-21 21:22:54 +0200 | [diff] [blame] | 164 | time.sleep(extra_sleep) |
| 165 | os.kill(serverproc.pid, signal.SIGKILL) |
| 166 | serverproc.wait() |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 167 | |
Roger Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 168 | |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 169 | class TestCases(object): |
| 170 | def __init__(self, libdir, port, gendirs, servers, verbose): |
| 171 | self.libdir = libdir |
| 172 | self.port = port |
| 173 | self.verbose = verbose |
| 174 | self.gendirs = gendirs |
| 175 | self.servers = servers |
| 176 | |
| 177 | def default_conf(self): |
| 178 | return { |
| 179 | 'gendir': self.gendirs[0], |
| 180 | 'server': self.servers[0], |
| 181 | 'proto': PROTOS[0], |
| 182 | 'zlib': False, |
| 183 | 'ssl': False, |
| 184 | } |
| 185 | |
| 186 | def run(self, conf, test_count): |
| 187 | with_zlib = conf['zlib'] |
| 188 | with_ssl = conf['ssl'] |
| 189 | try_server = conf['server'] |
| 190 | try_proto = conf['proto'] |
| 191 | genpydir = conf['gendir'] |
| 192 | # skip any servers that don't work with the Zlib transport |
| 193 | if with_zlib and try_server in SKIP_ZLIB: |
| 194 | return False |
| 195 | # skip any servers that don't work with SSL |
| 196 | if with_ssl and try_server in SKIP_SSL: |
| 197 | return False |
| 198 | if self.verbose > 0: |
| 199 | print('\nTest run #%d: (includes %s) Server=%s, Proto=%s, zlib=%s, SSL=%s' |
| 200 | % (test_count, genpydir, try_server, try_proto, with_zlib, with_ssl)) |
| 201 | runServiceTest(self.libdir, genpydir, try_server, try_proto, self.port, with_zlib, with_ssl, self.verbose) |
| 202 | if self.verbose > 0: |
| 203 | print('OK: Finished (includes %s) %s / %s proto / zlib=%s / SSL=%s. %d combinations tested.' |
| 204 | % (genpydir, try_server, try_proto, with_zlib, with_ssl, test_count)) |
| 205 | return True |
| 206 | |
| 207 | def test_feature(self, name, values): |
| 208 | test_count = 0 |
| 209 | conf = self.default_conf() |
| 210 | for try_server in values: |
| 211 | conf[name] = try_server |
| 212 | if self.run(conf, test_count): |
| 213 | test_count += 1 |
| 214 | return test_count |
| 215 | |
| 216 | def run_all_tests(self): |
| 217 | test_count = 0 |
| 218 | for try_server in self.servers: |
| 219 | for genpydir in self.gendirs: |
| 220 | for try_proto in PROTOS: |
| 221 | for with_zlib in (False, True): |
| 222 | # skip any servers that don't work with the Zlib transport |
| 223 | if with_zlib and try_server in SKIP_ZLIB: |
| 224 | continue |
| 225 | for with_ssl in (False, True): |
| 226 | # skip any servers that don't work with SSL |
| 227 | if with_ssl and try_server in SKIP_SSL: |
| 228 | continue |
| 229 | test_count += 1 |
| 230 | if self.verbose > 0: |
| 231 | print('\nTest run #%d: (includes %s) Server=%s, Proto=%s, zlib=%s, SSL=%s' |
| 232 | % (test_count, genpydir, try_server, try_proto, with_zlib, with_ssl)) |
| 233 | runServiceTest(self.libdir, genpydir, try_server, try_proto, self.port, with_zlib, with_ssl) |
| 234 | if self.verbose > 0: |
| 235 | print('OK: Finished (includes %s) %s / %s proto / zlib=%s / SSL=%s. %d combinations tested.' |
| 236 | % (genpydir, try_server, try_proto, with_zlib, with_ssl, test_count)) |
| 237 | return test_count |
| 238 | |
| 239 | |
| 240 | def default_libdir(): |
| 241 | if sys.version_info[0] == 2: |
| 242 | return glob.glob(DEFAULT_LIBDIR_GLOB)[0] |
| 243 | else: |
| 244 | return DEFAULT_LIBDIR_PY3 |
| 245 | |
| 246 | |
| 247 | def main(): |
| 248 | parser = OptionParser() |
| 249 | parser.add_option('--all', action="store_true", dest='all') |
| 250 | parser.add_option('--genpydirs', type='string', dest='genpydirs', |
| 251 | default='default,slots,newstyle,newstyleslots,dynamic,dynamicslots', |
| 252 | help='directory extensions for generated code, used as suffixes for \"gen-py-*\" added sys.path for individual tests') |
| 253 | parser.add_option("--port", type="int", dest="port", default=9090, |
| 254 | help="port number for server to listen on") |
| 255 | parser.add_option('-v', '--verbose', action="store_const", |
| 256 | dest="verbose", const=2, |
| 257 | help="verbose output") |
| 258 | parser.add_option('-q', '--quiet', action="store_const", |
| 259 | dest="verbose", const=0, |
| 260 | help="minimal output") |
| 261 | parser.add_option('-L', '--libdir', dest="libdir", default=default_libdir(), |
| 262 | help="directory path that contains Thrift Python library") |
| 263 | parser.set_defaults(verbose=1) |
| 264 | options, args = parser.parse_args() |
| 265 | |
| 266 | generated_dirs = [] |
| 267 | for gp_dir in options.genpydirs.split(','): |
| 268 | generated_dirs.append('gen-py-%s' % (gp_dir)) |
| 269 | |
| 270 | # commandline permits a single class name to be specified to override SERVERS=[...] |
| 271 | servers = SERVERS |
| 272 | if len(args) == 1: |
| 273 | if args[0] in SERVERS: |
| 274 | servers = args |
| 275 | else: |
| 276 | print('Unavailable server type "%s", please choose one of: %s' % (args[0], servers)) |
| 277 | sys.exit(0) |
| 278 | |
| 279 | tests = TestCases(options.libdir, options.port, generated_dirs, servers, options.verbose) |
| 280 | |
| 281 | # run tests without a client/server first |
| 282 | print('----------------') |
| 283 | print(' Executing individual test scripts with various generated code directories') |
| 284 | print(' Directories to be tested: ' + ', '.join(generated_dirs)) |
| 285 | print(' Scripts to be tested: ' + ', '.join(SCRIPTS)) |
| 286 | print('----------------') |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame] | 287 | for genpydir in generated_dirs: |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 288 | for script in SCRIPTS: |
| 289 | runScriptTest(options.libdir, genpydir, script) |
| 290 | |
| 291 | print('----------------') |
| 292 | print(' Executing Client/Server tests with various generated code directories') |
| 293 | print(' Servers to be tested: ' + ', '.join(servers)) |
| 294 | print(' Directories to be tested: ' + ', '.join(generated_dirs)) |
| 295 | print(' Protocols to be tested: ' + ', '.join(PROTOS)) |
| 296 | print(' Options to be tested: ZLIB(yes/no), SSL(yes/no)') |
| 297 | print('----------------') |
| 298 | |
| 299 | if options.all: |
| 300 | tests.run_all_tests() |
| 301 | else: |
| 302 | tests.test_feature('gendir', generated_dirs) |
| 303 | tests.test_feature('server', servers) |
| 304 | tests.test_feature('proto', PROTOS) |
| 305 | tests.test_feature('zlib', [False, True]) |
| 306 | tests.test_feature('ssl', [False, True]) |
| 307 | |
| 308 | |
| 309 | if __name__ == '__main__': |
| 310 | sys.exit(main()) |