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