blob: cab942e92867c0fe1387bbac275cf5230f50a6b3 [file] [log] [blame]
Roger Meier40cc2322014-06-11 11:09:14 +02001#!/usr/bin/env python
2
3#
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
22from __future__ import division
23import time
24import socket
25import subprocess
26import sys
27import os
28import signal
29import json
Roger Meier32f39822014-06-18 22:43:17 +020030import shutil
31import threading
Roger Meier40cc2322014-06-11 11:09:14 +020032from optparse import OptionParser
33
34parser = OptionParser()
35parser.add_option("--port", type="int", dest="port", default=9090,
36 help="port number for server to listen on")
37parser.add_option('-v', '--verbose', action="store_const",
38 dest="verbose", const=2,
39 help="verbose output")
40parser.add_option('-q', '--quiet', action="store_const",
41 dest="verbose", const=0,
42 help="minimal output")
43parser.set_defaults(verbose=1)
44options, args = parser.parse_args()
45
46def relfile(fname):
47 return os.path.join(os.path.dirname(__file__), fname)
48
cdwijayarathna3f679782014-07-09 14:00:33 +053049def getSocketArgs(socket_type):
50 if socket_type == 'ip':
51 return ""
52 elif socket_type == 'ip-ssl':
53 return "--ssl"
54 elif socket_type == 'domain':
55 return "--domain-socket=/tmp/ThriftTest.thrift"
56
57def runServiceTest(test_name, server_executable, server_extra_args, client_executable, client_extra_args, server_protocol, client_protocol, transport, port, use_zlib, socket_type):
Roger Meier40cc2322014-06-11 11:09:14 +020058 # Build command line arguments
59 server_args = [relfile(server_executable)]
60 cli_args = [relfile(client_executable)]
cdwijayarathna3f679782014-07-09 14:00:33 +053061 server_args.append('--protocol=%s' % server_protocol)
62 cli_args.append('--protocol=%s' % client_protocol)
63
Roger Meier40cc2322014-06-11 11:09:14 +020064 for which in (server_args, cli_args):
Roger Meier40cc2322014-06-11 11:09:14 +020065 which.append('--transport=%s' % transport)
66 which.append('--port=%d' % port) # default to 9090
67 if use_zlib:
68 which.append('--zlib')
cdwijayarathna3f679782014-07-09 14:00:33 +053069 if socket_type == 'ip-ssl':
Roger Meier40cc2322014-06-11 11:09:14 +020070 which.append('--ssl')
cdwijayarathna3f679782014-07-09 14:00:33 +053071 elif socket_type == 'domain':
72 which.append('--domain-socket=/tmp/ThriftTest.thrift')
Roger Meier40cc2322014-06-11 11:09:14 +020073# if options.verbose == 0:
74# which.append('-q')
75# if options.verbose == 2:
76# which.append('-v')
77
78 server_args.extend(server_extra_args)
79 cli_args.extend(client_extra_args)
Roger Meier32f39822014-06-18 22:43:17 +020080 server_log=open("log/" + test_name + "_server.log","a")
81 client_log=open("log/" + test_name + "_client.log","a")
Roger Meier40cc2322014-06-11 11:09:14 +020082
83 if options.verbose > 0:
84 print 'Testing server: %s' % (' '.join(server_args))
Roger Meier32f39822014-06-18 22:43:17 +020085 serverproc = subprocess.Popen(server_args, stdout=server_log, stderr=server_log)
Roger Meier40cc2322014-06-11 11:09:14 +020086 else:
Roger Meier32f39822014-06-18 22:43:17 +020087 serverproc = subprocess.Popen(server_args, stdout=server_log, stderr=server_log)
cdwijayarathna3f679782014-07-09 14:00:33 +053088
Roger Meier40cc2322014-06-11 11:09:14 +020089 def ensureServerAlive():
90 if serverproc.poll() is not None:
91 print ('FAIL: Server process (%s) failed with retcode %d'
92 % (' '.join(server_args), serverproc.returncode))
93 raise Exception('Server subprocess died, args: %s'
94 % (' '.join(server_args)))
95
96 # Wait for the server to start accepting connections on the given port.
97 sock = socket.socket()
98 sleep_time = 0.1 # Seconds
99 max_attempts = 100
100 try:
101 attempt = 0
cdwijayarathna3f679782014-07-09 14:00:33 +0530102
103 if socket_type != 'domain':
104 while sock.connect_ex(('127.0.0.1', port)) != 0:
105 attempt += 1
106 if attempt >= max_attempts:
107 raise Exception("TestServer not ready on port %d after %.2f seconds"
108 % (port, sleep_time * attempt))
109 ensureServerAlive()
110 time.sleep(sleep_time)
Roger Meier40cc2322014-06-11 11:09:14 +0200111 finally:
112 sock.close()
113
114 try:
Roger Meier32f39822014-06-18 22:43:17 +0200115 o = []
116 def target():
117 if options.verbose > 0:
118 print 'Testing client: %s' % (' '.join(cli_args))
119 process = subprocess.Popen(cli_args, stdout=client_log, stderr=client_log)
120 o.append(process)
121 process.communicate()
122 else:
123 process = subprocess.Popen(cli_args, stdout=client_log, stderr=client_log)
124 o.append(process)
125 process.communicate()
126 thread = threading.Thread(target=target)
127 thread.start()
128
129 thread.join(10)
130 if thread.is_alive():
131 print 'Terminating process'
132 o[0].terminate()
133 thread.join()
134 ret = o[0].returncode
Roger Meier40cc2322014-06-11 11:09:14 +0200135 if ret != 0:
136 return "Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args))
137 #raise Exception("Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args)))
138 finally:
139 # check that server didn't die
Roger Meier32f39822014-06-18 22:43:17 +0200140 #ensureServerAlive()
Roger Meier40cc2322014-06-11 11:09:14 +0200141 extra_sleep = 0
142 if extra_sleep > 0 and options.verbose > 0:
143 print ('Giving (protocol=%s,zlib=%s,ssl=%s) an extra %d seconds for child'
144 'processes to terminate via alarm'
145 % (protocol, use_zlib, use_ssl, extra_sleep))
146 time.sleep(extra_sleep)
147 os.kill(serverproc.pid, signal.SIGKILL)
148 serverproc.wait()
Roger Meier32f39822014-06-18 22:43:17 +0200149 client_log.flush()
150 server_log.flush()
151 client_log.close()
152 server_log.close()
Roger Meier40cc2322014-06-11 11:09:14 +0200153
154test_count = 0
155failed = 0
156
Roger Meier32f39822014-06-18 22:43:17 +0200157if os.path.exists('log'): shutil.rmtree('log')
158os.makedirs('log')
cdwijayarathna3f679782014-07-09 14:00:33 +0530159if os.path.exists('results.json'): os.remove('results.json')
160results_json = open("results.json","a")
161results_json.write("[\n")
Roger Meier32f39822014-06-18 22:43:17 +0200162
cdwijayarathna3f679782014-07-09 14:00:33 +0530163with open('tests.json') as data_file:
Roger Meier40cc2322014-06-11 11:09:14 +0200164 data = json.load(data_file)
165
Roger Meier32f39822014-06-18 22:43:17 +0200166#subprocess.call("export NODE_PATH=../lib/nodejs/test:../lib/nodejs/lib:${NODE_PATH}")
cdwijayarathna3f679782014-07-09 14:00:33 +0530167count = 0
Roger Meier40cc2322014-06-11 11:09:14 +0200168for server in data["server"]:
169 server_executable = server["executable"]
170 server_extra_args = ""
Roger Meier32f39822014-06-18 22:43:17 +0200171 server_lib = server["lib"]
Roger Meier40cc2322014-06-11 11:09:14 +0200172 if "extra_args" in server:
173 server_extra_args = server["extra_args"]
174 for protocol in server["protocols"]:
175 for transport in server["transports"]:
Roger Meier32f39822014-06-18 22:43:17 +0200176 for sock in server["sockets"]:
177 for client in data["client"]:
178 client_executable = client["executable"]
179 client_extra_args = ""
180 client_lib = client["lib"]
181 if "extra_args" in client:
182 client_extra_args = client["extra_args"]
183 if protocol in client["protocols"]:
184 if transport in client["transports"]:
185 if sock in client["sockets"]:
cdwijayarathna3f679782014-07-09 14:00:33 +0530186 if count != 0:
187 results_json.write(",\n")
188 count = 1
189 results_json.write("\t[\n\t\t\"" + server_lib + "\",\n\t\t\"" + client_lib + "\",\n\t\t\"" + protocol + "\",\n\t\t\"" + transport + "-" + sock + "\",\n" )
Roger Meier32f39822014-06-18 22:43:17 +0200190 test_name = server_lib + "_" + client_lib + "_" + protocol + "_" + transport + "_" + sock
cdwijayarathna3f679782014-07-09 14:00:33 +0530191 ret = runServiceTest(test_name, server_executable, server_extra_args, client_executable, client_extra_args, protocol, protocol, transport, 9090, 0, sock)
Roger Meier32f39822014-06-18 22:43:17 +0200192 if ret != None:
193 failed += 1
194 print "Error: %s" % ret
195 print "Using"
cdwijayarathna3f679782014-07-09 14:00:33 +0530196 print (' Server: %s --protocol=%s --transport=%s %s %s'
197 % (server_executable, protocol, transport, getSocketArgs(sock), ' '.join(server_extra_args)))
198 print (' Client: %s --protocol=%s --transport=%s %s %s'
199 % (client_executable, protocol, transport, getSocketArgs(sock), ''.join(client_extra_args)))
200 results_json.write("\t\t\"failure (<a href=\\\"log/" + test_name + "_client.log\\\">client</a>, <a href=\\\"log/" + test_name + "_server.log\\\">server</a>)\"\n")
201 else:
202 results_json.write("\t\t\"success (<a href=\\\"log/" + test_name + "_client.log\\\">client</a>, <a href=\\\"log/" + test_name + "_server.log\\\">server</a>)\"\n")
203 results_json.write("\t]")
Roger Meier32f39822014-06-18 22:43:17 +0200204 test_count += 1
cdwijayarathna3f679782014-07-09 14:00:33 +0530205 if protocol == 'binary' and 'accel' in client["protocols"]:
206 if transport in client["transports"]:
207 if sock in client["sockets"]:
208 if count != 0:
209 results_json.write(",\n")
210 count = 1
211 results_json.write("\t[\n\t\t\"" + server_lib + "\",\n\t\t\"" + client_lib + "\",\n\t\t\"accel-binary\",\n\t\t\"" + transport + "-" + sock + "\",\n" )
212 test_name = server_lib + "_" + client_lib + "_accel-binary_" + transport + "_" + sock
213 ret = runServiceTest(test_name, server_executable, server_extra_args, client_executable, client_extra_args, protocol, 'accel', transport, 9090, 0, sock)
214 if ret != None:
215 failed += 1
216 print "Error: %s" % ret
217 print "Using"
218 print (' Server: %s --protocol=%s --transport=%s %s %s'
219 % (server_executable, protocol, transport, getSocketArgs(sock), ' '.join(server_extra_args)))
220 print (' Client: %s --protocol=%s --transport=%s %s %s'
221 % (client_executable, protocol, transport , getSocketArgs(sock), ''.join(client_extra_args)))
222 results_json.write("\t\t\"failure (<a href=\\\"log/" + test_name + "_client.log\\\">client</a>, <a href=\\\"log/" + test_name + "_server.log\\\">server</a>)\"\n")
223 else:
224 results_json.write("\t\t\"success (<a href=\\\"log/" + test_name + "_client.log\\\">client</a>, <a href=\\\"log/" + test_name + "_server.log\\\">server</a>)\"\n")
225 results_json.write("\t]")
226 test_count += 1
227 if protocol == 'accel' and 'binary' in client["protocols"]:
228 if transport in client["transports"]:
229 if sock in client["sockets"]:
230 if count != 0:
231 results_json.write(",\n")
232 count = 1
233 results_json.write("\t[\n\t\t\"" + server_lib + "\",\n\t\t\"" + client_lib + "\",\n\t\t\"binary-accel\",\n\t\t\"" + transport + "-" + sock + "\",\n" )
234 test_name = server_lib + "_" + client_lib + "_accel-binary_" + transport + "_" + sock
235 ssl = 0
236 if sock == 'ip-ssl':
237 ssl = 1
238 ret = runServiceTest(test_name, server_executable, server_extra_args, client_executable, client_extra_args, protocol, 'binary', transport, 9090, 0, sock)
239 if ret != None:
240 failed += 1
241 print "Error: %s" % ret
242 print "Using"
243 print (' Server: %s --protocol=%s --transport=%s %s %s'
244 % (server_executable, protocol, transport + sock, getSocketArgs(sock), ' '.join(server_extra_args)))
245 print (' Client: %s --protocol=%s --transport=%s %s %s'
246 % (client_executable, protocol, transport + sock, getSocketArgs(sock), ''.join(client_extra_args)))
247 results_json.write("\t\t\"failure (<a href=\\\"log/" + test_name + "_client.log\\\">client</a>, <a href=\\\"log/" + test_name + "_server.log\\\">server</a>)\"\n")
248 else:
249 results_json.write("\t\t\"success (<a href=\\\"log/" + test_name + "_client.log\\\">client</a>, <a href=\\\"log/" + test_name + "_server.log\\\">server</a>)\"\n")
250 results_json.write("\t]")
251 test_count += 1
252results_json.write("\n]")
253results_json.flush()
254results_json.close()
255print '%s failed of %s tests in total' % (failed, test_count)