|  | #!/usr/bin/env python | 
|  |  | 
|  | # | 
|  | # Licensed to the Apache Software Foundation (ASF) under one | 
|  | # or more contributor license agreements. See the NOTICE file | 
|  | # distributed with this work for additional information | 
|  | # regarding copyright ownership. The ASF licenses this file | 
|  | # to you under the Apache License, Version 2.0 (the | 
|  | # "License"); you may not use this file except in compliance | 
|  | # with the License. You may obtain a copy of the License at | 
|  | # | 
|  | #   http://www.apache.org/licenses/LICENSE-2.0 | 
|  | # | 
|  | # Unless required by applicable law or agreed to in writing, | 
|  | # software distributed under the License is distributed on an | 
|  | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | 
|  | # KIND, either express or implied. See the License for the | 
|  | # specific language governing permissions and limitations | 
|  | # under the License. | 
|  | # | 
|  |  | 
|  | from __future__ import division | 
|  | import time | 
|  | import socket | 
|  | import subprocess | 
|  | import sys | 
|  | import os | 
|  | import signal | 
|  | import json | 
|  | import shutil | 
|  | import threading | 
|  | from optparse import OptionParser | 
|  |  | 
|  | parser = OptionParser() | 
|  | parser.add_option("--port", type="int", dest="port", default=9090, | 
|  | help="port number for server to listen on") | 
|  | parser.add_option('-v', '--verbose', action="store_const", | 
|  | dest="verbose", const=2, | 
|  | help="verbose output") | 
|  | parser.add_option('-q', '--quiet', action="store_const", | 
|  | dest="verbose", const=0, | 
|  | help="minimal output") | 
|  | parser.set_defaults(verbose=1) | 
|  | options, args = parser.parse_args() | 
|  |  | 
|  | def relfile(fname): | 
|  | return os.path.join(os.path.dirname(__file__), fname) | 
|  |  | 
|  | def getSocketArgs(socket_type): | 
|  | if socket_type == 'ip': | 
|  | return "" | 
|  | elif socket_type == 'ip-ssl': | 
|  | return "--ssl" | 
|  | elif socket_type == 'domain': | 
|  | return "--domain-socket=/tmp/ThriftTest.thrift" | 
|  |  | 
|  | def runServiceTest(test_name, server_executable, server_extra_args, client_executable, client_extra_args, server_protocol, client_protocol, transport, port, use_zlib, socket_type): | 
|  | # Build command line arguments | 
|  | server_args = [relfile(server_executable)] | 
|  | cli_args = [relfile(client_executable)] | 
|  | server_args.append('--protocol=%s' % server_protocol) | 
|  | cli_args.append('--protocol=%s' % client_protocol) | 
|  |  | 
|  | for which in (server_args, cli_args): | 
|  | which.append('--transport=%s' % transport) | 
|  | which.append('--port=%d' % port) # default to 9090 | 
|  | if use_zlib: | 
|  | which.append('--zlib') | 
|  | if socket_type == 'ip-ssl': | 
|  | which.append('--ssl') | 
|  | elif socket_type == 'domain': | 
|  | which.append('--domain-socket=/tmp/ThriftTest.thrift') | 
|  | #    if options.verbose == 0: | 
|  | #      which.append('-q') | 
|  | #    if options.verbose == 2: | 
|  | #      which.append('-v') | 
|  |  | 
|  | server_args.extend(server_extra_args) | 
|  | cli_args.extend(client_extra_args) | 
|  | server_log=open("log/" + test_name + "_server.log","a") | 
|  | client_log=open("log/" + test_name + "_client.log","a") | 
|  |  | 
|  | if options.verbose > 0: | 
|  | print 'Testing server: %s' % (' '.join(server_args)) | 
|  | serverproc = subprocess.Popen(server_args, stdout=server_log, stderr=server_log) | 
|  | else: | 
|  | serverproc = subprocess.Popen(server_args, stdout=server_log, stderr=server_log) | 
|  |  | 
|  | def ensureServerAlive(): | 
|  | if serverproc.poll() is not None: | 
|  | print ('FAIL: Server process (%s) failed with retcode %d' | 
|  | % (' '.join(server_args), serverproc.returncode)) | 
|  | raise Exception('Server subprocess died, args: %s' | 
|  | % (' '.join(server_args))) | 
|  |  | 
|  | # Wait for the server to start accepting connections on the given port. | 
|  | sock = socket.socket() | 
|  | sleep_time = 0.1  # Seconds | 
|  | max_attempts = 100 | 
|  | try: | 
|  | attempt = 0 | 
|  |  | 
|  | if socket_type != 'domain': | 
|  | while sock.connect_ex(('127.0.0.1', port)) != 0: | 
|  | attempt += 1 | 
|  | if attempt >= max_attempts: | 
|  | raise Exception("TestServer not ready on port %d after %.2f seconds" | 
|  | % (port, sleep_time * attempt)) | 
|  | ensureServerAlive() | 
|  | time.sleep(sleep_time) | 
|  | finally: | 
|  | sock.close() | 
|  |  | 
|  | try: | 
|  | o = [] | 
|  | def target(): | 
|  | if options.verbose > 0: | 
|  | print 'Testing client: %s' % (' '.join(cli_args)) | 
|  | process = subprocess.Popen(cli_args, stdout=client_log, stderr=client_log) | 
|  | o.append(process) | 
|  | process.communicate() | 
|  | else: | 
|  | process = subprocess.Popen(cli_args, stdout=client_log, stderr=client_log) | 
|  | o.append(process) | 
|  | process.communicate() | 
|  | thread = threading.Thread(target=target) | 
|  | thread.start() | 
|  |  | 
|  | thread.join(10) | 
|  | if thread.is_alive(): | 
|  | print 'Terminating process' | 
|  | o[0].terminate() | 
|  | thread.join() | 
|  | ret = o[0].returncode | 
|  | if ret != 0: | 
|  | return "Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args)) | 
|  | #raise Exception("Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args))) | 
|  | finally: | 
|  | # check that server didn't die | 
|  | #ensureServerAlive() | 
|  | extra_sleep = 0 | 
|  | if extra_sleep > 0 and options.verbose > 0: | 
|  | print ('Giving (protocol=%s,zlib=%s,ssl=%s) an extra %d seconds for child' | 
|  | 'processes to terminate via alarm' | 
|  | % (protocol, use_zlib, use_ssl, extra_sleep)) | 
|  | time.sleep(extra_sleep) | 
|  | os.kill(serverproc.pid, signal.SIGKILL) | 
|  | serverproc.wait() | 
|  | client_log.flush() | 
|  | server_log.flush() | 
|  | client_log.close() | 
|  | server_log.close() | 
|  |  | 
|  | test_count = 0 | 
|  | failed = 0 | 
|  |  | 
|  | if os.path.exists('log'): shutil.rmtree('log') | 
|  | os.makedirs('log') | 
|  | if os.path.exists('results.json'): os.remove('results.json') | 
|  | results_json = open("results.json","a") | 
|  | results_json.write("[\n") | 
|  |  | 
|  | with open('tests.json') as data_file: | 
|  | data = json.load(data_file) | 
|  |  | 
|  | #subprocess.call("export NODE_PATH=../lib/nodejs/test:../lib/nodejs/lib:${NODE_PATH}") | 
|  | count = 0 | 
|  | for server in data["server"]: | 
|  | server_executable = server["executable"] | 
|  | server_extra_args = "" | 
|  | server_lib = server["lib"] | 
|  | if "extra_args" in server: | 
|  | server_extra_args = server["extra_args"] | 
|  | for protocol in server["protocols"]: | 
|  | for transport in server["transports"]: | 
|  | for sock in server["sockets"]: | 
|  | for client in data["client"]: | 
|  | client_executable = client["executable"] | 
|  | client_extra_args = "" | 
|  | client_lib = client["lib"] | 
|  | if "extra_args" in client: | 
|  | client_extra_args = client["extra_args"] | 
|  | if protocol in client["protocols"]: | 
|  | if transport in client["transports"]: | 
|  | if sock in client["sockets"]: | 
|  | if count != 0: | 
|  | results_json.write(",\n") | 
|  | count = 1 | 
|  | results_json.write("\t[\n\t\t\"" + server_lib + "\",\n\t\t\"" + client_lib + "\",\n\t\t\"" + protocol + "\",\n\t\t\"" + transport + "-" + sock + "\",\n" ) | 
|  | test_name = server_lib + "_" + client_lib + "_" + protocol + "_" + transport + "_" + sock | 
|  | ret = runServiceTest(test_name, server_executable, server_extra_args, client_executable, client_extra_args, protocol, protocol, transport, 9090, 0, sock) | 
|  | if ret != None: | 
|  | failed += 1 | 
|  | print "Error: %s" % ret | 
|  | print "Using" | 
|  | print (' Server: %s --protocol=%s --transport=%s %s %s' | 
|  | % (server_executable, protocol, transport, getSocketArgs(sock), ' '.join(server_extra_args))) | 
|  | print (' Client: %s --protocol=%s --transport=%s %s %s' | 
|  | % (client_executable, protocol, transport, getSocketArgs(sock), ''.join(client_extra_args))) | 
|  | 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") | 
|  | else: | 
|  | 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") | 
|  | results_json.write("\t]") | 
|  | test_count += 1 | 
|  | if protocol == 'binary' and 'accel' in client["protocols"]: | 
|  | if transport in client["transports"]: | 
|  | if sock in client["sockets"]: | 
|  | if count != 0: | 
|  | results_json.write(",\n") | 
|  | count = 1 | 
|  | 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" ) | 
|  | test_name = server_lib + "_" + client_lib + "_accel-binary_" + transport + "_" + sock | 
|  | ret = runServiceTest(test_name, server_executable, server_extra_args, client_executable, client_extra_args, protocol, 'accel', transport, 9090, 0, sock) | 
|  | if ret != None: | 
|  | failed += 1 | 
|  | print "Error: %s" % ret | 
|  | print "Using" | 
|  | print (' Server: %s --protocol=%s --transport=%s %s %s' | 
|  | % (server_executable, protocol, transport, getSocketArgs(sock), ' '.join(server_extra_args))) | 
|  | print (' Client: %s --protocol=%s --transport=%s %s %s' | 
|  | % (client_executable, protocol, transport , getSocketArgs(sock), ''.join(client_extra_args))) | 
|  | 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") | 
|  | else: | 
|  | 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") | 
|  | results_json.write("\t]") | 
|  | test_count += 1 | 
|  | if protocol == 'accel' and 'binary' in client["protocols"]: | 
|  | if transport in client["transports"]: | 
|  | if sock in client["sockets"]: | 
|  | if count != 0: | 
|  | results_json.write(",\n") | 
|  | count = 1 | 
|  | 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" ) | 
|  | test_name = server_lib + "_" + client_lib + "_accel-binary_" + transport + "_" + sock | 
|  | ssl = 0 | 
|  | if sock == 'ip-ssl': | 
|  | ssl = 1 | 
|  | ret = runServiceTest(test_name, server_executable, server_extra_args, client_executable, client_extra_args, protocol, 'binary', transport, 9090, 0, sock) | 
|  | if ret != None: | 
|  | failed += 1 | 
|  | print "Error: %s" % ret | 
|  | print "Using" | 
|  | print (' Server: %s --protocol=%s --transport=%s %s %s' | 
|  | % (server_executable, protocol, transport + sock, getSocketArgs(sock), ' '.join(server_extra_args))) | 
|  | print (' Client: %s --protocol=%s --transport=%s %s %s' | 
|  | % (client_executable, protocol, transport + sock, getSocketArgs(sock), ''.join(client_extra_args))) | 
|  | 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") | 
|  | else: | 
|  | 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") | 
|  | results_json.write("\t]") | 
|  | test_count += 1 | 
|  | results_json.write("\n]") | 
|  | results_json.flush() | 
|  | results_json.close() | 
|  | print '%s failed of %s tests in total' % (failed, test_count) |