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 |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 23 | import time |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 24 | import subprocess |
| 25 | import sys |
| 26 | import os |
| 27 | import signal |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 28 | from optparse import OptionParser |
| 29 | |
| 30 | parser = OptionParser() |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame^] | 31 | parser.add_option('--genpydirs', type='string', dest='genpydirs', |
| 32 | default='default,slots,newstyle,newstyleslots,dynamic,dynamicslots', |
| 33 | help='directory extensions for generated code, used as suffixes for \"gen-py-*\" added sys.path for individual tests') |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 34 | parser.add_option("--port", type="int", dest="port", default=9090, |
| 35 | help="port number for server to listen on") |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 36 | parser.add_option('-v', '--verbose', action="store_const", |
| 37 | dest="verbose", const=2, |
| 38 | help="verbose output") |
| 39 | parser.add_option('-q', '--quiet', action="store_const", |
| 40 | dest="verbose", const=0, |
| 41 | help="minimal output") |
| 42 | parser.set_defaults(verbose=1) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 43 | options, args = parser.parse_args() |
| 44 | |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame^] | 45 | generated_dirs = [] |
| 46 | for gp_dir in options.genpydirs.split(','): |
| 47 | generated_dirs.append('gen-py-%s' % (gp_dir)) |
| 48 | |
| 49 | SCRIPTS = ['SerializationTest.py', 'TestEof.py', 'TestSyntax.py', 'TestSocket.py'] |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 50 | FRAMED = ["TNonblockingServer"] |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 51 | SKIP_ZLIB = ['TNonblockingServer', 'THttpServer'] |
| 52 | SKIP_SSL = ['TNonblockingServer', 'THttpServer'] |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame^] | 53 | EXTRA_DELAY = dict(TProcessPoolServer=3.5) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 54 | |
| 55 | PROTOS= [ |
| 56 | 'accel', |
| 57 | 'binary', |
| 58 | 'compact' ] |
| 59 | |
| 60 | SERVERS = [ |
| 61 | "TSimpleServer", |
| 62 | "TThreadedServer", |
| 63 | "TThreadPoolServer", |
| 64 | "TProcessPoolServer", # new! |
| 65 | "TForkingServer", |
| 66 | "TNonblockingServer", |
| 67 | "THttpServer" ] |
| 68 | |
| 69 | # Test for presence of multiprocessing module, and if it is not present, then |
| 70 | # remove it from the list of available servers. |
| 71 | try: |
| 72 | import multiprocessing |
| 73 | except: |
| 74 | print 'Warning: the multiprocessing module is unavailable. Skipping tests for TProcessPoolServer' |
| 75 | SERVERS.remove('TProcessPoolServer') |
| 76 | |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 77 | try: |
| 78 | import ssl |
| 79 | except: |
| 80 | print 'Warning, no ssl module available. Skipping all SSL tests.' |
| 81 | SKIP_SSL.extend(SERVERS) |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 82 | |
| 83 | # commandline permits a single class name to be specified to override SERVERS=[...] |
| 84 | if len(args) == 1: |
| 85 | if args[0] in SERVERS: |
| 86 | SERVERS = args |
| 87 | else: |
| 88 | print 'Unavailable server type "%s", please choose one of: %s' % (args[0], SERVERS) |
| 89 | sys.exit(0) |
| 90 | |
Mark Slee | 5299a95 | 2007-10-05 00:13:24 +0000 | [diff] [blame] | 91 | |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 92 | def relfile(fname): |
| 93 | return os.path.join(os.path.dirname(__file__), fname) |
| 94 | |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame^] | 95 | def runScriptTest(genpydir, script): |
| 96 | script_args = [sys.executable, relfile(script) ] |
| 97 | script_args.append('--genpydir=%s' % genpydir) |
| 98 | serverproc = subprocess.Popen(script_args) |
| 99 | print '\nTesting script: %s\n----' % (' '.join(script_args)) |
| 100 | ret = subprocess.call(script_args) |
| 101 | if ret != 0: |
| 102 | raise Exception("Script subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(script_args))) |
| 103 | |
| 104 | def runServiceTest(genpydir, server_class, proto, port, use_zlib, use_ssl): |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 105 | # Build command line arguments |
| 106 | server_args = [sys.executable, relfile('TestServer.py') ] |
| 107 | cli_args = [sys.executable, relfile('TestClient.py') ] |
| 108 | for which in (server_args, cli_args): |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame^] | 109 | which.append('--genpydir=%s' % genpydir) |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 110 | which.append('--proto=%s' % proto) # accel, binary or compact |
| 111 | which.append('--port=%d' % port) # default to 9090 |
| 112 | if use_zlib: |
| 113 | which.append('--zlib') |
| 114 | if use_ssl: |
| 115 | which.append('--ssl') |
| 116 | if options.verbose == 0: |
| 117 | which.append('-q') |
| 118 | if options.verbose == 2: |
| 119 | which.append('-v') |
| 120 | # server-specific option to select server class |
| 121 | server_args.append(server_class) |
| 122 | # client-specific cmdline options |
| 123 | if server_class in FRAMED: |
| 124 | cli_args.append('--framed') |
| 125 | if server_class == 'THttpServer': |
| 126 | cli_args.append('--http=/') |
| 127 | if options.verbose > 0: |
| 128 | print 'Testing server %s: %s' % (server_class, ' '.join(server_args)) |
| 129 | serverproc = subprocess.Popen(server_args) |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame^] | 130 | time.sleep(0.15) |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 131 | try: |
| 132 | if options.verbose > 0: |
| 133 | print 'Testing client: %s' % (' '.join(cli_args)) |
| 134 | ret = subprocess.call(cli_args) |
| 135 | if ret != 0: |
| 136 | raise Exception("Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args))) |
| 137 | finally: |
| 138 | # check that server didn't die |
| 139 | serverproc.poll() |
| 140 | if serverproc.returncode is not None: |
| 141 | print 'FAIL: Server process (%s) failed with retcode %d' % (' '.join(server_args), serverproc.returncode) |
| 142 | raise Exception('Server subprocess %s died, args: %s' % (server_class, ' '.join(server_args))) |
| 143 | else: |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame^] | 144 | extra_sleep = EXTRA_DELAY.get(server_class, 0) |
| 145 | if extra_sleep > 0 and options.verbose > 0: |
| 146 | print 'Giving %s (proto=%s,zlib=%s,ssl=%s) an extra %d seconds for child processes to terminate via alarm' % (server_class, |
| 147 | proto, use_zlib, use_ssl, extra_sleep) |
| 148 | time.sleep(extra_sleep) |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 149 | os.kill(serverproc.pid, signal.SIGKILL) |
| 150 | # wait for shutdown |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame^] | 151 | time.sleep(0.05) |
David Reiss | bcaa2ad | 2008-06-10 22:55:26 +0000 | [diff] [blame] | 152 | |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 153 | test_count = 0 |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame^] | 154 | # run tests without a client/server first |
| 155 | print '----------------' |
| 156 | print ' Executing individual test scripts with various generated code directories' |
| 157 | print ' Directories to be tested: ' + ', '.join(generated_dirs) |
| 158 | print ' Scripts to be tested: ' + ', '.join(SCRIPTS) |
| 159 | print '----------------' |
| 160 | for genpydir in generated_dirs: |
| 161 | for script in SCRIPTS: |
| 162 | runScriptTest(genpydir, script) |
| 163 | |
| 164 | print '----------------' |
| 165 | print ' Executing Client/Server tests with various generated code directories' |
| 166 | print ' Servers to be tested: ' + ', '.join(SERVERS) |
| 167 | print ' Directories to be tested: ' + ', '.join(generated_dirs) |
| 168 | print ' Protocols to be tested: ' + ', '.join(PROTOS) |
| 169 | print ' Options to be tested: ZLIB(yes/no), SSL(yes/no)' |
| 170 | print '----------------' |
Bryan Duxbury | 59d4efd | 2011-03-21 17:38:22 +0000 | [diff] [blame] | 171 | for try_server in SERVERS: |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame^] | 172 | for genpydir in generated_dirs: |
| 173 | for try_proto in PROTOS: |
| 174 | for with_zlib in (False, True): |
| 175 | # skip any servers that don't work with the Zlib transport |
| 176 | if with_zlib and try_server in SKIP_ZLIB: |
Bryan Duxbury | 1606659 | 2011-03-22 18:06:04 +0000 | [diff] [blame] | 177 | continue |
Roger Meier | f4eec7a | 2011-09-11 18:16:21 +0000 | [diff] [blame^] | 178 | for with_ssl in (False, True): |
| 179 | # skip any servers that don't work with SSL |
| 180 | if with_ssl and try_server in SKIP_SSL: |
| 181 | continue |
| 182 | test_count += 1 |
| 183 | if options.verbose > 0: |
| 184 | print '\nTest run #%d: (includes %s) Server=%s, Proto=%s, zlib=%s, SSL=%s' % (test_count, genpydir, try_server, try_proto, with_zlib, with_ssl) |
| 185 | runServiceTest(genpydir, try_server, try_proto, options.port, with_zlib, with_ssl) |
| 186 | if options.verbose > 0: |
| 187 | print 'OK: Finished (includes %s) %s / %s proto / zlib=%s / SSL=%s. %d combinations tested.' % (genpydir, try_server, try_proto, with_zlib, with_ssl, test_count) |