Skip to content

Commit

Permalink
run black
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-mangin committed Jul 14, 2024
1 parent 7f888f8 commit 820e3d3
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 87 deletions.
112 changes: 51 additions & 61 deletions qa/bin/functional
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def alarm_handler(number, frame): # pylint: disable=W0613
def color(prefix, suffix):
def code(value):
return f'\033[{value}m'

return code(prefix) + code(suffix)


Expand Down Expand Up @@ -170,11 +171,7 @@ State = Enum('State', 'NONE STARTING RUNNING FAIL SUCCESS SKIP')

class Record:
_index = 0
_listing = \
'0123456789' + \
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + \
'abcdefghijklmnopqrstuvwxyz' + \
'αβγδεζηθικλμνξοπρςστυφχψω'
_listing = '0123456789' + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + 'abcdefghijklmnopqrstuvwxyz' + 'αβγδεζηθικλμνξοπρςστυφχψω'

def __init__(self, nick, name):
self.nick = nick
Expand Down Expand Up @@ -229,9 +226,9 @@ class Record:
if self.state == State.FAIL:
return color(0, 91) + self.nick # RED
if self.state == State.SUCCESS:
return color(1, 92) + '✓' # GREEN
return color(1, 92) + '✓' # GREEN
if self.state == State.SKIP:
return color(0, 34) + '✖' # BLUE
return color(0, 34) + '✖' # BLUE

def result(self, success):
if success:
Expand Down Expand Up @@ -278,10 +275,10 @@ class Tests:
number = len(self._ordered)
lines = number // self._nl

for line in range(0, lines+1):
for line in range(0, lines + 1):
tests = []
start = line*self._nl
for n in range(start, start+self._nl):
start = line * self._nl
for n in range(start, start + self._nl):
if n >= number:
continue
nick = self._ordered[n]
Expand Down Expand Up @@ -318,7 +315,7 @@ class EncodingTests(Tests):

# def __lt__ (self, other):
# return self.nick.__lt__(other.nick)

# def __hash__ (self):
# return self.nick.__hash__()

Expand Down Expand Up @@ -370,20 +367,23 @@ class EncodingTests(Tests):
def explain(self, nick):
template = '\n'
template += 'exabgp\n'
template += '-'*55 + '\n\n'
template += '-' * 55 + '\n\n'
template += '%(client)s\n\n\n'
template += 'bgp deamon\n'
template += '-'*55 + '\n\n'
template += '-' * 55 + '\n\n'
template += '%(server)s\n\n\n'
template += 'The following extra configuration options could be used\n'
template += '-'*55 + '\n\n'
template += '-' * 55 + '\n\n'
template += 'export exabgp_debug_rotate=true\n'
template += 'export exabgp_debug_defensive=true\n'

print(template % {
'client': self.client(nick),
'server': self.server(nick),
})
print(
template
% {
'client': self.client(nick),
'server': self.server(nick),
}
)

def client(self, nick):
if not self.enable_by_nick(nick):
Expand Down Expand Up @@ -413,7 +413,11 @@ class EncodingTests(Tests):
test = self.get_by_nick(nick)

