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 | 0d4072b | 2014-08-09 21:32:21 +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 | 0d4072b | 2014-08-09 21:32:21 +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('\"') |
| 95 | if client_lib == 'java': |
| 96 | cli_args.append('\"') |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 97 | |
| 98 | server_args.extend(server_extra_args) |
| 99 | cli_args.extend(client_extra_args) |
Roger Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 100 | |
cdwijayarathna | 0d4072b | 2014-08-09 21:32:21 +0530 | [diff] [blame^] | 101 | server_log=open(relfile("log/" + test_name + "_server.log"),"a") |
| 102 | client_log=open(relfile("log/" + test_name + "_client.log"),"a") |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 103 | |
cdwijayarathna | 11066bd | 2014-07-26 23:02:13 +0530 | [diff] [blame] | 104 | 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) |
cdwijayarathna | 3f67978 | 2014-07-09 14:00:33 +0530 | [diff] [blame] | 112 | |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 113 | def ensureServerAlive(): |
| 114 | if serverproc.poll() is not None: |
cdwijayarathna | 11066bd | 2014-07-26 23:02:13 +0530 | [diff] [blame] | 115 | return 'Server subprocess died, args: %s' % (' '.join(server_args)) |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 116 | |
| 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 |
cdwijayarathna | 3f67978 | 2014-07-09 14:00:33 +0530 | [diff] [blame] | 123 | |
| 124 | if socket_type != 'domain': |
| 125 | while sock.connect_ex(('127.0.0.1', port)) != 0: |
| 126 | attempt += 1 |
| 127 | if attempt >= max_attempts: |
cdwijayarathna | 11066bd | 2014-07-26 23:02:13 +0530 | [diff] [blame] | 128 | 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] | 129 | ensureServerAlive() |
| 130 | time.sleep(sleep_time) |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 131 | finally: |
| 132 | sock.close() |
| 133 | |
| 134 | try: |
Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 135 | o = [] |
| 136 | def target(): |
cdwijayarathna | 11066bd | 2014-07-26 23:02:13 +0530 | [diff] [blame] | 137 | 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 Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 151 | 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() |
cdwijayarathna | 11066bd | 2014-07-26 23:02:13 +0530 | [diff] [blame] | 159 | if(len(o)==0): |
| 160 | return "Client subprocess failed, args: %s" % (' '.join(cli_args)) |
Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 161 | ret = o[0].returncode |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 162 | 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 Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 167 | #ensureServerAlive() |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 168 | 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) |
cdwijayarathna | 0d4072b | 2014-08-09 21:32:21 +0530 | [diff] [blame^] | 174 | os.kill(serverproc.pid, signal.SIGTERM) |
| 175 | #serverproc.wait() |
Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 176 | client_log.flush() |
| 177 | server_log.flush() |
| 178 | client_log.close() |
| 179 | server_log.close() |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 180 | |
| 181 | test_count = 0 |
| 182 | failed = 0 |
cdwijayarathna | 0d4072b | 2014-08-09 21:32:21 +0530 | [diff] [blame^] | 183 | hard_fail_count = 0 |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 184 | platform = platform.system() |
cdwijayarathna | 0d4072b | 2014-08-09 21:32:21 +0530 | [diff] [blame^] | 185 | if os.path.exists(relfile('log')): shutil.rmtree(relfile('log')) |
| 186 | os.makedirs(relfile('log')) |
| 187 | if os.path.exists(relfile('results.json')): os.remove(relfile('results.json')) |
| 188 | results_json = open(relfile("results.json"),"a") |
cdwijayarathna | 3f67978 | 2014-07-09 14:00:33 +0530 | [diff] [blame] | 189 | results_json.write("[\n") |
Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 190 | |
cdwijayarathna | 0d4072b | 2014-08-09 21:32:21 +0530 | [diff] [blame^] | 191 | with open(relfile('tests.json')) as data_file: |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 192 | data = json.load(data_file) |
| 193 | |
Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 194 | #subprocess.call("export NODE_PATH=../lib/nodejs/test:../lib/nodejs/lib:${NODE_PATH}") |
cdwijayarathna | 3f67978 | 2014-07-09 14:00:33 +0530 | [diff] [blame] | 195 | count = 0 |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 196 | for server in data["server"]: |
| 197 | server_executable = server["executable"] |
| 198 | server_extra_args = "" |
Roger Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 199 | server_lib = server["lib"] |
Roger Meier | 40cc232 | 2014-06-11 11:09:14 +0200 | [diff] [blame] | 200 | 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 Meier | 32f3982 | 2014-06-18 22:43:17 +0200 | [diff] [blame] | 204 | for sock in server["sockets"]: |
| 205 | for client in data["client"]: |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 206 | 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 Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 220 | 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] | 221 | if ret != None: |
| 222 | failed += 1 |
cdwijayarathna | 0d4072b | 2014-08-09 21:32:21 +0530 | [diff] [blame^] | 223 | if client["exit"] == "hard" and server["exit"] == "hard": |
| 224 | hard_fail_count +=1 |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 225 | 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 Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 231 | results_json.write("\t\t\"failure\",\n") |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 232 | else: |
Roger Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 233 | 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]") |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 235 | 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 Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 244 | 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 | |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 246 | if ret != None: |
| 247 | failed += 1 |
cdwijayarathna | 0d4072b | 2014-08-09 21:32:21 +0530 | [diff] [blame^] | 248 | if client["exit"] == "hard" and server["exit"] == "hard": |
| 249 | hard_fail_count +=1 |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 250 | 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 Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 256 | results_json.write("\t\t\"failure\",\n") |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 257 | else: |
Roger Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 258 | 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]") |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 260 | 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 Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 268 | 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) |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 270 | if ret != None: |
| 271 | failed += 1 |
cdwijayarathna | 0d4072b | 2014-08-09 21:32:21 +0530 | [diff] [blame^] | 272 | if client["exit"] == "hard" and server["exit"] == "hard": |
| 273 | hard_fail_count +=1 |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 274 | 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 Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 280 | results_json.write("\t\t\"failure\",\n") |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 281 | else: |
Roger Meier | e8bafb6 | 2014-08-01 23:39:32 +0200 | [diff] [blame] | 282 | 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]") |
cdwijayarathna | 75eb2a3 | 2014-07-28 21:54:56 +0530 | [diff] [blame] | 284 | test_count += 1 |
cdwijayarathna | 3f67978 | 2014-07-09 14:00:33 +0530 | [diff] [blame] | 285 | results_json.write("\n]") |
| 286 | results_json.flush() |
| 287 | results_json.close() |
cdwijayarathna | 0d4072b | 2014-08-09 21:32:21 +0530 | [diff] [blame^] | 288 | print '%s failed of %s tests in total' % (failed, test_count) |
| 289 | sys.exit(hard_fail_count) |