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") |
| 44 | parser.set_defaults(verbose=1) |
| 45 | options, args = parser.parse_args() |
| 46 | |
| 47 | def relfile(fname): |
| 48 | return os.path.join(os.path.dirname(__file__), fname) |
| 49 | |
cdwijayarathna | 3f67978 | 2014-07-09 14:00:33 +0530 | [diff] [blame] | 50 | def 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 Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 58 | 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] | 59 | # Build command line arguments |
Roger Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 60 | server_args = [] |
| 61 | cli_args = [] |
| 62 | if server_lib == 'java': |
cdwijayarathna | c53bbd2 | 2014-08-06 01:21:06 +0530 | [diff] [blame] | 63 | server_args.append(server_executable[0]) |
| 64 | server_args.append(server_executable[1]) |
| 65 | server_args.append(relfile(server_executable[2])) |
Roger Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 66 | server_args.extend(['-Dtestargs','\"']) |
| 67 | else: |
| 68 | server_args = [relfile(server_executable)] |
| 69 | if client_lib == 'java': |
cdwijayarathna | c53bbd2 | 2014-08-06 01:21:06 +0530 | [diff] [blame] | 70 | cli_args.append(client_executable[0]) |
| 71 | cli_args.append(client_executable[1]) |
| 72 | cli_args.append(relfile(client_executable[2])) |
Roger Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 73 | cli_args.extend(['-Dtestargs','\"']) |
| 74 | else: |
| 75 | cli_args = [relfile(client_executable)] |
| 76 | |
cdwijayarathna | 3f67978 | 2014-07-09 14:00:33 +0530 | [diff] [blame] | 77 | server_args.append('--protocol=%s' % server_protocol) |
| 78 | cli_args.append('--protocol=%s' % client_protocol) |
| 79 | |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 80 | for which in (server_args, cli_args): |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 81 | which.append('--transport=%s' % transport) |
| 82 | which.append('--port=%d' % port) # default to 9090 |
| 83 | if use_zlib: |
| 84 | which.append('--zlib') |
cdwijayarathna | 3f67978 | 2014-07-09 14:00:33 +0530 | [diff] [blame] | 85 | if socket_type == 'ip-ssl': |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 86 | which.append('--ssl') |
cdwijayarathna | 3f67978 | 2014-07-09 14:00:33 +0530 | [diff] [blame] | 87 | elif socket_type == 'domain': |
| 88 | which.append('--domain-socket=/tmp/ThriftTest.thrift') |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 89 | # if options.verbose == 0: |
| 90 | # which.append('-q') |
| 91 | # if options.verbose == 2: |
| 92 | # which.append('-v') |
Roger Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 93 | if server_lib == 'java': |
| 94 | server_args.append('\"') |
cdwijayarathna | c53bbd2 | 2014-08-06 01:21:06 +0530 | [diff] [blame] | 95 | elif server_lib == 'py': |
| 96 | server_args.append('--genpydir=' + relfile('py/gen-py')) |
Roger Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 97 | if client_lib == 'java': |
| 98 | cli_args.append('\"') |
cdwijayarathna | c53bbd2 | 2014-08-06 01:21:06 +0530 | [diff] [blame] | 99 | elif client_lib == 'py': |
| 100 | cli_args.append('--genpydir=' + relfile('py/gen-py')) |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 101 | |
| 102 | server_args.extend(server_extra_args) |
| 103 | cli_args.extend(client_extra_args) |
Roger Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 104 | |
cdwijayarathna | c53bbd2 | 2014-08-06 01:21:06 +0530 | [diff] [blame] | 105 | server_log=open(relfile("log/" + test_name + "_server.log"),"a") |
| 106 | client_log=open(relfile("log/" + test_name + "_client.log"),"a") |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 107 | |
cdwijayarathna | 11066bd | 2014-07-26 23:02:13 +0530 | [diff] [blame] | 108 | 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) |
cdwijayarathna | 3f67978 | 2014-07-09 14:00:33 +0530 | [diff] [blame] | 116 | |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 117 | def ensureServerAlive(): |
| 118 | if serverproc.poll() is not None: |
cdwijayarathna | 11066bd | 2014-07-26 23:02:13 +0530 | [diff] [blame] | 119 | return 'Server subprocess died, args: %s' % (' '.join(server_args)) |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 120 | |
| 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 |
cdwijayarathna | 3f67978 | 2014-07-09 14:00:33 +0530 | [diff] [blame] | 127 | |
| 128 | if socket_type != 'domain': |
| 129 | while sock.connect_ex(('127.0.0.1', port)) != 0: |
| 130 | attempt += 1 |
| 131 | if attempt >= max_attempts: |
cdwijayarathna | 11066bd | 2014-07-26 23:02:13 +0530 | [diff] [blame] | 132 | 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] | 133 | ensureServerAlive() |
| 134 | time.sleep(sleep_time) |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 135 | finally: |
| 136 | sock.close() |
| 137 | |
| 138 | try: |
Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 139 | o = [] |
| 140 | def target(): |
cdwijayarathna | 11066bd | 2014-07-26 23:02:13 +0530 | [diff] [blame] | 141 | 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 Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 155 | 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() |
cdwijayarathna | 11066bd | 2014-07-26 23:02:13 +0530 | [diff] [blame] | 163 | if(len(o)==0): |
| 164 | return "Client subprocess failed, args: %s" % (' '.join(cli_args)) |
Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 165 | ret = o[0].returncode |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 166 | 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 Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 171 | #ensureServerAlive() |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 172 | 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) |
cdwijayarathna | c53bbd2 | 2014-08-06 01:21:06 +0530 | [diff] [blame] | 178 | os.kill(serverproc.pid, signal.SIGTERM) |
| 179 | #serverproc.wait() |
Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 180 | client_log.flush() |
| 181 | server_log.flush() |
| 182 | client_log.close() |
| 183 | server_log.close() |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 184 | |
| 185 | test_count = 0 |
| 186 | failed = 0 |
cdwijayarathna | c53bbd2 | 2014-08-06 01:21:06 +0530 | [diff] [blame] | 187 | hard_fail_count = 0 |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 188 | platform = platform.system() |
cdwijayarathna | c53bbd2 | 2014-08-06 01:21:06 +0530 | [diff] [blame] | 189 | if os.path.exists(relfile('log')): shutil.rmtree(relfile('log')) |
| 190 | os.makedirs(relfile('log')) |
| 191 | if os.path.exists(relfile('results.json')): os.remove(relfile('results.json')) |
| 192 | results_json = open(relfile("results.json"),"a") |
cdwijayarathna | 3f67978 | 2014-07-09 14:00:33 +0530 | [diff] [blame] | 193 | results_json.write("[\n") |
Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 194 | |
cdwijayarathna | c53bbd2 | 2014-08-06 01:21:06 +0530 | [diff] [blame] | 195 | with open(relfile('tests.json')) as data_file: |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 196 | data = json.load(data_file) |
| 197 | |
Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 198 | #subprocess.call("export NODE_PATH=../lib/nodejs/test:../lib/nodejs/lib:${NODE_PATH}") |
cdwijayarathna | 3f67978 | 2014-07-09 14:00:33 +0530 | [diff] [blame] | 199 | count = 0 |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 200 | for server in data["server"]: |
| 201 | server_executable = server["executable"] |
| 202 | server_extra_args = "" |
Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 203 | server_lib = server["lib"] |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 204 | 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 Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 208 | for sock in server["sockets"]: |
| 209 | for client in data["client"]: |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 210 | 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 Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 224 | ret = runServiceTest(test_name, server_lib, server_executable, server_extra_args, client_lib, client_executable, client_extra_args, protocol, protocol, transport, 9090, 0, sock) |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 225 | if ret != None: |
| 226 | failed += 1 |
cdwijayarathna | c53bbd2 | 2014-08-06 01:21:06 +0530 | [diff] [blame] | 227 | if client["exit"] == "hard" and server["exit"] == "hard": |
| 228 | hard_fail_count +=1 |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 229 | 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 Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 235 | results_json.write("\t\t\"failure\",\n") |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 236 | else: |
Roger Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 237 | 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]") |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 239 | 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 Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 248 | 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 | |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 250 | if ret != None: |
| 251 | failed += 1 |
cdwijayarathna | c53bbd2 | 2014-08-06 01:21:06 +0530 | [diff] [blame] | 252 | if client["exit"] == "hard" and server["exit"] == "hard": |
| 253 | hard_fail_count +=1 |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 254 | 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 Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 260 | results_json.write("\t\t\"failure\",\n") |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 261 | else: |
Roger Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 262 | 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]") |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 264 | 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 Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 272 | 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) |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 274 | if ret != None: |
| 275 | failed += 1 |
cdwijayarathna | c53bbd2 | 2014-08-06 01:21:06 +0530 | [diff] [blame] | 276 | if client["exit"] == "hard" and server["exit"] == "hard": |
| 277 | hard_fail_count +=1 |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 278 | 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 Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 284 | results_json.write("\t\t\"failure\",\n") |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 285 | else: |
Roger Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 286 | 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]") |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 288 | test_count += 1 |
cdwijayarathna | 3f67978 | 2014-07-09 14:00:33 +0530 | [diff] [blame] | 289 | results_json.write("\n]") |
| 290 | results_json.flush() |
| 291 | results_json.close() |
cdwijayarathna | c53bbd2 | 2014-08-06 01:21:06 +0530 | [diff] [blame] | 292 | print '%s failed of %s tests in total' % (failed, test_count) |
| 293 | sys.exit(hard_fail_count) |