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 |
| 30 | from optparse import OptionParser |
| 31 | |
| 32 | parser = OptionParser() |
| 33 | parser.add_option("--port", type="int", dest="port", default=9090, |
| 34 | help="port number for server to listen on") |
| 35 | parser.add_option('-v', '--verbose', action="store_const", |
| 36 | dest="verbose", const=2, |
| 37 | help="verbose output") |
| 38 | parser.add_option('-q', '--quiet', action="store_const", |
| 39 | dest="verbose", const=0, |
| 40 | help="minimal output") |
| 41 | parser.set_defaults(verbose=1) |
| 42 | options, args = parser.parse_args() |
| 43 | |
| 44 | def relfile(fname): |
| 45 | return os.path.join(os.path.dirname(__file__), fname) |
| 46 | |
| 47 | def runServiceTest(server_executable, server_extra_args, client_executable, client_extra_args, protocol, transport, port, use_zlib, use_ssl): |
| 48 | # Build command line arguments |
| 49 | server_args = [relfile(server_executable)] |
| 50 | cli_args = [relfile(client_executable)] |
| 51 | for which in (server_args, cli_args): |
| 52 | which.append('--protocol=%s' % protocol) # accel, binary or compact |
| 53 | which.append('--transport=%s' % transport) |
| 54 | which.append('--port=%d' % port) # default to 9090 |
| 55 | if use_zlib: |
| 56 | which.append('--zlib') |
| 57 | if use_ssl: |
| 58 | which.append('--ssl') |
| 59 | # if options.verbose == 0: |
| 60 | # which.append('-q') |
| 61 | # if options.verbose == 2: |
| 62 | # which.append('-v') |
| 63 | |
| 64 | server_args.extend(server_extra_args) |
| 65 | cli_args.extend(client_extra_args) |
| 66 | |
| 67 | if options.verbose > 0: |
| 68 | print 'Testing server: %s' % (' '.join(server_args)) |
| 69 | serverproc = subprocess.Popen(server_args) |
| 70 | else: |
| 71 | serverproc = subprocess.Popen(server_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 72 | |
| 73 | def ensureServerAlive(): |
| 74 | if serverproc.poll() is not None: |
| 75 | print ('FAIL: Server process (%s) failed with retcode %d' |
| 76 | % (' '.join(server_args), serverproc.returncode)) |
| 77 | raise Exception('Server subprocess died, args: %s' |
| 78 | % (' '.join(server_args))) |
| 79 | |
| 80 | # Wait for the server to start accepting connections on the given port. |
| 81 | sock = socket.socket() |
| 82 | sleep_time = 0.1 # Seconds |
| 83 | max_attempts = 100 |
| 84 | try: |
| 85 | attempt = 0 |
| 86 | while sock.connect_ex(('127.0.0.1', port)) != 0: |
| 87 | attempt += 1 |
| 88 | if attempt >= max_attempts: |
| 89 | raise Exception("TestServer not ready on port %d after %.2f seconds" |
| 90 | % (port, sleep_time * attempt)) |
| 91 | ensureServerAlive() |
| 92 | time.sleep(sleep_time) |
| 93 | finally: |
| 94 | sock.close() |
| 95 | |
| 96 | try: |
| 97 | if options.verbose > 0: |
| 98 | print 'Testing client: %s' % (' '.join(cli_args)) |
| 99 | ret = subprocess.call(cli_args) |
| 100 | else: |
| 101 | ret = subprocess.call(cli_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 102 | if ret != 0: |
| 103 | return "Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args)) |
| 104 | #raise Exception("Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args))) |
| 105 | finally: |
| 106 | # check that server didn't die |
| 107 | ensureServerAlive() |
| 108 | extra_sleep = 0 |
| 109 | if extra_sleep > 0 and options.verbose > 0: |
| 110 | print ('Giving (protocol=%s,zlib=%s,ssl=%s) an extra %d seconds for child' |
| 111 | 'processes to terminate via alarm' |
| 112 | % (protocol, use_zlib, use_ssl, extra_sleep)) |
| 113 | time.sleep(extra_sleep) |
| 114 | os.kill(serverproc.pid, signal.SIGKILL) |
| 115 | serverproc.wait() |
| 116 | |
| 117 | test_count = 0 |
| 118 | failed = 0 |
| 119 | |
| 120 | with open('tests.json') as data_file: |
| 121 | data = json.load(data_file) |
| 122 | |
| 123 | for server in data["server"]: |
| 124 | server_executable = server["executable"] |
| 125 | server_extra_args = "" |
| 126 | if "extra_args" in server: |
| 127 | server_extra_args = server["extra_args"] |
| 128 | for protocol in server["protocols"]: |
| 129 | for transport in server["transports"]: |
| 130 | for client in data["client"]: |
| 131 | client_executable = client["executable"] |
| 132 | client_extra_args = "" |
| 133 | if "extra_args" in client: |
| 134 | client_extra_args = client["extra_args"] |
| 135 | if protocol in client["protocols"]: |
| 136 | if transport in client["transports"]: |
| 137 | ret = runServiceTest(server_executable, server_extra_args, client_executable, client_extra_args, protocol, transport, 9090, 0, 0) |
| 138 | if ret != None: |
| 139 | failed += 1 |
| 140 | print "Error: %s" % ret |
| 141 | print "Using" |
| 142 | print (' Server: %s --protocol=%s --transport=%s %s' |
| 143 | % (server_executable, protocol, transport, ' '.join(server_extra_args))) |
| 144 | print (' Client: %s --protocol=%s --transport=%s %s' |
| 145 | % (client_executable, protocol, transport, ''.join(client_extra_args))) |
| 146 | |
| 147 | |
| 148 | test_count += 1 |
| 149 | |
| 150 | print '%s failed of %s tests in total' % (failed, test_count) |
| 151 | |