blob: fa2a26411e45a3c45db393b46bfbcf1692618a79 [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
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090023from __future__ import print_function
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090024import copy
25import glob
26import os
27import signal
Roger Meier9b328532014-04-21 21:22:54 +020028import socket
Mark Slee5299a952007-10-05 00:13:24 +000029import subprocess
30import sys
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090031import time
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000032from optparse import OptionParser
33
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090034SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
35ROOT_DIR = os.path.dirname(os.path.dirname(SCRIPT_DIR))
36DEFAULT_LIBDIR_GLOB = os.path.join(ROOT_DIR, 'lib', 'py', 'build', 'lib.*')
37DEFAULT_LIBDIR_PY3 = os.path.join(ROOT_DIR, 'lib', 'py', 'build', 'lib')
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000038
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090039SCRIPTS = [
40 'TSimpleJSONProtocolTest.py',
41 'SerializationTest.py',
42 'TestEof.py',
43 'TestSyntax.py',
44 'TestSocket.py',
45]
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000046FRAMED = ["TNonblockingServer"]
Bryan Duxbury16066592011-03-22 18:06:04 +000047SKIP_ZLIB = ['TNonblockingServer', 'THttpServer']
48SKIP_SSL = ['TNonblockingServer', 'THttpServer']
Roger Meier6857b7f2015-09-16 19:53:07 +020049EXTRA_DELAY = dict(TProcessPoolServer=5.5)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000050
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090051PROTOS = [
52 'accel',
53 'binary',
54 'compact',
55 'json',
56]
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000057
58SERVERS = [
59 "TSimpleServer",
60 "TThreadedServer",
61 "TThreadPoolServer",
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090062 "TProcessPoolServer",
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000063 "TForkingServer",
64 "TNonblockingServer",
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090065 "THttpServer",
66]
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000067
Mark Slee5299a952007-10-05 00:13:24 +000068
David Reiss2a4bfd62008-04-07 23:45:00 +000069def relfile(fname):
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090070 return os.path.join(SCRIPT_DIR, fname)
David Reiss2a4bfd62008-04-07 23:45:00 +000071
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090072
73def setup_pypath(dirs):
74 env = copy.copy(os.environ)
75 pypath = env.get('PYTHONPATH', None)
76 if pypath:
77 dirs.append(pypath)
78 env['PYTHONPATH'] = ':'.join(dirs)
79 return env
80
81
82def runScriptTest(libdir, genpydir, script):
83 env = setup_pypath([libdir, genpydir])
84 script_args = [sys.executable, relfile(script)]
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090085 print('\nTesting script: %s\n----' % (' '.join(script_args)))
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090086 ret = subprocess.call(script_args, env=env)
Roger Meierf4eec7a2011-09-11 18:16:21 +000087 if ret != 0:
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090088 print('*** FAILED ***', file=sys.stderr)
89 print('LIBDIR: %s' % libdir, file=sys.stderr)
90 print('PY_GEN: %s' % genpydir, file=sys.stderr)
91 print('SCRIPT: %s' % script, file=sys.stderr)
Roger Meierf4eec7a2011-09-11 18:16:21 +000092 raise Exception("Script subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(script_args)))
Roger Meier76150722014-05-31 22:22:07 +020093
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090094
95def runServiceTest(libdir, genpydir, server_class, proto, port, use_zlib, use_ssl, verbose):
96 env = setup_pypath([libdir, genpydir])
Bryan Duxbury16066592011-03-22 18:06:04 +000097 # Build command line arguments
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090098 server_args = [sys.executable, relfile('TestServer.py')]
99 cli_args = [sys.executable, relfile('TestClient.py')]
Bryan Duxbury16066592011-03-22 18:06:04 +0000100 for which in (server_args, cli_args):
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900101 which.append('--protocol=%s' % proto) # accel, binary, compact or json
102 which.append('--port=%d' % port) # default to 9090
Bryan Duxbury16066592011-03-22 18:06:04 +0000103 if use_zlib:
104 which.append('--zlib')
105 if use_ssl:
106 which.append('--ssl')
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900107 if verbose == 0:
Bryan Duxbury16066592011-03-22 18:06:04 +0000108 which.append('-q')
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900109 if verbose == 2:
Bryan Duxbury16066592011-03-22 18:06:04 +0000110 which.append('-v')
111 # server-specific option to select server class
112 server_args.append(server_class)
113 # client-specific cmdline options
114 if server_class in FRAMED:
Roger Meier76150722014-05-31 22:22:07 +0200115 cli_args.append('--transport=framed')
116 else:
117 cli_args.append('--transport=buffered')
Bryan Duxbury16066592011-03-22 18:06:04 +0000118 if server_class == 'THttpServer':
119 cli_args.append('--http=/')
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900120 if verbose > 0:
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900121 print('Testing server %s: %s' % (server_class, ' '.join(server_args)))
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900122 serverproc = subprocess.Popen(server_args, env=env)
Roger Meier9b328532014-04-21 21:22:54 +0200123
124 def ensureServerAlive():
125 if serverproc.poll() is not None:
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900126 print(('FAIL: Server process (%s) failed with retcode %d')
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900127 % (' '.join(server_args), serverproc.returncode))
Roger Meier9b328532014-04-21 21:22:54 +0200128 raise Exception('Server subprocess %s died, args: %s'
129 % (server_class, ' '.join(server_args)))
130
131 # Wait for the server to start accepting connections on the given port.
132 sock = socket.socket()
133 sleep_time = 0.1 # Seconds
134 max_attempts = 100
135 try:
136 attempt = 0
137 while sock.connect_ex(('127.0.0.1', port)) != 0:
138 attempt += 1
139 if attempt >= max_attempts:
140 raise Exception("TestServer not ready on port %d after %.2f seconds"
141 % (port, sleep_time * attempt))
142 ensureServerAlive()
143 time.sleep(sleep_time)
144 finally:
145 sock.close()
146
Bryan Duxbury16066592011-03-22 18:06:04 +0000147 try:
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900148 if verbose > 0:
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900149 print('Testing client: %s' % (' '.join(cli_args)))
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900150 ret = subprocess.call(cli_args, env=env)
Bryan Duxbury16066592011-03-22 18:06:04 +0000151 if ret != 0:
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900152 print('*** FAILED ***', file=sys.stderr)
153 print('LIBDIR: %s' % libdir, file=sys.stderr)
154 print('PY_GEN: %s' % genpydir, file=sys.stderr)
Bryan Duxbury16066592011-03-22 18:06:04 +0000155 raise Exception("Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args)))
156 finally:
157 # check that server didn't die
Roger Meier9b328532014-04-21 21:22:54 +0200158 ensureServerAlive()
159 extra_sleep = EXTRA_DELAY.get(server_class, 0)
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900160 if extra_sleep > 0 and verbose > 0:
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900161 print('Giving %s (proto=%s,zlib=%s,ssl=%s) an extra %d seconds for child'
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900162 'processes to terminate via alarm'
163 % (server_class, proto, use_zlib, use_ssl, extra_sleep))
Roger Meier9b328532014-04-21 21:22:54 +0200164 time.sleep(extra_sleep)
165 os.kill(serverproc.pid, signal.SIGKILL)
166 serverproc.wait()
David Reissbcaa2ad2008-06-10 22:55:26 +0000167
Roger Meier76150722014-05-31 22:22:07 +0200168
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900169class TestCases(object):
170 def __init__(self, libdir, port, gendirs, servers, verbose):
171 self.libdir = libdir
172 self.port = port
173 self.verbose = verbose
174 self.gendirs = gendirs
175 self.servers = servers
176
177 def default_conf(self):
178 return {
179 'gendir': self.gendirs[0],
180 'server': self.servers[0],
181 'proto': PROTOS[0],
182 'zlib': False,
183 'ssl': False,
184 }
185
186 def run(self, conf, test_count):
187 with_zlib = conf['zlib']
188 with_ssl = conf['ssl']
189 try_server = conf['server']
190 try_proto = conf['proto']
191 genpydir = conf['gendir']
192 # skip any servers that don't work with the Zlib transport
193 if with_zlib and try_server in SKIP_ZLIB:
194 return False
195 # skip any servers that don't work with SSL
196 if with_ssl and try_server in SKIP_SSL:
197 return False
198 if self.verbose > 0:
199 print('\nTest run #%d: (includes %s) Server=%s, Proto=%s, zlib=%s, SSL=%s'
200 % (test_count, genpydir, try_server, try_proto, with_zlib, with_ssl))
201 runServiceTest(self.libdir, genpydir, try_server, try_proto, self.port, with_zlib, with_ssl, self.verbose)
202 if self.verbose > 0:
203 print('OK: Finished (includes %s) %s / %s proto / zlib=%s / SSL=%s. %d combinations tested.'
204 % (genpydir, try_server, try_proto, with_zlib, with_ssl, test_count))
205 return True
206
207 def test_feature(self, name, values):
208 test_count = 0
209 conf = self.default_conf()
210 for try_server in values:
211 conf[name] = try_server
212 if self.run(conf, test_count):
213 test_count += 1
214 return test_count
215
216 def run_all_tests(self):
217 test_count = 0
218 for try_server in self.servers:
219 for genpydir in self.gendirs:
220 for try_proto in PROTOS:
221 for with_zlib in (False, True):
222 # skip any servers that don't work with the Zlib transport
223 if with_zlib and try_server in SKIP_ZLIB:
224 continue
225 for with_ssl in (False, True):
226 # skip any servers that don't work with SSL
227 if with_ssl and try_server in SKIP_SSL:
228 continue
229 test_count += 1
230 if self.verbose > 0:
231 print('\nTest run #%d: (includes %s) Server=%s, Proto=%s, zlib=%s, SSL=%s'
232 % (test_count, genpydir, try_server, try_proto, with_zlib, with_ssl))
233 runServiceTest(self.libdir, genpydir, try_server, try_proto, self.port, with_zlib, with_ssl)
234 if self.verbose > 0:
235 print('OK: Finished (includes %s) %s / %s proto / zlib=%s / SSL=%s. %d combinations tested.'
236 % (genpydir, try_server, try_proto, with_zlib, with_ssl, test_count))
237 return test_count
238
239
240def default_libdir():
241 if sys.version_info[0] == 2:
242 return glob.glob(DEFAULT_LIBDIR_GLOB)[0]
243 else:
244 return DEFAULT_LIBDIR_PY3
245
246
247def main():
248 parser = OptionParser()
249 parser.add_option('--all', action="store_true", dest='all')
250 parser.add_option('--genpydirs', type='string', dest='genpydirs',
251 default='default,slots,newstyle,newstyleslots,dynamic,dynamicslots',
252 help='directory extensions for generated code, used as suffixes for \"gen-py-*\" added sys.path for individual tests')
253 parser.add_option("--port", type="int", dest="port", default=9090,
254 help="port number for server to listen on")
255 parser.add_option('-v', '--verbose', action="store_const",
256 dest="verbose", const=2,
257 help="verbose output")
258 parser.add_option('-q', '--quiet', action="store_const",
259 dest="verbose", const=0,
260 help="minimal output")
261 parser.add_option('-L', '--libdir', dest="libdir", default=default_libdir(),
262 help="directory path that contains Thrift Python library")
263 parser.set_defaults(verbose=1)
264 options, args = parser.parse_args()
265
266 generated_dirs = []
267 for gp_dir in options.genpydirs.split(','):
268 generated_dirs.append('gen-py-%s' % (gp_dir))
269
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.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('----------------')
Roger Meierf4eec7a2011-09-11 18:16:21 +0000287 for genpydir in generated_dirs:
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900288 for script in SCRIPTS:
289 runScriptTest(options.libdir, 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()
301 else:
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])
307
308
309if __name__ == '__main__':
310 sys.exit(main())