2007-10-05 00:13:24 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2009-03-30 21:35:00 +00:00
|
|
|
#
|
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
# or more contributor license agreements. See the NOTICE file
|
|
|
|
# distributed with this work for additional information
|
|
|
|
# regarding copyright ownership. The ASF licenses this file
|
|
|
|
# to you under the Apache License, Version 2.0 (the
|
|
|
|
# "License"); you may not use this file except in compliance
|
|
|
|
# with the License. You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing,
|
|
|
|
# software distributed under the License is distributed on an
|
|
|
|
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
# KIND, either express or implied. See the License for the
|
|
|
|
# specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
#
|
|
|
|
|
2011-03-21 17:38:22 +00:00
|
|
|
from __future__ import division
|
2008-06-10 22:55:26 +00:00
|
|
|
import time
|
2014-04-21 19:22:54 +00:00
|
|
|
import socket
|
2007-10-05 00:13:24 +00:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import signal
|
2011-03-21 17:38:22 +00:00
|
|
|
from optparse import OptionParser
|
2007-10-05 00:13:24 +00:00
|
|
|
|
2011-03-21 17:38:22 +00:00
|
|
|
parser = OptionParser()
|
2011-09-11 18:16:21 +00:00
|
|
|
parser.add_option('--genpydirs', type='string', dest='genpydirs',
|
|
|
|
default='default,slots,newstyle,newstyleslots,dynamic,dynamicslots',
|
|
|
|
help='directory extensions for generated code, used as suffixes for \"gen-py-*\" added sys.path for individual tests')
|
2011-03-21 17:38:22 +00:00
|
|
|
parser.add_option("--port", type="int", dest="port", default=9090,
|
|
|
|
help="port number for server to listen on")
|
2014-05-31 20:22:07 +00:00
|
|
|
parser.add_option('-v', '--verbose', action="store_const",
|
2011-03-22 18:06:04 +00:00
|
|
|
dest="verbose", const=2,
|
|
|
|
help="verbose output")
|
2014-05-31 20:22:07 +00:00
|
|
|
parser.add_option('-q', '--quiet', action="store_const",
|
2011-03-22 18:06:04 +00:00
|
|
|
dest="verbose", const=0,
|
|
|
|
help="minimal output")
|
|
|
|
parser.set_defaults(verbose=1)
|
2011-03-21 17:38:22 +00:00
|
|
|
options, args = parser.parse_args()
|
2008-04-07 23:45:00 +00:00
|
|
|
|
2011-09-11 18:16:21 +00:00
|
|
|
generated_dirs = []
|
|
|
|
for gp_dir in options.genpydirs.split(','):
|
|
|
|
generated_dirs.append('gen-py-%s' % (gp_dir))
|
|
|
|
|
2012-12-26 21:09:55 +00:00
|
|
|
SCRIPTS = ['TSimpleJSONProtocolTest.py',
|
|
|
|
'SerializationTest.py',
|
|
|
|
'TestEof.py',
|
|
|
|
'TestSyntax.py',
|
|
|
|
'TestSocket.py']
|
2008-11-07 23:09:31 +00:00
|
|
|
FRAMED = ["TNonblockingServer"]
|
2011-03-22 18:06:04 +00:00
|
|
|
SKIP_ZLIB = ['TNonblockingServer', 'THttpServer']
|
|
|
|
SKIP_SSL = ['TNonblockingServer', 'THttpServer']
|
2011-09-11 18:16:21 +00:00
|
|
|
EXTRA_DELAY = dict(TProcessPoolServer=3.5)
|
2011-03-21 17:38:22 +00:00
|
|
|
|
|
|
|
PROTOS= [
|
|
|
|
'accel',
|
|
|
|
'binary',
|
2012-11-02 00:05:42 +00:00
|
|
|
'compact']
|
|
|
|
# FIXME: add json
|
|
|
|
# disabled because json HTTP test hangs... why?
|
2011-03-21 17:38:22 +00:00
|
|
|
|
|
|
|
SERVERS = [
|
|
|
|
"TSimpleServer",
|
|
|
|
"TThreadedServer",
|
|
|
|
"TThreadPoolServer",
|
|
|
|
"TProcessPoolServer", # new!
|
|
|
|
"TForkingServer",
|
|
|
|
"TNonblockingServer",
|
|
|
|
"THttpServer" ]
|
|
|
|
|
|
|
|
# Test for presence of multiprocessing module, and if it is not present, then
|
|
|
|
# remove it from the list of available servers.
|
|
|
|
try:
|
|
|
|
import multiprocessing
|
|
|
|
except:
|
|
|
|
print 'Warning: the multiprocessing module is unavailable. Skipping tests for TProcessPoolServer'
|
|
|
|
SERVERS.remove('TProcessPoolServer')
|
|
|
|
|
2011-03-22 18:06:04 +00:00
|
|
|
try:
|
|
|
|
import ssl
|
|
|
|
except:
|
|
|
|
print 'Warning, no ssl module available. Skipping all SSL tests.'
|
|
|
|
SKIP_SSL.extend(SERVERS)
|
2008-11-07 23:09:31 +00:00
|
|
|
|
2011-03-21 17:38:22 +00:00
|
|
|
# commandline permits a single class name to be specified to override SERVERS=[...]
|
|
|
|
if len(args) == 1:
|
|
|
|
if args[0] in SERVERS:
|
|
|
|
SERVERS = args
|
|
|
|
else:
|
|
|
|
print 'Unavailable server type "%s", please choose one of: %s' % (args[0], SERVERS)
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
def relfile(fname):
|
|
|
|
return os.path.join(os.path.dirname(__file__), fname)
|
|
|
|
|
2011-09-11 18:16:21 +00:00
|
|
|
def runScriptTest(genpydir, script):
|
|
|
|
script_args = [sys.executable, relfile(script) ]
|
|
|
|
script_args.append('--genpydir=%s' % genpydir)
|
|
|
|
serverproc = subprocess.Popen(script_args)
|
|
|
|
print '\nTesting script: %s\n----' % (' '.join(script_args))
|
|
|
|
ret = subprocess.call(script_args)
|
|
|
|
if ret != 0:
|
|
|
|
raise Exception("Script subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(script_args)))
|
2014-05-31 20:22:07 +00:00
|
|
|
|
2011-09-11 18:16:21 +00:00
|
|
|
def runServiceTest(genpydir, server_class, proto, port, use_zlib, use_ssl):
|
2011-03-22 18:06:04 +00:00
|
|
|
# Build command line arguments
|
|
|
|
server_args = [sys.executable, relfile('TestServer.py') ]
|
|
|
|
cli_args = [sys.executable, relfile('TestClient.py') ]
|
|
|
|
for which in (server_args, cli_args):
|
2011-09-11 18:16:21 +00:00
|
|
|
which.append('--genpydir=%s' % genpydir)
|
2014-05-31 20:22:07 +00:00
|
|
|
which.append('--protocol=%s' % proto) # accel, binary or compact
|
2011-03-22 18:06:04 +00:00
|
|
|
which.append('--port=%d' % port) # default to 9090
|
|
|
|
if use_zlib:
|
|
|
|
which.append('--zlib')
|
|
|
|
if use_ssl:
|
|
|
|
which.append('--ssl')
|
|
|
|
if options.verbose == 0:
|
|
|
|
which.append('-q')
|
|
|
|
if options.verbose == 2:
|
|
|
|
which.append('-v')
|
|
|
|
# server-specific option to select server class
|
|
|
|
server_args.append(server_class)
|
|
|
|
# client-specific cmdline options
|
|
|
|
if server_class in FRAMED:
|
2014-05-31 20:22:07 +00:00
|
|
|
cli_args.append('--transport=framed')
|
|
|
|
else:
|
|
|
|
cli_args.append('--transport=buffered')
|
2011-03-22 18:06:04 +00:00
|
|
|
if server_class == 'THttpServer':
|
|
|
|
cli_args.append('--http=/')
|
|
|
|
if options.verbose > 0:
|
|
|
|
print 'Testing server %s: %s' % (server_class, ' '.join(server_args))
|
|
|
|
serverproc = subprocess.Popen(server_args)
|
2014-04-21 19:22:54 +00:00
|
|
|
|
|
|
|
def ensureServerAlive():
|
|
|
|
if serverproc.poll() is not None:
|
|
|
|
print ('FAIL: Server process (%s) failed with retcode %d'
|
|
|
|
% (' '.join(server_args), serverproc.returncode))
|
|
|
|
raise Exception('Server subprocess %s died, args: %s'
|
|
|
|
% (server_class, ' '.join(server_args)))
|
|
|
|
|
|
|
|
# Wait for the server to start accepting connections on the given port.
|
|
|
|
sock = socket.socket()
|
|
|
|
sleep_time = 0.1 # Seconds
|
|
|
|
max_attempts = 100
|
|
|
|
try:
|
|
|
|
attempt = 0
|
|
|
|
while sock.connect_ex(('127.0.0.1', port)) != 0:
|
|
|
|
attempt += 1
|
|
|
|
if attempt >= max_attempts:
|
|
|
|
raise Exception("TestServer not ready on port %d after %.2f seconds"
|
|
|
|
% (port, sleep_time * attempt))
|
|
|
|
ensureServerAlive()
|
|
|
|
time.sleep(sleep_time)
|
|
|
|
finally:
|
|
|
|
sock.close()
|
|
|
|
|
2011-03-22 18:06:04 +00:00
|
|
|
try:
|
|
|
|
if options.verbose > 0:
|
|
|
|
print 'Testing client: %s' % (' '.join(cli_args))
|
|
|
|
ret = subprocess.call(cli_args)
|
|
|
|
if ret != 0:
|
|
|
|
raise Exception("Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args)))
|
|
|
|
finally:
|
|
|
|
# check that server didn't die
|
2014-04-21 19:22:54 +00:00
|
|
|
ensureServerAlive()
|
|
|
|
extra_sleep = EXTRA_DELAY.get(server_class, 0)
|
|
|
|
if extra_sleep > 0 and options.verbose > 0:
|
|
|
|
print ('Giving %s (proto=%s,zlib=%s,ssl=%s) an extra %d seconds for child'
|
|
|
|
'processes to terminate via alarm'
|
|
|
|
% (server_class, proto, use_zlib, use_ssl, extra_sleep))
|
|
|
|
time.sleep(extra_sleep)
|
|
|
|
os.kill(serverproc.pid, signal.SIGKILL)
|
|
|
|
serverproc.wait()
|
2008-06-10 22:55:26 +00:00
|
|
|
|
2011-03-22 18:06:04 +00:00
|
|
|
test_count = 0
|
2011-09-11 18:16:21 +00:00
|
|
|
# run tests without a client/server first
|
|
|
|
print '----------------'
|
|
|
|
print ' Executing individual test scripts with various generated code directories'
|
|
|
|
print ' Directories to be tested: ' + ', '.join(generated_dirs)
|
|
|
|
print ' Scripts to be tested: ' + ', '.join(SCRIPTS)
|
|
|
|
print '----------------'
|
|
|
|
for genpydir in generated_dirs:
|
|
|
|
for script in SCRIPTS:
|
|
|
|
runScriptTest(genpydir, script)
|
2014-05-31 20:22:07 +00:00
|
|
|
|
2011-09-11 18:16:21 +00:00
|
|
|
print '----------------'
|
|
|
|
print ' Executing Client/Server tests with various generated code directories'
|
|
|
|
print ' Servers to be tested: ' + ', '.join(SERVERS)
|
|
|
|
print ' Directories to be tested: ' + ', '.join(generated_dirs)
|
|
|
|
print ' Protocols to be tested: ' + ', '.join(PROTOS)
|
|
|
|
print ' Options to be tested: ZLIB(yes/no), SSL(yes/no)'
|
|
|
|
print '----------------'
|
2011-03-21 17:38:22 +00:00
|
|
|
for try_server in SERVERS:
|
2011-09-11 18:16:21 +00:00
|
|
|
for genpydir in generated_dirs:
|
|
|
|
for try_proto in PROTOS:
|
|
|
|
for with_zlib in (False, True):
|
|
|
|
# skip any servers that don't work with the Zlib transport
|
|
|
|
if with_zlib and try_server in SKIP_ZLIB:
|
2011-03-22 18:06:04 +00:00
|
|
|
continue
|
2011-09-11 18:16:21 +00:00
|
|
|
for with_ssl in (False, True):
|
|
|
|
# skip any servers that don't work with SSL
|
|
|
|
if with_ssl and try_server in SKIP_SSL:
|
|
|
|
continue
|
|
|
|
test_count += 1
|
|
|
|
if options.verbose > 0:
|
|
|
|
print '\nTest run #%d: (includes %s) Server=%s, Proto=%s, zlib=%s, SSL=%s' % (test_count, genpydir, try_server, try_proto, with_zlib, with_ssl)
|
|
|
|
runServiceTest(genpydir, try_server, try_proto, options.port, with_zlib, with_ssl)
|
|
|
|
if options.verbose > 0:
|
|
|
|
print 'OK: Finished (includes %s) %s / %s proto / zlib=%s / SSL=%s. %d combinations tested.' % (genpydir, try_server, try_proto, with_zlib, with_ssl, test_count)
|