blob: 150f2be1e3163e7c1249a039ef278406b4192105 [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 Sukegawa10308cb2016-02-03 01:57:03 +090040 'FastbinaryTest.py',
41 'TestFrozen.py',
42 'TSimpleJSONProtocolTest.py',
43 'SerializationTest.py',
44 'TestEof.py',
45 'TestSyntax.py',
46 'TestSocket.py',
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090047]
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 = [
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090054 'accel',
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090055 'accelc',
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090056 'binary',
57 'compact',
58 'json',
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090059]
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000060
61SERVERS = [
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090062 "TSimpleServer",
63 "TThreadedServer",
64 "TThreadPoolServer",
65 "TProcessPoolServer",
66 "TForkingServer",
67 "TNonblockingServer",
68 "THttpServer",
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090069]
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000070
Mark Slee5299a952007-10-05 00:13:24 +000071
David Reiss2a4bfd62008-04-07 23:45:00 +000072def relfile(fname):
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090073 return os.path.join(SCRIPT_DIR, fname)
David Reiss2a4bfd62008-04-07 23:45:00 +000074
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090075
Nobuaki Sukegawaa3b88a02016-01-06 20:44:17 +090076def setup_pypath(libdir, gendir):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090077 dirs = [libdir, gendir]
78 env = copy.deepcopy(os.environ)
79 pypath = env.get('PYTHONPATH', None)
80 if pypath:
81 dirs.append(pypath)
82 env['PYTHONPATH'] = ':'.join(dirs)
83 if gendir.endswith('gen-py-no_utf8strings'):
84 env['THRIFT_TEST_PY_NO_UTF8STRINGS'] = '1'
85 return env
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090086
87
Nobuaki Sukegawaa3b88a02016-01-06 20:44:17 +090088def runScriptTest(libdir, genbase, genpydir, script):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090089 env = setup_pypath(libdir, os.path.join(genbase, genpydir))
90 script_args = [sys.executable, relfile(script)]
91 print('\nTesting script: %s\n----' % (' '.join(script_args)))
92 ret = subprocess.call(script_args, env=env)
93 if ret != 0:
94 print('*** FAILED ***', file=sys.stderr)
95 print('LIBDIR: %s' % libdir, file=sys.stderr)
96 print('PY_GEN: %s' % genpydir, file=sys.stderr)
97 print('SCRIPT: %s' % script, file=sys.stderr)
98 raise Exception("Script subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(script_args)))
Roger Meier76150722014-05-31 22:22:07 +020099
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900100
Nobuaki Sukegawaa3b88a02016-01-06 20:44:17 +0900101def runServiceTest(libdir, genbase, genpydir, server_class, proto, port, use_zlib, use_ssl, verbose):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900102 env = setup_pypath(libdir, os.path.join(genbase, genpydir))
103 # Build command line arguments
104 server_args = [sys.executable, relfile('TestServer.py')]
105 cli_args = [sys.executable, relfile('TestClient.py')]
106 for which in (server_args, cli_args):
107 which.append('--protocol=%s' % proto) # accel, binary, compact or json
108 which.append('--port=%d' % port) # default to 9090
109 if use_zlib:
110 which.append('--zlib')
111 if use_ssl:
112 which.append('--ssl')
113 if verbose == 0:
114 which.append('-q')
115 if verbose == 2:
116 which.append('-v')
117 # server-specific option to select server class
118 server_args.append(server_class)
119 # client-specific cmdline options
120 if server_class in FRAMED:
121 cli_args.append('--transport=framed')
122 else:
123 cli_args.append('--transport=buffered')
124 if server_class == 'THttpServer':
125 cli_args.append('--http=/')
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900126 if verbose > 0:
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900127 print('Testing server %s: %s' % (server_class, ' '.join(server_args)))
128 serverproc = subprocess.Popen(server_args, env=env)
129
130 def ensureServerAlive():
131 if serverproc.poll() is not None:
132 print(('FAIL: Server process (%s) failed with retcode %d')
133 % (' '.join(server_args), serverproc.returncode))
134 raise Exception('Server subprocess %s died, args: %s'
135 % (server_class, ' '.join(server_args)))
136
137 # Wait for the server to start accepting connections on the given port.
138 sock = socket.socket()
139 sleep_time = 0.1 # Seconds
140 max_attempts = 100
141 try:
142 attempt = 0
143 while sock.connect_ex(('127.0.0.1', port)) != 0:
144 attempt += 1
145 if attempt >= max_attempts:
146 raise Exception("TestServer not ready on port %d after %.2f seconds"
147 % (port, sleep_time * attempt))
148 ensureServerAlive()
149 time.sleep(sleep_time)
150 finally:
151 sock.close()
152
153 try:
154 if verbose > 0:
155 print('Testing client: %s' % (' '.join(cli_args)))
156 ret = subprocess.call(cli_args, env=env)
157 if ret != 0:
158 print('*** FAILED ***', file=sys.stderr)
159 print('LIBDIR: %s' % libdir, file=sys.stderr)
160 print('PY_GEN: %s' % genpydir, file=sys.stderr)
161 raise Exception("Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args)))
162 finally:
163 # check that server didn't die
164 ensureServerAlive()
165 extra_sleep = EXTRA_DELAY.get(server_class, 0)
166 if extra_sleep > 0 and verbose > 0:
167 print('Giving %s (proto=%s,zlib=%s,ssl=%s) an extra %d seconds for child'
168 'processes to terminate via alarm'
169 % (server_class, proto, use_zlib, use_ssl, extra_sleep))
170 time.sleep(extra_sleep)
171 os.kill(serverproc.pid, signal.SIGKILL)
172 serverproc.wait()
David Reissbcaa2ad2008-06-10 22:55:26 +0000173
Roger Meier76150722014-05-31 22:22:07 +0200174
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900175class TestCases(object):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900176 def __init__(self, genbase, libdir, port, gendirs, servers, verbose):
177 self.genbase = genbase
178 self.libdir = libdir
179 self.port = port
180 self.verbose = verbose
181 self.gendirs = gendirs
182 self.servers = servers
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900183
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900184 def default_conf(self):
185 return {
186 'gendir': self.gendirs[0],
187 'server': self.servers[0],
188 'proto': PROTOS[0],
189 'zlib': False,
190 'ssl': False,
191 }
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900192
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900193 def run(self, conf, test_count):
194 with_zlib = conf['zlib']
195 with_ssl = conf['ssl']
196 try_server = conf['server']
197 try_proto = conf['proto']
198 genpydir = conf['gendir']
199 # skip any servers that don't work with the Zlib transport
200 if with_zlib and try_server in SKIP_ZLIB:
201 return False
202 # skip any servers that don't work with SSL
203 if with_ssl and try_server in SKIP_SSL:
204 return False
205 if self.verbose > 0:
206 print('\nTest run #%d: (includes %s) Server=%s, Proto=%s, zlib=%s, SSL=%s'
207 % (test_count, genpydir, try_server, try_proto, with_zlib, with_ssl))
208 runServiceTest(self.libdir, self.genbase, genpydir, try_server, try_proto, self.port, with_zlib, with_ssl, self.verbose)
209 if self.verbose > 0:
210 print('OK: Finished (includes %s) %s / %s proto / zlib=%s / SSL=%s. %d combinations tested.'
211 % (genpydir, try_server, try_proto, with_zlib, with_ssl, test_count))
212 return True
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900213
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900214 def test_feature(self, name, values):
215 test_count = 0
216 conf = self.default_conf()
217 for try_server in values:
218 conf[name] = try_server
219 if self.run(conf, test_count):
220 test_count += 1
221 return test_count
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900222
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900223 def run_all_tests(self):
224 test_count = 0
225 for try_server in self.servers:
226 for genpydir in self.gendirs:
227 for try_proto in PROTOS:
228 for with_zlib in (False, True):
229 # skip any servers that don't work with the Zlib transport
230 if with_zlib and try_server in SKIP_ZLIB:
231 continue
232 for with_ssl in (False, True):
233 # skip any servers that don't work with SSL
234 if with_ssl and try_server in SKIP_SSL:
235 continue
236 test_count += 1
237 if self.verbose > 0:
238 print('\nTest run #%d: (includes %s) Server=%s, Proto=%s, zlib=%s, SSL=%s'
239 % (test_count, genpydir, try_server, try_proto, with_zlib, with_ssl))
240 runServiceTest(self.libdir, self.genbase, genpydir, try_server, try_proto, self.port, with_zlib, with_ssl)
241 if self.verbose > 0:
242 print('OK: Finished (includes %s) %s / %s proto / zlib=%s / SSL=%s. %d combinations tested.'
243 % (genpydir, try_server, try_proto, with_zlib, with_ssl, test_count))
244 return test_count
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900245
246
247def default_libdir():
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900248 if sys.version_info[0] == 2:
249 return glob.glob(DEFAULT_LIBDIR_GLOB)[0]
250 else:
251 return DEFAULT_LIBDIR_PY3
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900252
253
254def main():
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900255 parser = OptionParser()
256 parser.add_option('--all', action="store_true", dest='all')
257 parser.add_option('--genpydirs', type='string', dest='genpydirs',
258 default='default,slots,oldstyle,no_utf8strings,dynamic,dynamicslots',
259 help='directory extensions for generated code, used as suffixes for \"gen-py-*\" added sys.path for individual tests')
260 parser.add_option("--port", type="int", dest="port", default=9090,
261 help="port number for server to listen on")
262 parser.add_option('-v', '--verbose', action="store_const",
263 dest="verbose", const=2,
264 help="verbose output")
265 parser.add_option('-q', '--quiet', action="store_const",
266 dest="verbose", const=0,
267 help="minimal output")
268 parser.add_option('-L', '--libdir', dest="libdir", default=default_libdir(),
269 help="directory path that contains Thrift Python library")
270 parser.add_option('--gen-base', dest="gen_base", default=SCRIPT_DIR,
271 help="directory path that contains Thrift Python library")
272 parser.set_defaults(verbose=1)
273 options, args = parser.parse_args()
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900274
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900275 generated_dirs = []
276 for gp_dir in options.genpydirs.split(','):
277 generated_dirs.append('gen-py-%s' % (gp_dir))
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900278
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900279 # commandline permits a single class name to be specified to override SERVERS=[...]
280 servers = SERVERS
281 if len(args) == 1:
282 if args[0] in SERVERS:
283 servers = args
284 else:
285 print('Unavailable server type "%s", please choose one of: %s' % (args[0], servers))
286 sys.exit(0)
287
288 tests = TestCases(options.gen_base, options.libdir, options.port, generated_dirs, servers, options.verbose)
289
290 # run tests without a client/server first
291 print('----------------')
292 print(' Executing individual test scripts with various generated code directories')
293 print(' Directories to be tested: ' + ', '.join(generated_dirs))
294 print(' Scripts to be tested: ' + ', '.join(SCRIPTS))
295 print('----------------')
296 for genpydir in generated_dirs:
297 for script in SCRIPTS:
298 runScriptTest(options.libdir, options.gen_base, genpydir, script)
299
300 print('----------------')
301 print(' Executing Client/Server tests with various generated code directories')
302 print(' Servers to be tested: ' + ', '.join(servers))
303 print(' Directories to be tested: ' + ', '.join(generated_dirs))
304 print(' Protocols to be tested: ' + ', '.join(PROTOS))
305 print(' Options to be tested: ZLIB(yes/no), SSL(yes/no)')
306 print('----------------')
307
308 if options.all:
309 tests.run_all_tests()
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900310 else:
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900311 tests.test_feature('gendir', generated_dirs)
312 tests.test_feature('server', servers)
313 tests.test_feature('proto', PROTOS)
314 tests.test_feature('zlib', [False, True])
315 tests.test_feature('ssl', [False, True])
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900316
317
318if __name__ == '__main__':
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900319 sys.exit(main())