diff --git a/emulation/client2/root/ServerLog.py b/emulation/client2/root/ServerLog.py new file mode 100644 index 0000000000000000000000000000000000000000..9af479502d4c2eba3eb3da4840d58909f7a74648 --- /dev/null +++ b/emulation/client2/root/ServerLog.py @@ -0,0 +1,106 @@ +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 diff --git a/emulation/client2/root/TestAssert.py b/emulation/client2/root/TestAssert.py new file mode 100644 index 0000000000000000000000000000000000000000..4641f1dc60a3f17c339922b4e6401d22ee066629 --- /dev/null +++ b/emulation/client2/root/TestAssert.py @@ -0,0 +1,80 @@ +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 diff --git a/emulation/router1.startup b/emulation/router1.startup index f6cac3bcbaca51a49179e26febde592d3b6f3996..79b421b4aa2580ad2b8619a3b87f138f42e9fe00 100644 --- a/emulation/router1.startup +++ b/emulation/router1.startup @@ -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 diff --git a/emulation/router2.startup b/emulation/router2.startup index 10c506671cc0ddc3a14b2723690341afcf41a4bd..cbd43d268d9843028590d88f8af52912b2aadd80 100644 --- a/emulation/router2.startup +++ b/emulation/router2.startup @@ -20,3 +20,5 @@ done # install python #apt-get update && apt-get --assume-yes install python3 python3-pip +echo VTYSH_PAGER=more > /etc/environment + diff --git a/emulation/router3.startup b/emulation/router3.startup index 6b4a236899d573166baa14840b25f6aaa64913b0..b721747cbc5485e6d9c2b8284d75dce0d7f9dc03 100644 --- a/emulation/router3.startup +++ b/emulation/router3.startup @@ -20,3 +20,5 @@ done # install python #apt-get update && apt-get --assume-yes install python3 python3-pip +echo VTYSH_PAGER=more > /etc/environment + diff --git a/emulation/router4.startup b/emulation/router4.startup index 5f6168a05f171de5e4f16cf68a90623d4e676a46..8341ae197ba219c62e16b0c347832a4e723f6fc1 100644 --- a/emulation/router4.startup +++ b/emulation/router4.startup @@ -21,3 +21,5 @@ done # install python #apt-get update && apt-get --assume-yes install python3 python3-pip +echo VTYSH_PAGER=more > /etc/environment + diff --git a/emulation/router5.startup b/emulation/router5.startup index 64ea73ef532b76bc132730d37b1c592e64f4150f..713bc8ddbcfe097d5f7c948ef7fde9f580d0f241 100644 --- a/emulation/router5.startup +++ b/emulation/router5.startup @@ -20,3 +20,5 @@ done # install python #apt-get update && apt-get --assume-yes install python3 python3-pip +echo VTYSH_PAGER=more > /etc/environment + diff --git a/emulation/router6.startup b/emulation/router6.startup index bd82deab6d3c6d5c4b7cf78d58211ca47eeec7d6..8d81d41ccf25d6797b4d8f9dece9a92987dde9fe 100644 --- a/emulation/router6.startup +++ b/emulation/router6.startup @@ -20,3 +20,5 @@ done # install python #apt-get update && apt-get --assume-yes install python3 python3-pip +echo VTYSH_PAGER=more > /etc/environment +