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 |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 25 | import os |
| 26 | import signal |
Roger Meier | 9b32853 | 2014-04-21 21:22:54 +0200 | [diff] [blame] | 27 | import socket |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 28 | import subprocess |
| 29 | import sys |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 30 | import time |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 31 | from optparse import OptionParser |
| 32 | |
Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame^] | 33 | from util import local_libpath |
| 34 | |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 35 | SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__)) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 36 | |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 37 | SCRIPTS = [ |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 38 | 'FastbinaryTest.py', |
| 39 | 'TestFrozen.py', |
| 40 | 'TSimpleJSONProtocolTest.py', |
| 41 | 'SerializationTest.py', |
| 42 | 'TestEof.py', |
| 43 | 'TestSyntax.py', |
| 44 | 'TestSocket.py', |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 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 = [ |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 52 | 'accel', |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 53 | 'accelc', |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 54 | 'binary', |
| 55 | 'compact', |
| 56 | 'json', |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 57 | ] |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 58 | |
| 59 | SERVERS = [ |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 60 | "TSimpleServer", |
| 61 | "TThreadedServer", |
| 62 | "TThreadPoolServer", |
| 63 | "TProcessPoolServer", |
| 64 | "TForkingServer", |
| 65 | "TNonblockingServer", |
| 66 | "THttpServer", |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 67 | ] |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 68 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 69 | |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 70 | def relfile(fname): |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 71 | return os.path.join(SCRIPT_DIR, fname) |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 72 | |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 73 | |
Nobuaki Sukegawa | a3b88a0 | 2016-01-06 20:44:17 +0900 | [diff] [blame] | 74 | def setup_pypath(libdir, gendir): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 75 | dirs = [libdir, gendir] |
| 76 | env = copy.deepcopy(os.environ) |
| 77 | pypath = env.get('PYTHONPATH', None) |
| 78 | if pypath: |
| 79 | dirs.append(pypath) |
| 80 | env['PYTHONPATH'] = ':'.join(dirs) |
| 81 | if gendir.endswith('gen-py-no_utf8strings'): |
| 82 | env['THRIFT_TEST_PY_NO_UTF8STRINGS'] = '1' |
| 83 | return env |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 84 | |
| 85 | |
Nobuaki Sukegawa | a3b88a0 | 2016-01-06 20:44:17 +0900 | [diff] [blame] | 86 | def runScriptTest(libdir, genbase, genpydir, script): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 87 | env = setup_pypath(libdir, os.path.join(genbase, genpydir)) |
| 88 | script_args = [sys.executable, relfile(script)] |
| 89 | print('\nTesting script: %s\n----' % (' '.join(script_args))) |
| 90 | ret = subprocess.call(script_args, env=env) |
| 91 | if ret != 0: |
| 92 | print('*** FAILED ***', file=sys.stderr) |
| 93 | print('LIBDIR: %s' % libdir, file=sys.stderr) |
| 94 | print('PY_GEN: %s' % genpydir, file=sys.stderr) |
| 95 | print('SCRIPT: %s' % script, file=sys.stderr) |
| 96 | 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] | 97 | |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 98 | |
Nobuaki Sukegawa | a3b88a0 | 2016-01-06 20:44:17 +0900 | [diff] [blame] | 99 | def runServiceTest(libdir, genbase, genpydir, server_class, proto, port, use_zlib, use_ssl, verbose): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 100 | env = setup_pypath(libdir, os.path.join(genbase, genpydir)) |
| 101 | # Build command line arguments |
| 102 | server_args = [sys.executable, relfile('TestServer.py')] |
| 103 | cli_args = [sys.executable, relfile('TestClient.py')] |
| 104 | for which in (server_args, cli_args): |
| 105 | which.append('--protocol=%s' % proto) # accel, binary, compact or json |
| 106 | which.append('--port=%d' % port) # default to 9090 |
| 107 | if use_zlib: |
| 108 | which.append('--zlib') |
| 109 | if use_ssl: |
| 110 | which.append('--ssl') |
| 111 | if verbose == 0: |
| 112 | which.append('-q') |
| 113 | if verbose == 2: |
| 114 | which.append('-v') |
| 115 | # server-specific option to select server class |
| 116 | server_args.append(server_class) |
| 117 | # client-specific cmdline options |
| 118 | if server_class in FRAMED: |
| 119 | cli_args.append('--transport=framed') |
| 120 | else: |
| 121 | cli_args.append('--transport=buffered') |
| 122 | if server_class == 'THttpServer': |
| 123 | cli_args.append('--http=/') |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 124 | if verbose > 0: |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 125 | print('Testing server %s: %s' % (server_class, ' '.join(server_args))) |
| 126 | serverproc = subprocess.Popen(server_args, env=env) |
| 127 | |
| 128 | def ensureServerAlive(): |
| 129 | if serverproc.poll() is not None: |
| 130 | print(('FAIL: Server process (%s) failed with retcode %d') |
| 131 | % (' '.join(server_args), serverproc.returncode)) |
| 132 | raise Exception('Server subprocess %s died, args: %s' |
| 133 | % (server_class, ' '.join(server_args))) |
| 134 | |
| 135 | # Wait for the server to start accepting connections on the given port. |
| 136 | sock = socket.socket() |
| 137 | sleep_time = 0.1 # Seconds |
| 138 | max_attempts = 100 |
| 139 | try: |
| 140 | attempt = 0 |
| 141 | while sock.connect_ex(('127.0.0.1', port)) != 0: |
| 142 | attempt += 1 |
| 143 | if attempt >= max_attempts: |
| 144 | raise Exception("TestServer not ready on port %d after %.2f seconds" |
| 145 | % (port, sleep_time * attempt)) |
| 146 | ensureServerAlive() |
| 147 | time.sleep(sleep_time) |
| 148 | finally: |
| 149 | sock.close() |
| 150 | |
| 151 | try: |
| 152 | if verbose > 0: |
| 153 | print('Testing client: %s' % (' '.join(cli_args))) |
| 154 | ret = subprocess.call(cli_args, env=env) |
| 155 | if ret != 0: |
| 156 | print('*** FAILED ***', file=sys.stderr) |
| 157 | print('LIBDIR: %s' % libdir, file=sys.stderr) |
| 158 | print('PY_GEN: %s' % genpydir, file=sys.stderr) |
| 159 | raise Exception("Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args))) |
| 160 | finally: |
| 161 | # check that server didn't die |
| 162 | ensureServerAlive() |
| 163 | extra_sleep = EXTRA_DELAY.get(server_class, 0) |
| 164 | if extra_sleep > 0 and verbose > 0: |
| 165 | print('Giving %s (proto=%s,zlib=%s,ssl=%s) an extra %d seconds for child' |
| 166 | 'processes to terminate via alarm' |
| 167 | % (server_class, proto, use_zlib, use_ssl, extra_sleep)) |
| 168 | time.sleep(extra_sleep) |
| 169 | os.kill(serverproc.pid, signal.SIGKILL) |
| 170 | serverproc.wait() |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 171 | |
Roger Meier | 7615072 | 2014-05-31 22:22:07 +0200 | [diff] [blame] | 172 | |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 173 | class TestCases(object): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 174 | def __init__(self, genbase, libdir, port, gendirs, servers, verbose): |
| 175 | self.genbase = genbase |
| 176 | self.libdir = libdir |
| 177 | self.port = port |
| 178 | self.verbose = verbose |
| 179 | self.gendirs = gendirs |
| 180 | self.servers = servers |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 181 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 182 | def default_conf(self): |
| 183 | return { |
| 184 | 'gendir': self.gendirs[0], |
| 185 | 'server': self.servers[0], |
| 186 | 'proto': PROTOS[0], |
| 187 | 'zlib': False, |
| 188 | 'ssl': False, |
| 189 | } |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 190 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 191 | def run(self, conf, test_count): |
| 192 | with_zlib = conf['zlib'] |
| 193 | with_ssl = conf['ssl'] |
| 194 | try_server = conf['server'] |
| 195 | try_proto = conf['proto'] |
| 196 | genpydir = conf['gendir'] |
| 197 | # skip any servers that don't work with the Zlib transport |
| 198 | if with_zlib and try_server in SKIP_ZLIB: |
| 199 | return False |
| 200 | # skip any servers that don't work with SSL |
| 201 | if with_ssl and try_server in SKIP_SSL: |
| 202 | return False |
| 203 | if self.verbose > 0: |
| 204 | print('\nTest run #%d: (includes %s) Server=%s, Proto=%s, zlib=%s, SSL=%s' |
| 205 | % (test_count, genpydir, try_server, try_proto, with_zlib, with_ssl)) |
| 206 | runServiceTest(self.libdir, self.genbase, genpydir, try_server, try_proto, self.port, with_zlib, with_ssl, self.verbose) |
| 207 | if self.verbose > 0: |
| 208 | print('OK: Finished (includes %s) %s / %s proto / zlib=%s / SSL=%s. %d combinations tested.' |
| 209 | % (genpydir, try_server, try_proto, with_zlib, with_ssl, test_count)) |
| 210 | return True |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 211 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 212 | def test_feature(self, name, values): |
| 213 | test_count = 0 |
| 214 | conf = self.default_conf() |
| 215 | for try_server in values: |
| 216 | conf[name] = try_server |
| 217 | if self.run(conf, test_count): |
| 218 | test_count += 1 |
| 219 | return test_count |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 220 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 221 | def run_all_tests(self): |
| 222 | test_count = 0 |
| 223 | for try_server in self.servers: |
| 224 | for genpydir in self.gendirs: |
| 225 | for try_proto in PROTOS: |
| 226 | for with_zlib in (False, True): |
| 227 | # skip any servers that don't work with the Zlib transport |
| 228 | if with_zlib and try_server in SKIP_ZLIB: |
| 229 | continue |
| 230 | for with_ssl in (False, True): |
| 231 | # skip any servers that don't work with SSL |
| 232 | if with_ssl and try_server in SKIP_SSL: |
| 233 | continue |
| 234 | test_count += 1 |
| 235 | if self.verbose > 0: |
| 236 | print('\nTest run #%d: (includes %s) Server=%s, Proto=%s, zlib=%s, SSL=%s' |
| 237 | % (test_count, genpydir, try_server, try_proto, with_zlib, with_ssl)) |
| 238 | runServiceTest(self.libdir, self.genbase, genpydir, try_server, try_proto, self.port, with_zlib, with_ssl) |
| 239 | if self.verbose > 0: |
| 240 | print('OK: Finished (includes %s) %s / %s proto / zlib=%s / SSL=%s. %d combinations tested.' |
| 241 | % (genpydir, try_server, try_proto, with_zlib, with_ssl, test_count)) |
| 242 | return test_count |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 243 | |
| 244 | |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 245 | def main(): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 246 | parser = OptionParser() |
| 247 | parser.add_option('--all', action="store_true", dest='all') |
| 248 | parser.add_option('--genpydirs', type='string', dest='genpydirs', |
| 249 | default='default,slots,oldstyle,no_utf8strings,dynamic,dynamicslots', |
| 250 | help='directory extensions for generated code, used as suffixes for \"gen-py-*\" added sys.path for individual tests') |
| 251 | parser.add_option("--port", type="int", dest="port", default=9090, |
| 252 | help="port number for server to listen on") |
| 253 | parser.add_option('-v', '--verbose', action="store_const", |
| 254 | dest="verbose", const=2, |
| 255 | help="verbose output") |
| 256 | parser.add_option('-q', '--quiet', action="store_const", |
| 257 | dest="verbose", const=0, |
| 258 | help="minimal output") |
Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame^] | 259 | parser.add_option('-L', '--libdir', dest="libdir", default=local_libpath(), |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 260 | help="directory path that contains Thrift Python library") |
| 261 | parser.add_option('--gen-base', dest="gen_base", default=SCRIPT_DIR, |
| 262 | help="directory path that contains Thrift Python library") |
| 263 | parser.set_defaults(verbose=1) |
| 264 | options, args = parser.parse_args() |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 265 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 266 | generated_dirs = [] |
| 267 | for gp_dir in options.genpydirs.split(','): |
| 268 | generated_dirs.append('gen-py-%s' % (gp_dir)) |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 269 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 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.gen_base, 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('----------------') |
| 287 | for genpydir in generated_dirs: |
| 288 | for script in SCRIPTS: |
| 289 | runScriptTest(options.libdir, options.gen_base, 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() |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 301 | else: |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 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]) |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 307 | |
| 308 | |
| 309 | if __name__ == '__main__': |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 310 | sys.exit(main()) |