From: Martin Winter Date: Wed, 31 May 2017 02:39:21 +0000 (-0700) Subject: lib: cleanup diff text output formatting issue and move to library X-Git-Tag: frr-7.1-dev~151^2~317 X-Git-Url: https://git.puffer.fish/?a=commitdiff_plain;h=17070436a3701489c3c33bbe3bbefb0a63afbd0d;p=matthieu%2Ffrr.git lib: cleanup diff text output formatting issue and move to library Signed-off-by: Martin Winter --- diff --git a/tests/topotests/all-protocol-startup/test_all_protocol_startup.py b/tests/topotests/all-protocol-startup/test_all_protocol_startup.py index 6e2f12ac5d..bbec24a0b4 100755 --- a/tests/topotests/all-protocol-startup/test_all_protocol_startup.py +++ b/tests/topotests/all-protocol-startup/test_all_protocol_startup.py @@ -30,7 +30,6 @@ test_all_protocol_startup.py: Test of all protocols at same time import os import re import sys -import difflib import pytest from time import sleep @@ -336,9 +335,9 @@ def test_rip_status(): actual = ('\n'.join(actual.splitlines()) + '\n').splitlines(1) # Generate Diff - diff = ''.join(difflib.context_diff(actual, expected, - fromfile="actual IP RIP status", - tofile="expected IP RIP status")) + diff = topotest.get_textdiff(actual, expected, + title1="actual IP RIP status", + title2="expected IP RIP status") # Empty string if it matches, otherwise diff contains unified diff if diff: @@ -391,9 +390,9 @@ def test_ripng_status(): actual = ('\n'.join(actual.splitlines()) + '\n').splitlines(1) # Generate Diff - diff = ''.join(difflib.context_diff(actual, expected, - fromfile="actual IPv6 RIPng status", - tofile="expected IPv6 RIPng status")) + diff = topotest.get_textdiff(actual, expected, + title1="actual IPv6 RIPng status", + title2="expected IPv6 RIPng status") # Empty string if it matches, otherwise diff contains unified diff if diff: @@ -444,9 +443,9 @@ def test_ospfv2_interfaces(): actual = ('\n'.join(actual.splitlines()) + '\n').splitlines(1) # Generate Diff - diff = ''.join(difflib.context_diff(actual, expected, - fromfile="actual SHOW IP OSPF INTERFACE", - tofile="expected SHOW IP OSPF INTERFACE")) + diff = topotest.get_textdiff(actual, expected, + title1="actual SHOW IP OSPF INTERFACE", + title2="expected SHOW IP OSPF INTERFACE") # Empty string if it matches, otherwise diff contains unified diff if diff: @@ -503,9 +502,9 @@ def test_isis_interfaces(): actual = ('\n'.join(actual.splitlines()) + '\n').splitlines(1) # Generate Diff - diff = ''.join(difflib.context_diff(actual, expected, - fromfile="actual SHOW ISIS INTERFACE DETAIL", - tofile="expected SHOW ISIS OSPF6 INTERFACE DETAIL")) + diff = topotest.get_textdiff(actual, expected, + title1="actual SHOW ISIS INTERFACE DETAIL", + title2="expected SHOW ISIS OSPF6 INTERFACE DETAIL") # Empty string if it matches, otherwise diff contains unified diff if diff: @@ -584,9 +583,9 @@ def test_bgp_summary(): actual = ('\n'.join(actual.splitlines()) + '\n').splitlines(1) # Generate Diff - diff = ''.join(difflib.context_diff(actual, expected, - fromfile="actual SHOW IP BGP SUMMARY", - tofile="expected SHOW IP BGP SUMMARY")) + diff = topotest.get_textdiff(actual, expected, + title1="actual SHOW IP BGP SUMMARY", + title2="expected SHOW IP BGP SUMMARY") # Empty string if it matches, otherwise diff contains unified diff if diff: @@ -666,9 +665,9 @@ def test_bgp_ipv6_summary(): actual = ('\n'.join(actual.splitlines()) + '\n').splitlines(1) # Generate Diff - diff = ''.join(difflib.context_diff(actual, expected, - fromfile="actual SHOW BGP IPv6 SUMMARY", - tofile="expected SHOW BGP IPv6 SUMMARY")) + diff = topotest.get_textdiff(actual, expected, + title1="actual SHOW BGP IPv6 SUMMARY", + title2="expected SHOW BGP IPv6 SUMMARY") # Empty string if it matches, otherwise diff contains unified diff if diff: @@ -719,9 +718,9 @@ def test_bgp_ipv4(): actual = ('\n'.join(actual.splitlines()) + '\n').splitlines(1) # Generate Diff - diff = ''.join(difflib.context_diff(actual, expected, - fromfile="actual SHOW BGP IPv4", - tofile="expected SHOW BGP IPv4")) + diff = topotest.get_textdiff(actual, expected, + title1="actual SHOW BGP IPv4", + title2="expected SHOW BGP IPv4") # Empty string if it matches, otherwise diff contains unified diff if diff: @@ -772,9 +771,9 @@ def test_bgp_ipv6(): actual = ('\n'.join(actual.splitlines()) + '\n').splitlines(1) # Generate Diff - diff = ''.join(difflib.context_diff(actual, expected, - fromfile="actual SHOW BGP IPv6", - tofile="expected SHOW BGP IPv6")) + diff = topotest.get_textdiff(actual, expected, + title1="actual SHOW BGP IPv6", + title2="expected SHOW BGP IPv6") # Empty string if it matches, otherwise diff contains unified diff if diff: @@ -828,9 +827,9 @@ def test_mpls_interfaces(): actual = ('\n'.join(actual.splitlines()) + '\n').splitlines(1) # Generate Diff - diff = ''.join(difflib.context_diff(actual, expected, - fromfile="actual MPLS LDP interface status", - tofile="expected MPLS LDP interface status")) + diff = topotest.get_textdiff(actual, expected, + title1="actual MPLS LDP interface status", + title2="expected MPLS LDP interface status") # Empty string if it matches, otherwise diff contains unified diff if diff: diff --git a/tests/topotests/bgp_multiview_topo1/test_bgp_multiview_topo1.py b/tests/topotests/bgp_multiview_topo1/test_bgp_multiview_topo1.py index d7c350f5e5..d850787fa4 100755 --- a/tests/topotests/bgp_multiview_topo1/test_bgp_multiview_topo1.py +++ b/tests/topotests/bgp_multiview_topo1/test_bgp_multiview_topo1.py @@ -65,7 +65,6 @@ test_bgp_multiview_topo1.py: Simple Quagga/FRR Route-Server Test import os import re import sys -import difflib import pytest from time import sleep @@ -298,10 +297,9 @@ def test_bgp_routingTable(): actual = ('\n'.join(actual.splitlines()) + '\n').splitlines(1) # Generate Diff - diff = ''.join(difflib.context_diff(actual, expected, - fromfile="actual BGP routing table", - tofile="expected BGP routing table")) - # Empty string if it matches, otherwise diff contains unified diff + diff = topotest.get_textdiff(actual, expected, + title1="actual BGP routing table", + title2="expected BGP routing table") if diff: sys.stderr.write('r%s failed Routing Table Check for view %s:\n%s\n' diff --git a/tests/topotests/ldp-topo1/test_ldp_topo1.py b/tests/topotests/ldp-topo1/test_ldp_topo1.py index af2bae1853..82ee80fc24 100755 --- a/tests/topotests/ldp-topo1/test_ldp_topo1.py +++ b/tests/topotests/ldp-topo1/test_ldp_topo1.py @@ -62,7 +62,6 @@ r3-eth1 .3 | | .3 r3-eth0 | .4 r4-eth0 import os import re import sys -import difflib import pytest from time import sleep @@ -236,9 +235,9 @@ def test_mpls_interfaces(): actual = ('\n'.join(actual.splitlines()) + '\n').splitlines(1) # Generate Diff - diff = ''.join(difflib.context_diff(actual, expected, - fromfile="actual MPLS LDP interface status", - tofile="expected MPLS LDP interface status")) + diff = topotest.get_textdiff(actual, expected, + title1="actual MPLS LDP interface status", + title2="expected MPLS LDP interface status") # Empty string if it matches, otherwise diff contains unified diff if diff: @@ -357,9 +356,9 @@ def test_mpls_ldp_discovery(): actual = ('\n'.join(actual.splitlines()) + '\n').splitlines(1) # Generate Diff - diff = ''.join(difflib.context_diff(actual, expected, - fromfile="actual MPLS LDP discovery output", - tofile="expected MPLS LDP discovery output")) + diff = topotest.get_textdiff(actual, expected, + title1="actual MPLS LDP discovery output", + title2="expected MPLS LDP discovery output") # Empty string if it matches, otherwise diff contains unified diff if diff: @@ -422,9 +421,9 @@ def test_mpls_ldp_neighbor(): actual = ('\n'.join(actual.splitlines()) + '\n').splitlines(1) # Generate Diff - diff = ''.join(difflib.context_diff(actual, expected, - fromfile="actual MPLS LDP neighbor output", - tofile="expected MPLS LDP neighbor output")) + diff = topotest.get_textdiff(actual, expected, + title1="actual MPLS LDP neighbor output", + title2="expected MPLS LDP neighbor output") # Empty string if it matches, otherwise diff contains unified diff if diff: @@ -503,9 +502,9 @@ def test_mpls_ldp_binding(): swapped = True # Generate Diff - diff = ''.join(difflib.context_diff(actual, expected, - fromfile="actual MPLS LDP binding output", - tofile="expected MPLS LDP binding output")) + diff = topotest.get_textdiff(actual, expected, + title1="actual MPLS LDP binding output", + title2="expected MPLS LDP binding output") # Empty string if it matches, otherwise diff contains unified diff if diff: @@ -561,9 +560,9 @@ def test_zebra_ipv4_routingTable(): actual = ('\n'.join(actual.splitlines()) + '\n').splitlines(1) # Generate Diff - diff = ''.join(difflib.context_diff(actual, expected, - fromfile="actual IPv4 zebra routing table", - tofile="expected IPv4 zera routing table")) + diff = topotest.get_textdiff(actual, expected, + title1="actual IPv4 zebra routing table", + title2="expected IPv4 zebra routing table") # Empty string if it matches, otherwise diff contains unified diff if diff: @@ -631,9 +630,9 @@ def test_mpls_table(): swapped = True # Generate Diff - diff = ''.join(difflib.context_diff(actual, expected, - fromfile="actual MPLS table output", - tofile="expected MPLS table output")) + diff = topotest.get_textdiff(actual, expected, + title1="actual MPLS table output", + title2="expected MPLS table output") # Empty string if it matches, otherwise diff contains unified diff if diff: @@ -735,9 +734,9 @@ def test_linux_mpls_routes(): actual = ('\n'.join(actual) + '\n').splitlines(1) # Generate Diff - diff = ''.join(difflib.context_diff(actual, expected, - fromfile="actual Linux Kernel MPLS route", - tofile="expected Linux Kernel MPLS route")) + diff = topotest.get_textdiff(actual, expected, + title1="actual Linux Kernel MPLS route", + title2="expected Linux Kernel MPLS route") # Empty string if it matches, otherwise diff contains unified diff if diff: diff --git a/tests/topotests/lib/topotest.py b/tests/topotests/lib/topotest.py index b3cff8ca7d..6572bc3390 100644 --- a/tests/topotests/lib/topotest.py +++ b/tests/topotests/lib/topotest.py @@ -30,6 +30,7 @@ import glob import StringIO import subprocess import platform +import difflib from mininet.topo import Topo from mininet.net import Mininet @@ -73,6 +74,15 @@ def pid_exists(pid): else: return True +def get_textdiff(text1, text2, title1="", title2=""): + "Returns empty string if same or formatted diff" + + diff = '\n'.join(difflib.context_diff(text1, text2, + fromfile=title1, tofile=title2)) + # Clean up line endings + diff = os.linesep.join([s for s in diff.splitlines() if s]) + return diff + def checkAddressSanitizerError(output, router, component): "Checks for AddressSanitizer in output. If found, then logs it and returns true, false otherwise" diff --git a/tests/topotests/ospf6-topo1/test_ospf6_topo1.py b/tests/topotests/ospf6-topo1/test_ospf6_topo1.py index 90489e4bec..8c2b80382d 100755 --- a/tests/topotests/ospf6-topo1/test_ospf6_topo1.py +++ b/tests/topotests/ospf6-topo1/test_ospf6_topo1.py @@ -70,29 +70,9 @@ test_ospf6_topo1.py: -----/ """ -# import os -# import re -# import sys -# import difflib -# import StringIO -# import glob -# import subprocess - -# from mininet.topo import Topo -# from mininet.net import Mininet -# from mininet.node import Node, OVSSwitch, Host -# from mininet.log import setLogLevel, info -# from mininet.cli import CLI - -# from functools import partial -# from time import sleep - -# import pytest - import os import re import sys -import difflib import pytest from time import sleep @@ -295,9 +275,9 @@ def test_ospfv3_routingTable(): actual = ('\n'.join(actual.splitlines()) + '\n').splitlines(1) # Generate Diff - diff = ''.join(difflib.context_diff(actual, expected, - fromfile="actual OSPFv3 IPv6 routing table", - tofile="expected OSPFv3 IPv6 routing table")) + diff = topotest.get_textdiff(actual, expected, + title1="actual OSPFv3 IPv6 routing table", + title2="expected OSPFv3 IPv6 routing table") # Empty string if it matches, otherwise diff contains unified diff if diff: @@ -363,9 +343,9 @@ def test_linux_ipv6_kernel_routingTable(): # print(line.rstrip()) # Generate Diff - diff = ''.join(difflib.context_diff(actual, expected, - fromfile="actual IPv6 kernel routing table", - tofile="expected IPv6 kernel routing table")) + diff = topotest.get_textdiff(actual, expected, + title1="actual OSPFv3 IPv6 routing table", + title2="expected OSPFv3 IPv6 routing table") # Empty string if it matches, otherwise diff contains unified diff if diff: diff --git a/tests/topotests/rip-topo1/test_rip_topo1.py b/tests/topotests/rip-topo1/test_rip_topo1.py index 60b43ee42e..44be48c702 100755 --- a/tests/topotests/rip-topo1/test_rip_topo1.py +++ b/tests/topotests/rip-topo1/test_rip_topo1.py @@ -30,7 +30,6 @@ test_rip_topo1.py: Testing RIPv2 import os import re import sys -import difflib import pytest from time import sleep @@ -213,9 +212,9 @@ def test_rip_status(): actual = ('\n'.join(actual.splitlines()) + '\n').splitlines(1) # Generate Diff - diff = ''.join(difflib.context_diff(actual, expected, - fromfile="actual IP RIP status", - tofile="expected IP RIP status")) + diff = topotest.get_textdiff(actual, expected, + title1="actual IP RIP status", + title2="expected IP RIP status") # Empty string if it matches, otherwise diff contains unified diff if diff: @@ -265,9 +264,9 @@ def test_rip_routes(): actual = ('\n'.join(actual.splitlines()) + '\n').splitlines(1) # Generate Diff - diff = ''.join(difflib.context_diff(actual, expected, - fromfile="actual SHOW IP RIP", - tofile="expected SHOW IP RIP")) + diff = topotest.get_textdiff(actual, expected, + title1="actual SHOW IP RIP", + title2="expected SHOW IP RIP") # Empty string if it matches, otherwise diff contains unified diff if diff: @@ -317,9 +316,9 @@ def test_zebra_ipv4_routingTable(): actual = ('\n'.join(actual.splitlines()) + '\n').splitlines(1) # Generate Diff - diff = ''.join(difflib.context_diff(actual, expected, - fromfile="actual Zebra IPv4 routing table", - tofile="expected Zebra IPv4 routing table")) + diff = topotest.get_textdiff(actual, expected, + title1="actual Zebra IPv4 routing table", + title2="expected Zebra IPv4 routing table") # Empty string if it matches, otherwise diff contains unified diff if diff: diff --git a/tests/topotests/ripng-topo1/test_ripng_topo1.py b/tests/topotests/ripng-topo1/test_ripng_topo1.py index e790808e3b..518f4e13e6 100755 --- a/tests/topotests/ripng-topo1/test_ripng_topo1.py +++ b/tests/topotests/ripng-topo1/test_ripng_topo1.py @@ -30,7 +30,6 @@ test_ripng_topo1.py: Test of RIPng Topology import os import re import sys -import difflib import pytest import unicodedata from time import sleep @@ -216,9 +215,9 @@ def test_ripng_status(): actual = ('\n'.join(actual.splitlines()) + '\n').splitlines(1) # Generate Diff - diff = ''.join(difflib.context_diff(actual, expected, - fromfile="actual IPv6 RIPng status", - tofile="expected IPv6 RIPng status")) + diff = topotest.get_textdiff(actual, expected, + title1="actual IPv6 RIPng status", + title2="expected IPv6 RIPng status") # Empty string if it matches, otherwise diff contains unified diff if diff: @@ -273,9 +272,9 @@ def test_ripng_routes(): actual = ('\n'.join(actual.splitlines()) + '\n').splitlines(1) # Generate Diff - diff = ''.join(difflib.context_diff(actual, expected, - fromfile="actual SHOW IPv6 RIPng", - tofile="expected SHOW IPv6 RIPng")) + diff = topotest.get_textdiff(actual, expected, + title1="actual SHOW IPv6 RIPng", + title2="expected SHOW IPv6 RIPng") # Empty string if it matches, otherwise diff contains unified diff if diff: @@ -327,9 +326,9 @@ def test_zebra_ipv6_routingTable(): actual = ('\n'.join(actual.splitlines()) + '\n').splitlines(1) # Generate Diff - diff = ''.join(difflib.context_diff(actual, expected, - fromfile="actual Zebra IPv6 routing table", - tofile="expected Zebra IPv6 routing table")) + diff = topotest.get_textdiff(actual, expected, + title1="actual Zebra IPv6 routing table", + title2="expected Zebra IPv6 routing table") # Empty string if it matches, otherwise diff contains unified diff if diff: