forked from kpdyer/fteproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
systemtests
executable file
·212 lines (151 loc) · 5.97 KB
/
systemtests
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import time
import socket
import string
import random
import fteproxy.conf
import fteproxy.defs
import fteproxy.network_io
BYTES_TO_SEND = 2 ** 18
TRIALS = 2 ** 8
BLOCK_SIZE = 2 ** 12
TIMEOUT = 120
BIND_IP = '127.0.0.1'
CLIENT_PORT = 8079
SERVER_PORT = 8080
PROXY_PORT = 8081
def executeCmd(cmd):
os.system(cmd)
def main():
"""Start an fteproxy server.
Iterate through the FTE formats available to fteproxy.
For each format, spawn a client using that format, connect to the fteproxy server and pump data through the connection.
Verify the data successfully traversed the client and server, upon success output performance statistics.
"""
def_dir = os.path.join(fteproxy.conf.getValue('general.defs_dir'))
def_file = fteproxy.conf.getValue('fteproxy.defs.release') + '.json'
def_abspath = os.path.normpath(os.path.join(def_dir, def_file))
print 'Testing formats in', def_abspath
start_server()
languages = fteproxy.defs.load_definitions().keys()
for language in languages:
if language.endswith('request'):
language_name = language[:-8]
elapsed, bytesSent = doTest(language_name)
throughput = ((1.0 * bytesSent / elapsed) / (2 ** 20)) * \
(2 ** 3)
throughput = round(throughput, 2)
upstream_regex = fteproxy.defs.getRegex(language_name + '-request')
downstream_regex = fteproxy.defs.getRegex(language_name + '-response')
print ' + SUCCESS, format_name="' + language_name + '"'
print ' - client-server regex: "' + upstream_regex + '"'
print ' - server-client regex: "' + downstream_regex + '"'
print ' - test duration: ' + str(round(elapsed, 2)) + " seconds"
print ' - goodput: ' + str(throughput) + 'Mbps'
stop_server()
print "****** ALL TESTS COMPLETE, SUCCESS!!"
def random_string(size):
"""Return a random alphanumeric string of length ``size``."""
chars = string.ascii_uppercase + string.digits
return ''.join(random.choice(chars) for x in range(size))
def doTest(language_name):
"""Given the ``language_name``, start the fteproxy client and pump
``BYTES_TO_SEND*TIRALS`` bytes via the fteproxy client and server.
"""
elapsed = -1
bytesSent = 0
try:
start_client(language_name)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((BIND_IP, PROXY_PORT))
sock.listen(100)
new_stream = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
new_stream.connect((BIND_IP, CLIENT_PORT))
conn, addr = sock.accept()
conn.settimeout(0.1)
startTimer = time.time()
expected_msg = random_string(BYTES_TO_SEND)
elapsed = -1
for i in range(TRIALS):
bytesSent += BYTES_TO_SEND
actual_msg = ''
expected_buffer = expected_msg
while True:
if expected_buffer:
bytessent = new_stream.send(expected_buffer[:BLOCK_SIZE])
expected_buffer = expected_buffer[bytessent:]
else:
break
while True:
time.sleep(0.00001)
[is_alive, data] = fteproxy.network_io.recvall_from_socket(conn)
actual_msg += data
assert expected_msg.startswith(actual_msg)
if (actual_msg == expected_msg):
break
if elapsed > TIMEOUT or not is_alive:
raise Exception(
"!!!!!!!!!!! System tests failed to execute properly")
elapsed = time.time() - startTimer
fteproxy.network_io.close_socket(conn)
fteproxy.network_io.close_socket(new_stream)
fteproxy.network_io.close_socket(sock)
finally:
stop_client()
return elapsed, bytesSent
def start_client(language_name):
"""Start our fteproxy client, block until it's ready."""
executeCmd("./bin/fteproxy --quiet --mode client"
+ " --upstream-format " + language_name + "-request"
+ " --downstream-format " + language_name + "-response"
+ " --client_ip " + BIND_IP
+ " --client_port " + str(CLIENT_PORT)
+ " --server_ip " + BIND_IP + " --server_port " + str(SERVER_PORT) + " &")
waitForListener(BIND_IP, CLIENT_PORT)
def start_server():
"""Start our fteproxy server, block until it's ready."""
executeCmd("./bin/fteproxy --quiet --mode server"
+ " --server_ip " + BIND_IP
+ " --server_port " + str(SERVER_PORT)
+ " --proxy_ip " + BIND_IP + " --proxy_port " + str(PROXY_PORT) + " &")
waitForListener(BIND_IP, SERVER_PORT)
def stop_client():
"""Stop our fteproxy client."""
executeCmd("./bin/fteproxy --quiet --mode client --stop")
time.sleep(1)
def stop_server():
"""Stop our fteproxy server."""
executeCmd("./bin/fteproxy --quiet --mode server --stop")
time.sleep(1)
class ListenerNotReady(Exception):
pass
def waitForListener(ip, port, maxWait=60):
"""Block until a there's a process listening on ip:port.
Return ``true`` on success, raise ``ListenerNotReady`` exception otherwise.
"""
time.sleep(5)
startTime = time.time()
success = False
new_stream = None
while not success and (time.time() - startTime) < maxWait:
time.sleep(0.1)
try:
new_stream = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
new_stream.settimeout(0.1)
new_stream.connect((ip, port))
success = True
except:
continue
finally:
if new_stream:
new_stream.close()
new_stream = None
if not success:
raise ListenerNotReady()
time.sleep(1)
return success
if __name__ == '__main__':
main()