]> git.puffer.fish Git - matthieu/frr.git/commitdiff
topotest: implement 'ip route' functions
authorRafael Zalamena <rzalamena@gmail.com>
Mon, 3 Jul 2017 18:57:20 +0000 (15:57 -0300)
committerDonald Sharp <sharpd@cumulusnetworks.com>
Wed, 28 Nov 2018 01:22:12 +0000 (20:22 -0500)
Implement an abstraction to the commands 'ip route' to get the node
current routing table state.

tests/topotests/lib/topotest.py

index fba04079707ed53c28c218aa34f8273cb5ebec95..1b5f7eed497b04efd9640b3e9facf84c4cde38b7 100644 (file)
@@ -287,6 +287,83 @@ def version_cmp(v1, v2):
             return -1
     return 0
 
+def ip4_route(node):
+    """
+    Gets a structured return of the command 'ip route'. It can be used in
+    conjuction with json_cmp() to provide accurate assert explanations.
+
+    Return example:
+    {
+        '10.0.1.0/24': {
+            'dev': 'eth0',
+            'via': '172.16.0.1',
+            'proto': '188',
+        },
+        '10.0.2.0/24': {
+            'dev': 'eth1',
+            'proto': 'kernel',
+        }
+    }
+    """
+    output = normalize_text(node.run('ip route')).splitlines()
+    result = {}
+    for line in output:
+        columns = line.split(' ')
+        route = result[columns[0]] = {}
+        prev = None
+        for column in columns:
+            if prev == 'dev':
+                route['dev'] = column
+            if prev == 'via':
+                route['via'] = column
+            if prev == 'proto':
+                route['proto'] = column
+            if prev == 'metric':
+                route['metric'] = column
+            if prev == 'scope':
+                route['scope'] = column
+            prev = column
+
+    return result
+
+def ip6_route(node):
+    """
+    Gets a structured return of the command 'ip -6 route'. It can be used in
+    conjuction with json_cmp() to provide accurate assert explanations.
+
+    Return example:
+    {
+        '2001:db8:1::/64': {
+            'dev': 'eth0',
+            'proto': '188',
+        },
+        '2001:db8:2::/64': {
+            'dev': 'eth1',
+            'proto': 'kernel',
+        }
+    }
+    """
+    output = normalize_text(node.run('ip -6 route')).splitlines()
+    result = {}
+    for line in output:
+        columns = line.split(' ')
+        route = result[columns[0]] = {}
+        prev = None
+        for column in columns:
+            if prev == 'dev':
+                route['dev'] = column
+            if prev == 'via':
+                route['via'] = column
+            if prev == 'proto':
+                route['proto'] = column
+            if prev == 'metric':
+                route['metric'] = column
+            if prev == 'pref':
+                route['pref'] = column
+            prev = column
+
+    return result
+
 def checkAddressSanitizerError(output, router, component):
     "Checks for AddressSanitizer in output. If found, then logs it and returns true, false otherwise"