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