blob: b213d1acc5d6b84c4fb4de2b3890198bb3adb72d [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',
Ozan Can Altioke46419b2018-03-20 15:02:28 +030041 'TestRenderedDoubleConstants.py',
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090042 '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']
Nobuaki Sukegawa4626fd82017-02-12 21:11:36 +090050SKIP_SSL = ['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
Nobuaki Sukegawa06e8fd42016-02-28 12:50:03 +090061
62def default_servers():
63 servers = [
64 'TSimpleServer',
65 'TThreadedServer',
66 'TThreadPoolServer',
67 'TNonblockingServer',
68 'THttpServer',
69 ]
70 if platform.system() != 'Windows':
71 servers.append('TProcessPoolServer')
72 servers.append('TForkingServer')
73 return servers
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000074
Mark Slee5299a952007-10-05 00:13:24 +000075
David Reiss2a4bfd62008-04-07 23:45:00 +000076def relfile(fname):
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090077 return os.path.join(SCRIPT_DIR, fname)
David Reiss2a4bfd62008-04-07 23:45:00 +000078
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090079
Nobuaki Sukegawaa3b88a02016-01-06 20:44:17 +090080def setup_pypath(libdir, gendir):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090081 dirs = [libdir, gendir]
82 env = copy.deepcopy(os.environ)
83 pypath = env.get('PYTHONPATH', None)
84 if pypath:
85 dirs.append(pypath)
Nobuaki Sukegawa06e8fd42016-02-28 12:50:03 +090086 env['PYTHONPATH'] = os.pathsep.join(dirs)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090087 if gendir.endswith('gen-py-no_utf8strings'):
88 env['THRIFT_TEST_PY_NO_UTF8STRINGS'] = '1'
89 return env
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090090
91
Nobuaki Sukegawaa3b88a02016-01-06 20:44:17 +090092def runScriptTest(libdir, genbase, genpydir, script):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090093 env = setup_pypath(libdir, os.path.join(genbase, genpydir))
94 script_args = [sys.executable, relfile(script)]
95 print('\nTesting script: %s\n----' % (' '.join(script_args)))
96 ret = subprocess.call(script_args, env=env)
97 if ret != 0:
98 print('*** FAILED ***', file=sys.stderr)
99 print('LIBDIR: %s' % libdir, file=sys.stderr)
100 print('PY_GEN: %s' % genpydir, file=sys.stderr)
101 print('SCRIPT: %s' % script, file=sys.stderr)
102 raise Exception("Script subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(script_args)))
Roger Meier76150722014-05-31 22:22:07 +0200103
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900104
Nobuaki Sukegawaa3b88a02016-01-06 20:44:17 +0900105def runServiceTest(libdir, genbase, genpydir, server_class, proto, port, use_zlib, use_ssl, verbose):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900106 env = setup_pypath(libdir, os.path.join(genbase, genpydir))
107 # Build command line arguments
108 server_args = [sys.executable, relfile('TestServer.py')]
109 cli_args = [sys.executable, relfile('TestClient.py')]
110 for which in (server_args, cli_args):
111 which.append('--protocol=%s' % proto) # accel, binary, compact or json
112 which.append('--port=%d' % port) # default to 9090
113 if use_zlib:
114 which.append('--zlib')
115 if use_ssl:
116 which.append('--ssl')
117 if verbose == 0:
118 which.append('-q')
119 if verbose == 2:
120 which.append('-v')
121 # server-specific option to select server class
122 server_args.append(server_class)
123 # client-specific cmdline options
124 if server_class in FRAMED:
125 cli_args.append('--transport=framed')
126 else:
127 cli_args.append('--transport=buffered')
128 if server_class == 'THttpServer':
129 cli_args.append('--http=/')
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900130 if verbose > 0:
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900131 print('Testing server %s: %s' % (server_class, ' '.join(server_args)))
132 serverproc = subprocess.Popen(server_args, env=env)
133
134 def ensureServerAlive():
135 if serverproc.poll() is not None:
136 print(('FAIL: Server process (%s) failed with retcode %d')
137 % (' '.join(server_args), serverproc.returncode))
138 raise Exception('Server subprocess %s died, args: %s'
139 % (server_class, ' '.join(server_args)))
140
141 # Wait for the server to start accepting connections on the given port.
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900142 sleep_time = 0.1 # Seconds
143 max_attempts = 100
Nobuaki Sukegawae9b32342016-02-27 03:44:02 +0900144 attempt = 0
145 while True:
146 sock4 = socket.socket()
147 sock6 = socket.socket(socket.AF_INET6)
148 try:
149 if sock4.connect_ex(('127.0.0.1', port)) == 0 \
150 or sock6.connect_ex(('::1', port)) == 0:
151 break
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900152 attempt += 1
153 if attempt >= max_attempts:
154 raise Exception("TestServer not ready on port %d after %.2f seconds"
155 % (port, sleep_time * attempt))
156 ensureServerAlive()
157 time.sleep(sleep_time)
Nobuaki Sukegawae9b32342016-02-27 03:44:02 +0900158 finally:
159 sock4.close()
160 sock6.close()
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900161
162 try:
163 if verbose > 0:
164 print('Testing client: %s' % (' '.join(cli_args)))
165 ret = subprocess.call(cli_args, env=env)
166 if ret != 0:
167 print('*** FAILED ***', file=sys.stderr)
168 print('LIBDIR: %s' % libdir, file=sys.stderr)
169 print('PY_GEN: %s' % genpydir, file=sys.stderr)
170 raise Exception("Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args)))
171 finally:
172 # check that server didn't die
173 ensureServerAlive()
174 extra_sleep = EXTRA_DELAY.get(server_class, 0)
175 if extra_sleep > 0 and verbose > 0:
176 print('Giving %s (proto=%s,zlib=%s,ssl=%s) an extra %d seconds for child'
177 'processes to terminate via alarm'
178 % (server_class, proto, use_zlib, use_ssl, extra_sleep))
179 time.sleep(extra_sleep)
Nobuaki Sukegawa06e8fd42016-02-28 12:50:03 +0900180 sig = signal.SIGKILL if platform.system() != 'Windows' else signal.SIGABRT
181 os.kill(serverproc.pid, sig)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900182 serverproc.wait()
David Reissbcaa2ad2008-06-10 22:55:26 +0000183
Roger Meier76150722014-05-31 22:22:07 +0200184
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900185class TestCases(object):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900186 def __init__(self, genbase, libdir, port, gendirs, servers, verbose):
187 self.genbase = genbase
188 self.libdir = libdir
189 self.port = port
190 self.verbose = verbose
191 self.gendirs = gendirs
192 self.servers = servers
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900193
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900194 def default_conf(self):
195 return {
196 'gendir': self.gendirs[0],
197 'server': self.servers[0],
198 'proto': PROTOS[0],
199 'zlib': False,
200 'ssl': False,
201 }
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900202
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900203 def run(self, conf, test_count):
204 with_zlib = conf['zlib']
205 with_ssl = conf['ssl']
206 try_server = conf['server']
207 try_proto = conf['proto']
208 genpydir = conf['gendir']
209 # skip any servers that don't work with the Zlib transport
210 if with_zlib and try_server in SKIP_ZLIB:
211 return False
212 # skip any servers that don't work with SSL
213 if with_ssl and try_server in SKIP_SSL:
214 return False
215 if self.verbose > 0:
216 print('\nTest run #%d: (includes %s) Server=%s, Proto=%s, zlib=%s, SSL=%s'
217 % (test_count, genpydir, try_server, try_proto, with_zlib, with_ssl))
218 runServiceTest(self.libdir, self.genbase, genpydir, try_server, try_proto, self.port, with_zlib, with_ssl, self.verbose)
219 if self.verbose > 0:
220 print('OK: Finished (includes %s) %s / %s proto / zlib=%s / SSL=%s. %d combinations tested.'
221 % (genpydir, try_server, try_proto, with_zlib, with_ssl, test_count))
222 return True
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900223
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900224 def test_feature(self, name, values):
225 test_count = 0
226 conf = self.default_conf()
227 for try_server in values:
228 conf[name] = try_server
229 if self.run(conf, test_count):
230 test_count += 1
231 return test_count
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900232
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900233 def run_all_tests(self):
234 test_count = 0
235 for try_server in self.servers:
236 for genpydir in self.gendirs:
237 for try_proto in PROTOS:
238 for with_zlib in (False, True):
239 # skip any servers that don't work with the Zlib transport
240 if with_zlib and try_server in SKIP_ZLIB:
241 continue
242 for with_ssl in (False, True):
243 # skip any servers that don't work with SSL
244 if with_ssl and try_server in SKIP_SSL:
245 continue
246 test_count += 1
247 if self.verbose > 0:
248 print('\nTest run #%d: (includes %s) Server=%s, Proto=%s, zlib=%s, SSL=%s'
249 % (test_count, genpydir, try_server, try_proto, with_zlib, with_ssl))
250 runServiceTest(self.libdir, self.genbase, genpydir, try_server, try_proto, self.port, with_zlib, with_ssl)
251 if self.verbose > 0:
252 print('OK: Finished (includes %s) %s / %s proto / zlib=%s / SSL=%s. %d combinations tested.'
253 % (genpydir, try_server, try_proto, with_zlib, with_ssl, test_count))
254 return test_count
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900255
256
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900257def main():
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900258 parser = OptionParser()
259 parser.add_option('--all', action="store_true", dest='all')
260 parser.add_option('--genpydirs', type='string', dest='genpydirs',
261 default='default,slots,oldstyle,no_utf8strings,dynamic,dynamicslots',
262 help='directory extensions for generated code, used as suffixes for \"gen-py-*\" added sys.path for individual tests')
263 parser.add_option("--port", type="int", dest="port", default=9090,
264 help="port number for server to listen on")
265 parser.add_option('-v', '--verbose', action="store_const",
266 dest="verbose", const=2,
267 help="verbose output")
268 parser.add_option('-q', '--quiet', action="store_const",
269 dest="verbose", const=0,
270 help="minimal output")
Nobuaki Sukegawa7af189a2016-02-11 16:21:01 +0900271 parser.add_option('-L', '--libdir', dest="libdir", default=local_libpath(),
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900272 help="directory path that contains Thrift Python library")
273 parser.add_option('--gen-base', dest="gen_base", default=SCRIPT_DIR,
274 help="directory path that contains Thrift Python library")
275 parser.set_defaults(verbose=1)
276 options, args = parser.parse_args()
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900277
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900278 generated_dirs = []
279 for gp_dir in options.genpydirs.split(','):
280 generated_dirs.append('gen-py-%s' % (gp_dir))
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900281
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900282 # commandline permits a single class name to be specified to override SERVERS=[...]
Nobuaki Sukegawa06e8fd42016-02-28 12:50:03 +0900283 servers = default_servers()
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900284 if len(args) == 1:
Nobuaki Sukegawa06e8fd42016-02-28 12:50:03 +0900285 if args[0] in servers:
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900286 servers = args
287 else:
288 print('Unavailable server type "%s", please choose one of: %s' % (args[0], servers))
289 sys.exit(0)
290
291 tests = TestCases(options.gen_base, options.libdir, options.port, generated_dirs, servers, options.verbose)
292
293 # run tests without a client/server first
294 print('----------------')
295 print(' Executing individual test scripts with various generated code directories')
296 print(' Directories to be tested: ' + ', '.join(generated_dirs))
297 print(' Scripts to be tested: ' + ', '.join(SCRIPTS))
298 print('----------------')
299 for genpydir in generated_dirs:
300 for script in SCRIPTS:
301 runScriptTest(options.libdir, options.gen_base, genpydir, script)
302
303 print('----------------')
304 print(' Executing Client/Server tests with various generated code directories')
305 print(' Servers to be tested: ' + ', '.join(servers))
306 print(' Directories to be tested: ' + ', '.join(generated_dirs))
307 print(' Protocols to be tested: ' + ', '.join(PROTOS))
308 print(' Options to be tested: ZLIB(yes/no), SSL(yes/no)')
309 print('----------------')
310
311 if options.all:
312 tests.run_all_tests()
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900313 else:
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900314 tests.test_feature('gendir', generated_dirs)
315 tests.test_feature('server', servers)
316 tests.test_feature('proto', PROTOS)
317 tests.test_feature('zlib', [False, True])
318 tests.test_feature('ssl', [False, True])
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900319
320
321if __name__ == '__main__':
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900322 sys.exit(main())