Commit e38ffa98 authored by Pedro Oliveira's avatar Pedro Oliveira

fix quagga end blocking & server log classes

parent f5fb3c58
import pickle
import logging
import logging.handlers
import socketserver
import struct
from TestAssert import Test1, Test2
import sys
from threading import Lock
class LogRecordStreamHandler(socketserver.StreamRequestHandler):
"""Handler for a streaming logging request.
This basically logs the record using whatever logging policy is
configured locally.
"""
currentTest = Test1()
nextTests = [Test2()]
lock = Lock()
main = None
def handle(self):
"""
Handle multiple requests - each expected to be a 4-byte length,
followed by the LogRecord in pickle format. Logs the record
according to whatever policy is configured locally.
"""
logging.FileHandler('server.log').setLevel(logging.DEBUG)
while True:
chunk = self.connection.recv(4)
if len(chunk) < 4:
break
slen = struct.unpack('>L', chunk)[0]
chunk = self.connection.recv(slen)
while len(chunk) < slen:
chunk = chunk + self.connection.recv(slen - len(chunk))
obj = self.unPickle(chunk)
record = logging.makeLogRecord(obj)
self.handleLogRecord(record)
def unPickle(self, data):
return pickle.loads(data)
def handleLogRecord(self, record):
# if a name is specified, we use the named logger rather than the one
# implied by the record.
if self.server.logname is not None:
name = self.server.logname
else:
name = record.name
logger = logging.getLogger(name)
logger.addFilter(logging.Filter('pim'))
# N.B. EVERY record gets logged. This is because Logger.handle
# is normally called AFTER logger-level filtering. If you want
# to do filtering, do it at the client end to save wasting
# cycles and network bandwidth!
#if record.routername == "R2":
logger.handle(record)
with LogRecordStreamHandler.lock:
if LogRecordStreamHandler.currentTest and record.routername in ["R2","R3","R4","R5","R6"] and record.name in ("pim.KernelEntry.DownstreamInterface.Assert", "pim.KernelEntry.UpstreamInterface.Assert") and LogRecordStreamHandler.currentTest.test(record):
if len(LogRecordStreamHandler.nextTests) > 0:
LogRecordStreamHandler.currentTest = LogRecordStreamHandler.nextTests.pop(0)
if LogRecordStreamHandler.currentTest is None:
LogRecordStreamHandler.main.abort = 1
class LogRecordSocketReceiver(socketserver.ThreadingTCPServer):
"""
Simple TCP socket-based logging receiver suitable for testing.
"""
allow_reuse_address = True
def __init__(self, host='10.5.5.100',
port=logging.handlers.DEFAULT_TCP_LOGGING_PORT,
handler=LogRecordStreamHandler):
handler.main = self
socketserver.ThreadingTCPServer.__init__(self, (host, port), handler)
self.abort = 0
self.timeout = 1
self.logname = None
def serve_until_stopped(self):
import select
abort = 0
while not abort:
rd, wr, ex = select.select([self.socket.fileno()],
[], [],
self.timeout)
if rd:
self.handle_request()
abort = self.abort
def main():
logging.basicConfig(
format='%(relativeCreated)5d %(name)-50s %(levelname)-8s %(tree)-35s %(vif)-2s %(routername)-2s %(message)s',
)
#filename='example.log')
tcpserver = LogRecordSocketReceiver()
print('About to start TCP server...')
tcpserver.serve_until_stopped()
if __name__ == '__main__':
main()
\ No newline at end of file
import logging
class ContextFilter(logging.Filter):
"""
This is a filter which injects contextual information into the log.
Rather than use actual contextual information, we just use random
data in this demo.
"""
def __init__(self, tree, router_name):
super().__init__()
def filter(self, record):
return record.routername in ["R2","R3","R4","R5","R6"]
class Test1(logging.Filter):
expectedState = {"R2": "L",
"R3": "L",
"R4": "W",
"R5": "L",
"R6": "L",
}
Success = {"R2": False,
"R3": False,
"R4": False,
"R5": False,
"R6": False,
}
def __init__(self):
print("Test1: No info about (10.1.1.100,224.12.12.12)")
print("Expected: R4 WINNER")
super().__init__()
def test(self, record):
if record.routername not in self.expectedState:
return False
if record.msg == self.expectedState.get(record.routername):
self.Success[record.routername] = True
if sum(self.Success.values()) == len(self.Success):
# tudo certo
print("Test1 Success")
return True
return False
class Test2(logging.Filter):
expectedState = {"R2": "L",
"R3": "W",
"R5": "L",
"R6": "L",
}
Success = {"R2": False,
"R3": False,
"R5": False,
"R6": False,
}
def __init__(self):
print("Test2: Kill assert winner")
print("Expected: R3 WINNER")
super().__init__()
def test(self, record):
if record.routername not in self.expectedState:
return False
if record.msg == self.expectedState.get(record.routername):
self.Success[record.routername] = True
if sum(self.Success.values()) == len(self.Success):
# tudo certo
print("Test2 Success")
return True
return False
......@@ -16,4 +16,5 @@ iptables -t nat -A POSTROUTING -s 0.0.0.0/0 -o eth4 -j MASQUERADE
#apt-get update && apt-get --assume-yes install python3 python3-pip
echo VTYSH_PAGER=more > /etc/environment
......@@ -20,3 +20,5 @@ done
# install python
#apt-get update && apt-get --assume-yes install python3 python3-pip
echo VTYSH_PAGER=more > /etc/environment
......@@ -20,3 +20,5 @@ done
# install python
#apt-get update && apt-get --assume-yes install python3 python3-pip
echo VTYSH_PAGER=more > /etc/environment
......@@ -21,3 +21,5 @@ done
# install python
#apt-get update && apt-get --assume-yes install python3 python3-pip
echo VTYSH_PAGER=more > /etc/environment
......@@ -20,3 +20,5 @@ done
# install python
#apt-get update && apt-get --assume-yes install python3 python3-pip
echo VTYSH_PAGER=more > /etc/environment
......@@ -20,3 +20,5 @@ done
# install python
#apt-get update && apt-get --assume-yes install python3 python3-pip
echo VTYSH_PAGER=more > /etc/environment
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment