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