]> git.puffer.fish Git - matthieu/frr.git/commitdiff
eigrp-topo1: solve output ordering problem
authorRafael Zalamena <rzalamena@opensourcerouting.org>
Tue, 17 Oct 2017 20:06:25 +0000 (18:06 -0200)
committerDonald Sharp <sharpd@cumulusnetworks.com>
Wed, 28 Nov 2018 01:22:12 +0000 (20:22 -0500)
Transform 'show ip eigrp topo' output into data structures and compare
using json_cmp() to avoid expecting output order.

tests/topotests/eigrp-topo1/r1/show_ip_eigrp.json [new file with mode: 0644]
tests/topotests/eigrp-topo1/r2/show_ip_eigrp.json [new file with mode: 0644]
tests/topotests/eigrp-topo1/r3/show_ip_eigrp.json [new file with mode: 0644]
tests/topotests/eigrp-topo1/test_eigrp_topo1.py

diff --git a/tests/topotests/eigrp-topo1/r1/show_ip_eigrp.json b/tests/topotests/eigrp-topo1/r1/show_ip_eigrp.json
new file mode 100644 (file)
index 0000000..be0fdcf
--- /dev/null
@@ -0,0 +1,32 @@
+{
+    "P": {
+        "192.168.1.0/24": {
+            "fd": "28160",
+            "interface": " r1-eth0",
+            "serno": "0",
+            "successors": "1",
+            "via": "Connected"
+        },
+        "192.168.3.0/24": {
+            "fd": "33280",
+            "interface": " r1-eth1",
+            "serno": "0",
+            "successors": "1",
+            "via": "193.1.1.2 (33280/30720)"
+        },
+        "193.1.1.0/26": {
+            "fd": "28160",
+            "interface": " r1-eth1",
+            "serno": "0",
+            "successors": "1",
+            "via": "Connected"
+        },
+        "193.1.2.0/24": {
+            "fd": "30720",
+            "interface": " r1-eth1",
+            "serno": "0",
+            "successors": "1",
+            "via": "193.1.1.2 (30720/28160)"
+        }
+    }
+}
diff --git a/tests/topotests/eigrp-topo1/r2/show_ip_eigrp.json b/tests/topotests/eigrp-topo1/r2/show_ip_eigrp.json
new file mode 100644 (file)
index 0000000..ae9f441
--- /dev/null
@@ -0,0 +1,32 @@
+{
+    "P": {
+        "192.168.1.0/24": {
+            "fd": "30720",
+            "interface": " r2-eth0",
+            "serno": "0",
+            "successors": "1",
+            "via": "193.1.1.1 (30720/28160)"
+        },
+        "192.168.3.0/24": {
+            "fd": "30720",
+            "interface": " r2-eth1",
+            "serno": "0",
+            "successors": "1",
+            "via": "193.1.2.2 (30720/28160)"
+        },
+        "193.1.1.0/26": {
+            "fd": "28160",
+            "interface": " r2-eth0",
+            "serno": "0",
+            "successors": "1",
+            "via": "Connected"
+        },
+        "193.1.2.0/24": {
+            "fd": "28160",
+            "interface": " r2-eth1",
+            "serno": "0",
+            "successors": "1",
+            "via": "Connected"
+        }
+    }
+}
diff --git a/tests/topotests/eigrp-topo1/r3/show_ip_eigrp.json b/tests/topotests/eigrp-topo1/r3/show_ip_eigrp.json
new file mode 100644 (file)
index 0000000..83db66c
--- /dev/null
@@ -0,0 +1,32 @@
+{
+    "P": {
+        "192.168.1.0/24": {
+            "fd": "33280",
+            "interface": " r3-eth1",
+            "serno": "0",
+            "successors": "1",
+            "via": "193.1.2.1 (33280/30720)"
+        },
+        "192.168.3.0/24": {
+            "fd": "28160",
+            "interface": " r3-eth0",
+            "serno": "0",
+            "successors": "1",
+            "via": "Connected"
+        },
+        "193.1.1.0/26": {
+            "fd": "30720",
+            "interface": " r3-eth1",
+            "serno": "0",
+            "successors": "1",
+            "via": "193.1.2.1 (30720/28160)"
+        },
+        "193.1.2.0/24": {
+            "fd": "28160",
+            "interface": " r3-eth1",
+            "serno": "0",
+            "successors": "1",
+            "via": "Connected"
+        }
+    }
+}
index a09bf065684f2726717bee07dba12fc892486c81..de8cb81f8ff25720d9a01831f4301b119d2ce568 100755 (executable)
@@ -140,32 +140,18 @@ def test_eigrp_routes():
     # Verify EIGRP Status
     logger.info("Verifying EIGRP routes")
 
