blob: 0111a2f050aa83e3c3e7b4da648432c58745d0dc [file] [log] [blame]
Roger Meier40cc2322014-06-11 11:09:14 +02001#!/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
22from __future__ import division
23import time
24import socket
25import subprocess
26import sys
27import os
28import signal
29import json
Roger Meier32f39822014-06-18 22:43:17 +020030import shutil
31import threading
Roger Meier40cc2322014-06-11 11:09:14 +020032from optparse import OptionParser
33
34parser = OptionParser()
35parser.add_option("--port", type="int", dest="port", default=9090,
36 help="port number for server to listen on")
37parser.add_option('-v', '--verbose', action="store_const",
38 dest="verbose", const=2,
39 help="verbose output")
40parser.add_option('-q', '--quiet', action="store_const",
41 dest="verbose", const=0,
42 help="minimal output")
43parser.set_defaults(verbose=1)
44options, args = parser.parse_args()
45
46def relfile(fname):
47 return os.path.join(os.path.dirname(__file__), fname)
48
Roger Meier32f39822014-06-18 22:43:17 +020049def runServiceTest(test_name, server_executable, server_extra_args, client_executable, client_extra_args, protocol, transport, port, use_zlib, use_ssl):
Roger Meier40cc2322014-06-11 11:09:14 +020050 # Build command line arguments
51 server_args = [relfile(server_executable)]
52 cli_args = [relfile(client_executable)]
53 for which in (server_args, cli_args):
54 which.append('--protocol=%s' % protocol) # accel, binary or compact
55 which.append('--transport=%s' % transport)
56 which.append('--port=%d' % port) # default to 9090
57 if use_zlib:
58 which.append('--zlib')
59 if use_ssl:
60 which.append('--ssl')
61# if options.verbose == 0:
62# which.append('-q')
63# if options.verbose == 2:
64# which.append('-v')
65
66 server_args.extend(server_extra_args)
67 cli_args.extend(client_extra_args)
Roger Meier32f39822014-06-18 22:43:17 +020068 server_log=open("log/" + test_name + "_server.log","a")
69 client_log=open("log/" + test_name + "_client.log","a")
Roger Meier40cc2322014-06-11 11:09:14 +020070
71 if options.verbose > 0:
72 print 'Testing server: %s' % (' '.join(server_args))
Roger Meier32f39822014-06-18 22:43:17 +020073 serverproc = subprocess.Popen(server_args, stdout=server_log, stderr=server_log)
Roger Meier40cc2322014-06-11 11:09:14 +020074 else:
Roger Meier32f39822014-06-18 22:43:17 +020075 serverproc = subprocess.Popen(server_args, stdout=server_log, stderr=server_log)
Roger Meier40cc2322014-06-11 11:09:14 +020076
77 def ensureServerAlive():
78 if serverproc.poll() is not None:
79 print ('FAIL: Server process (%s) failed with retcode %d'
80 % (' '.join(server_args), serverproc.returncode))
81 raise Exception('Server subprocess died, args: %s'
82 % (' '.join(server_args)))
83
84 # Wait for the server to start accepting connections on the given port.
85 sock = socket.socket()
86 sleep_time = 0.1 # Seconds
87 max_attempts = 100
88 try:
89 attempt = 0
90 while sock.connect_ex(('127.0.0.1', port)) != 0:
91 attempt += 1
92 if attempt >= max_attempts:
93 raise Exception("TestServer not ready on port %d after %.2f seconds"
94 % (port, sleep_time * attempt))
95 ensureServerAlive()
96 time.sleep(sleep_time)
97 finally:
98 sock.close()
99
100 try:
Roger Meier32f39822014-06-18 22:43:17 +0200101 o = []
102 def target():
103 if options.verbose > 0:
104 print 'Testing client: %s' % (' '.join(cli_args))
105 process = subprocess.Popen(cli_args, stdout=client_log, stderr=client_log)
106 o.append(process)
107 process.communicate()
108 else:
109 process = subprocess.Popen(cli_args, stdout=client_log, stderr=client_log)
110 o.append(process)
111 process.communicate()
112 thread = threading.Thread(target=target)
113 thread.start()
114
115 thread.join(10)
116 if thread.is_alive():
117 print 'Terminating process'
118 o[0].terminate()
119 thread.join()
120 ret = o[0].returncode
Roger Meier40cc2322014-06-11 11:09:14 +0200121 if ret != 0:
122 return "Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args))
123 #raise Exception("Client subprocess failed, retcode=%d, args: %s" % (ret, ' '.join(cli_args)))
124 finally:
125 # check that server didn't die
Roger Meier32f39822014-06-18 22:43:17 +0200126 #ensureServerAlive()
Roger Meier40cc2322014-06-11 11:09:14 +0200127 extra_sleep = 0
128 if extra_sleep > 0 and options.verbose > 0:
129 print ('Giving (protocol=%s,zlib=%s,ssl=%s) an extra %d seconds for child'
130 'processes to terminate via alarm'
131 % (protocol, use_zlib, use_ssl, extra_sleep))
132 time.sleep(extra_sleep)
133 os.kill(serverproc.pid, signal.SIGKILL)
134 serverproc.wait()
Roger Meier32f39822014-06-18 22:43:17 +0200135 client_log.flush()
136 server_log.flush()
137 client_log.close()
138 server_log.close()
Roger Meier40cc2322014-06-11 11:09:14 +0200139
140test_count = 0
141failed = 0
142
Roger Meier32f39822014-06-18 22:43:17 +0200143if os.path.exists('log'): shutil.rmtree('log')
144os.makedirs('log')
145
Roger Meier40cc2322014-06-11 11:09:14 +0200146with open('tests.json') as data_file:
147 data = json.load(data_file)
148
Roger Meier32f39822014-06-18 22:43:17 +0200149#subprocess.call("export NODE_PATH=../lib/nodejs/test:../lib/nodejs/lib:${NODE_PATH}")
150
Roger Meier40cc2322014-06-11 11:09:14 +0200151for server in data["server"]:
152 server_executable = server["executable"]
153 server_extra_args = ""
Roger Meier32f39822014-06-18 22:43:17 +0200154 server_lib = server["lib"]
Roger Meier40cc2322014-06-11 11:09:14 +0200155 if "extra_args" in server:
156 server_extra_args = server["extra_args"]
157 for protocol in server["protocols"]:
158 for transport in server["transports"]:
Roger Meier32f39822014-06-18 22:43:17 +0200159 for sock in server["sockets"]:
160 for client in data["client"]:
161 client_executable = client["executable"]
162 client_extra_args = ""
163 client_lib = client["lib"]
164 if "extra_args" in client:
165 client_extra_args = client["extra_args"]
166 if protocol in client["protocols"]:
167 if transport in client["transports"]:
168 if sock in client["sockets"]:
169 test_name = server_lib + "_" + client_lib + "_" + protocol + "_" + transport + "_" + sock
170 ssl = 0
171 if sock == 'ip-ssl':
172 ssl = 1
173 ret = runServiceTest(test_name, server_executable, server_extra_args, client_executable, client_extra_args, protocol, transport, 9090, 0, ssl)
174 if ret != None:
175 failed += 1
176 print "Error: %s" % ret
177 print "Using"
178 print (' Server: %s --protocol=%s --transport=%s %s'
179 % (server_executable, protocol, transport, ' '.join(server_extra_args)))
180 print (' Client: %s --protocol=%s --transport=%s %s'
181 % (client_executable, protocol, transport, ''.join(client_extra_args)))
Roger Meier40cc2322014-06-11 11:09:14 +0200182
183
Roger Meier32f39822014-06-18 22:43:17 +0200184 test_count += 1
Roger Meier40cc2322014-06-11 11:09:14 +0200185
186print '%s failed of %s tests in total' % (failed, test_count)
187