blob: f83f5574c832c618275c6f6c35ebcbed22ad63dc [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 = [
Nobuaki Sukegawa7b894692015-12-23 21:45:06 +090040 'FastbinaryTest.py',
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +090041 'TestFrozen.py',
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090042 'TSimpleJSONProtocolTest.py',
43 'SerializationTest.py',
44 'TestEof.py',
45 'TestSyntax.py',
46 'TestSocket.py',
47]
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000048FRAMED = ["TNonblockingServer"]
Bryan Duxbury16066592011-03-22 18:06:04 +000049SKIP_ZLIB = ['TNonblockingServer', 'THttpServer']
50SKIP_SSL = ['TNonblockingServer', 'THttpServer']
Roger Meier6857b7f2015-09-16 19:53:07 +020051EXTRA_DELAY = dict(TProcessPoolServer=5.5)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000052
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090053PROTOS = [
54 'accel',
55 'binary',
56 'compact',
57 'json',
58]
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000059
60SERVERS = [
61 "TSimpleServer",
62 "TThreadedServer",
63 "TThreadPoolServer",
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090064 "TProcessPoolServer",
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000065 "TForkingServer",
66 "TNonblockingServer",
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090067 "THttpServer",
68]
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000069
Mark Slee5299a952007-10-05 00:13:24 +000070
David Reiss2a4bfd62008-04-07 23:45:00 +000071def relfile(fname):
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090072 return os.path.join(SCRIPT_DIR, fname)
David Reiss2a4bfd62008-04-07 23:45:00 +000073
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090074
75def setup_pypath(dirs):
76 env = copy.copy(os.environ)
77 pypath = env.get('PYTHONPATH', None)
78 if pypath:
79 dirs.append(pypath)
80 env['PYTHONPATH'] = ':'.join(dirs)
81 return env
82
83
84def runScriptTest(libdir, genpydir, script):
85 env = setup_pypath([libdir, genpydir])
86 script_args = [sys.executable, relfile(script)]
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090087 print('\nTesting script: %s\n----' % (' '.join(script_args)))
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090088 ret = subprocess.call(script_args, env=env)
Roger Meierf4eec7a2011-09-11 18:16:21 +000089 if ret != 0:
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090090 print('*** FAILED ***', file=sys.stderr)
91 print('LIBDIR: %s' % libdir, file=sys.stderr)
92 print('PY_GEN: %s' % genpydir, file=sys.stderr)
93 print('SCRIPT: %s' % script, file=sys.stderr)
Roger Meierf4eec7a2011-09-11 18:16:21 +000094 raise Exception("Script subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(script_args)))
Roger Meier76150722014-05-31 22:22:07 +020095
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090096
97def runServiceTest(libdir, genpydir, server_class, proto, port, use_zlib, use_ssl, verbose):
98 env = setup_pypath([libdir, genpydir])
Bryan Duxbury16066592011-03-22 18:06:04 +000099 # Build command line arguments
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900100 server_args = [sys.executable, relfile('TestServer.py')]
101 cli_args = [sys.executable, relfile('TestClient.py')]
Bryan Duxbury16066592011-03-22 18:06:04 +0000102 for which in (server_args, cli_args):
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900103 which.append('--protocol=%s' % proto) # accel, binary, compact or json
104 which.append('--port=%d' % port) # default to 9090
Bryan Duxbury16066592011-03-22 18:06:04 +0000105 if use_zlib:
106 which.append('--zlib')
107 if use_ssl:
108 which.append('--ssl')
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900109 if verbose == 0:
Bryan Duxbury16066592011-03-22 18:06:04 +0000110 which.append('-q')
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900111 if verbose == 2:
Bryan Duxbury16066592011-03-22 18:06:04 +0000112 which.append('-v')
113 # server-specific option to select server class
114 server_args.append(server_class)
115 # client-specific cmdline options
116 if server_class in FRAMED:
Roger Meier76150722014-05-31 22:22:07 +0200117 cli_args.append('--transport=framed')
118 else:
119 cli_args.append('--transport=buffered')
Bryan Duxbury16066592011-03-22 18:06:04 +0000120 if server_class == 'THttpServer':
121 cli_args.append('--http=/')
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900122 if verbose > 0:
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900123 print('Testing server %s: %s' % (server_class, ' '.join(server_args)))
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900124 serverproc = subprocess.Popen(server_args, env=env)
Roger Meier9b328532014-04-21 21:22:54 +0200125
126 def ensureServerAlive():
127 if serverproc.poll() is not None:
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900128 print(('FAIL: Server process (%s) failed with retcode %d')
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900129 % (' '.join(server_args), serverproc.returncode))
Roger Meier9b328532014-04-21 21:22:54 +0200130 raise Exception('Server subprocess %s died, args: %s'
131 % (server_class, ' '.join(server_args)))
132
133 # Wait for the server to start accepting connections on the given port.
134 sock = socket.socket()
135 sleep_time = 0.1 # Seconds
136 max_attempts = 100
137 try:
138 attempt = 0
139 while sock.connect_ex(('127.0.0.1', port)) != 0:
140 attempt += 1
141 if attempt >= max_attempts:
142 raise Exception("TestServer not ready on port %d after %.2f seconds"
143 % (port, sleep_time * attempt))
144 ensureServerAlive()
145 time.sleep(sleep_time)
146 finally:
147 sock.close()
148
Bryan Duxbury16066592011-03-22 18:06:04 +0000149 try:
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900150 if verbose > 0:
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900151 print('Testing client: %s' % (' '.join(cli_args)))
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900152 ret = subprocess.call(cli_args, env=env)
Bryan Duxbury16066592011-03-22 18:06:04 +0000153 if ret != 0:
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900154 print('*** FAILED ***', file=sys.stderr)
155 print('LIBDIR: %s' % libdir, file=sys.stderr)
156 print('PY_GEN: %s' % genpydir, file=sys.stderr)
Bryan Duxbury16066592011-03-22 18:06:04 +0000157 raise Exception("Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args)))
158 finally:
159 # check that server didn't die
Roger Meier9b328532014-04-21 21:22:54 +0200160 ensureServerAlive()
161 extra_sleep = EXTRA_DELAY.get(server_class, 0)
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900162 if extra_sleep > 0 and verbose > 0:
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900163 print('Giving %s (proto=%s,zlib=%s,ssl=%s) an extra %d seconds for child'
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900164 'processes to terminate via alarm'
165 % (server_class, proto, use_zlib, use_ssl, extra_sleep))
Roger Meier9b328532014-04-21 21:22:54 +0200166 time.sleep(extra_sleep)
167 os.kill(serverproc.pid, signal.SIGKILL)
168 serverproc.wait()
David Reissbcaa2ad2008-06-10 22:55:26 +0000169
Roger Meier76150722014-05-31 22:22:07 +0200170
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900171class TestCases(object):
172 def __init__(self, libdir, port, gendirs, servers, verbose):
173 self.libdir = libdir
174 self.port = port
175 self.verbose = verbose
176 self.gendirs = gendirs
177 self.servers = servers
178
179 def default_conf(self):
180 return {
181 'gendir': self.gendirs[0],
182 'server': self.servers[0],
183 'proto': PROTOS[0],
184 'zlib': False,
185 'ssl': False,
186 }
187
188 def run(self, conf, test_count):
189 with_zlib = conf['zlib']
190 with_ssl = conf['ssl']
191 try_server = conf['server']
192 try_proto = conf['proto']
193 genpydir = conf['gendir']
194 # skip any servers that don't work with the Zlib transport
195 if with_zlib and try_server in SKIP_ZLIB:
196 return False
197 # skip any servers that don't work with SSL
198 if with_ssl and try_server in SKIP_SSL:
199 return False
200 if self.verbose > 0:
201 print('\nTest run #%d: (includes %s) Server=%s, Proto=%s, zlib=%s, SSL=%s'
202 % (test_count, genpydir, try_server, try_proto, with_zlib, with_ssl))
203 runServiceTest(self.libdir, genpydir, try_server, try_proto, self.port, with_zlib, with_ssl, self.verbose)
204 if self.verbose > 0:
205 print('OK: Finished (includes %s) %s / %s proto / zlib=%s / SSL=%s. %d combinations tested.'
206 % (genpydir, try_server, try_proto, with_zlib, with_ssl, test_count))
207 return True
208
209 def test_feature(self, name, values):
210 test_count = 0
211 conf = self.default_conf()
212 for try_server in values:
213 conf[name] = try_server
214 if self.run(conf, test_count):
215 test_count += 1
216 return test_count
217
218 def run_all_tests(self):
219 test_count = 0
220 for try_server in self.servers:
221 for genpydir in self.gendirs:
222 for try_proto in PROTOS:
223 for with_zlib in (False, True):
224 # skip any servers that don't work with the Zlib transport
225 if with_zlib and try_server in SKIP_ZLIB:
226 continue
227 for with_ssl in (False, True):
228 # skip any servers that don't work with SSL
229 if with_ssl and try_server in SKIP_SSL:
230 continue
231 test_count += 1
232 if self.verbose > 0:
233 print('\nTest run #%d: (includes %s) Server=%s, Proto=%s, zlib=%s, SSL=%s'
234 % (test_count, genpydir, try_server, try_proto, with_zlib, with_ssl))
235 runServiceTest(self.libdir, genpydir, try_server, try_proto, self.port, with_zlib, with_ssl)
236 if self.verbose > 0:
237 print('OK: Finished (includes %s) %s / %s proto / zlib=%s / SSL=%s. %d combinations tested.'
238 % (genpydir, try_server, try_proto, with_zlib, with_ssl, test_count))
239 return test_count
240
241
242def default_libdir():
243 if sys.version_info[0] == 2:
244 return glob.glob(DEFAULT_LIBDIR_GLOB)[0]
245 else:
246 return DEFAULT_LIBDIR_PY3
247
248
249def main():
250 parser = OptionParser()
251 parser.add_option('--all', action="store_true", dest='all')
252 parser.add_option('--genpydirs', type='string', dest='genpydirs',
253 default='default,slots,newstyle,newstyleslots,dynamic,dynamicslots',
254 help='directory extensions for generated code, used as suffixes for \"gen-py-*\" added sys.path for individual tests')
255 parser.add_option("--port", type="int", dest="port", default=9090,
256 help="port number for server to listen on")
257 parser.add_option('-v', '--verbose', action="store_const",
258 dest="verbose", const=2,
259 help="verbose output")
260 parser.add_option('-q', '--quiet', action="store_const",
261 dest="verbose", const=0,
262 help="minimal output")
263 parser.add_option('-L', '--libdir', dest="libdir", default=default_libdir(),
264 help="directory path that contains Thrift Python library")
265 parser.set_defaults(verbose=1)
266 options, args = parser.parse_args()
267
268 generated_dirs = []
269 for gp_dir in options.genpydirs.split(','):
270 generated_dirs.append('gen-py-%s' % (gp_dir))
271
272 # commandline permits a single class name to be specified to override SERVERS=[...]
273 servers = SERVERS
274 if len(args) == 1:
275 if args[0] in SERVERS:
276 servers = args
277 else:
278 print('Unavailable server type "%s", please choose one of: %s' % (args[0], servers))
279 sys.exit(0)
280
281 tests = TestCases(options.libdir, options.port, generated_dirs, servers, options.verbose)
282
283 # run tests without a client/server first
284 print('----------------')
285 print(' Executing individual test scripts with various generated code directories')
286 print(' Directories to be tested: ' + ', '.join(generated_dirs))
287 print(' Scripts to be tested: ' + ', '.join(SCRIPTS))
288 print('----------------')
Roger Meierf4eec7a2011-09-11 18:16:21 +0000289 for genpydir in generated_dirs:
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900290 for script in SCRIPTS:
291 runScriptTest(options.libdir, genpydir, script)
292
293 print('----------------')
294 print(' Executing Client/Server tests with various generated code directories')
295 print(' Servers to be tested: ' + ', '.join(servers))
296 print(' Directories to be tested: ' + ', '.join(generated_dirs))
297 print(' Protocols to be tested: ' + ', '.join(PROTOS))
298 print(' Options to be tested: ZLIB(yes/no), SSL(yes/no)')
299 print('----------------')
300
301 if options.all:
302 tests.run_all_tests()
303 else:
304 tests.test_feature('gendir', generated_dirs)
305 tests.test_feature('server', servers)
306 tests.test_feature('proto', PROTOS)
307 tests.test_feature('zlib', [False, True])
308 tests.test_feature('ssl', [False, True])
309
310
311if __name__ == '__main__':
312 sys.exit(main())