blob: b7f43320ad115e0965ce84fbd5bcae0ced22ffd5 [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
Roger Meier9b328532014-04-21 21:22:54 +020024import socket
Mark Slee5299a952007-10-05 00:13:24 +000025import subprocess
26import sys
27import os
28import signal
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000029from optparse import OptionParser
30
31parser = OptionParser()
Roger Meierf4eec7a2011-09-11 18:16:21 +000032parser.add_option('--genpydirs', type='string', dest='genpydirs',
33 default='default,slots,newstyle,newstyleslots,dynamic,dynamicslots',
34 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 +000035parser.add_option("--port", type="int", dest="port", default=9090,
36 help="port number for server to listen on")
Roger Meier76150722014-05-31 22:22:07 +020037parser.add_option('-v', '--verbose', action="store_const",
Bryan Duxbury16066592011-03-22 18:06:04 +000038 dest="verbose", const=2,
39 help="verbose output")
Roger Meier76150722014-05-31 22:22:07 +020040parser.add_option('-q', '--quiet', action="store_const",
Bryan Duxbury16066592011-03-22 18:06:04 +000041 dest="verbose", const=0,
42 help="minimal output")
43parser.set_defaults(verbose=1)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000044options, args = parser.parse_args()
45
Roger Meierf4eec7a2011-09-11 18:16:21 +000046generated_dirs = []
47for gp_dir in options.genpydirs.split(','):
48 generated_dirs.append('gen-py-%s' % (gp_dir))
49
Roger Meier0895dfe2012-12-26 22:09:55 +010050SCRIPTS = ['TSimpleJSONProtocolTest.py',
51 'SerializationTest.py',
52 'TestEof.py',
53 'TestSyntax.py',
54 'TestSocket.py']
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000055FRAMED = ["TNonblockingServer"]
Bryan Duxbury16066592011-03-22 18:06:04 +000056SKIP_ZLIB = ['TNonblockingServer', 'THttpServer']
57SKIP_SSL = ['TNonblockingServer', 'THttpServer']
Roger Meier6857b7f2015-09-16 19:53:07 +020058EXTRA_DELAY = dict(TProcessPoolServer=5.5)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000059
60PROTOS= [
61 'accel',
62 'binary',
Roger Meier840f3ef2015-09-21 23:11:46 +020063 'compact',
64 'json']
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000065
66SERVERS = [
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.
77try:
78 import multiprocessing
79except:
80 print 'Warning: the multiprocessing module is unavailable. Skipping tests for TProcessPoolServer'
81 SERVERS.remove('TProcessPoolServer')
82
Bryan Duxbury16066592011-03-22 18:06:04 +000083try:
84 import ssl
85except:
86 print 'Warning, no ssl module available. Skipping all SSL tests.'
87 SKIP_SSL.extend(SERVERS)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000088
89# commandline permits a single class name to be specified to override SERVERS=[...]
90if 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 Slee5299a952007-10-05 00:13:24 +000097
David Reiss2a4bfd62008-04-07 23:45:00 +000098def relfile(fname):
99 return os.path.join(os.path.dirname(__file__), fname)
100
Roger Meierf4eec7a2011-09-11 18:16:21 +0000101def 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)))
Roger Meier76150722014-05-31 22:22:07 +0200109
Roger Meierf4eec7a2011-09-11 18:16:21 +0000110def runServiceTest(genpydir, server_class, proto, port, use_zlib, use_ssl):
Bryan Duxbury16066592011-03-22 18:06:04 +0000111 # 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 Meierf4eec7a2011-09-11 18:16:21 +0000115 which.append('--genpydir=%s' % genpydir)
Roger Meier76150722014-05-31 22:22:07 +0200116 which.append('--protocol=%s' % proto) # accel, binary or compact
Bryan Duxbury16066592011-03-22 18:06:04 +0000117 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:
Roger Meier76150722014-05-31 22:22:07 +0200130 cli_args.append('--transport=framed')
131 else:
132 cli_args.append('--transport=buffered')
Bryan Duxbury16066592011-03-22 18:06:04 +0000133 if server_class == 'THttpServer':
134 cli_args.append('--http=/')
135 if options.verbose > 0:
136 print 'Testing server %s: %s' % (server_class, ' '.join(server_args))
137 serverproc = subprocess.Popen(server_args)
Roger Meier9b328532014-04-21 21:22:54 +0200138
139 def ensureServerAlive():
140 if serverproc.poll() is not None:
141 print ('FAIL: Server process (%s) failed with retcode %d'
142 % (' '.join(server_args), serverproc.returncode))
143 raise Exception('Server subprocess %s died, args: %s'
144 % (server_class, ' '.join(server_args)))
145
146 # Wait for the server to start accepting connections on the given port.
147 sock = socket.socket()
148 sleep_time = 0.1 # Seconds
149 max_attempts = 100
150 try:
151 attempt = 0
152 while sock.connect_ex(('127.0.0.1', port)) != 0:
153 attempt += 1
154 if attempt >= max_attempts:
155 raise Exception("TestServer not ready on port %d after %.2f seconds"
156 % (port, sleep_time * attempt))
157 ensureServerAlive()
158 time.sleep(sleep_time)
159 finally:
160 sock.close()
161
Bryan Duxbury16066592011-03-22 18:06:04 +0000162 try:
163 if options.verbose > 0:
164 print 'Testing client: %s' % (' '.join(cli_args))
165 ret = subprocess.call(cli_args)
166 if ret != 0:
167 raise Exception("Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args)))
168 finally:
169 # check that server didn't die
Roger Meier9b328532014-04-21 21:22:54 +0200170 ensureServerAlive()
171 extra_sleep = EXTRA_DELAY.get(server_class, 0)
172 if extra_sleep > 0 and options.verbose > 0:
173 print ('Giving %s (proto=%s,zlib=%s,ssl=%s) an extra %d seconds for child'
174 'processes to terminate via alarm'
175 % (server_class, proto, use_zlib, use_ssl, extra_sleep))
176 time.sleep(extra_sleep)
177 os.kill(serverproc.pid, signal.SIGKILL)
178 serverproc.wait()
David Reissbcaa2ad2008-06-10 22:55:26 +0000179
Bryan Duxbury16066592011-03-22 18:06:04 +0000180test_count = 0
Roger Meierf4eec7a2011-09-11 18:16:21 +0000181# run tests without a client/server first
182print '----------------'
183print ' Executing individual test scripts with various generated code directories'
184print ' Directories to be tested: ' + ', '.join(generated_dirs)
185print ' Scripts to be tested: ' + ', '.join(SCRIPTS)
186print '----------------'
187for genpydir in generated_dirs:
188 for script in SCRIPTS:
189 runScriptTest(genpydir, script)
Roger Meier76150722014-05-31 22:22:07 +0200190
Roger Meierf4eec7a2011-09-11 18:16:21 +0000191print '----------------'
192print ' Executing Client/Server tests with various generated code directories'
193print ' Servers to be tested: ' + ', '.join(SERVERS)
194print ' Directories to be tested: ' + ', '.join(generated_dirs)
195print ' Protocols to be tested: ' + ', '.join(PROTOS)
196print ' Options to be tested: ZLIB(yes/no), SSL(yes/no)'
197print '----------------'
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000198for try_server in SERVERS:
Roger Meierf4eec7a2011-09-11 18:16:21 +0000199 for genpydir in generated_dirs:
200 for try_proto in PROTOS:
201 for with_zlib in (False, True):
202 # skip any servers that don't work with the Zlib transport
203 if with_zlib and try_server in SKIP_ZLIB:
Bryan Duxbury16066592011-03-22 18:06:04 +0000204 continue
Roger Meierf4eec7a2011-09-11 18:16:21 +0000205 for with_ssl in (False, True):
206 # skip any servers that don't work with SSL
207 if with_ssl and try_server in SKIP_SSL:
208 continue
209 test_count += 1
210 if options.verbose > 0:
211 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)
212 runServiceTest(genpydir, try_server, try_proto, options.port, with_zlib, with_ssl)
213 if options.verbose > 0:
214 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)