blob: c1443ec17849ab8efc36b0800fd6f61fb4fbfad6 [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 Sukegawa06e8fd42016-02-28 12:50:03 +090024import platform
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090025import copy
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090026import 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 Sukegawa7af189a2016-02-11 16:21:01 +090034from util import local_libpath
35
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090036SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000037
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090038SCRIPTS = [
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090039 'FastbinaryTest.py',
40 'TestFrozen.py',
41 'TSimpleJSONProtocolTest.py',
42 'SerializationTest.py',
43 'TestEof.py',
44 'TestSyntax.py',
45 'TestSocket.py',
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090046]
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000047FRAMED = ["TNonblockingServer"]
Bryan Duxbury16066592011-03-22 18:06:04 +000048SKIP_ZLIB = ['TNonblockingServer', 'THttpServer']
49SKIP_SSL = ['TNonblockingServer', 'THttpServer']
Roger Meier6857b7f2015-09-16 19:53:07 +020050EXTRA_DELAY = dict(TProcessPoolServer=5.5)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000051
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090052PROTOS = [
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090053 'accel',
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090054 'accelc',
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090055 'binary',
56 'compact',
57 'json',
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090058]
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000059
Nobuaki Sukegawa06e8fd42016-02-28 12:50:03 +090060
61def default_servers():
62 servers = [
63 'TSimpleServer',
64 'TThreadedServer',
65 'TThreadPoolServer',
66 'TNonblockingServer',
67 'THttpServer',
68 ]
69 if platform.system() != 'Windows':
70 servers.append('TProcessPoolServer')
71 servers.append('TForkingServer')
72 return servers
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000073
Mark Slee5299a952007-10-05 00:13:24 +000074
David Reiss2a4bfd62008-04-07 23:45:00 +000075def relfile(fname):
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090076 return os.path.join(SCRIPT_DIR, fname)
David Reiss2a4bfd62008-04-07 23:45:00 +000077
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090078
Nobuaki Sukegawaa3b88a02016-01-06 20:44:17 +090079def setup_pypath(libdir, gendir):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090080 dirs = [libdir, gendir]
81 env = copy.deepcopy(os.environ)
82 pypath = env.get('PYTHONPATH', None)
83 if pypath:
84 dirs.append(pypath)
Nobuaki Sukegawa06e8fd42016-02-28 12:50:03 +090085 env['PYTHONPATH'] = os.pathsep.join(dirs)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090086 if gendir.endswith('gen-py-no_utf8strings'):
87 env['THRIFT_TEST_PY_NO_UTF8STRINGS'] = '1'
88 return env
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090089
90
Nobuaki Sukegawaa3b88a02016-01-06 20:44:17 +090091def runScriptTest(libdir, genbase, genpydir, script):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090092 env = setup_pypath(libdir, os.path.join(genbase, genpydir))
93 script_args = [sys.executable, relfile(script)]
94 print('\nTesting script: %s\n----' % (' '.join(script_args)))
95 ret = subprocess.call(script_args, env=env)
96 if ret != 0:
97 print('*** FAILED ***', file=sys.stderr)
98 print('LIBDIR: %s' % libdir, file=sys.stderr)
99 print('PY_GEN: %s' % genpydir, file=sys.stderr)
100 print('SCRIPT: %s' % script, file=sys.stderr)
101 raise Exception("Script subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(script_args)))
Roger Meier76150722014-05-31 22:22:07 +0200102
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900103
Nobuaki Sukegawaa3b88a02016-01-06 20:44:17 +0900104def runServiceTest(libdir, genbase, genpydir, server_class, proto, port, use_zlib, use_ssl, verbose):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900105 env = setup_pypath(libdir, os.path.join(genbase, genpydir))
106 # Build command line arguments
107 server_args = [sys.executable, relfile('TestServer.py')]
108 cli_args = [sys.executable, relfile('TestClient.py')]
109 for which in (server_args, cli_args):
110 which.append('--protocol=%s' % proto) # accel, binary, compact or json
111 which.append('--port=%d' % port) # default to 9090
112 if use_zlib:
113 which.append('--zlib')
114 if use_ssl:
115 which.append('--ssl')
116 if verbose == 0:
117 which.append('-q')
118 if verbose == 2:
119 which.append('-v')
120 # server-specific option to select server class
121 server_args.append(server_class)
122 # client-specific cmdline options
123 if server_class in FRAMED:
124 cli_args.append('--transport=framed')
125 else:
126 cli_args.append('--transport=buffered')
127 if server_class == 'THttpServer':
128 cli_args.append('--http=/')
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900129 if verbose > 0:
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900130 print('Testing server %s: %s' % (server_class, ' '.join(server_args)))
131 serverproc = subprocess.Popen(server_args, env=env)
132
133 def ensureServerAlive():
134 if serverproc.poll() is not None:
135 print(('FAIL: Server process (%s) failed with retcode %d')
136 % (' '.join(server_args), serverproc.returncode))
137 raise Exception('Server subprocess %s died, args: %s'
138 % (server_class, ' '.join(server_args)))
139
140 # Wait for the server to start accepting connections on the given port.
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900141 sleep_time = 0.1 # Seconds
142 max_attempts = 100
Nobuaki Sukegawae9b32342016-02-27 03:44:02 +0900143 attempt = 0
144 while True:
145 sock4 = socket.socket()
146 sock6 = socket.socket(socket.AF_INET6)
147 try:
148 if sock4.connect_ex(('127.0.0.1', port)) == 0 \
149 or sock6.connect_ex(('::1', port)) == 0:
150 break
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900151 attempt += 1
152 if attempt >= max_attempts:
153 raise Exception("TestServer not ready on port %d after %.2f seconds"
154 % (port, sleep_time * attempt))
155 ensureServerAlive()
156 time.sleep(sleep_time)
Nobuaki Sukegawae9b32342016-02-27 03:44:02 +0900157 finally:
158 sock4.close()
159 sock6.close()
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900160
161 try:
162 if verbose > 0:
163 print('Testing client: %s' % (' '.join(cli_args)))
164 ret = subprocess.call(cli_args, env=env)
165 if ret != 0:
166 print('*** FAILED ***', file=sys.stderr)
167 print('LIBDIR: %s' % libdir, file=sys.stderr)
168 print('PY_GEN: %s' % genpydir, file=sys.stderr)
169 raise Exception("Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args)))
170 finally:
171 # check that server didn't die
172 ensureServerAlive()
173 extra_sleep = EXTRA_DELAY.get(server_class, 0)
174 if extra_sleep > 0 and verbose > 0:
175 print('Giving %s (proto=%s,zlib=%s,ssl=%s) an extra %d seconds for child'
176 'processes to terminate via alarm'
177 % (server_class, proto, use_zlib, use_ssl, extra_sleep))
178 time.sleep(extra_sleep)
Nobuaki Sukegawa06e8fd42016-02-28 12:50:03 +0900179 sig = signal.SIGKILL if platform.system() != 'Windows' else signal.SIGABRT
180 os.kill(serverproc.pid, sig)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900181 serverproc.wait()
David Reissbcaa2ad2008-06-10 22:55:26 +0000182
Roger Meier76150722014-05-31 22:22:07 +0200183
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900184class TestCases(object):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900185 def __init__(self, genbase, libdir, port, gendirs, servers, verbose):
186 self.genbase = genbase
187 self.libdir = libdir
188 self.port = port
189 self.verbose = verbose
190 self.gendirs = gendirs
191 self.servers = servers
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900192
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900193 def default_conf(self):
194 return {
195 'gendir': self.gendirs[0],
196 'server': self.servers[0],
197 'proto': PROTOS[0],
198 'zlib': False,
199 'ssl': False,
200 }
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900201
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900202 def run(self, conf, test_count):
203 with_zlib = conf['zlib']
204 with_ssl = conf['ssl']
205 try_server = conf['server']
206 try_proto = conf['proto']
207 genpydir = conf['gendir']
208 # skip any servers that don't work with the Zlib transport
209 if with_zlib and try_server in SKIP_ZLIB:
210 return False
211 # skip any servers that don't work with SSL
212 if with_ssl and try_server in SKIP_SSL:
213 return False
214 if self.verbose > 0:
215 print('\nTest run #%d: (includes %s) Server=%s, Proto=%s, zlib=%s, SSL=%s'
216 % (test_count, genpydir, try_server, try_proto, with_zlib, with_ssl))
217 runServiceTest(self.libdir, self.genbase, genpydir, try_server, try_proto, self.port, with_zlib, with_ssl, self.verbose)
218 if self.verbose > 0:
219 print('OK: Finished (includes %s) %s / %s proto / zlib=%s / SSL=%s. %d combinations tested.'
220 % (genpydir, try_server, try_proto, with_zlib, with_ssl, test_count))
221 return True
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900222
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900223 def test_feature(self, name, values):
224 test_count = 0
225 conf = self.default_conf()
226 for try_server in values:
227 conf[name] = try_server
228 if self.run(conf, test_count):
229 test_count += 1
230 return test_count
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900231
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900232 def run_all_tests(self):
233 test_count = 0
234 for try_server in self.servers:
235 for genpydir in self.gendirs:
236 for try_proto in PROTOS:
237 for with_zlib in (False, True):
238 # skip any servers that don't work with the Zlib transport
239 if with_zlib and try_server in SKIP_ZLIB:
240 continue
241 for with_ssl in (False, True):
242 # skip any servers that don't work with SSL
243 if with_ssl and try_server in SKIP_SSL:
244 continue
245 test_count += 1
246 if self.verbose > 0:
247 print('\nTest run #%d: (includes %s) Server=%s, Proto=%s, zlib=%s, SSL=%s'
248 % (test_count, genpydir, try_server, try_proto, with_zlib, with_ssl))
249 runServiceTest(self.libdir, self.genbase, genpydir, try_server, try_proto, self.port, with_zlib, with_ssl)
250 if self.verbose > 0:
251 print('OK: Finished (includes %s) %s / %s proto / zlib=%s / SSL=%s. %d combinations tested.'
252 % (genpydir, try_server, try_proto, with_zlib, with_ssl, test_count))
253 return test_count
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900254
255
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900256def main():
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900257 parser = OptionParser()
258 parser.add_option('--all', action="store_true", dest='all')
259 parser.add_option('--genpydirs', type='string', dest='genpydirs',
260 default='default,slots,oldstyle,no_utf8strings,dynamic,dynamicslots',
261 help='directory extensions for generated code, used as suffixes for \"gen-py-*\" added sys.path for individual tests')
262 parser.add_option("--port", type="int", dest="port", default=9090,
263 help="port number for server to listen on")
264 parser.add_option('-v', '--verbose', action="store_const",
265 dest="verbose", const=2,
266 help="verbose output")
267 parser.add_option('-q', '--quiet', action="store_const",
268 dest="verbose", const=0,
269 help="minimal output")
Nobuaki Sukegawa7af189a2016-02-11 16:21:01 +0900270 parser.add_option('-L', '--libdir', dest="libdir", default=local_libpath(),
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900271 help="directory path that contains Thrift Python library")
272 parser.add_option('--gen-base', dest="gen_base", default=SCRIPT_DIR,
273 help="directory path that contains Thrift Python library")
274 parser.set_defaults(verbose=1)
275 options, args = parser.parse_args()
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900276
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900277 generated_dirs = []
278 for gp_dir in options.genpydirs.split(','):
279 generated_dirs.append('gen-py-%s' % (gp_dir))
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900280
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900281 # commandline permits a single class name to be specified to override SERVERS=[...]
Nobuaki Sukegawa06e8fd42016-02-28 12:50:03 +0900282 servers = default_servers()
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900283 if len(args) == 1:
Nobuaki Sukegawa06e8fd42016-02-28 12:50:03 +0900284 if args[0] in servers:
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900285 servers = args
286 else:
287 print('Unavailable server type "%s", please choose one of: %s' % (args[0], servers))
288 sys.exit(0)
289
290 tests = TestCases(options.gen_base, options.libdir, options.port, generated_dirs, servers, options.verbose)
291
292 # run tests without a client/server first
293 print('----------------')
294 print(' Executing individual test scripts with various generated code directories')
295 print(' Directories to be tested: ' + ', '.join(generated_dirs))
296 print(' Scripts to be tested: ' + ', '.join(SCRIPTS))
297 print('----------------')
298 for genpydir in generated_dirs:
299 for script in SCRIPTS:
300 runScriptTest(options.libdir, options.gen_base, genpydir, script)
301
302 print('----------------')
303 print(' Executing Client/Server tests with various generated code directories')
304 print(' Servers to be tested: ' + ', '.join(servers))
305 print(' Directories to be tested: ' + ', '.join(generated_dirs))
306 print(' Protocols to be tested: ' + ', '.join(PROTOS))
307 print(' Options to be tested: ZLIB(yes/no), SSL(yes/no)')
308 print('----------------')
309
310 if options.all:
311 tests.run_all_tests()
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900312 else:
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900313 tests.test_feature('gendir', generated_dirs)
314 tests.test_feature('server', servers)
315 tests.test_feature('proto', PROTOS)
316 tests.test_feature('zlib', [False, True])
317 tests.test_feature('ssl', [False, True])
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900318
319
320if __name__ == '__main__':
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900321 sys.exit(main())