| 1 |
|
| 2 |
|
| 3 |
""" |
| 4 |
cache.py |
| 5 |
|
| 6 |
Created by Thomas Mangin |
| 7 |
Copyright (c) 2013-2017 Exa Networks. All rights reserved. |
| 8 |
License: 3-clause BSD. (See the COPYRIGHT file) |
| 9 |
""" |
| 10 |
|
| 11 |
import os |
| 12 |
import re |
| 13 |
import sys |
| 14 |
import json |
| 15 |
import glob |
| 16 |
import time |
| 17 |
import signal |
| 18 |
import argparse |
| 19 |
import subprocess |
| 20 |
from enum import Enum |
| 21 |
|
| 22 |
|
| 23 |
INTERPRETER = os.environ.get('INTERPRETER', '') |
| 24 |
if not INTERPRETER: |
| 25 |
INTERPRETER = os.environ.get('__PYVENV_LAUNCHER__', sys.executable) |
| 26 |
|
| 27 |
|
| 28 |
class Alarm(Exception): |
| 29 |
pass |
| 30 |
|
| 31 |
|
| 32 |
def flush(*args, **kwars): |
| 33 |
print(*args, **kwars) |
| 34 |
sys.stdout.flush() |
| 35 |
|
| 36 |
|
| 37 |
def alarm_handler(number, frame): |
| 38 |
raise Alarm() |
| 39 |
|
| 40 |
|
| 41 |
def color(prefix, suffix): |
| 42 |
def code(value): |
| 43 |
return f'\033[{value}m' |
| 44 |
|
| 45 |
return code(prefix) + code(suffix) |
| 46 |
|
| 47 |
|
| 48 |
class Port: |
| 49 |
base = 1790 |
| 50 |
|
| 51 |
@classmethod |
| 52 |
def get(cls): |
| 53 |
current = cls.base |
| 54 |
cls.base += 1 |
| 55 |
return current |
| 56 |
|
| 57 |
|
| 58 |
class Path: |
| 59 |
PROGRAM = os.path.realpath(__file__) |
| 60 |
ROOT = os.path.abspath(os.path.join(os.path.dirname(PROGRAM), os.path.join('..', '..'))) |
| 61 |
SRC = os.path.join(ROOT, 'src') |
| 62 |
|
| 63 |
ETC = os.path.join(ROOT, 'etc', 'exabgp') |
| 64 |
EXABGP = os.path.join(ROOT, 'sbin', 'exabgp') |
| 65 |
BGP = os.path.join(ROOT, 'qa', 'sbin', 'bgp') |
| 66 |
DECODING = os.path.join(os.path.join(ROOT, 'qa', 'decoding')) |
| 67 |
ENCODING = os.path.join(os.path.join(ROOT, 'qa', 'encoding')) |
| 68 |
|
| 69 |
ALL_ETC = glob.glob(os.path.join(ETC, '*.conf')) |
| 70 |
ALL_ETC.sort() |
| 71 |
ALL_DECODING = glob.glob(os.path.join(DECODING, '*')) |
| 72 |
ALL_DECODING.sort() |
| 73 |
ALL_ENCODING = glob.glob(os.path.join(ENCODING, '*.ci')) |
| 74 |
ALL_ENCODING.sort() |
| 75 |
|
| 76 |
@classmethod |
| 77 |
def setup_env(cls): |
| 78 |
os.environ['PYTHONPATH'] = cls.SRC |
| 79 |
|
| 80 |
@staticmethod |
| 81 |
def etc(fname): |
| 82 |
return os.path.abspath(os.path.join(Path.ETC, fname)) |
| 83 |
|
| 84 |
@staticmethod |
| 85 |
def ci(fname, ext): |
| 86 |
return os.path.abspath(os.path.join(Path.ENCODING, fname) + '.' + ext) |
| 87 |
|
| 88 |
@classmethod |
| 89 |
def validate(cls): |
| 90 |
if not os.path.isdir(cls.ETC): |
| 91 |
sys.exit('could not find etc folder') |
| 92 |
|
| 93 |
if not os.path.isdir(cls.ENCODING): |
| 94 |
sys.exit('could not find tests in the qa/encoding folder') |
| 95 |
|
| 96 |
if not os.path.isdir(cls.DECODING): |
| 97 |
sys.exit('could not find the tests in qa/decoding') |
| 98 |
|
| 99 |
if not os.path.isfile(cls.EXABGP): |
| 100 |
sys.exit('could not find exabgp') |
| 101 |
|
| 102 |
if not os.path.isfile(cls.BGP): |
| 103 |
sys.exit('could not find the sequence daemon') |
| 104 |
|
| 105 |
|
| 106 |
class Exec(object): |
| 107 |
def __init__(self): |
| 108 |
self.code = -1 |
| 109 |
self.stdout = b'' |
| 110 |
self.stderr = b'' |
| 111 |
self.message = '' |
| 112 |
self._process = None |
| 113 |
self.command = [] |
| 114 |
|
| 115 |
def run(self, command): |
| 116 |
self.command = ' '.join([_ if ' ' not in _ else f"'{_}'" for _ in command]) |
| 117 |
self._process = subprocess.Popen( |
| 118 |
command, |
| 119 |
stdout=subprocess.PIPE, |
| 120 |
stderr=subprocess.PIPE, |
| 121 |
) |
| 122 |
return self |
| 123 |
|
| 124 |
def ready(self): |
| 125 |
signal.signal(signal.SIGALRM, alarm_handler) |
| 126 |
try: |
| 127 |
signal.alarm(1) |
| 128 |
polled = self._process.poll() |
| 129 |
signal.alarm(0) |
| 130 |
except Alarm: |
| 131 |
return False |
| 132 |
except (OSError, ValueError): |
| 133 |
return True |
| 134 |
if polled is None: |
| 135 |
return False |
| 136 |
return True |
| 137 |
|
| 138 |
def report(self, reason='issue with exabgp'): |
| 139 |
flush(reason) |
| 140 |
flush(f'command> {self.command}') |
| 141 |
flush(f'return: {self.code}') |
| 142 |
flush(f'stdout: {self.stdout.decode()}') |
| 143 |
flush(f'stderr: {self.stderr.decode()}') |
| 144 |
flush(f'message: {self.message}') |
| 145 |
|
| 146 |
def collect(self): |
| 147 |
if self.stdout: |
| 148 |
return |
| 149 |
if self.stderr: |
| 150 |
return |
| 151 |
if self.code != -1: |
| 152 |
return |
| 153 |
|
| 154 |
signal.signal(signal.SIGALRM, alarm_handler) |
| 155 |
try: |
| 156 |
signal.alarm(15) |
| 157 |
self.stdout, self.stderr = self._process.communicate() |
| 158 |
self.code = self._process.returncode |
| 159 |
signal.alarm(0) |
| 160 |
except ValueError as exc: |
| 161 |
self.message = str(exc) |
| 162 |
pass |
| 163 |
except Alarm as exc: |
| 164 |
self.message = str(exc) |
| 165 |
pass |
| 166 |
|
| 167 |
def terminate(self): |
| 168 |
try: |
| 169 |
self._process.send_signal(signal.SIGTERM) |
| 170 |
except OSError: |
| 171 |
pass |
| 172 |
self.collect() |
| 173 |
|
| 174 |
def __del__(self): |
| 175 |
self.terminate |
| 176 |
|
| 177 |
|
| 178 |
State = Enum('State', 'NONE STARTING RUNNING FAIL SUCCESS SKIP') |
| 179 |
|
| 180 |
|
| 181 |
class Record: |
| 182 |
_index = 0 |
| 183 |
_listing = '0123456789' + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + 'abcdefghijklmnopqrstuvwxyz' + 'αβγδεζηθικλμνξοπρςστυφχψω' |
| 184 |
|
| 185 |
def __init__(self, nick, name): |
| 186 |
self.nick = nick |
| 187 |
self.name = name |
| 188 |
self.conf = dict() |
| 189 |
self.files = [] |
| 190 |
self.state = State.SKIP |
| 191 |
|
| 192 |
@classmethod |
| 193 |
def new(cls, name): |
| 194 |
return cls(cls.next_nick(), name) |
| 195 |
|
| 196 |
@classmethod |
| 197 |
def next_nick(cls): |
| 198 |
nick = cls._listing[cls._index] |
| 199 |
cls._index += 1 |
| 200 |
return nick |
| 201 |
|
| 202 |
def skip(self): |
| 203 |
self.state = State.SKIP |
| 204 |
return self |
| 205 |
|
| 206 |
def fail(self): |
| 207 |
self.state = State.FAIL |
| 208 |
return self |
| 209 |
|
| 210 |
def activate(self): |
| 211 |
self.state = State.NONE |
| 212 |
return self |
| 213 |
|
| 214 |
def is_active(self): |
| 215 |
return self.state not in (State.SKIP, State.FAIL, State.SUCCESS) |
| 216 |
|
| 217 |
def setup(self): |
| 218 |
if self.state == State.NONE: |
| 219 |
self.state = State.STARTING |
| 220 |
return |
| 221 |
if self.state == State.STARTING: |
| 222 |
self.state = State.RUNNING |
| 223 |
return |
| 224 |
|
| 225 |
def running(self): |
| 226 |
self.state = State.RUNNING |
| 227 |
|
| 228 |
def colored(self): |
| 229 |
if self.state == State.NONE: |
| 230 |
return color(0, 30) + self.nick |
| 231 |
if self.state == State.STARTING: |
| 232 |
return color(1, 30) + self.nick |
| 233 |
if self.state == State.RUNNING: |
| 234 |
return color(0, 0) + self.nick |
| 235 |
if self.state == State.FAIL: |
| 236 |
return color(0, 91) + self.nick |
| 237 |
if self.state == State.SUCCESS: |
| 238 |
return color(1, 92) + '✓'EN |
| 239 |
if self.state == State.SKIP: |
| 240 |
return color(0, 34) + '✖'UE |
| 241 |
|
| 242 |
def result(self, success): |
| 243 |
if success: |
| 244 |
self.state = State.SUCCESS |
| 245 |
else: |
| 246 |
self.state = State.FAIL |
| 247 |
return success |
| 248 |
|
| 249 |
|
| 250 |
class Tests: |
| 251 |
def __init__(self, klass): |
| 252 |
self.klass = klass |
| 253 |
self._by_nick = {} |
| 254 |
self._ordered = [] |
| 255 |
self._nl = 3 |
| 256 |
|
| 257 |
def new(self, name): |
| 258 |
test = self.klass.new(name) |
| 259 |
self._by_nick[test.nick] = test |
| 260 |
self._ordered.append(test.nick) |
| 261 |
self._ordered.sort() |
| 262 |
return test |
| 263 |
|
| 264 |
def enable_by_nick(self, nick): |
| 265 |
if nick in self._by_nick: |
| 266 |
self._by_nick[nick].activate() |
| 267 |
return True |
| 268 |
return False |
| 269 |
|
| 270 |
def enable_all(self): |
| 271 |
for nick in self._by_nick: |
| 272 |
self._by_nick[nick].activate() |
| 273 |
|
| 274 |
def get_by_nick(self, nick): |
| 275 |
return self._by_nick[nick] |
| 276 |
|
| 277 |
def registered(self): |
| 278 |
return [self._by_nick[nick] for nick in self._ordered] |
| 279 |
|
| 280 |
def selected(self): |
| 281 |
return [self._by_nick[nick] for nick in self._ordered if self._by_nick[nick].is_active()] |
| 282 |
|
| 283 |
def _iterate(self): |
| 284 |
number = len(self._ordered) |
| 285 |
lines = number // self._nl |
| 286 |
|
| 287 |
for line in range(0, lines + 1): |
| 288 |
tests = [] |
| 289 |
start = line * self._nl |
| 290 |
for n in range(start, start + self._nl): |
| 291 |
if n >= number: |
| 292 |
continue |
| 293 |
nick = self._ordered[n] |
| 294 |
tests.append(self._by_nick[nick]) |
| 295 |
yield tests |
| 296 |
|
| 297 |
def listing(self): |
| 298 |
sys.stdout.write('\n') |
| 299 |
sys.stdout.write('The available tests are:\n') |
| 300 |
sys.stdout.write('\n') |
| 301 |
for tests in self._iterate(): |
| 302 |
for test in tests: |
| 303 |
sys.stdout.write(f' {test.nick:2} {test.name:25}') |
| 304 |
sys.stdout.write('\n') |
| 305 |
sys.stdout.write('\n') |
| 306 |
sys.stdout.flush() |
| 307 |
|
| 308 |
def short_list(self): |
| 309 |
sys.stdout.write(' '.join([test.nick for test in self.registered()])) |
| 310 |
sys.stdout.write('\n') |
| 311 |
sys.stdout.flush() |
| 312 |
|
| 313 |
def display(self): |
| 314 |
for test in self.registered(): |
| 315 |
sys.stdout.write(' %s' % test.colored()) |
| 316 |
sys.stdout.write('%s\r' % color(0, 0)) |
| 317 |
sys.stdout.flush() |
| 318 |
|
| 319 |
|
| 320 |
def parse_msg_options(msg_file): |
| 321 |
"""Parse option directives from .msg file""" |
| 322 |
tcp_connections = None |
| 323 |
try: |
| 324 |
with open(msg_file, 'r') as f: |
| 325 |
for line in f: |
| 326 |
line = line.strip() |
| 327 |
if line.startswith('option:tcp_connections:'): |
| 328 |
tcp_connections = int(line.split(':')[-1]) |
| 329 |
break |
| 330 |
except Exception: |
| 331 |
pass |
| 332 |
return tcp_connections |
| 333 |
|
| 334 |
|
| 335 |
class EncodingTests(Tests): |
| 336 |
class Test(Record, Exec): |
| 337 |
def __init__(self, nick, name): |
| 338 |
Record.__init__(self, nick, name) |
| 339 |
Exec.__init__(self) |
| 340 |
self._check = b'successful' |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
def success(self): |
| 352 |
self.collect() |
| 353 |
if self.code == 0: |
| 354 |
if self._check in self.stdout: |
| 355 |
if os.getenv('DEBUG', None) is not None: |
| 356 |
self.report('completed successfully') |
| 357 |
return True |
| 358 |
if self._check in self.stderr: |
| 359 |
if os.getenv('DEBUG', None) is not None: |
| 360 |
self.report('completed successfully') |
| 361 |
return True |
| 362 |
|
| 363 |
self.report(f'failed with code {self.code}') |
| 364 |
return False |
| 365 |
|
| 366 |
API = re.compile(r'^\s*run\s+(.*)\s*?;\s*?$') |
| 367 |
|
| 368 |
def __init__(self): |
| 369 |
super().__init__(self.Test) |
| 370 |
|
| 371 |
for filename in Path.ALL_ENCODING: |
| 372 |
name = os.path.basename(filename)[:-3] |
| 373 |
test = self.new(name) |
| 374 |
with open(filename, 'r') as reader: |
| 375 |
content = reader.readline() |
| 376 |
test.conf['confs'] = [Path.etc(_) for _ in content.split()] |
| 377 |
test.conf['ci'] = Path.ci(name, 'ci') |
| 378 |
test.conf['msg'] = Path.ci(name, 'msg') |
| 379 |
test.conf['port'] = Port.get() |
| 380 |
test.files.extend(test.conf['confs']) |
| 381 |
test.files.append(test.conf['ci']) |
| 382 |
test.files.append(test.conf['msg']) |
| 383 |
|
| 384 |
for f in test.conf['confs']: |
| 385 |
with open(f) as reader: |
| 386 |
for line in reader: |
| 387 |
found = self.API.match(line) |
| 388 |
if not found: |
| 389 |
continue |
| 390 |
name = found.group(1) |
| 391 |
if not name.startswith('/'): |
| 392 |
name = Path.etc(name) |
| 393 |
if name not in test.files: |
| 394 |
test.files.append(name) |
| 395 |
|
| 396 |
def explain(self, nick): |
| 397 |
template = '\n' |
| 398 |
template += 'exabgp\n' |
| 399 |
template += '-' * 55 + '\n\n' |
| 400 |
template += '%(client)s\n\n\n' |
| 401 |
template += 'bgp deamon\n' |
| 402 |
template += '-' * 55 + '\n\n' |
| 403 |
template += '%(server)s\n\n\n' |
| 404 |
template += 'The following extra configuration options could be used\n' |
| 405 |
template += '-' * 55 + '\n\n' |
| 406 |
template += 'export exabgp_debug_rotate=true\n' |
| 407 |
template += 'export exabgp_debug_defensive=true\n' |
| 408 |
|
| 409 |
flush( |
| 410 |
template |
| 411 |
% { |
| 412 |
'client': self.client(nick), |
| 413 |
'server': self.server(nick), |
| 414 |
} |
| 415 |
) |
| 416 |
|
| 417 |
def client(self, nick): |
| 418 |
if not self.enable_by_nick(nick): |
| 419 |
sys.exit('no such test') |
| 420 |
test = self.get_by_nick(nick) |
| 421 |
|
| 422 |
|
| 423 |
tcp_connections = parse_msg_options(test.conf['msg']) or 1 |
| 424 |
|
| 425 |
config = { |
| 426 |
'env': ' \\\n '.join( |
| 427 |
[ |
| 428 |
'exabgp_version=5.0.0-0+test', |
| 429 |
f'exabgp_tcp_connections={tcp_connections}', |
| 430 |
'exabgp_api_cli=false', |
| 431 |
'exabgp_debug_rotate=true', |
| 432 |
'exabgp_debug_configuration=true', |
| 433 |
"exabgp_tcp_bind=''", |
| 434 |
'exabgp_tcp_port=%d' % test.conf['port'], |
| 435 |
'INTERPRETER=%s' % INTERPRETER, |
| 436 |
] |
| 437 |
), |
| 438 |
'exabgp': Path.EXABGP, |
| 439 |
'confs': ' \\\n '.join(test.conf['confs']), |
| 440 |
} |
| 441 |
return 'env \\\n %(env)s \\\n %(exabgp)s -d -p \\\n %(confs)s' % config |
| 442 |
|
| 443 |
def server(self, nick): |
| 444 |
if not self.enable_by_nick(nick): |
| 445 |
sys.exit('no such test') |
| 446 |
test = self.get_by_nick(nick) |
| 447 |
|
| 448 |
config = { |
| 449 |
'env': ' \\\n '.join( |
| 450 |
[ |
| 451 |
'exabgp_tcp_port=%d' % test.conf['port'], |
| 452 |
] |
| 453 |
), |
| 454 |
'interpreter': INTERPRETER, |
| 455 |
'bgp': Path.BGP, |
| 456 |
'msg': test.conf['msg'], |
| 457 |
} |
| 458 |
|
| 459 |
return 'env \\\n %(env)s \\\n %(interpreter)s %(bgp)s --view \\\n %(msg)s' % config |
| 460 |
|
| 461 |
def dry(self): |
| 462 |
result = [] |
| 463 |
for test in self.selected(): |
| 464 |
result.append( |
| 465 |
' '.join( |
| 466 |
[ |
| 467 |
'>', |
| 468 |
sys.argv[0], |
| 469 |
'encoding', |
| 470 |
'--server', |
| 471 |
test.nick, |
| 472 |
'--port', |
| 473 |
f'{test.conf["port"]}', |
| 474 |
] |
| 475 |
) |
| 476 |
) |
| 477 |
result.append( |
| 478 |
' '.join( |
| 479 |
[ |
| 480 |
'>', |
| 481 |
sys.argv[0], |
| 482 |
'encoding', |
| 483 |
'--client', |
| 484 |
test.nick, |
| 485 |
'--port', |
| 486 |
f'{test.conf["port"]}', |
| 487 |
] |
| 488 |
) |
| 489 |
) |
| 490 |
return '\n'.join(result) |
| 491 |
|
| 492 |
def run_selected(self, timeout): |
| 493 |
success = True |
| 494 |
client = dict() |
| 495 |
|
| 496 |
for test in self.selected(): |
| 497 |
test.setup() |
| 498 |
self.display() |
| 499 |
test.run( |
| 500 |
[ |
| 501 |
sys.argv[0], |
| 502 |
'encoding', |
| 503 |
'--server', |
| 504 |
test.nick, |
| 505 |
'--port', |
| 506 |
f'{test.conf["port"]}', |
| 507 |
] |
| 508 |
) |
| 509 |
time.sleep(0.005) |
| 510 |
|
| 511 |
time.sleep(0.02) |
| 512 |
|
| 513 |
for test in self.selected(): |
| 514 |
test.setup() |
| 515 |
self.display() |
| 516 |
client[test.nick] = Exec().run( |
| 517 |
[ |
| 518 |
sys.argv[0], |
| 519 |
'encoding', |
| 520 |
'--client', |
| 521 |
test.nick, |
| 522 |
'--port', |
| 523 |
f'{test.conf["port"]}', |
| 524 |
] |
| 525 |
) |
| 526 |
time.sleep(0.005) |
| 527 |
|
| 528 |
exit_time = time.time() + timeout |
| 529 |
|
| 530 |
running = set(self.selected()) |
| 531 |
|
| 532 |
while running and time.time() < exit_time: |
| 533 |
self.display() |
| 534 |
for test in list(running): |
| 535 |
if not test.ready(): |
| 536 |
continue |
| 537 |
if not client[test.nick].ready(): |
| 538 |
continue |
| 539 |
running.remove(test) |
| 540 |
client[test.nick].terminate() |
| 541 |
success = test.result(test.success()) and success |
| 542 |
self.display() |
| 543 |
time.sleep(0.1) |
| 544 |
|
| 545 |
for test in running: |
| 546 |
test.fail() |
| 547 |
test.terminate() |
| 548 |
success = False |
| 549 |
|
| 550 |
self.display() |
| 551 |
return success |
| 552 |
|
| 553 |
|
| 554 |
class DecodingTests(Tests): |
| 555 |
class Test(Record, Exec): |
| 556 |
def __init__(self, nick, name): |
| 557 |
Record.__init__(self, nick, name) |
| 558 |
Exec.__init__(self) |
| 559 |
|
| 560 |
def _cleanup(self, decoded): |
| 561 |
decoded.pop('exabgp', None) |
| 562 |
decoded.pop('host', None) |
| 563 |
decoded.pop('pid', None) |
| 564 |
decoded.pop('ppid', None) |
| 565 |
decoded.pop('time', None) |
| 566 |
decoded.pop('version', None) |
| 567 |
return decoded |
| 568 |
|
| 569 |
def success(self): |
| 570 |
self.collect() |
| 571 |
if self.stderr: |
| 572 |
self.report('stderr is \n' + self.stderr.decode()) |
| 573 |
return False |
| 574 |
if not self.stdout: |
| 575 |
self.report('no stdout received') |
| 576 |
return False |
| 577 |
try: |
| 578 |
decoded = json.loads(self.stdout) |
| 579 |
self._cleanup(decoded) |
| 580 |
except Exception: |
| 581 |
self.report('issue, report to decode the JSON') |
| 582 |
return False |
| 583 |
if decoded != self.conf['json']: |
| 584 |
from pprint import pformat |
| 585 |
|
| 586 |
failure = 'issue, JSON does not match' |
| 587 |
failure += f'\ndecoded : {pformat(decoded)}\n' |
| 588 |
failure += f'\nexpected: {pformat(self.conf["json"])}' |
| 589 |
self.report(failure) |
| 590 |
return False |
| 591 |
return True |
| 592 |
|
| 593 |
def __init__(self): |
| 594 |
super().__init__(self.Test) |
| 595 |
|
| 596 |
for filename in Path.ALL_DECODING: |
| 597 |
name = os.path.basename(filename).split('.')[0] |
| 598 |
test = self.new(name) |
| 599 |
with open(filename, 'r') as reader: |
| 600 |
words = reader.readline().split() |
| 601 |
test.conf['type'] = words[0] |
| 602 |
test.conf['family'] = '' if words[0] == 'open' else f'{words[1]} {words[2]}' |
| 603 |
packet = reader.readline().replace(' ', '').strip() |
| 604 |
test.conf['packet'] = packet |
| 605 |
expected = reader.readline().strip() |
| 606 |
decoded = json.loads(expected) |
| 607 |
test.conf['json'] = test._cleanup(decoded) |
| 608 |
test.files.append(filename) |
| 609 |
|
| 610 |
def listing(self): |
| 611 |
sys.stdout.write('\n') |
| 612 |
sys.stdout.write('The available tests are:\n') |
| 613 |
sys.stdout.write('\n') |
| 614 |
for tests in self._iterate(): |
| 615 |
for test in tests: |
| 616 |
sys.stdout.write(f' {test.nick:2} {test.name:25}') |
| 617 |
sys.stdout.write('\n') |
| 618 |
sys.stdout.write('\n') |
| 619 |
sys.stdout.flush() |
| 620 |
|
| 621 |
def dry(self): |
| 622 |
result = [] |
| 623 |
for test in self.selected(): |
| 624 |
result.append( |
| 625 |
' '.join( |
| 626 |
[ |
| 627 |
'>', |
| 628 |
Path.EXABGP, |
| 629 |
'decode', |
| 630 |
f"'-f {test.conf['family']}'" if test.conf['family'] else '', |
| 631 |
'--%s' % test.conf['type'], |
| 632 |
test.conf['packet'], |
| 633 |
] |
| 634 |
) |
| 635 |
) |
| 636 |
return '\n'.join(result) |
| 637 |
|
| 638 |
def run_selected(self, timeout): |
| 639 |
success = True |
| 640 |
for test in self.selected(): |
| 641 |
test.running() |
| 642 |
self.display() |
| 643 |
message = test.conf['type'] |
| 644 |
if message == 'open': |
| 645 |
cmd = [ |
| 646 |
Path.EXABGP, |
| 647 |
'decode', |
| 648 |
'--%s' % test.conf['type'], |
| 649 |
test.conf['packet'], |
| 650 |
] |
| 651 |
elif message in ['update', 'nlri']: |
| 652 |
cmd = [ |
| 653 |
Path.EXABGP, |
| 654 |
'decode', |
| 655 |
'-f', |
| 656 |
test.conf['family'], |
| 657 |
'--%s' % test.conf['type'], |
| 658 |
test.conf['packet'], |
| 659 |
] |
| 660 |
else: |
| 661 |
raise ValueError(f'invalid message type: {message}') |
| 662 |
test.run(cmd) |
| 663 |
|
| 664 |
for test in self.selected(): |
| 665 |
self.display() |
| 666 |
success = test.result(test.success()) and success |
| 667 |
time.sleep(0.05) |
| 668 |
|
| 669 |
exit_time = time.time() + timeout |
| 670 |
running = set(self.selected()) |
| 671 |
|
| 672 |
while running and time.time() < exit_time: |
| 673 |
self.display() |
| 674 |
for test in list(running): |
| 675 |
if not test.ready(): |
| 676 |
continue |
| 677 |
running.remove(test) |
| 678 |
success = test.result(test.success()) and success |
| 679 |
self.display() |
| 680 |
time.sleep(0.1) |
| 681 |
|
| 682 |
for test in running: |
| 683 |
test.fail() |
| 684 |
test.terminate() |
| 685 |
success = False |
| 686 |
|
| 687 |
self.display() |
| 688 |
return success |
| 689 |
|
| 690 |
|
| 691 |
class ParsingTests(Tests): |
| 692 |
class Test(Record, Exec): |
| 693 |
def __init__(self, nick, name): |
| 694 |
Record.__init__(self, nick, name) |
| 695 |
Exec.__init__(self) |
| 696 |
|
| 697 |
def success(self): |
| 698 |
self.collect() |
| 699 |
if self.code != 0: |
| 700 |
self.report('return code is not zero') |
| 701 |
return False |
| 702 |
|
| 703 |
return self.code == 0 |
| 704 |
|
| 705 |
def __init__(self): |
| 706 |
super().__init__(self.Test) |
| 707 |
|
| 708 |
for filename in Path.ALL_ETC: |
| 709 |
name = os.path.basename(filename).split('.')[0] |
| 710 |
test = self.new(name) |
| 711 |
test.conf['fname'] = filename |
| 712 |
test.files.append(filename) |
| 713 |
|
| 714 |
def listing(self): |
| 715 |
sys.stdout.write('\n') |
| 716 |
sys.stdout.write('The available tests are:\n') |
| 717 |
sys.stdout.write('\n') |
| 718 |
for tests in self._iterate(): |
| 719 |
for test in tests: |
| 720 |
sys.stdout.write(f' {test.nick:2} {test.name:25}') |
| 721 |
sys.stdout.write('\n') |
| 722 |
sys.stdout.write('\n') |
| 723 |
sys.stdout.flush() |
| 724 |
|
| 725 |
def dry(self): |
| 726 |
result = [] |
| 727 |
for test in self.selected(): |
| 728 |
result.append(' '.join(['>', Path.EXABGP, 'validate', '-nrv', test.conf['fname']])) |
| 729 |
return '\n'.join(result) |
| 730 |
|
| 731 |
def run_selected(self, timeout): |
| 732 |
success = True |
| 733 |
|
| 734 |
for test in self.selected(): |
| 735 |
test.running() |
| 736 |
test.run([Path.EXABGP, 'validate', '-nrv', test.conf['fname']]) |
| 737 |
time.sleep(0.005) |
| 738 |
|
| 739 |
time.sleep(0.02) |
| 740 |
|
| 741 |
for test in self.selected(): |
| 742 |
success = test.result(test.success()) and success |
| 743 |
time.sleep(0.005) |
| 744 |
|
| 745 |
exit_time = time.time() + timeout |
| 746 |
running = set(self.selected()) |
| 747 |
|
| 748 |
while running and time.time() < exit_time: |
| 749 |
for test in list(running): |
| 750 |
if not test.ready(): |
| 751 |
continue |
| 752 |
running.remove(test) |
| 753 |
success = test.result(test.success()) and success |
| 754 |
self.display() |
| 755 |
time.sleep(0.1) |
| 756 |
|
| 757 |
for test in running: |
| 758 |
test.fail() |
| 759 |
test.terminate() |
| 760 |
success = False |
| 761 |
|
| 762 |
self.display() |
| 763 |
return success |
| 764 |
|
| 765 |
|
| 766 |
def add_test(subparser, name, tests, extra): |
| 767 |
sub = subparser.add_parser(name, help=f'run {name} test') |
| 768 |
if 'dry' in extra: |
| 769 |
sub.add_argument('--dry', help='show the action', action='store_true') |
| 770 |
if 'server' in extra: |
| 771 |
sub.add_argument('--server', help='start the server for a test', action='store_true') |
| 772 |
if 'client' in extra: |
| 773 |
sub.add_argument('--client', help='start the client for a test', action='store_true') |
| 774 |
if 'list' in extra: |
| 775 |
sub.add_argument('--list', help='list the files making a test', action='store_true') |
| 776 |
if 'short_list' in extra: |
| 777 |
sub.add_argument('--short-list', help='list test codes only (space separated)', action='store_true') |
| 778 |
if 'edit' in extra: |
| 779 |
sub.add_argument('--edit', help='edit the files making a test', action='store_true') |
| 780 |
if 'timeout' in extra: |
| 781 |
sub.add_argument('--timeout', help='timeout for test failure', type=int, default=60) |
| 782 |
if 'port' in extra: |
| 783 |
sub.add_argument('--port', help='base port to use', type=int, default=1790) |
| 784 |
sub.add_argument('test', help='name of the test to run', nargs='?', default=None) |
| 785 |
|
| 786 |
def func(parsed): |
| 787 |
if 'edit' in extra and parsed.edit: |
| 788 |
if not tests.enable_by_nick(parsed.test): |
| 789 |
sys.exit('no such test') |
| 790 |
test = tests.get_by_nick(parsed.test) |
| 791 |
if not test.files: |
| 792 |
sys.exit('no file for the test') |
| 793 |
editor = os.environ.get('EDITOR', 'vi') |
| 794 |
command = '%s %s' % (editor, ' '.join(test.files)) |
| 795 |
flush(f'> {command}') |
| 796 |
if not parsed.dry: |
| 797 |
sys.exit(os.system(command)) |
| 798 |
return |
| 799 |
|
| 800 |
if 'list' in extra and parsed.list: |
| 801 |
tests.listing() |
| 802 |
return |
| 803 |
|
| 804 |
if 'short_list' in extra and parsed.short_list: |
| 805 |
tests.short_list() |
| 806 |
return |
| 807 |
|
| 808 |
if 'client' in extra and parsed.client: |
| 809 |
command = tests.client(parsed.test) |
| 810 |
flush(f'client> {command}') |
| 811 |
if not parsed.dry: |
| 812 |
sys.exit(os.system(command)) |
| 813 |
return |
| 814 |
|
| 815 |
if 'server' in extra and parsed.server: |
| 816 |
command = tests.server(parsed.test) |
| 817 |
flush(f'server> {command}') |
| 818 |
if not parsed.dry: |
| 819 |
sys.exit(os.system(command)) |
| 820 |
return |
| 821 |
|
| 822 |
if 'timeout' not in parsed: |
| 823 |
parsed.timeout = 0 |
| 824 |
|
| 825 |
if parsed.test: |
| 826 |
if not tests.enable_by_nick(parsed.test): |
| 827 |
sys.exit(f'could not find test {parsed.test}') |
| 828 |
else: |
| 829 |
tests.enable_all() |
| 830 |
|
| 831 |
if parsed.dry: |
| 832 |
command = tests.dry() |
| 833 |
flush(command) |
| 834 |
sys.exit(0) |
| 835 |
|
| 836 |
exit = tests.run_selected(parsed.timeout) |
| 837 |
sys.stdout.write('\n') |
| 838 |
sys.exit(0 if exit else 1) |
| 839 |
|
| 840 |
sub.set_defaults(func=func) |
| 841 |
|
| 842 |
|
| 843 |
if __name__ == '__main__': |
| 844 |
Path.validate() |
| 845 |
Path.setup_env() |
| 846 |
|
| 847 |
decoding = DecodingTests() |
| 848 |
encoding = EncodingTests() |
| 849 |
parsing = ParsingTests() |
| 850 |
|
| 851 |
parser = argparse.ArgumentParser(description='The BGP swiss army knife of networking functional testing tool') |
| 852 |
subparser = parser.add_subparsers() |
| 853 |
|
| 854 |
add_test(subparser, 'decoding', decoding, ['list', 'short_list', 'edit', 'dry', 'timeout', 'port']) |
| 855 |
add_test( |
| 856 |
subparser, |
| 857 |
'encoding', |
| 858 |
encoding, |
| 859 |
['list', 'short_list', 'edit', 'dry', 'timeout', 'port', 'server', 'client'], |
| 860 |
) |
| 861 |
add_test(subparser, 'parsing', parsing, ['list', 'short_list', 'dry', 'edit']) |
| 862 |
|
| 863 |
parsed = parser.parse_args() |
| 864 |
if vars(parsed): |
| 865 |
parsed.func(parsed) |
| 866 |
else: |
| 867 |
parser.print_help() |