Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 1 | #!/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 | |
| 22 | from __future__ import division |
| 23 | import time |
| 24 | import socket |
| 25 | import subprocess |
| 26 | import sys |
| 27 | import os |
| 28 | import signal |
| 29 | import json |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 30 | import platform |
Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 31 | import shutil |
| 32 | import threading |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 33 | from optparse import OptionParser |
| 34 | |
| 35 | parser = OptionParser() |
| 36 | parser.add_option("--port", type="int", dest="port", default=9090, |
| 37 | help="port number for server to listen on") |
| 38 | parser.add_option('-v', '--verbose', action="store_const", |
| 39 | dest="verbose", const=2, |
| 40 | help="verbose output") |
| 41 | parser.add_option('-q', '--quiet', action="store_const", |
| 42 | dest="verbose", const=0, |
| 43 | help="minimal output") |
cdwijayarathna | d104165 | 2014-08-15 23:42:20 +0530 | [diff] [blame] | 44 | parser.add_option("--server", type="string", dest="servers", default="", |
| 45 | help="list of servers to test seperated by commas, eg:- --server=cpp,java") |
| 46 | parser.add_option("--client", type="string", dest="clients", default="", |
| 47 | help="list of clients to test seperated by commas, eg:- --client=cpp,java") |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 48 | parser.set_defaults(verbose=1) |
| 49 | options, args = parser.parse_args() |
| 50 | |
cdwijayarathna | d104165 | 2014-08-15 23:42:20 +0530 | [diff] [blame] | 51 | if options.servers == "": |
| 52 | serversList = [] |
| 53 | else: |
| 54 | serversList = options.servers.split(",") |
| 55 | if options.clients == "": |
| 56 | clientsList = [] |
| 57 | else: |
| 58 | clientsList = options.clients.split(",") |
| 59 | |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 60 | def relfile(fname): |
| 61 | return os.path.join(os.path.dirname(__file__), fname) |
| 62 | |
cdwijayarathna | 3f67978 | 2014-07-09 14:00:33 +0530 | [diff] [blame] | 63 | def getSocketArgs(socket_type): |
| 64 | if socket_type == 'ip': |
| 65 | return "" |
| 66 | elif socket_type == 'ip-ssl': |
| 67 | return "--ssl" |
| 68 | elif socket_type == 'domain': |
| 69 | return "--domain-socket=/tmp/ThriftTest.thrift" |
| 70 | |
Roger Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 71 | def 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 Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 72 | # Build command line arguments |
Roger Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 73 | server_args = [] |
| 74 | cli_args = [] |
| 75 | if server_lib == 'java': |
cdwijayarathna | 0d4072b | 2014-08-09 21:32:21 +0530 | [diff] [blame] | 76 | server_args.append(server_executable[0]) |
| 77 | server_args.append(server_executable[1]) |
| 78 | server_args.append(relfile(server_executable[2])) |
Roger Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 79 | server_args.extend(['-Dtestargs','\"']) |
| 80 | else: |
| 81 | server_args = [relfile(server_executable)] |
| 82 | if client_lib == 'java': |
cdwijayarathna | 0d4072b | 2014-08-09 21:32:21 +0530 | [diff] [blame] | 83 | cli_args.append(client_executable[0]) |
| 84 | cli_args.append(client_executable[1]) |
| 85 | cli_args.append(relfile(client_executable[2])) |
Roger Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 86 | cli_args.extend(['-Dtestargs','\"']) |
| 87 | else: |
| 88 | cli_args = [relfile(client_executable)] |
| 89 | |
cdwijayarathna | 3f67978 | 2014-07-09 14:00:33 +0530 | [diff] [blame] | 90 | server_args.append('--protocol=%s' % server_protocol) |
| 91 | cli_args.append('--protocol=%s' % client_protocol) |
| 92 | |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 93 | for which in (server_args, cli_args): |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 94 | which.append('--transport=%s' % transport) |
| 95 | which.append('--port=%d' % port) # default to 9090 |
| 96 | if use_zlib: |
| 97 | which.append('--zlib') |
cdwijayarathna | 3f67978 | 2014-07-09 14:00:33 +0530 | [diff] [blame] | 98 | if socket_type == 'ip-ssl': |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 99 | which.append('--ssl') |
cdwijayarathna | 3f67978 | 2014-07-09 14:00:33 +0530 | [diff] [blame] | 100 | elif socket_type == 'domain': |
| 101 | which.append('--domain-socket=/tmp/ThriftTest.thrift') |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 102 | # if options.verbose == 0: |
| 103 | # which.append('-q') |
| 104 | # if options.verbose == 2: |
| 105 | # which.append('-v') |
Roger Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 106 | if server_lib == 'java': |
| 107 | server_args.append('\"') |
| 108 | if client_lib == 'java': |
| 109 | cli_args.append('\"') |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 110 | |
| 111 | server_args.extend(server_extra_args) |
| 112 | cli_args.extend(client_extra_args) |
Roger Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 113 | |
cdwijayarathna | 0d4072b | 2014-08-09 21:32:21 +0530 | [diff] [blame] | 114 | server_log=open(relfile("log/" + test_name + "_server.log"),"a") |
| 115 | client_log=open(relfile("log/" + test_name + "_client.log"),"a") |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 116 | |
cdwijayarathna | 11066bd | 2014-07-26 23:02:13 +0530 | [diff] [blame] | 117 | try: |
| 118 | if options.verbose > 0: |
| 119 | print 'Testing server: %s' % (' '.join(server_args)) |
| 120 | serverproc = subprocess.Popen(server_args, stdout=server_log, stderr=server_log) |
| 121 | else: |
| 122 | serverproc = subprocess.Popen(server_args, stdout=server_log, stderr=server_log) |
| 123 | except OSError as e: |
| 124 | return "OS error({0}): {1}".format(e.errno, e.strerror) |
cdwijayarathna | 3f67978 | 2014-07-09 14:00:33 +0530 | [diff] [blame] | 125 | |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 126 | def ensureServerAlive(): |
| 127 | if serverproc.poll() is not None: |
cdwijayarathna | 11066bd | 2014-07-26 23:02:13 +0530 | [diff] [blame] | 128 | return 'Server subprocess died, args: %s' % (' '.join(server_args)) |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 129 | |
| 130 | # Wait for the server to start accepting connections on the given port. |
| 131 | sock = socket.socket() |
| 132 | sleep_time = 0.1 # Seconds |
| 133 | max_attempts = 100 |
| 134 | try: |
| 135 | attempt = 0 |
cdwijayarathna | 3f67978 | 2014-07-09 14:00:33 +0530 | [diff] [blame] | 136 | |
| 137 | if socket_type != 'domain': |
| 138 | while sock.connect_ex(('127.0.0.1', port)) != 0: |
| 139 | attempt += 1 |
| 140 | if attempt >= max_attempts: |
cdwijayarathna | 11066bd | 2014-07-26 23:02:13 +0530 | [diff] [blame] | 141 | return "TestServer not ready on port %d after %.2f seconds" % (port, sleep_time * attempt) |
cdwijayarathna | 3f67978 | 2014-07-09 14:00:33 +0530 | [diff] [blame] | 142 | ensureServerAlive() |
| 143 | time.sleep(sleep_time) |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 144 | finally: |
| 145 | sock.close() |
| 146 | |
| 147 | try: |
Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 148 | o = [] |
| 149 | def target(): |
cdwijayarathna | 11066bd | 2014-07-26 23:02:13 +0530 | [diff] [blame] | 150 | try: |
| 151 | if options.verbose > 0: |
| 152 | print 'Testing client: %s' % (' '.join(cli_args)) |
| 153 | process = subprocess.Popen(cli_args, stdout=client_log, stderr=client_log) |
| 154 | o.append(process) |
| 155 | process.communicate() |
| 156 | else: |
| 157 | process = subprocess.Popen(cli_args, stdout=client_log, stderr=client_log) |
| 158 | o.append(process) |
| 159 | process.communicate() |
| 160 | except OSError as e: |
| 161 | return "OS error({0}): {1}".format(e.errno, e.strerror) |
| 162 | except: |
| 163 | return "Unexpected error:", sys.exc_info()[0] |
Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 164 | thread = threading.Thread(target=target) |
| 165 | thread.start() |
| 166 | |
| 167 | thread.join(10) |
| 168 | if thread.is_alive(): |
| 169 | print 'Terminating process' |
| 170 | o[0].terminate() |
| 171 | thread.join() |
cdwijayarathna | 11066bd | 2014-07-26 23:02:13 +0530 | [diff] [blame] | 172 | if(len(o)==0): |
| 173 | return "Client subprocess failed, args: %s" % (' '.join(cli_args)) |
Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 174 | ret = o[0].returncode |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 175 | if ret != 0: |
| 176 | return "Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args)) |
| 177 | #raise Exception("Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args))) |
| 178 | finally: |
| 179 | # check that server didn't die |
Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 180 | #ensureServerAlive() |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 181 | extra_sleep = 0 |
| 182 | if extra_sleep > 0 and options.verbose > 0: |
| 183 | print ('Giving (protocol=%s,zlib=%s,ssl=%s) an extra %d seconds for child' |
| 184 | 'processes to terminate via alarm' |
| 185 | % (protocol, use_zlib, use_ssl, extra_sleep)) |
| 186 | time.sleep(extra_sleep) |
cdwijayarathna | 0d4072b | 2014-08-09 21:32:21 +0530 | [diff] [blame] | 187 | os.kill(serverproc.pid, signal.SIGTERM) |
| 188 | #serverproc.wait() |
Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 189 | client_log.flush() |
| 190 | server_log.flush() |
| 191 | client_log.close() |
| 192 | server_log.close() |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 193 | |
| 194 | test_count = 0 |
| 195 | failed = 0 |
cdwijayarathna | 0d4072b | 2014-08-09 21:32:21 +0530 | [diff] [blame] | 196 | hard_fail_count = 0 |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 197 | platform = platform.system() |
cdwijayarathna | 0d4072b | 2014-08-09 21:32:21 +0530 | [diff] [blame] | 198 | if os.path.exists(relfile('log')): shutil.rmtree(relfile('log')) |
| 199 | os.makedirs(relfile('log')) |
| 200 | if os.path.exists(relfile('results.json')): os.remove(relfile('results.json')) |
| 201 | results_json = open(relfile("results.json"),"a") |
cdwijayarathna | 3f67978 | 2014-07-09 14:00:33 +0530 | [diff] [blame] | 202 | results_json.write("[\n") |
Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 203 | |
cdwijayarathna | 0d4072b | 2014-08-09 21:32:21 +0530 | [diff] [blame] | 204 | with open(relfile('tests.json')) as data_file: |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 205 | data = json.load(data_file) |
| 206 | |
Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 207 | #subprocess.call("export NODE_PATH=../lib/nodejs/test:../lib/nodejs/lib:${NODE_PATH}") |
cdwijayarathna | 3f67978 | 2014-07-09 14:00:33 +0530 | [diff] [blame] | 208 | count = 0 |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 209 | for server in data["server"]: |
cdwijayarathna | d104165 | 2014-08-15 23:42:20 +0530 | [diff] [blame] | 210 | if (server["lib"] in serversList or len(serversList) == 0) and platform in server["platform"]: |
| 211 | server_executable = server["executable"] |
| 212 | server_extra_args = "" |
| 213 | server_lib = server["lib"] |
| 214 | if "extra_args" in server: |
| 215 | server_extra_args = server["extra_args"] |
| 216 | for protocol in server["protocols"]: |
| 217 | for transport in server["transports"]: |
| 218 | for sock in server["sockets"]: |
| 219 | for client in data["client"]: |
| 220 | if (client["lib"] in clientsList or len(clientsList) == 0) and platform in client["platform"]: |
| 221 | client_executable = client["executable"] |
| 222 | client_extra_args = "" |
| 223 | client_lib = client["lib"] |
| 224 | if "extra_args" in client: |
| 225 | client_extra_args = client["extra_args"] |
| 226 | if protocol in client["protocols"]: |
| 227 | if transport in client["transports"]: |
| 228 | if sock in client["sockets"]: |
| 229 | if count != 0: |
| 230 | results_json.write(",\n") |
| 231 | count = 1 |
| 232 | results_json.write("\t[\n\t\t\"" + server_lib + "\",\n\t\t\"" + client_lib + "\",\n\t\t\"" + protocol + "\",\n\t\t\"" + transport + "-" + sock + "\",\n" ) |
| 233 | test_name = server_lib + "_" + client_lib + "_" + protocol + "_" + transport + "_" + sock |
cdwijayarathna | 9ea3d2e | 2014-08-16 17:28:40 +0530 | [diff] [blame] | 234 | ret = runServiceTest(test_name, server_lib, server_executable, server_extra_args, client_lib, client_executable, client_extra_args, protocol, protocol, transport, options.port, 0, sock) |
cdwijayarathna | d104165 | 2014-08-15 23:42:20 +0530 | [diff] [blame] | 235 | if ret != None: |
| 236 | failed += 1 |
| 237 | if client["exit"] == "hard" and server["exit"] == "hard": |
| 238 | hard_fail_count +=1 |
| 239 | print "Error: %s" % ret |
| 240 | print "Using" |
| 241 | print (' Server: %s --protocol=%s --transport=%s %s %s' |
| 242 | % (server_executable, protocol, transport, getSocketArgs(sock), ' '.join(server_extra_args))) |
| 243 | print (' Client: %s --protocol=%s --transport=%s %s %s' |
| 244 | % (client_executable, protocol, transport, getSocketArgs(sock), ''.join(client_extra_args))) |
| 245 | results_json.write("\t\t\"failure\",\n") |
| 246 | else: |
| 247 | results_json.write("\t\t\"success\",\n") |
| 248 | 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]") |
| 249 | test_count += 1 |
| 250 | if protocol == 'binary' and 'accel' in client["protocols"]: |
| 251 | if transport in client["transports"]: |
| 252 | if sock in client["sockets"]: |
| 253 | if count != 0: |
| 254 | results_json.write(",\n") |
| 255 | count = 1 |
| 256 | 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" ) |
| 257 | test_name = server_lib + "_" + client_lib + "_accel-binary_" + transport + "_" + sock |
cdwijayarathna | 9ea3d2e | 2014-08-16 17:28:40 +0530 | [diff] [blame] | 258 | ret = runServiceTest(test_name, server_lib,server_executable, server_extra_args, client_lib, client_executable, client_extra_args, protocol, 'accel', transport, options.port, 0, sock) |
Roger Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 259 | |
cdwijayarathna | d104165 | 2014-08-15 23:42:20 +0530 | [diff] [blame] | 260 | if ret != None: |
| 261 | failed += 1 |
| 262 | if client["exit"] == "hard" and server["exit"] == "hard": |
| 263 | hard_fail_count +=1 |
| 264 | print "Error: %s" % ret |
| 265 | print "Using" |
| 266 | print (' Server: %s --protocol=%s --transport=%s %s %s' |
| 267 | % (server_executable, protocol, transport, getSocketArgs(sock), ' '.join(server_extra_args))) |
| 268 | print (' Client: %s --protocol=%s --transport=%s %s %s' |
| 269 | % (client_executable, protocol, transport , getSocketArgs(sock), ''.join(client_extra_args))) |
| 270 | results_json.write("\t\t\"failure\",\n") |
| 271 | else: |
| 272 | results_json.write("\t\t\"success\",\n") |
| 273 | 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]") |
| 274 | test_count += 1 |
| 275 | if protocol == 'accel' and 'binary' in client["protocols"]: |
| 276 | if transport in client["transports"]: |
| 277 | if sock in client["sockets"]: |
| 278 | if count != 0: |
| 279 | results_json.write(",\n") |
| 280 | count = 1 |
| 281 | 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" ) |
| 282 | test_name = server_lib + "_" + client_lib + "_binary-accel_" + transport + "_" + sock |
cdwijayarathna | 9ea3d2e | 2014-08-16 17:28:40 +0530 | [diff] [blame] | 283 | ret = runServiceTest(test_name, server_lib,server_executable, server_extra_args, client_lib, client_executable, client_extra_args, protocol, 'binary', transport, options.port, 0, sock) |
cdwijayarathna | d104165 | 2014-08-15 23:42:20 +0530 | [diff] [blame] | 284 | if ret != None: |
| 285 | failed += 1 |
| 286 | if client["exit"] == "hard" and server["exit"] == "hard": |
| 287 | hard_fail_count +=1 |
| 288 | print "Error: %s" % ret |
| 289 | print "Using" |
| 290 | print (' Server: %s --protocol=%s --transport=%s %s %s' |
| 291 | % (server_executable, protocol, transport + sock, getSocketArgs(sock), ' '.join(server_extra_args))) |
| 292 | print (' Client: %s --protocol=%s --transport=%s %s %s' |
| 293 | % (client_executable, protocol, transport + sock, getSocketArgs(sock), ''.join(client_extra_args))) |
| 294 | results_json.write("\t\t\"failure\",\n") |
| 295 | else: |
| 296 | results_json.write("\t\t\"success\",\n") |
| 297 | 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]") |
| 298 | test_count += 1 |
cdwijayarathna | 3f67978 | 2014-07-09 14:00:33 +0530 | [diff] [blame] | 299 | results_json.write("\n]") |
| 300 | results_json.flush() |
| 301 | results_json.close() |
cdwijayarathna | 0d4072b | 2014-08-09 21:32:21 +0530 | [diff] [blame] | 302 | print '%s failed of %s tests in total' % (failed, test_count) |
| 303 | sys.exit(hard_fail_count) |