blob: f9121c8bbfa149beb7b8e989f4a225919e83a216 [file] [log] [blame]
Mark Slee5299a952007-10-05 00:13:24 +00001#!/usr/bin/env python
2
David Reissea2cba82009-03-30 21:35:00 +00003#
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 Duxbury59d4efd2011-03-21 17:38:22 +000022from __future__ import division
David Reissbcaa2ad2008-06-10 22:55:26 +000023import time
Mark Slee5299a952007-10-05 00:13:24 +000024import subprocess
25import sys
26import os
27import signal
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000028from optparse import OptionParser
29
30parser = OptionParser()
Roger Meierf4eec7a2011-09-11 18:16:21 +000031parser.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 Duxbury59d4efd2011-03-21 17:38:22 +000034parser.add_option("--port", type="int", dest="port", default=9090,
35 help="port number for server to listen on")
Bryan Duxbury16066592011-03-22 18:06:04 +000036parser.add_option('-v', '--verbose', action="store_const",
37 dest="verbose", const=2,
38 help="verbose output")
39parser.add_option('-q', '--quiet', action="store_const",
40 dest="verbose", const=0,
41 help="minimal output")
42parser.set_defaults(verbose=1)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000043options, args = parser.parse_args()
44
Roger Meierf4eec7a2011-09-11 18:16:21 +000045generated_dirs = []
46for gp_dir in options.genpydirs.split(','):
47 generated_dirs.append('gen-py-%s' % (gp_dir))
48
49SCRIPTS = ['SerializationTest.py', 'TestEof.py', 'TestSyntax.py', 'TestSocket.py']
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000050FRAMED = ["TNonblockingServer"]
Bryan Duxbury16066592011-03-22 18:06:04 +000051SKIP_ZLIB = ['TNonblockingServer', 'THttpServer']
52SKIP_SSL = ['TNonblockingServer', 'THttpServer']
Roger Meierf4eec7a2011-09-11 18:16:21 +000053EXTRA_DELAY = dict(TProcessPoolServer=3.5)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000054
55PROTOS= [
56 'accel',
57 'binary',
Roger Meier85fb6de2012-11-02 00:05:42 +000058 'compact']
59# FIXME: add json
60# disabled because json HTTP test hangs... why?
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000061
62SERVERS = [
63 "TSimpleServer",
64 "TThreadedServer",
65 "TThreadPoolServer",
66 "TProcessPoolServer", # new!
67 "TForkingServer",
68 "TNonblockingServer",
69 "THttpServer" ]
70
71# Test for presence of multiprocessing module, and if it is not present, then
72# remove it from the list of available servers.
73try:
74 import multiprocessing
75except:
76 print 'Warning: the multiprocessing module is unavailable. Skipping tests for TProcessPoolServer'
77 SERVERS.remove('TProcessPoolServer')
78
Bryan Duxbury16066592011-03-22 18:06:04 +000079try:
80 import ssl
81except:
82 print 'Warning, no ssl module available. Skipping all SSL tests.'
83 SKIP_SSL.extend(SERVERS)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000084
85# commandline permits a single class name to be specified to override SERVERS=[...]
86if len(args) == 1:
87 if args[0] in SERVERS:
88 SERVERS = args
89 else:
90 print 'Unavailable server type "%s", please choose one of: %s' % (args[0], SERVERS)
91 sys.exit(0)
92
Mark Slee5299a952007-10-05 00:13:24 +000093
David Reiss2a4bfd62008-04-07 23:45:00 +000094def relfile(fname):
95 return os.path.join(os.path.dirname(__file__), fname)
96
Roger Meierf4eec7a2011-09-11 18:16:21 +000097def runScriptTest(genpydir, script):
98 script_args = [sys.executable, relfile(script) ]
99 script_args.append('--genpydir=%s' % genpydir)
100 serverproc = subprocess.Popen(script_args)
101 print '\nTesting script: %s\n----' % (' '.join(script_args))
102 ret = subprocess.call(script_args)
103 if ret != 0:
104 raise Exception("Script subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(script_args)))
105
106def runServiceTest(genpydir, server_class, proto, port, use_zlib, use_ssl):
Bryan Duxbury16066592011-03-22 18:06:04 +0000107 # Build command line arguments
108 server_args = [sys.executable, relfile('TestServer.py') ]
109 cli_args = [sys.executable, relfile('TestClient.py') ]
110 for which in (server_args, cli_args):
Roger Meierf4eec7a2011-09-11 18:16:21 +0000111 which.append('--genpydir=%s' % genpydir)
Bryan Duxbury16066592011-03-22 18:06:04 +0000112 which.append('--proto=%s' % proto) # accel, binary or compact
113 which.append('--port=%d' % port) # default to 9090
114 if use_zlib:
115 which.append('--zlib')
116 if use_ssl:
117 which.append('--ssl')
118 if options.verbose == 0:
119 which.append('-q')
120 if options.verbose == 2:
121 which.append('-v')
122 # server-specific option to select server class
123 server_args.append(server_class)
124 # client-specific cmdline options
125 if server_class in FRAMED:
126 cli_args.append('--framed')
127 if server_class == 'THttpServer':
128 cli_args.append('--http=/')
129 if options.verbose > 0:
130 print 'Testing server %s: %s' % (server_class, ' '.join(server_args))
131 serverproc = subprocess.Popen(server_args)
Roger Meierf4eec7a2011-09-11 18:16:21 +0000132 time.sleep(0.15)
Bryan Duxbury16066592011-03-22 18:06:04 +0000133 try:
134 if options.verbose > 0:
135 print 'Testing client: %s' % (' '.join(cli_args))
136 ret = subprocess.call(cli_args)
137 if ret != 0:
138 raise Exception("Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args)))
139 finally:
140 # check that server didn't die
141 serverproc.poll()
142 if serverproc.returncode is not None:
143 print 'FAIL: Server process (%s) failed with retcode %d' % (' '.join(server_args), serverproc.returncode)
144 raise Exception('Server subprocess %s died, args: %s' % (server_class, ' '.join(server_args)))
145 else:
Roger Meierf4eec7a2011-09-11 18:16:21 +0000146 extra_sleep = EXTRA_DELAY.get(server_class, 0)
147 if extra_sleep > 0 and options.verbose > 0:
148 print 'Giving %s (proto=%s,zlib=%s,ssl=%s) an extra %d seconds for child processes to terminate via alarm' % (server_class,
149 proto, use_zlib, use_ssl, extra_sleep)
150 time.sleep(extra_sleep)
Bryan Duxbury16066592011-03-22 18:06:04 +0000151 os.kill(serverproc.pid, signal.SIGKILL)
152 # wait for shutdown
Roger Meierf4eec7a2011-09-11 18:16:21 +0000153 time.sleep(0.05)
David Reissbcaa2ad2008-06-10 22:55:26 +0000154
Bryan Duxbury16066592011-03-22 18:06:04 +0000155test_count = 0
Roger Meierf4eec7a2011-09-11 18:16:21 +0000156# run tests without a client/server first
157print '----------------'
158print ' Executing individual test scripts with various generated code directories'
159print ' Directories to be tested: ' + ', '.join(generated_dirs)
160print ' Scripts to be tested: ' + ', '.join(SCRIPTS)
161print '----------------'
162for genpydir in generated_dirs:
163 for script in SCRIPTS:
164 runScriptTest(genpydir, script)
165
166print '----------------'
167print ' Executing Client/Server tests with various generated code directories'
168print ' Servers to be tested: ' + ', '.join(SERVERS)
169print ' Directories to be tested: ' + ', '.join(generated_dirs)
170print ' Protocols to be tested: ' + ', '.join(PROTOS)
171print ' Options to be tested: ZLIB(yes/no), SSL(yes/no)'
172print '----------------'
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000173for try_server in SERVERS:
Roger Meierf4eec7a2011-09-11 18:16:21 +0000174 for genpydir in generated_dirs:
175 for try_proto in PROTOS:
176 for with_zlib in (False, True):
177 # skip any servers that don't work with the Zlib transport
178 if with_zlib and try_server in SKIP_ZLIB:
Bryan Duxbury16066592011-03-22 18:06:04 +0000179 continue
Roger Meierf4eec7a2011-09-11 18:16:21 +0000180 for with_ssl in (False, True):
181 # skip any servers that don't work with SSL
182 if with_ssl and try_server in SKIP_SSL:
183 continue
184 test_count += 1
185 if options.verbose > 0:
186 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)
187 runServiceTest(genpydir, try_server, try_proto, options.port, with_zlib, with_ssl)
188 if options.verbose > 0:
189 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)