Commit 6b99c6d1 authored by Pedro Oliveira's avatar Pedro Oliveira

added 1 test & consider interface name in tests

parent e38ffa98
...@@ -3,19 +3,24 @@ import logging ...@@ -3,19 +3,24 @@ import logging
import logging.handlers import logging.handlers
import socketserver import socketserver
import struct import struct
from TestAssert import Test1, Test2 from TestAssert import Test1, Test2, Test3
import sys
from threading import Lock from threading import Lock
class CustomFilter(logging.Filter):
def filter(self, record):
return record.name in ("pim.KernelEntry.DownstreamInterface.Assert", "pim.KernelEntry.UpstreamInterface.Assert", "pim.KernelInterface")
class LogRecordStreamHandler(socketserver.StreamRequestHandler): class LogRecordStreamHandler(socketserver.StreamRequestHandler):
"""Handler for a streaming logging request. """Handler for a streaming logging request.
This basically logs the record using whatever logging policy is This basically logs the record using whatever logging policy is
configured locally. configured locally.
""" """
currentTest = Test1() currentTest = Test1()
nextTests = [Test2()] currentTest.print_test()
nextTests = [Test2(), Test3()]
lock = Lock() lock = Lock()
main = None main = None
...@@ -25,7 +30,7 @@ class LogRecordStreamHandler(socketserver.StreamRequestHandler): ...@@ -25,7 +30,7 @@ class LogRecordStreamHandler(socketserver.StreamRequestHandler):
followed by the LogRecord in pickle format. Logs the record followed by the LogRecord in pickle format. Logs the record
according to whatever policy is configured locally. according to whatever policy is configured locally.
""" """
logging.FileHandler('server.log').setLevel(logging.DEBUG) #logging.FileHandler('server.log').setLevel(logging.DEBUG)
while True: while True:
chunk = self.connection.recv(4) chunk = self.connection.recv(4)
if len(chunk) < 4: if len(chunk) < 4:
...@@ -49,7 +54,7 @@ class LogRecordStreamHandler(socketserver.StreamRequestHandler): ...@@ -49,7 +54,7 @@ class LogRecordStreamHandler(socketserver.StreamRequestHandler):
else: else:
name = record.name name = record.name
logger = logging.getLogger(name) logger = logging.getLogger(name)
logger.addFilter(logging.Filter('pim')) logger.addFilter(CustomFilter())
# N.B. EVERY record gets logged. This is because Logger.handle # N.B. EVERY record gets logged. This is because Logger.handle
# is normally called AFTER logger-level filtering. If you want # is normally called AFTER logger-level filtering. If you want
# to do filtering, do it at the client end to save wasting # to do filtering, do it at the client end to save wasting
...@@ -60,8 +65,10 @@ class LogRecordStreamHandler(socketserver.StreamRequestHandler): ...@@ -60,8 +65,10 @@ class LogRecordStreamHandler(socketserver.StreamRequestHandler):
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 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: if len(LogRecordStreamHandler.nextTests) > 0:
LogRecordStreamHandler.currentTest = LogRecordStreamHandler.nextTests.pop(0) LogRecordStreamHandler.currentTest = LogRecordStreamHandler.nextTests.pop(0)
if LogRecordStreamHandler.currentTest is None: LogRecordStreamHandler.currentTest.print_test()
LogRecordStreamHandler.main.abort = 1 else:
LogRecordStreamHandler.currentTest = None
LogRecordStreamHandler.main.abort = True
...@@ -73,7 +80,7 @@ class LogRecordSocketReceiver(socketserver.ThreadingTCPServer): ...@@ -73,7 +80,7 @@ class LogRecordSocketReceiver(socketserver.ThreadingTCPServer):
allow_reuse_address = True allow_reuse_address = True
def __init__(self, host='10.5.5.100', def __init__(self, host='localhost',
port=logging.handlers.DEFAULT_TCP_LOGGING_PORT, port=logging.handlers.DEFAULT_TCP_LOGGING_PORT,
handler=LogRecordStreamHandler): handler=LogRecordStreamHandler):
handler.main = self handler.main = self
...@@ -93,14 +100,16 @@ class LogRecordSocketReceiver(socketserver.ThreadingTCPServer): ...@@ -93,14 +100,16 @@ class LogRecordSocketReceiver(socketserver.ThreadingTCPServer):
self.handle_request() self.handle_request()
abort = self.abort abort = self.abort
def main(): def main():
logging.basicConfig( logging.basicConfig(
format='%(relativeCreated)5d %(name)-50s %(levelname)-8s %(tree)-35s %(vif)-2s %(routername)-2s %(message)s', format='%(name)-50s %(levelname)-8s %(tree)-35s %(vif)-2s %(interfacename)-5s %(routername)-2s %(message)s',
) )
#filename='example.log') #filename='example.log')
tcpserver = LogRecordSocketReceiver() tcpserver = LogRecordSocketReceiver(host='10.5.5.100')
print('About to start TCP server...') print('About to start TCP server...')
tcpserver.serve_until_stopped() tcpserver.serve_until_stopped()
if __name__ == '__main__': if __name__ == '__main__':
main() main()
\ No newline at end of file
import logging import logging
from abc import ABCMeta
class ContextFilter(logging.Filter): class ContextFilter(logging.Filter):
""" """
...@@ -7,74 +8,94 @@ class ContextFilter(logging.Filter): ...@@ -7,74 +8,94 @@ class ContextFilter(logging.Filter):
Rather than use actual contextual information, we just use random Rather than use actual contextual information, we just use random
data in this demo. data in this demo.
""" """
def __init__(self, tree, router_name): def __init__(self):
super().__init__() super().__init__()
def filter(self, record): def filter(self, record):
return record.routername in ["R2","R3","R4","R5","R6"] return record.routername in ["R2","R3","R4","R5","R6"]
class Test1(logging.Filter): class Test():
expectedState = {"R2": "L", __metaclass__ = ABCMeta
"R3": "L",
"R4": "W",
"R5": "L",
"R6": "L",
}
Success = {"R2": False,
"R3": False,
"R4": False,
"R5": False,
"R6": False,
}
def __init__(self): def __init__(self, testName, expectedState, success):
print("Test1: No info about (10.1.1.100,224.12.12.12)") self.testName = testName
print("Expected: R4 WINNER") self.expectedState = expectedState
super().__init__() self.success = success
def test(self, record): def test(self, record):
if record.routername not in self.expectedState: if record.routername not in self.expectedState:
return False return False
if record.msg == self.expectedState.get(record.routername): if record.msg == self.expectedState.get(record.routername).get(record.interfacename):
self.Success[record.routername] = True self.success[record.routername][record.interfacename] = True
if sum(self.Success.values()) == len(self.Success):
# tudo certo for interface_test in self.success.values():
print("Test1 Success") if False in interface_test.values():
return True return False
return False print('\x1b[1;32;40m' + self.testName + ' Success' + '\x1b[0m')
return True
class Test1(Test):
def __init__(self):
expectedState = {"R2": {"eth1": "L"},
"R3": {"eth1": "L"},
"R4": {"eth1": "W"},
"R5": {"eth0": "L"},
"R6": {"eth0": "L"},
}
success = {"R2": {"eth1": False},
"R3": {"eth1": False},
"R4": {"eth1": False},
"R5": {"eth0": False},
"R6": {"eth0": False},
}
super().__init__("Test1", expectedState, success)
def print_test(self):
print("Test1: No info about (10.1.1.100,224.12.12.12)")
print("Expected: R4 WINNER")
class Test2(logging.Filter):
expectedState = {"R2": "L",
"R3": "W",
"R5": "L",
"R6": "L",
}
Success = {"R2": False,
"R3": False,
"R5": False,
"R6": False,
}
class Test2(Test):
def __init__(self): def __init__(self):
expectedState = {"R2": {"eth1": "L"},
"R3": {"eth1": "W"},
"R5": {"eth0": "L"},
"R6": {"eth0": "L"},
}
success = {"R2": {"eth1": False},
"R3": {"eth1": False},
"R5": {"eth0": False},
"R6": {"eth0": False},
}
super().__init__("Test2", expectedState, success)
def print_test(self):
print("Test2: Kill assert winner") print("Test2: Kill assert winner")
print("Expected: R3 WINNER") print("Expected: R3 WINNER")
super().__init__()
def test(self, record):
if record.routername not in self.expectedState: class Test3(Test):
return False
if record.msg == self.expectedState.get(record.routername): def __init__(self):
self.Success[record.routername] = True expectedState = {"R2": {"eth1": "NI"},
if sum(self.Success.values()) == len(self.Success): "R3": {"eth1": "NI"},
# tudo certo }
print("Test2 Success")
return True success = {"R2": {"eth1": False},
return False "R3": {"eth1": False},
}
super().__init__("Test3", expectedState, success)
def print_test(self):
print("Test3: CouldAssert of AssertWinner(R3) -> False")
print("Expected: everyone NI")
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