-    failures = 0
     router_list = tgen.routers().values()
     for router in router_list:
-        refTableFile = '{}/{}/show_ip_eigrp.ref'.format(CWD, router.name)
+        refTableFile = '{}/{}/show_ip_eigrp.json'.format(CWD, router.name)
 
         # Read expected result from file
-        expected = open(refTableFile).read().rstrip()
+        expected = json.loads(open(refTableFile).read())
 
         # Actual output from router
-        actual = router.vtysh_cmd('show ip eigrp topo').rstrip()
-        # Drop Time
-        actual = re.sub(r"[0-9][0-9]:[0-5][0-9]", "XX:XX", actual)
-
-        # Generate Diff
-        diff = topotest.difflines(actual, expected,
-                                  title1="actual SHOW IP EIGRP",
-                                  title2="expected SHOW IP EIGRP")
-
-        # Empty string if it matches, otherwise diff contains unified diff
-        if diff:
-            failures += 1
-        else:
-            logger.info('{} ok'.format(router.name))
-
-        assert failures == 0, 'SHOW IP EIGRP failed for router {}:\n{}'.format(router.name, diff)
+        actual = ip_eigrp_topo(router)
 
+        assertmsg = '"show ip eigrp topo" mismatches on {}'.format(router.name)
+        assert topotest.json_cmp(actual, expected) is None, assertmsg
 
 
 def test_zebra_ipv4_routingTable():
@@ -213,3 +199,88 @@ def test_shutdown_check_stderr():
 if __name__ == '__main__':
     args = ["-s"] + sys.argv[1:]
     sys.exit(pytest.main(args))
+
+#
+# Auxiliary Functions
+#
+def ip_eigrp_topo(node):
+    """
+    Parse 'show ip eigrp topo' from `node` and returns a dict with the
+    result.
+
+    Example:
+    {
+        'P': {
+            '192.168.1.0/24': {
+                'sucessors': 1,
+                'fd': 112233,
+                'serno': 0,
+                'via': 'Connected',
+                'interface': 'eth0',
+            },
+            '192.168.2.0/24': {
+                'sucessors': 1,
+                'fd': 112234,
+                'serno': 0,
+                'via': 'Connected',
+                'interface': 'eth1',
+            }
+        }
+    }
+    """
+    output = topotest.normalize_text(node.vtysh_cmd('show ip eigrp topo')).splitlines()
+    result = {}
+    for idx, line in enumerate(output):
+        columns = line.split(' ', 1)
+
+        # Parse the following format into python dicts
+        # code A.B.C.D/E, X successors, FD is Y, serno: Z
+        #       via FOO, interface-name
+        code = columns[0]
+        if code not in ['P', 'A', 'U', 'Q', 'R', 'r', 's']:
+            continue
+
+        if not result.has_key(code):
+            result[code] = {}
+
+        # Split network from the rest
+        columns = columns[1].split(',')
+
+        # Parse first line data
+        network = columns[0]
+        result[code][network] = {}
+        for column in columns:
+            # Skip the network column
+            if column == columns[0]:
+                continue
+
+            match = re.search(r'(\d+) successors', column)
+            if match is not None:
+                result[code][network]['successors'] = match.group(1)
+                continue
+
+            match = re.search(r'FD is (\d+)', column)
+            if match is not None:
+                result[code][network]['fd'] = match.group(1)
+                continue
+
+            match = re.search(r'serno: (\d+)', column)
+            if match is not None:
+                result[code][network]['serno'] = match.group(1)
+                continue
+
+        # Parse second line data
+        nextline = output[idx + 1]
+        columns = topotest.normalize_text(nextline).split(',')
+        for column in columns:
+            match = re.search(r'via (.+)', column)
+            if match is not None:
+                result[code][network]['via'] = match.group(1)
+                continue
+
+            match = re.search(r'(.+)', column)
+            if match is not None:
+                result[code][network]['interface'] = match.group(1)
+                continue
+
+    return result