blob: f69cb620596645aec5943e020cfd454979f3eb92 [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
cdwijayarathna75eb2a32014-07-28 21:54:56 +053030import platform
Roger Meier32f39822014-06-18 22:43:17 +020031import shutil
32import threading
Roger Meier40cc2322014-06-11 11:09:14 +020033from optparse import OptionParser
34
35parser = OptionParser()
36parser.add_option("--port", type="int", dest="port", default=9090,
37 help="port number for server to listen on")
38parser.add_option('-v', '--verbose', action="store_const",
39 dest="verbose", const=2,
40 help="verbose output")
41parser.add_option('-q', '--quiet', action="store_const",
42 dest="verbose", const=0,
43 help="minimal output")
44parser.set_defaults(verbose=1)
45options, args = parser.parse_args()
46
47def relfile(fname):
48 return os.path.join(os.path.dirname(__file__), fname)
49
cdwijayarathna3f679782014-07-09 14:00:33 +053050def getSocketArgs(socket_type):
51 if socket_type == 'ip':
52 return ""
53 elif socket_type == 'ip-ssl':
54 return "--ssl"
55 elif socket_type == 'domain':
56 return "--domain-socket=/tmp/ThriftTest.thrift"
57
Roger Meiere8bafb62014-08-01 23:39:32 +020058def runServiceTest(test_name, server_lib, server_executable, server_extra_args, client_lib, client_executable, client_extra_args, server_protocol, client_protocol, transport, port, use_zlib, socket_type):
Roger Meier40cc2322014-06-11 11:09:14 +020059 # Build command line arguments
Roger Meiere8bafb62014-08-01 23:39:32 +020060 server_args = []
61 cli_args = []
62 if server_lib == 'java':
cdwijayarathnac53bbd22014-08-06 01:21:06 +053063 server_args.append(server_executable[0])
64 server_args.append(server_executable[1])
65 server_args.append(relfile(server_executable[2]))
Roger Meiere8bafb62014-08-01 23:39:32 +020066 server_args.extend(['-Dtestargs','\"'])
67 else:
68 server_args = [relfile(server_executable)]
69 if client_lib == 'java':
cdwijayarathnac53bbd22014-08-06 01:21:06 +053070 cli_args.append(client_executable[0])
71 cli_args.append(client_executable[1])
72 cli_args.append(relfile(client_executable[2]))
Roger Meiere8bafb62014-08-01 23:39:32 +020073 cli_args.extend(['-Dtestargs','\"'])
74 else:
75 cli_args = [relfile(client_executable)]
76
cdwijayarathna3f679782014-07-09 14:00:33 +053077 server_args.append('--protocol=%s' % server_protocol)
78 cli_args.append('--protocol=%s' % client_protocol)
79
Roger Meier40cc2322014-06-11 11:09:14 +020080 for which in (server_args, cli_args):
Roger Meier40cc2322014-06-11 11:09:14 +020081 which.append('--transport=%s' % transport)
82 which.append('--port=%d' % port) # default to 9090
83 if use_zlib:
84 which.append('--zlib')
cdwijayarathna3f679782014-07-09 14:00:33 +053085 if socket_type == 'ip-ssl':
Roger Meier40cc2322014-06-11 11:09:14 +020086 which.append('--ssl')
cdwijayarathna3f679782014-07-09 14:00:33 +053087 elif socket_type == 'domain':
88 which.append('--domain-socket=/tmp/ThriftTest.thrift')
Roger Meier40cc2322014-06-11 11:09:14 +020089# if options.verbose == 0:
90# which.append('-q')
91# if options.verbose == 2:
92# which.append('-v')
Roger Meiere8bafb62014-08-01 23:39:32 +020093 if server_lib == 'java':
94 server_args.append('\"')
cdwijayarathnac53bbd22014-08-06 01:21:06 +053095 elif server_lib == 'py':
96 server_args.append('--genpydir=' + relfile('py/gen-py'))
Roger Meiere8bafb62014-08-01 23:39:32 +020097 if client_lib == 'java':
98 cli_args.append('\"')
cdwijayarathnac53bbd22014-08-06 01:21:06 +053099 elif client_lib == 'py':
100 cli_args.append('--genpydir=' + relfile('py/gen-py'))
Roger Meier40cc2322014-06-11 11:09:14 +0200101
102 server_args.extend(server_extra_args)
103 cli_args.extend(client_extra_args)
Roger Meiere8bafb62014-08-01 23:39:32 +0200104
cdwijayarathnac53bbd22014-08-06 01:21:06 +0530105 server_log=open(relfile("log/" + test_name + "_server.log"),"a")
106 client_log=open(relfile("log/" + test_name + "_client.log"),"a")
Roger Meier40cc2322014-06-11 11:09:14 +0200107
cdwijayarathna11066bd2014-07-26 23:02:13 +0530108 try:
109 if options.verbose > 0:
110 print 'Testing server: %s' % (' '.join(server_args))
111 serverproc = subprocess.Popen(server_args, stdout=server_log, stderr=server_log)
112 else:
113 serverproc = subprocess.Popen(server_args, stdout=server_log, stderr=server_log)
114 except OSError as e:
115 return "OS error({0}): {1}".format(e.errno, e.strerror)
cdwijayarathna3f679782014-07-09 14:00:33 +0530116
Roger Meier40cc2322014-06-11 11:09:14 +0200117 def ensureServerAlive():
118 if serverproc.poll() is not None:
cdwijayarathna11066bd2014-07-26 23:02:13 +0530119 return 'Server subprocess died, args: %s' % (' '.join(server_args))
Roger Meier40cc2322014-06-11 11:09:14 +0200120
121 # Wait for the server to start accepting connections on the given port.
122 sock = socket.socket()
123 sleep_time = 0.1 # Seconds
124 max_attempts = 100
125 try:
126 attempt = 0
cdwijayarathna3f679782014-07-09 14:00:33 +0530127
128 if socket_type != 'domain':
129 while sock.connect_ex(('127.0.0.1', port)) != 0:
130 attempt += 1
131 if attempt >= max_attempts:
cdwijayarathna11066bd2014-07-26 23:02:13 +0530132 return "TestServer not ready on port %d after %.2f seconds" % (port, sleep_time * attempt)
cdwijayarathna3f679782014-07-09 14:00:33 +0530133 ensureServerAlive()
134 time.sleep(sleep_time)
Roger Meier40cc2322014-06-11 11:09:14 +0200135 finally:
136 sock.close()
137
138 try:
Roger Meier32f39822014-06-18 22:43:17 +0200139 o = []
140 def target():
cdwijayarathna11066bd2014-07-26 23:02:13 +0530141 try:
142 if options.verbose > 0:
143 print 'Testing client: %s' % (' '.join(cli_args))
144 process = subprocess.Popen(cli_args, stdout=client_log, stderr=client_log)
145 o.append(process)
146 process.communicate()
147 else:
148 process = subprocess.Popen(cli_args, stdout=client_log, stderr=client_log)
149 o.append(process)
150 process.communicate()
151 except OSError as e:
152 return "OS error({0}): {1}".format(e.errno, e.strerror)
153 except:
154 return "Unexpected error:", sys.exc_info()[0]
Roger Meier32f39822014-06-18 22:43:17 +0200155 thread = threading.Thread(target=target)
156 thread.start()
157
158 thread.join(10)
159 if thread.is_alive():
160 print 'Terminating process'
161 o[0].terminate()
162 thread.join()
cdwijayarathna11066bd2014-07-26 23:02:13 +0530163 if(len(o)==0):
164 return "Client subprocess failed, args: %s" % (' '.join(cli_args))
Roger Meier32f39822014-06-18 22:43:17 +0200165 ret = o[0].returncode
Roger Meier40cc2322014-06-11 11:09:14 +0200166 if ret != 0:
167 return "Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args))
168 #raise Exception("Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args)))
169 finally:
170 # check that server didn't die
Roger Meier32f39822014-06-18 22:43:17 +0200171 #ensureServerAlive()
Roger Meier40cc2322014-06-11 11:09:14 +0200172 extra_sleep = 0
173 if extra_sleep > 0 and options.verbose > 0:
174 print ('Giving (protocol=%s,zlib=%s,ssl=%s) an extra %d seconds for child'
175 'processes to terminate via alarm'
176 % (protocol, use_zlib, use_ssl, extra_sleep))
177 time.sleep(extra_sleep)
cdwijayarathnac53bbd22014-08-06 01:21:06 +0530178 os.kill(serverproc.pid, signal.SIGTERM)
179 #serverproc.wait()
Roger Meier32f39822014-06-18 22:43:17 +0200180 client_log.flush()
181 server_log.flush()
182 client_log.close()
183 server_log.close()
Roger Meier40cc2322014-06-11 11:09:14 +0200184
185test_count = 0
186failed = 0
cdwijayarathnac53bbd22014-08-06 01:21:06 +0530187hard_fail_count = 0
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530188platform = platform.system()
cdwijayarathnac53bbd22014-08-06 01:21:06 +0530189if os.path.exists(relfile('log')): shutil.rmtree(relfile('log'))
190os.makedirs(relfile('log'))
191if os.path.exists(relfile('results.json')): os.remove(relfile('results.json'))
192results_json = open(relfile("results.json"),"a")
cdwijayarathna3f679782014-07-09 14:00:33 +0530193results_json.write("[\n")
Roger Meier32f39822014-06-18 22:43:17 +0200194
cdwijayarathnac53bbd22014-08-06 01:21:06 +0530195with open(relfile('tests.json')) as data_file:
Roger Meier40cc2322014-06-11 11:09:14 +0200196 data = json.load(data_file)
197
Roger Meier32f39822014-06-18 22:43:17 +0200198#subprocess.call("export NODE_PATH=../lib/nodejs/test:../lib/nodejs/lib:${NODE_PATH}")
cdwijayarathna3f679782014-07-09 14:00:33 +0530199count = 0
Roger Meier40cc2322014-06-11 11:09:14 +0200200for server in data["server"]:
201 server_executable = server["executable"]
202 server_extra_args = ""
Roger Meier32f39822014-06-18 22:43:17 +0200203 server_lib = server["lib"]
Roger Meier40cc2322014-06-11 11:09:14 +0200204 if "extra_args" in server:
205 server_extra_args = server["extra_args"]
206 for protocol in server["protocols"]:
207 for transport in server["transports"]:
Roger Meier32f39822014-06-18 22:43:17 +0200208 for sock in server["sockets"]:
209 for client in data["client"]:
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530210 if platform in server["platform"] and platform in client["platform"]:
211 client_executable = client["executable"]
212 client_extra_args = ""
213 client_lib = client["lib"]
214 if "extra_args" in client:
215 client_extra_args = client["extra_args"]
216 if protocol in client["protocols"]:
217 if transport in client["transports"]:
218 if sock in client["sockets"]:
219 if count != 0:
220 results_json.write(",\n")
221 count = 1
222 results_json.write("\t[\n\t\t\"" + server_lib + "\",\n\t\t\"" + client_lib + "\",\n\t\t\"" + protocol + "\",\n\t\t\"" + transport + "-" + sock + "\",\n" )
223 test_name = server_lib + "_" + client_lib + "_" + protocol + "_" + transport + "_" + sock
Roger Meiere8bafb62014-08-01 23:39:32 +0200224 ret = runServiceTest(test_name, server_lib, server_executable, server_extra_args, client_lib, client_executable, client_extra_args, protocol, protocol, transport, 9090, 0, sock)
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530225 if ret != None:
226 failed += 1
cdwijayarathnac53bbd22014-08-06 01:21:06 +0530227 if client["exit"] == "hard" and server["exit"] == "hard":
228 hard_fail_count +=1
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530229 print "Error: %s" % ret
230 print "Using"
231 print (' Server: %s --protocol=%s --transport=%s %s %s'
232 % (server_executable, protocol, transport, getSocketArgs(sock), ' '.join(server_extra_args)))
233 print (' Client: %s --protocol=%s --transport=%s %s %s'
234 % (client_executable, protocol, transport, getSocketArgs(sock), ''.join(client_extra_args)))
Roger Meiere8bafb62014-08-01 23:39:32 +0200235 results_json.write("\t\t\"failure\",\n")
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530236 else:
Roger Meiere8bafb62014-08-01 23:39:32 +0200237 results_json.write("\t\t\"success\",\n")
238 results_json.write("\t\t{\n\t\t\t\"Client\":\"log/" + test_name + "_client.log\",\n\t\t\t\"Server\":\"log/" + test_name + "_server.log\"\n\t\t}\n\t]")
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530239 test_count += 1
240 if protocol == 'binary' and 'accel' in client["protocols"]:
241 if transport in client["transports"]:
242 if sock in client["sockets"]:
243 if count != 0:
244 results_json.write(",\n")
245 count = 1
246 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" )
247 test_name = server_lib + "_" + client_lib + "_accel-binary_" + transport + "_" + sock
Roger Meiere8bafb62014-08-01 23:39:32 +0200248 ret = runServiceTest(test_name, server_lib,server_executable, server_extra_args, client_lib, client_executable, client_extra_args, protocol, 'accel', transport, 9090, 0, sock)
249
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530250 if ret != None:
251 failed += 1
cdwijayarathnac53bbd22014-08-06 01:21:06 +0530252 if client["exit"] == "hard" and server["exit"] == "hard":
253 hard_fail_count +=1
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530254 print "Error: %s" % ret
255 print "Using"
256 print (' Server: %s --protocol=%s --transport=%s %s %s'
257 % (server_executable, protocol, transport, getSocketArgs(sock), ' '.join(server_extra_args)))
258 print (' Client: %s --protocol=%s --transport=%s %s %s'
259 % (client_executable, protocol, transport , getSocketArgs(sock), ''.join(client_extra_args)))
Roger Meiere8bafb62014-08-01 23:39:32 +0200260 results_json.write("\t\t\"failure\",\n")
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530261 else:
Roger Meiere8bafb62014-08-01 23:39:32 +0200262 results_json.write("\t\t\"success\",\n")
263 results_json.write("\t\t{\n\t\t\t\"Client\":\"log/" + test_name + "_client.log\",\n\t\t\t\"Server\":\"log/" + test_name + "_server.log\"\n\t\t}\n\t]")
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530264 test_count += 1
265 if protocol == 'accel' and 'binary' in client["protocols"]:
266 if transport in client["transports"]:
267 if sock in client["sockets"]:
268 if count != 0:
269 results_json.write(",\n")
270 count = 1
271 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" )
Roger Meiere8bafb62014-08-01 23:39:32 +0200272 test_name = server_lib + "_" + client_lib + "_binary-accel_" + transport + "_" + sock
273 ret = runServiceTest(test_name, server_lib,server_executable, server_extra_args, client_lib, client_executable, client_extra_args, protocol, 'binary', transport, 9090, 0, sock)
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530274 if ret != None:
275 failed += 1
cdwijayarathnac53bbd22014-08-06 01:21:06 +0530276 if client["exit"] == "hard" and server["exit"] == "hard":
277 hard_fail_count +=1
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530278 print "Error: %s" % ret
279 print "Using"
280 print (' Server: %s --protocol=%s --transport=%s %s %s'
281 % (server_executable, protocol, transport + sock, getSocketArgs(sock), ' '.join(server_extra_args)))
282 print (' Client: %s --protocol=%s --transport=%s %s %s'
283 % (client_executable, protocol, transport + sock, getSocketArgs(sock), ''.join(client_extra_args)))
Roger Meiere8bafb62014-08-01 23:39:32 +0200284 results_json.write("\t\t\"failure\",\n")
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530285 else:
Roger Meiere8bafb62014-08-01 23:39:32 +0200286 results_json.write("\t\t\"success\",\n")
287 results_json.write("\t\t{\n\t\t\t\"Client\":\"log/" + test_name + "_client.log\",\n\t\t\t\"Server\":\"log/" + test_name + "_server.log\"\n\t\t}\n\t]")
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530288 test_count += 1
cdwijayarathna3f679782014-07-09 14:00:33 +0530289results_json.write("\n]")
290results_json.flush()
291results_json.close()
cdwijayarathnac53bbd22014-08-06 01:21:06 +0530292print '%s failed of %s tests in total' % (failed, test_count)
293sys.exit(hard_fail_count)