blob: 00d2d0d24bf120a129a0fcff42368ec9e598876c [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':
63 server_executable[2] = relfile(server_executable[2])
64 server_args.extend(server_executable)
65 server_args.extend(['-Dtestargs','\"'])
66 else:
67 server_args = [relfile(server_executable)]
68 if client_lib == 'java':
69 client_executable[2] = relfile(client_executable[2])
70 cli_args.extend(client_executable)
71 cli_args.extend(['-Dtestargs','\"'])
72 else:
73 cli_args = [relfile(client_executable)]
74
cdwijayarathna3f679782014-07-09 14:00:33 +053075 server_args.append('--protocol=%s' % server_protocol)
76 cli_args.append('--protocol=%s' % client_protocol)
77
Roger Meier40cc2322014-06-11 11:09:14 +020078 for which in (server_args, cli_args):
Roger Meier40cc2322014-06-11 11:09:14 +020079 which.append('--transport=%s' % transport)
80 which.append('--port=%d' % port) # default to 9090
81 if use_zlib:
82 which.append('--zlib')
cdwijayarathna3f679782014-07-09 14:00:33 +053083 if socket_type == 'ip-ssl':
Roger Meier40cc2322014-06-11 11:09:14 +020084 which.append('--ssl')
cdwijayarathna3f679782014-07-09 14:00:33 +053085 elif socket_type == 'domain':
86 which.append('--domain-socket=/tmp/ThriftTest.thrift')
Roger Meier40cc2322014-06-11 11:09:14 +020087# if options.verbose == 0:
88# which.append('-q')
89# if options.verbose == 2:
90# which.append('-v')
Roger Meiere8bafb62014-08-01 23:39:32 +020091 if server_lib == 'java':
92 server_args.append('\"')
93 if client_lib == 'java':
94 cli_args.append('\"')
Roger Meier40cc2322014-06-11 11:09:14 +020095
96 server_args.extend(server_extra_args)
97 cli_args.extend(client_extra_args)
Roger Meiere8bafb62014-08-01 23:39:32 +020098
Roger Meier32f39822014-06-18 22:43:17 +020099 server_log=open("log/" + test_name + "_server.log","a")
100 client_log=open("log/" + test_name + "_client.log","a")
Roger Meier40cc2322014-06-11 11:09:14 +0200101
cdwijayarathna11066bd2014-07-26 23:02:13 +0530102 try:
103 if options.verbose > 0:
104 print 'Testing server: %s' % (' '.join(server_args))
105 serverproc = subprocess.Popen(server_args, stdout=server_log, stderr=server_log)
106 else:
107 serverproc = subprocess.Popen(server_args, stdout=server_log, stderr=server_log)
108 except OSError as e:
109 return "OS error({0}): {1}".format(e.errno, e.strerror)
cdwijayarathna3f679782014-07-09 14:00:33 +0530110
Roger Meier40cc2322014-06-11 11:09:14 +0200111 def ensureServerAlive():
112 if serverproc.poll() is not None:
cdwijayarathna11066bd2014-07-26 23:02:13 +0530113 return 'Server subprocess died, args: %s' % (' '.join(server_args))
Roger Meier40cc2322014-06-11 11:09:14 +0200114
115 # Wait for the server to start accepting connections on the given port.
116 sock = socket.socket()
117 sleep_time = 0.1 # Seconds
118 max_attempts = 100
119 try:
120 attempt = 0
cdwijayarathna3f679782014-07-09 14:00:33 +0530121
122 if socket_type != 'domain':
123 while sock.connect_ex(('127.0.0.1', port)) != 0:
124 attempt += 1
125 if attempt >= max_attempts:
cdwijayarathna11066bd2014-07-26 23:02:13 +0530126 return "TestServer not ready on port %d after %.2f seconds" % (port, sleep_time * attempt)
cdwijayarathna3f679782014-07-09 14:00:33 +0530127 ensureServerAlive()
128 time.sleep(sleep_time)
Roger Meier40cc2322014-06-11 11:09:14 +0200129 finally:
130 sock.close()
131
132 try:
Roger Meier32f39822014-06-18 22:43:17 +0200133 o = []
134 def target():
cdwijayarathna11066bd2014-07-26 23:02:13 +0530135 try:
136 if options.verbose > 0:
137 print 'Testing client: %s' % (' '.join(cli_args))
138 process = subprocess.Popen(cli_args, stdout=client_log, stderr=client_log)
139 o.append(process)
140 process.communicate()
141 else:
142 process = subprocess.Popen(cli_args, stdout=client_log, stderr=client_log)
143 o.append(process)
144 process.communicate()
145 except OSError as e:
146 return "OS error({0}): {1}".format(e.errno, e.strerror)
147 except:
148 return "Unexpected error:", sys.exc_info()[0]
Roger Meier32f39822014-06-18 22:43:17 +0200149 thread = threading.Thread(target=target)
150 thread.start()
151
152 thread.join(10)
153 if thread.is_alive():
154 print 'Terminating process'
155 o[0].terminate()
156 thread.join()
cdwijayarathna11066bd2014-07-26 23:02:13 +0530157 if(len(o)==0):
158 return "Client subprocess failed, args: %s" % (' '.join(cli_args))
Roger Meier32f39822014-06-18 22:43:17 +0200159 ret = o[0].returncode
Roger Meier40cc2322014-06-11 11:09:14 +0200160 if ret != 0:
161 return "Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args))
162 #raise Exception("Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args)))
163 finally:
164 # check that server didn't die
Roger Meier32f39822014-06-18 22:43:17 +0200165 #ensureServerAlive()
Roger Meier40cc2322014-06-11 11:09:14 +0200166 extra_sleep = 0
167 if extra_sleep > 0 and options.verbose > 0:
168 print ('Giving (protocol=%s,zlib=%s,ssl=%s) an extra %d seconds for child'
169 'processes to terminate via alarm'
170 % (protocol, use_zlib, use_ssl, extra_sleep))
171 time.sleep(extra_sleep)
172 os.kill(serverproc.pid, signal.SIGKILL)
173 serverproc.wait()
Roger Meier32f39822014-06-18 22:43:17 +0200174 client_log.flush()
175 server_log.flush()
176 client_log.close()
177 server_log.close()
Roger Meier40cc2322014-06-11 11:09:14 +0200178
179test_count = 0
180failed = 0
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530181platform = platform.system()
Roger Meier32f39822014-06-18 22:43:17 +0200182if os.path.exists('log'): shutil.rmtree('log')
183os.makedirs('log')
cdwijayarathna3f679782014-07-09 14:00:33 +0530184if os.path.exists('results.json'): os.remove('results.json')
185results_json = open("results.json","a")
186results_json.write("[\n")
Roger Meier32f39822014-06-18 22:43:17 +0200187
cdwijayarathna3f679782014-07-09 14:00:33 +0530188with open('tests.json') as data_file:
Roger Meier40cc2322014-06-11 11:09:14 +0200189 data = json.load(data_file)
190
Roger Meier32f39822014-06-18 22:43:17 +0200191#subprocess.call("export NODE_PATH=../lib/nodejs/test:../lib/nodejs/lib:${NODE_PATH}")
cdwijayarathna3f679782014-07-09 14:00:33 +0530192count = 0
Roger Meier40cc2322014-06-11 11:09:14 +0200193for server in data["server"]:
194 server_executable = server["executable"]
195 server_extra_args = ""
Roger Meier32f39822014-06-18 22:43:17 +0200196 server_lib = server["lib"]
Roger Meier40cc2322014-06-11 11:09:14 +0200197 if "extra_args" in server:
198 server_extra_args = server["extra_args"]
199 for protocol in server["protocols"]:
200 for transport in server["transports"]:
Roger Meier32f39822014-06-18 22:43:17 +0200201 for sock in server["sockets"]:
202 for client in data["client"]:
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530203 if platform in server["platform"] and platform in client["platform"]:
204 client_executable = client["executable"]
205 client_extra_args = ""
206 client_lib = client["lib"]
207 if "extra_args" in client:
208 client_extra_args = client["extra_args"]
209 if protocol in client["protocols"]:
210 if transport in client["transports"]:
211 if sock in client["sockets"]:
212 if count != 0:
213 results_json.write(",\n")
214 count = 1
215 results_json.write("\t[\n\t\t\"" + server_lib + "\",\n\t\t\"" + client_lib + "\",\n\t\t\"" + protocol + "\",\n\t\t\"" + transport + "-" + sock + "\",\n" )
216 test_name = server_lib + "_" + client_lib + "_" + protocol + "_" + transport + "_" + sock
Roger Meiere8bafb62014-08-01 23:39:32 +0200217 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 +0530218 if ret != None:
219 failed += 1
220 print "Error: %s" % ret
221 print "Using"
222 print (' Server: %s --protocol=%s --transport=%s %s %s'
223 % (server_executable, protocol, transport, getSocketArgs(sock), ' '.join(server_extra_args)))
224 print (' Client: %s --protocol=%s --transport=%s %s %s'
225 % (client_executable, protocol, transport, getSocketArgs(sock), ''.join(client_extra_args)))
Roger Meiere8bafb62014-08-01 23:39:32 +0200226 results_json.write("\t\t\"failure\",\n")
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530227 else:
Roger Meiere8bafb62014-08-01 23:39:32 +0200228 results_json.write("\t\t\"success\",\n")
229 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 +0530230 test_count += 1
231 if protocol == 'binary' and 'accel' in client["protocols"]:
232 if transport in client["transports"]:
233 if sock in client["sockets"]:
234 if count != 0:
235 results_json.write(",\n")
236 count = 1
237 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" )
238 test_name = server_lib + "_" + client_lib + "_accel-binary_" + transport + "_" + sock
Roger Meiere8bafb62014-08-01 23:39:32 +0200239 ret = runServiceTest(test_name, server_lib,server_executable, server_extra_args, client_lib, client_executable, client_extra_args, protocol, 'accel', transport, 9090, 0, sock)
240
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530241 if ret != None:
242 failed += 1
243 print "Error: %s" % ret
244 print "Using"
245 print (' Server: %s --protocol=%s --transport=%s %s %s'
246 % (server_executable, protocol, transport, getSocketArgs(sock), ' '.join(server_extra_args)))
247 print (' Client: %s --protocol=%s --transport=%s %s %s'
248 % (client_executable, protocol, transport , getSocketArgs(sock), ''.join(client_extra_args)))
Roger Meiere8bafb62014-08-01 23:39:32 +0200249 results_json.write("\t\t\"failure\",\n")
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530250 else:
Roger Meiere8bafb62014-08-01 23:39:32 +0200251 results_json.write("\t\t\"success\",\n")
252 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 +0530253 test_count += 1
254 if protocol == 'accel' and 'binary' in client["protocols"]:
255 if transport in client["transports"]:
256 if sock in client["sockets"]:
257 if count != 0:
258 results_json.write(",\n")
259 count = 1
260 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 +0200261 test_name = server_lib + "_" + client_lib + "_binary-accel_" + transport + "_" + sock
262 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 +0530263 if ret != None:
264 failed += 1
265 print "Error: %s" % ret
266 print "Using"
267 print (' Server: %s --protocol=%s --transport=%s %s %s'
268 % (server_executable, protocol, transport + sock, getSocketArgs(sock), ' '.join(server_extra_args)))
269 print (' Client: %s --protocol=%s --transport=%s %s %s'
270 % (client_executable, protocol, transport + sock, getSocketArgs(sock), ''.join(client_extra_args)))
Roger Meiere8bafb62014-08-01 23:39:32 +0200271 results_json.write("\t\t\"failure\",\n")
cdwijayarathna75eb2a32014-07-28 21:54:56 +0530272 else:
Roger Meiere8bafb62014-08-01 23:39:32 +0200273 results_json.write("\t\t\"success\",\n")
274 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 +0530275 test_count += 1
cdwijayarathna3f679782014-07-09 14:00:33 +0530276results_json.write("\n]")
277results_json.flush()
278results_json.close()
279print '%s failed of %s tests in total' % (failed, test_count)