blob: 7224bac913c6fda4483dab121f6fcb4f2f8633d1 [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
David Reissbcaa2ad2008-06-10 22:55:26 +000024import time
Roger Meier9b328532014-04-21 21:22:54 +020025import socket
Mark Slee5299a952007-10-05 00:13:24 +000026import subprocess
27import sys
28import os
29import signal
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000030from optparse import OptionParser
31
32parser = OptionParser()
Roger Meierf4eec7a2011-09-11 18:16:21 +000033parser.add_option('--genpydirs', type='string', dest='genpydirs',
34 default='default,slots,newstyle,newstyleslots,dynamic,dynamicslots',
35 help='directory extensions for generated code, used as suffixes for \"gen-py-*\" added sys.path for individual tests')
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000036parser.add_option("--port", type="int", dest="port", default=9090,
37 help="port number for server to listen on")
Roger Meier76150722014-05-31 22:22:07 +020038parser.add_option('-v', '--verbose', action="store_const",
Bryan Duxbury16066592011-03-22 18:06:04 +000039 dest="verbose", const=2,
40 help="verbose output")
Roger Meier76150722014-05-31 22:22:07 +020041parser.add_option('-q', '--quiet', action="store_const",
Bryan Duxbury16066592011-03-22 18:06:04 +000042 dest="verbose", const=0,
43 help="minimal output")
44parser.set_defaults(verbose=1)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000045options, args = parser.parse_args()
46
Roger Meierf4eec7a2011-09-11 18:16:21 +000047generated_dirs = []
48for gp_dir in options.genpydirs.split(','):
49 generated_dirs.append('gen-py-%s' % (gp_dir))
50
Roger Meier0895dfe2012-12-26 22:09:55 +010051SCRIPTS = ['TSimpleJSONProtocolTest.py',
52 'SerializationTest.py',
53 'TestEof.py',
54 'TestSyntax.py',
55 'TestSocket.py']
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000056FRAMED = ["TNonblockingServer"]
Bryan Duxbury16066592011-03-22 18:06:04 +000057SKIP_ZLIB = ['TNonblockingServer', 'THttpServer']
58SKIP_SSL = ['TNonblockingServer', 'THttpServer']
Roger Meier6857b7f2015-09-16 19:53:07 +020059EXTRA_DELAY = dict(TProcessPoolServer=5.5)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000060
61PROTOS= [
62 'accel',
63 'binary',
Roger Meier840f3ef2015-09-21 23:11:46 +020064 'compact',
65 'json']
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000066
67SERVERS = [
68 "TSimpleServer",
69 "TThreadedServer",
70 "TThreadPoolServer",
71 "TProcessPoolServer", # new!
72 "TForkingServer",
73 "TNonblockingServer",
74 "THttpServer" ]
75
76# Test for presence of multiprocessing module, and if it is not present, then
77# remove it from the list of available servers.
78try:
79 import multiprocessing
80except:
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090081 print('Warning: the multiprocessing module is unavailable. Skipping tests for TProcessPoolServer')
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000082 SERVERS.remove('TProcessPoolServer')
83
Bryan Duxbury16066592011-03-22 18:06:04 +000084try:
85 import ssl
86except:
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090087 print('Warning, no ssl module available. Skipping all SSL tests.')
Bryan Duxbury16066592011-03-22 18:06:04 +000088 SKIP_SSL.extend(SERVERS)
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000089
90# commandline permits a single class name to be specified to override SERVERS=[...]
91if len(args) == 1:
92 if args[0] in SERVERS:
93 SERVERS = args
94 else:
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090095 print('Unavailable server type "%s", please choose one of: %s' % (args[0], SERVERS))
Bryan Duxbury59d4efd2011-03-21 17:38:22 +000096 sys.exit(0)
97
Mark Slee5299a952007-10-05 00:13:24 +000098
David Reiss2a4bfd62008-04-07 23:45:00 +000099def relfile(fname):
100 return os.path.join(os.path.dirname(__file__), fname)
101
Roger Meierf4eec7a2011-09-11 18:16:21 +0000102def runScriptTest(genpydir, script):
103 script_args = [sys.executable, relfile(script) ]
104 script_args.append('--genpydir=%s' % genpydir)
105 serverproc = subprocess.Popen(script_args)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900106 print('\nTesting script: %s\n----' % (' '.join(script_args)))
Roger Meierf4eec7a2011-09-11 18:16:21 +0000107 ret = subprocess.call(script_args)
108 if ret != 0:
109 raise Exception("Script subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(script_args)))
Roger Meier76150722014-05-31 22:22:07 +0200110
Roger Meierf4eec7a2011-09-11 18:16:21 +0000111def runServiceTest(genpydir, server_class, proto, port, use_zlib, use_ssl):
Bryan Duxbury16066592011-03-22 18:06:04 +0000112 # Build command line arguments
113 server_args = [sys.executable, relfile('TestServer.py') ]
114 cli_args = [sys.executable, relfile('TestClient.py') ]
115 for which in (server_args, cli_args):
Roger Meierf4eec7a2011-09-11 18:16:21 +0000116 which.append('--genpydir=%s' % genpydir)
Roger Meier76150722014-05-31 22:22:07 +0200117 which.append('--protocol=%s' % proto) # accel, binary or compact
Bryan Duxbury16066592011-03-22 18:06:04 +0000118 which.append('--port=%d' % port) # default to 9090
119 if use_zlib:
120 which.append('--zlib')
121 if use_ssl:
122 which.append('--ssl')
123 if options.verbose == 0:
124 which.append('-q')
125 if options.verbose == 2:
126 which.append('-v')
127 # server-specific option to select server class
128 server_args.append(server_class)
129 # client-specific cmdline options
130 if server_class in FRAMED:
Roger Meier76150722014-05-31 22:22:07 +0200131 cli_args.append('--transport=framed')
132 else:
133 cli_args.append('--transport=buffered')
Bryan Duxbury16066592011-03-22 18:06:04 +0000134 if server_class == 'THttpServer':
135 cli_args.append('--http=/')
136 if options.verbose > 0:
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900137 print('Testing server %s: %s' % (server_class, ' '.join(server_args)))
Bryan Duxbury16066592011-03-22 18:06:04 +0000138 serverproc = subprocess.Popen(server_args)
Roger Meier9b328532014-04-21 21:22:54 +0200139
140 def ensureServerAlive():
141 if serverproc.poll() is not None:
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900142 print(('FAIL: Server process (%s) failed with retcode %d')
Roger Meier9b328532014-04-21 21:22:54 +0200143 % (' '.join(server_args), serverproc.returncode))
144 raise Exception('Server subprocess %s died, args: %s'
145 % (server_class, ' '.join(server_args)))
146
147 # Wait for the server to start accepting connections on the given port.
148 sock = socket.socket()
149 sleep_time = 0.1 # Seconds
150 max_attempts = 100
151 try:
152 attempt = 0
153 while sock.connect_ex(('127.0.0.1', port)) != 0:
154 attempt += 1
155 if attempt >= max_attempts:
156 raise Exception("TestServer not ready on port %d after %.2f seconds"
157 % (port, sleep_time * attempt))
158 ensureServerAlive()
159 time.sleep(sleep_time)
160 finally:
161 sock.close()
162
Bryan Duxbury16066592011-03-22 18:06:04 +0000163 try:
164 if options.verbose > 0:
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900165 print('Testing client: %s' % (' '.join(cli_args)))
Bryan Duxbury16066592011-03-22 18:06:04 +0000166 ret = subprocess.call(cli_args)
167 if ret != 0:
168 raise Exception("Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args)))
169 finally:
170 # check that server didn't die
Roger Meier9b328532014-04-21 21:22:54 +0200171 ensureServerAlive()
172 extra_sleep = EXTRA_DELAY.get(server_class, 0)
173 if extra_sleep > 0 and options.verbose > 0:
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900174 print('Giving %s (proto=%s,zlib=%s,ssl=%s) an extra %d seconds for child'
Roger Meier9b328532014-04-21 21:22:54 +0200175 'processes to terminate via alarm'
176 % (server_class, proto, use_zlib, use_ssl, extra_sleep))
177 time.sleep(extra_sleep)
178 os.kill(serverproc.pid, signal.SIGKILL)
179 serverproc.wait()
David Reissbcaa2ad2008-06-10 22:55:26 +0000180
Bryan Duxbury16066592011-03-22 18:06:04 +0000181test_count = 0
Roger Meierf4eec7a2011-09-11 18:16:21 +0000182# run tests without a client/server first
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900183print('----------------')
184print(' Executing individual test scripts with various generated code directories')
185print(' Directories to be tested: ' + ', '.join(generated_dirs))
186print(' Scripts to be tested: ' + ', '.join(SCRIPTS))
187print('----------------')
Roger Meierf4eec7a2011-09-11 18:16:21 +0000188for genpydir in generated_dirs:
189 for script in SCRIPTS:
190 runScriptTest(genpydir, script)
Roger Meier76150722014-05-31 22:22:07 +0200191
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900192print('----------------')
193print(' Executing Client/Server tests with various generated code directories')
194print(' Servers to be tested: ' + ', '.join(SERVERS))
195print(' Directories to be tested: ' + ', '.join(generated_dirs))
196print(' Protocols to be tested: ' + ', '.join(PROTOS))
197print(' Options to be tested: ZLIB(yes/no), SSL(yes/no)')
198print('----------------')
Bryan Duxbury59d4efd2011-03-21 17:38:22 +0000199for try_server in SERVERS:
Roger Meierf4eec7a2011-09-11 18:16:21 +0000200 for genpydir in generated_dirs:
201 for try_proto in PROTOS:
202 for with_zlib in (False, True):
203 # skip any servers that don't work with the Zlib transport
204 if with_zlib and try_server in SKIP_ZLIB:
Bryan Duxbury16066592011-03-22 18:06:04 +0000205 continue
Roger Meierf4eec7a2011-09-11 18:16:21 +0000206 for with_ssl in (False, True):
207 # skip any servers that don't work with SSL
208 if with_ssl and try_server in SKIP_SSL:
209 continue
210 test_count += 1
211 if options.verbose > 0:
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900212 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))
Roger Meierf4eec7a2011-09-11 18:16:21 +0000213 runServiceTest(genpydir, try_server, try_proto, options.port, with_zlib, with_ssl)
214 if options.verbose > 0:
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900215 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))