config = {
'env': ' \\\n '.join(['exabgp_tcp_port=%d' % test.conf['port'],]),
'env': ' \\\n '.join(
[
'exabgp_tcp_port=%d' % test.conf['port'],
]
),
'interpreter': INTERPRETER,
'bgp': Path.BGP,
'msg': test.conf['msg'],
Expand All @@ -424,18 +428,12 @@ class EncodingTests(Tests):
def dry(self):
result = []
for test in self.selected():
result.append(' '.join([
'>',
sys.argv[0],
'encoding', '--server', test.nick,
'--port', f'{test.conf["port"]}'
]))
result.append(' '.join([
'>',
sys.argv[0],
'encoding', '--client', test.nick,
'--port', f'{test.conf["port"]}'
]))
result.append(
' '.join(['>', sys.argv[0], 'encoding', '--server', test.nick, '--port', f'{test.conf["port"]}'])
)
result.append(
' '.join(['>', sys.argv[0], 'encoding', '--client', test.nick, '--port', f'{test.conf["port"]}'])
)
return '\n'.join(result)

def run_selected(self, timeout):
Expand All @@ -445,23 +443,17 @@ class EncodingTests(Tests):
for test in self.selected():
test.setup()
self.display()
test.run([
sys.argv[0],
'encoding', '--server', test.nick,
'--port', f'{test.conf["port"]}'
])
test.run([sys.argv[0], 'encoding', '--server', test.nick, '--port', f'{test.conf["port"]}'])
time.sleep(0.005)

time.sleep(0.02)

for test in self.selected():
test.setup()
self.display()
client[test.nick] = Exec().run([
sys.argv[0],
'encoding', '--client', test.nick,
'--port', f'{test.conf["port"]}'
])
client[test.nick] = Exec().run(
[sys.argv[0], 'encoding', '--client', test.nick, '--port', f'{test.conf["port"]}']
)
time.sleep(0.005)

exit_time = time.time() + timeout
Expand Down Expand Up @@ -520,6 +512,7 @@ class DecodingTests(Tests):
return False
if decoded != self.conf['json']:
from pprint import pformat

failure = 'issue, JSON does not match'
failure += f'\ndecoded : {pformat(decoded)}\n'
failure += f'\nexpected: {pformat(self.conf["json"])}'
Expand Down Expand Up @@ -558,12 +551,19 @@ class DecodingTests(Tests):
def dry(self):
result = []
for test in self.selected():
result.append(' '.join([
'>',
Path.EXABGP,
'decode', '-f', f"'{test.conf['family']}'", test.conf['type'],
test.conf['packet']
]))
result.append(
' '.join(
[
'>',
Path.EXABGP,
'decode',
'-f',
f"'{test.conf['family']}'",
test.conf['type'],
test.conf['packet'],
]
)
)
return '\n'.join(result)

def run_selected(self, timeout):
Expand All @@ -572,11 +572,7 @@ class DecodingTests(Tests):
for test in self.selected():
test.running()
self.display()
test.run([
Path.EXABGP,
'decode', '-f', test.conf['family'], test.conf['type'],
test.conf['packet']
])
test.run([Path.EXABGP, 'decode', '-f', test.conf['family'], test.conf['type'], test.conf['packet']])

for test in self.selected():
self.display()
Expand Down Expand Up @@ -641,22 +637,15 @@ class ParsingTests(Tests):
def dry(self):
result = []
for test in self.selected():
result.append(' '.join([
'>',
Path.EXABGP,
'validate', '-nrv', test.conf['fname']
]))
result.append(' '.join(['>', Path.EXABGP, 'validate', '-nrv', test.conf['fname']]))
return '\n'.join(result)

def run_selected(self, timeout):
success = True

for test in self.selected():
test.running()
test.run([
Path.EXABGP,
'validate', '-nrv', test.conf['fname']
])
test.run([Path.EXABGP, 'validate', '-nrv', test.conf['fname']])
time.sleep(0.005)

time.sleep(0.02)
Expand Down Expand Up @@ -684,6 +673,7 @@ class ParsingTests(Tests):
self.display()
return success


def add_test(subparser, name, tests, extra):
sub = subparser.add_parser(name, help=f'run {name} test')
if 'dry' in extra:
Expand Down
54 changes: 28 additions & 26 deletions qa/sbin/bgp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def flushed(output):


def indent(msg, indent=12):
sys.stdout.write(' '*indent)
sys.stdout.write(' ' * indent)
flushed(msg)


Expand Down Expand Up @@ -68,9 +68,7 @@ def print_payload(prefix, header, body):


def print_rule(prefix, rule):
flushed(
f'{prefix:12}{rule[:32]}:{rule[32:36]}:{rule[36:38]}:{rule[38:]}'
)
flushed(f'{prefix:12}{rule[:32]}:{rule[32:36]}:{rule[36:38]}:{rule[38:]}')


def dump(value):
Expand Down Expand Up @@ -170,22 +168,31 @@ class Message:
@staticmethod
def keepalive():
return bytearray(
[0xFF, ] * 16
[
0xFF,
]
* 16
+ [0x0, 0x13, 0x4]
)

@staticmethod
def eor():
return bytearray(
[0xFF, ] * 16
+ [0x0, 0x17, 0x02] +
+ [0x00, 0x00, 0x00, 0x00]
[
0xFF,
]
* 16
+ [0x0, 0x17, 0x02]
+ +[0x00, 0x00, 0x00, 0x00]
)

@staticmethod
def default_route():
return bytearray(
[0xFF, ] * 16
[
0xFF,
]
* 16
+ [0x00, 0x31]
+ [
0x02,
Expand All @@ -206,7 +213,10 @@ class Message:
@staticmethod
def notify(notification):
return bytearray(
[0xFF, ] * 16
[
0xFF,
]
* 16
+ [0x00, 19 + 2 + len(notification)]
+ [0x03]
+ [0x06]
Expand Down Expand Up @@ -354,14 +364,7 @@ class Message:
offset += param_len + 2

# No "Support for 4 octet AS number capability" found simply replace the 16-bit ASN number field.
open = (
self.header
+ self.body[0:1]
+ pack("!H", asn)
+ self.body[3:8]
+ pack("!H", byte_id)[1:]
+ self.body[9:]
)
open = self.header + self.body[0:1] + pack("!H", asn) + self.body[3:8] + pack("!H", byte_id)[1:] + self.body[9:]
return self._add_capa66(add_capa66, open)


Expand Down Expand Up @@ -422,7 +425,7 @@ class Checker(object):
conn = 'A'
seq = int(prefix)

raw = (encoding == 'raw')
raw = encoding == 'raw'
self.raw = self.raw or raw
if raw:
content = content.replace(':', '')
Expand All @@ -443,7 +446,7 @@ class Checker(object):
return False
if not self.sequences:
return False

self.messages = self.sequences.pop(0)
return self

Expand Down Expand Up @@ -499,7 +502,7 @@ class Checker(object):
return True
flushed('received extra message')
# FIXME: change API to not have to convert
print_payload('additional', msg_bytes(received[:19*2]), msg_bytes(received[19*2:]))
print_payload('additional', msg_bytes(received[: 19 * 2]), msg_bytes(received[19 * 2 :]))
return False

for check in self.messages:
Expand All @@ -516,7 +519,7 @@ class Checker(object):
flushed('')
flushed('unexpected message:')
if self.raw:
print_payload('received', msg_bytes(received[:19*2]), msg_bytes(received[19*2:]))
print_payload('received', msg_bytes(received[: 19 * 2]), msg_bytes(received[19 * 2 :]))
else:
print_prefixed('received', received)

Expand Down Expand Up @@ -567,9 +570,9 @@ class BGPProtocol(asyncio.Protocol):
except ConnectionResetError:
if self.service.checker.completed():
self.service.exit(0)
return '',''
return '', ''
if not header:
return '',''
return '', ''
length = unpack('!H', header[16:18])[0]
body = await self.reader.read(length - 19)
return header, body
Expand Down Expand Up @@ -778,8 +781,7 @@ async def main(options, checker, queue):
service = BGPService(loop, queue, options, checker)

server = await asyncio.start_server(
lambda reader, writer: BGPProtocol(service, reader, writer).handle_bgp(),
sock=sock
lambda reader, writer: BGPProtocol(service, reader, writer).handle_bgp(), sock=sock
)
# perhaps set backlog to 1 ..

Expand Down

0 comments on commit 820e3d3

Please sign in to comment.