forked from quic-interop/quic-interop-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interop.py
476 lines (426 loc) · 17.5 KB
/
interop.py
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
import json
import logging
import os
import random
import re
import shutil
import statistics
import string
import subprocess
import sys
import tempfile
from datetime import datetime
from enum import Enum
from typing import Callable, List, Tuple
import prettytable
from termcolor import colored
import testcases
from testcases import Perspective
def random_string(length: int):
""" Generate a random string of fixed length """
letters = string.ascii_lowercase
return "".join(random.choice(letters) for i in range(length))
class TestResult(Enum):
SUCCEEDED = "succeeded"
FAILED = "failed"
UNSUPPORTED = "unsupported"
class MeasurementResult:
result = TestResult
details = str
class LogFileFormatter(logging.Formatter):
def format(self, record):
msg = super(LogFileFormatter, self).format(record)
# remove color control characters
return re.compile(r"\x1B[@-_][0-?]*[ -/]*[@-~]").sub("", msg)
class InteropRunner:
_start_time = 0
test_results = {}
measurement_results = {}
compliant = {}
_implementations = {}
_servers = []
_clients = []
_tests = []
_measurements = []
_output = ""
_log_dir = ""
def __init__(
self,
implementations: dict,
servers: List[str],
clients: List[str],
tests: List[testcases.TestCase],
measurements: List[testcases.Measurement],
output: str,
debug: bool,
log_dir="",
):
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
console = logging.StreamHandler(stream=sys.stderr)
if debug:
console.setLevel(logging.DEBUG)
else:
console.setLevel(logging.INFO)
logger.addHandler(console)
self._start_time = datetime.now()
self._tests = tests
self._measurements = measurements
self._servers = servers
self._clients = clients
self._implementations = implementations
self._output = output
self._log_dir = log_dir
if len(self._log_dir) == 0:
self._log_dir = "logs_{:%Y-%m-%dT%H:%M:%S}".format(self._start_time)
if os.path.exists(self._log_dir):
sys.exit("Log dir " + self._log_dir + " already exists.")
logging.info("Saving logs to %s.", self._log_dir)
for server in servers:
self.test_results[server] = {}
self.measurement_results[server] = {}
for client in clients:
self.test_results[server][client] = {}
for test in self._tests:
self.test_results[server][client][test] = {}
self.measurement_results[server][client] = {}
for measurement in measurements:
self.measurement_results[server][client][measurement] = {}
def _is_unsupported(self, lines: List[str]) -> bool:
return any("exited with code 127" in str(line) for line in lines) or any(
"exit status 127" in str(line) for line in lines
)
def _check_impl_is_compliant(self, name: str) -> bool:
""" check if an implementation return UNSUPPORTED for unknown test cases """
if name in self.compliant:
logging.debug(
"%s already tested for compliance: %s", name, str(self.compliant)
)
return self.compliant[name]
client_log_dir = tempfile.TemporaryDirectory(dir="/tmp", prefix="logs_client_")
www_dir = tempfile.TemporaryDirectory(dir="/tmp", prefix="compliance_www_")
downloads_dir = tempfile.TemporaryDirectory(
dir="/tmp", prefix="compliance_downloads_"
)
# check that the client is capable of returning UNSUPPORTED
logging.debug("Checking compliance of %s client", name)
cmd = (
"TESTCASE_CLIENT=" + random_string(6) + " "
"SERVER_LOGS=/dev/null "
"CLIENT_LOGS=" + client_log_dir.name + " "
"WWW=" + www_dir.name + " "
"DOWNLOADS=" + downloads_dir.name + " "
'SCENARIO="simple-p2p --delay=15ms --bandwidth=10Mbps --queue=25" '
"CLIENT=" + self._implementations[name] + " "
"docker-compose up --timeout 0 --abort-on-container-exit sim client"
)
output = subprocess.run(
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
if not self._is_unsupported(output.stdout.splitlines()):
logging.error("%s client not compliant.", name)
logging.debug("%s", output.stdout.decode("utf-8"))
self.compliant[name] = False
return False
logging.debug("%s client compliant.", name)
# check that the server is capable of returning UNSUPPORTED
logging.debug("Checking compliance of %s server", name)
server_log_dir = tempfile.TemporaryDirectory(dir="/tmp", prefix="logs_server_")
cmd = (
"TESTCASE_SERVER=" + random_string(6) + " "
"SERVER_LOGS=" + server_log_dir.name + " "
"CLIENT_LOGS=/dev/null "
"WWW=" + www_dir.name + " "
"DOWNLOADS=" + downloads_dir.name + " "
"SERVER=" + self._implementations[name] + " "
"docker-compose up server"
)
output = subprocess.run(
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
if not self._is_unsupported(output.stdout.splitlines()):
logging.error("%s server not compliant.", name)
logging.debug("%s", output.stdout.decode("utf-8"))
self.compliant[name] = False
return False
logging.debug("%s server compliant.", name)
# remember compliance test outcome
self.compliant[name] = True
return True
def _print_results(self):
"""print the interop table"""
logging.info("Run took %s", datetime.now() - self._start_time)
def get_letters(result):
return "".join(
[test.abbreviation() for test in cell if cell[test] is result]
)
if len(self._tests) > 0:
t = prettytable.PrettyTable()
t.hrules = prettytable.ALL
t.vrules = prettytable.ALL
t.field_names = [""] + [name for name in self._servers]
for client in self._clients:
row = [client]
for server in self._servers:
cell = self.test_results[server][client]
res = colored(get_letters(TestResult.SUCCEEDED), "green") + "\n"
res += colored(get_letters(TestResult.UNSUPPORTED), "yellow") + "\n"
res += colored(get_letters(TestResult.FAILED), "red")
row += [res]
t.add_row(row)
print(t)
if len(self._measurements) > 0:
t = prettytable.PrettyTable()
t.hrules = prettytable.ALL
t.vrules = prettytable.ALL
t.field_names = [""] + [name for name in self._servers]
for client in self._clients:
row = [client]
for server in self._servers:
cell = self.measurement_results[server][client]
results = []
for measurement in self._measurements:
res = cell[measurement]
if not hasattr(res, "result"):
continue
if res.result == TestResult.SUCCEEDED:
results.append(
colored(
measurement.abbreviation() + ": " + res.details,
"green",
)
)
elif res.result == TestResult.UNSUPPORTED:
results.append(
colored(measurement.abbreviation(), "yellow")
)
elif res.result == TestResult.FAILED:
results.append(colored(measurement.abbreviation(), "red"))
row += ["\n".join(results)]
t.add_row(row)
print(t)
def _export_results(self):
if not self._output:
return
out = {
"start_time": self._start_time.timestamp(),
"end_time": datetime.now().timestamp(),
"log_dir": self._log_dir,
"servers": [name for name in self._servers],
"clients": [name for name in self._clients],
"results": [],
"measurements": [],
}
for client in self._clients:
for server in self._servers:
results = []
for test in self._tests:
r = None
if hasattr(self.test_results[server][client][test], "value"):
r = self.test_results[server][client][test].value
results.append(
{"abbr": test.abbreviation(), "name": test.name(), "result": r}
)
out["results"].append(results)
measurements = []
for measurement in self._measurements:
res = self.measurement_results[server][client][measurement]
if not hasattr(res, "result"):
continue
measurements.append(
{
"name": measurement.name(),
"abbr": measurement.abbreviation(),
"result": res.result.value,
"details": res.details,
}
)
out["measurements"].append(measurements)
f = open(self._output, "w")
json.dump(out, f)
f.close()
def _copy_logs(self, container: str, dir: tempfile.TemporaryDirectory):
r = subprocess.run(
'docker cp "$(docker-compose --log-level ERROR ps -q '
+ container
+ ')":/logs/. '
+ dir.name,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
if r.returncode != 0:
logging.info(
"Copying logs from %s failed: %s", container, r.stdout.decode("utf-8")
)
def _run_testcase(
self, server: str, client: str, test: Callable[[], testcases.TestCase]
) -> TestResult:
return self._run_test(server, client, None, test)[0]
def _run_test(
self,
server: str,
client: str,
log_dir_prefix: None,
test: Callable[[], testcases.TestCase],
) -> Tuple[TestResult, float]:
start_time = datetime.now()
sim_log_dir = tempfile.TemporaryDirectory(dir="/tmp", prefix="logs_sim_")
server_log_dir = tempfile.TemporaryDirectory(dir="/tmp", prefix="logs_server_")
client_log_dir = tempfile.TemporaryDirectory(dir="/tmp", prefix="logs_client_")
log_file = tempfile.NamedTemporaryFile(dir="/tmp", prefix="output_log_")
log_handler = logging.FileHandler(log_file.name)
log_handler.setLevel(logging.DEBUG)
formatter = LogFileFormatter("%(asctime)s %(message)s")
log_handler.setFormatter(formatter)
logging.getLogger().addHandler(log_handler)
testcase = test(
sim_log_dir=sim_log_dir,
client_keylog_file=client_log_dir.name + "/keys.log",
server_keylog_file=server_log_dir.name + "/keys.log",
)
print(
"Server: "
+ server
+ ". Client: "
+ client
+ ". Running test case: "
+ str(testcase)
)
reqs = " ".join(["https://server:443/" + p for p in testcase.get_paths()])
logging.debug("Requests: %s", reqs)
params = (
"TESTCASE_SERVER=" + testcase.testname(Perspective.SERVER) + " "
"TESTCASE_CLIENT=" + testcase.testname(Perspective.CLIENT) + " "
"WWW=" + testcase.www_dir() + " "
"DOWNLOADS=" + testcase.download_dir() + " "
"SERVER_LOGS=" + server_log_dir.name + " "
"CLIENT_LOGS=" + client_log_dir.name + " "
'SCENARIO="{}" '
"CLIENT=" + self._implementations[client] + " "
"SERVER=" + self._implementations[server] + " "
'REQUESTS="' + reqs + '" '
).format(testcase.scenario())
params += " ".join(testcase.additional_envs())
containers = "sim client server " + " ".join(testcase.additional_containers())
cmd = (
params
+ " docker-compose up --abort-on-container-exit --timeout 1 "
+ containers
)
logging.debug("Command: %s", cmd)
status = TestResult.FAILED
output = ""
expired = False
try:
r = subprocess.run(
cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
timeout=testcase.timeout(),
)
output = r.stdout
except subprocess.TimeoutExpired as ex:
output = ex.stdout
expired = True
logging.debug("%s", output.decode("utf-8"))
if expired:
logging.debug("Test failed: took longer than %ds.", testcase.timeout())
r = subprocess.run(
"docker-compose stop " + containers,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
timeout=60,
)
logging.debug("%s", r.stdout.decode("utf-8"))
# copy the pcaps from the simulator
self._copy_logs("sim", sim_log_dir)
self._copy_logs("client", client_log_dir)
self._copy_logs("server", server_log_dir)
if not expired:
lines = output.splitlines()
if self._is_unsupported(lines):
status = TestResult.UNSUPPORTED
elif any("client exited with code 0" in str(line) for line in lines):
if testcase.check():
status = TestResult.SUCCEEDED
# save logs
logging.getLogger().removeHandler(log_handler)
log_handler.close()
if status == TestResult.FAILED or status == TestResult.SUCCEEDED:
log_dir = self._log_dir + "/" + server + "_" + client + "/" + str(testcase)
if log_dir_prefix:
log_dir += "/" + log_dir_prefix
shutil.copytree(server_log_dir.name, log_dir + "/server")
shutil.copytree(client_log_dir.name, log_dir + "/client")
shutil.copytree(sim_log_dir.name, log_dir + "/sim")
shutil.copyfile(log_file.name, log_dir + "/output.txt")
if status == TestResult.FAILED:
shutil.copytree(testcase.www_dir(), log_dir + "/www")
try:
shutil.copytree(testcase.download_dir(), log_dir + "/downloads")
except Exception as exception:
logging.info("Could not copy downloaded files: %s", exception)
testcase.cleanup()
server_log_dir.cleanup()
client_log_dir.cleanup()
sim_log_dir.cleanup()
logging.debug("Test took %ss", (datetime.now() - start_time).total_seconds())
# measurements also have a value
if hasattr(testcase, "result"):
value = testcase.result()
else:
value = None
return status, value
def _run_measurement(
self, server: str, client: str, test: Callable[[], testcases.Measurement]
) -> MeasurementResult:
values = []
for i in range(0, test.repetitions()):
result, value = self._run_test(server, client, "%d" % (i + 1), test)
if result != TestResult.SUCCEEDED:
res = MeasurementResult()
res.result = result
res.details = ""
return res
values.append(value)
logging.debug(values)
res = MeasurementResult()
res.result = TestResult.SUCCEEDED
res.details = "{:.0f} (± {:.0f}) {}".format(
statistics.mean(values), statistics.stdev(values), test.unit()
)
return res
def run(self):
"""run the interop test suite and output the table"""
nr_failed = 0
for server in self._servers:
for client in self._clients:
logging.debug(
"Running with server %s (%s) and client %s (%s)",
server,
self._implementations[server],
client,
self._implementations[client],
)
if not (
self._check_impl_is_compliant(server)
and self._check_impl_is_compliant(client)
):
logging.info("Not compliant, skipping")
continue
# run the test cases
for testcase in self._tests:
status = self._run_testcase(server, client, testcase)
self.test_results[server][client][testcase] = status
if status == TestResult.FAILED:
nr_failed += 1
# run the measurements
for measurement in self._measurements:
res = self._run_measurement(server, client, measurement)
self.measurement_results[server][client][measurement] = res
self._print_results()
self._export_results()
return nr_failed