blob: 0068c81d0a2f573961fbe4bd300a0890ac8f5f37 [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':
cdwijayarathna0d4072b2014-08-09 21:32:21 +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':
cdwijayarathna0d4072b2014-08-09 21:32:21 +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('\"')
95 if client_lib == 'java':
96 cli_args.append('\"')
Roger Meier40cc2322014-06-11 11:09:14 +020097
98 server_args.extend(server_extra_args)
99 cli_args.extend(client_extra_args)
Roger Meiere8bafb62014-08-01 23:39:32 +0200100
cdwijayarathna0d4072b2014-08-09 21:32:21 +0530101 server_log=open(relfile("log/" + test_name + "_server.log"),"a")
102 client_log=open(relfile("log/" + test_name + "_client.log"),"a")
Roger Meier40cc2322014-06-11 11:09:14 +0200103
cdwijayarathna11066bd2014-07-26 23:02:13 +0530104 try:
105 if options.verbose > 0:
106 print 'Testing server: %s' % (' '.join(server_args))
107 serverproc = subprocess.Popen(server_args, stdout=server_log, stderr=server_log)
108 else:
109 serverproc = subprocess.Popen(server_args, stdout=server_log, stderr=server_log)
110 except OSError as e:
111 return "OS error({0}): {1}".format(e.errno, e.strerror)
cdwijayarathna3f679782014-07-09 14:00:33 +0530112
Roger Meier40cc2322014-06-11 11:09:14 +0200113 def ensureServerAlive():
114 if serverproc.poll() is not None:
cdwijayarathna11066bd2014-07-26 23:02:13 +0530115 return 'Server subprocess died, args: %s' % (' '.join(server_args))
Roger Meier40cc2322014-06-11 11:09:14 +0200116
117 # Wait for the server to start accepting connections on the given port.
118 sock = socket.socket()
119 sleep_time = 0.1 # Seconds
120 max_attempts = 100
121 try:
122 attempt = 0
cdwijayarathna3f679782014-07-09 14:00:33 +0530123
124 if socket_type != 'domain':
125 while sock.connect_ex(('127.0.0.1', port)) != 0:
126 attempt += 1
127 if attempt >= max_attempts:
cdwijayarathna11066bd2014-07-26 23:02:13 +0530128 return "TestServer not ready on port %d after %.2f seconds" % (port, sleep_time * attempt)
cdwijayarathna3f679782014-07-09 14:00:33 +0530129 ensureServerAlive()
130 time.sleep(sleep_time)
Roger Meier40cc2322014-06-11 11:09:14 +0200131 finally:
132 sock.close()
133
134 try:
Roger Meier32f39822014-06-18 22:43:17 +0200135 o = []
136 def target():
cdwijayarathna11066bd2014-07-26 23:02:13 +0530137 try:
138 if options.verbose > 0:
139 print 'Testing client: %s' % (' '.join(cli_args))
140 process = subprocess.Popen(cli_args, stdout=client_log, stderr=client_log)
141 o.append(process)
142 process.communicate()
143 else:
144 process = subprocess.Popen(cli_args, stdout=client_log, stderr=client_log)
145 o.append(process)
146 process.communicate()
147 except OSError as e:
148 return "OS error({0}): {1}".format(e.errno, e.strerror)
149 except:
150 return "Unexpected error:", sys.exc_info()[0]
Roger Meier32f39822014-06-18 22:43:17 +0200151 thread = threading.Thread(target=target)
152 thread.start()
153
154 thread.join(10)
155 if thread.is_alive():
156 print 'Terminating process'
157 o[0].terminate()
158 thread.join()
cdwijayarathna11066bd2014-07-26 23:02:13 +0530159 if(len(o)==0):
160 return "Client subprocess failed, args: %s" % (' '.join(cli_args))
Roger Meier32f39822014-06-18 22:43:17 +0200161 ret = o[0].returncode
Roger Meier40cc2322014-06-11 11:09:14 +0200162 if ret != 0:
163 return "Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args))
164 #raise Exception("Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args)))
165 finally:
166 # check that server didn't die
Roger Meier32f39822014-06-18 22:43:17 +0200167 #ensureServerAlive()
Roger Meier40cc2322014-06-11 11:09:14 +0200168 extra_sleep = 0
169 if extra_sleep > 0 and options.verbose > 0:
170 print ('Giving (protocol=%s,zlib=%s,ssl=%s) an extra %d seconds for child'
171 'processes to terminate via alarm'
172 % (protocol, use_zlib, use_ssl, extra_sleep))
173 time.sleep(extra_sleep)
cdwijayarathna0d4072b2014-08-09 21:32:21 +0530174 os.kill(serverproc.pid, signal.SIGTERM)
175 #serverproc.wait()
Roger Meier32f39822014-06-18 22:43:17 +0200176 client_log.flush()
177 server_log.flush()
178 client_log.close()
179 server_log.close()
Roger Meier40cc2322014-06-11 11:09:14 +0200180
181test_count = 0
182failed = 0
cdwijayarathna0d4072b2014-08-09 21:32:21 +0530183hard_fail_count = 0
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530184platform = platform.system()
cdwijayarathna0d4072b2014-08-09 21:32:21 +0530185if os.path.exists(relfile('log')): shutil.rmtree(relfile('log'))
186os.makedirs(relfile('log'))
187if os.path.exists(relfile('results.json')): os.remove(relfile('results.json'))
188results_json = open(relfile("results.json"),"a")
cdwijayarathna3f679782014-07-09 14:00:33 +0530189results_json.write("[\n")
Roger Meier32f39822014-06-18 22:43:17 +0200190
cdwijayarathna0d4072b2014-08-09 21:32:21 +0530191with open(relfile('tests.json')) as data_file:
Roger Meier40cc2322014-06-11 11:09:14 +0200192 data = json.load(data_file)
193
Roger Meier32f39822014-06-18 22:43:17 +0200194#subprocess.call("export NODE_PATH=../lib/nodejs/test:../lib/nodejs/lib:${NODE_PATH}")
cdwijayarathna3f679782014-07-09 14:00:33 +0530195count = 0
Roger Meier40cc2322014-06-11 11:09:14 +0200196for server in data["server"]:
197 server_executable = server["executable"]
198 server_extra_args = ""
Roger Meier32f39822014-06-18 22:43:17 +0200199 server_lib = server["lib"]
Roger Meier40cc2322014-06-11 11:09:14 +0200200 if "extra_args" in server:
201 server_extra_args = server["extra_args"]
202 for protocol in server["protocols"]:
203 for transport in server["transports"]:
Roger Meier32f39822014-06-18 22:43:17 +0200204 for sock in server["sockets"]:
205 for client in data["client"]:
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530206 if platform in server["platform"] and platform in client["platform"]:
207 client_executable = client["executable"]
208 client_extra_args = ""
209 client_lib = client["lib"]
210 if "extra_args" in client:
211 client_extra_args = client["extra_args"]
212 if protocol in client["protocols"]:
213 if transport in client["transports"]:
214 if sock in client["sockets"]:
215 if count != 0:
216 results_json.write(",\n")
217 count = 1
218 results_json.write("\t[\n\t\t\"" + server_lib + "\",\n\t\t\"" + client_lib + "\",\n\t\t\"" + protocol + "\",\n\t\t\"" + transport + "-" + sock + "\",\n" )
219 test_name = server_lib + "_" + client_lib + "_" + protocol + "_" + transport + "_" + sock
Roger Meiere8bafb62014-08-01 23:39:32 +0200220 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 +0530221 if ret != None:
222 failed += 1
cdwijayarathna0d4072b2014-08-09 21:32:21 +0530223 if client["exit"] == "hard" and server["exit"] == "hard":
224 hard_fail_count +=1
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530225 print "Error: %s" % ret
226 print "Using"
227 print (' Server: %s --protocol=%s --transport=%s %s %s'
228 % (server_executable, protocol, transport, getSocketArgs(sock), ' '.join(server_extra_args)))
229 print (' Client: %s --protocol=%s --transport=%s %s %s'
230 % (client_executable, protocol, transport, getSocketArgs(sock), ''.join(client_extra_args)))
Roger Meiere8bafb62014-08-01 23:39:32 +0200231 results_json.write("\t\t\"failure\",\n")
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530232 else:
Roger Meiere8bafb62014-08-01 23:39:32 +0200233 results_json.write("\t\t\"success\",\n")
234 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 +0530235 test_count += 1
236 if protocol == 'binary' and 'accel' in client["protocols"]:
237 if transport in client["transports"]:
238 if sock in client["sockets"]:
239 if count != 0:
240 results_json.write(",\n")
241 count = 1
242 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" )
243 test_name = server_lib + "_" + client_lib + "_accel-binary_" + transport + "_" + sock
Roger Meiere8bafb62014-08-01 23:39:32 +0200244 ret = runServiceTest(test_name, server_lib,server_executable, server_extra_args, client_lib, client_executable, client_extra_args, protocol, 'accel', transport, 9090, 0, sock)
245
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530246 if ret != None:
247 failed += 1
cdwijayarathna0d4072b2014-08-09 21:32:21 +0530248 if client["exit"] == "hard" and server["exit"] == "hard":
249 hard_fail_count +=1
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530250 print "Error: %s" % ret
251 print "Using"
252 print (' Server: %s --protocol=%s --transport=%s %s %s'
253 % (server_executable, protocol, transport, getSocketArgs(sock), ' '.join(server_extra_args)))
254 print (' Client: %s --protocol=%s --transport=%s %s %s'
255 % (client_executable, protocol, transport , getSocketArgs(sock), ''.join(client_extra_args)))
Roger Meiere8bafb62014-08-01 23:39:32 +0200256 results_json.write("\t\t\"failure\",\n")
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530257 else:
Roger Meiere8bafb62014-08-01 23:39:32 +0200258 results_json.write("\t\t\"success\",\n")
259 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 +0530260 test_count += 1
261 if protocol == 'accel' and 'binary' in client["protocols"]:
262 if transport in client["transports"]:
263 if sock in client["sockets"]:
264 if count != 0:
265 results_json.write(",\n")
266 count = 1
267 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 +0200268 test_name = server_lib + "_" + client_lib + "_binary-accel_" + transport + "_" + sock
269 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 +0530270 if ret != None:
271 failed += 1
cdwijayarathna0d4072b2014-08-09 21:32:21 +0530272 if client["exit"] == "hard" and server["exit"] == "hard":
273 hard_fail_count +=1
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530274 print "Error: %s" % ret
275 print "Using"
276 print (' Server: %s --protocol=%s --transport=%s %s %s'
277 % (server_executable, protocol, transport + sock, getSocketArgs(sock), ' '.join(server_extra_args)))
278 print (' Client: %s --protocol=%s --transport=%s %s %s'
279 % (client_executable, protocol, transport + sock, getSocketArgs(sock), ''.join(client_extra_args)))
Roger Meiere8bafb62014-08-01 23:39:32 +0200280 results_json.write("\t\t\"failure\",\n")
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530281 else:
Roger Meiere8bafb62014-08-01 23:39:32 +0200282 results_json.write("\t\t\"success\",\n")
283 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 +0530284 test_count += 1
cdwijayarathna3f679782014-07-09 14:00:33 +0530285results_json.write("\n]")
286results_json.flush()
287results_json.close()
cdwijayarathna0d4072b2014-08-09 21:32:21 +0530288print '%s failed of %s tests in total' % (failed, test_count)
289sys.exit(hard_fail_count)