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