From 65954fb61c9fbf6742cd820e658f2b3656e925d9 Mon Sep 17 00:00:00 2001 From: Jonathan Yantis Date: Tue, 23 Aug 2016 14:57:37 -0400 Subject: [PATCH] Fixed pathfinding bugs: - Added testrand.py to massively test pathfinding --- nglib/query/path.py | 37 +++++++++++++++++++++---------------- test/testrand.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 16 deletions(-) create mode 100755 test/testrand.py diff --git a/nglib/query/path.py b/nglib/query/path.py index 41320ec..ef243cb 100644 --- a/nglib/query/path.py +++ b/nglib/query/path.py @@ -80,6 +80,13 @@ def get_full_path(src, dst, popt, rtype="NGTREE"): srctree, dsttree, srcswp, dstswp = None, None, None, None + if 'vrfcidr' not in n1tree['_child001']: + print("Warning: Could not locate", src, file=sys.stderr) + return + if 'vrfcidr' not in n2tree['_child001']: + print("Warning: Could not locate", dst, file=sys.stderr) + return + # Routing Check routing = True if n1tree['_child001']['vrfcidr'] == n2tree['_child001']['vrfcidr']: @@ -94,28 +101,26 @@ def get_full_path(src, dst, popt, rtype="NGTREE"): router = n1tree['_child001']['Router'] if 'StandbyRouter' in n1tree['_child001']: router = router + '|' + n1tree['_child001']['StandbyRouter'] - if routing: - if 'Switch' in srctree and srctree['Switch']: - srcswp = get_switched_path(srctree['Switch'], router, popt) - else: - srctree = None - print("Warning: Could not find source switch data in NetDB", file=sys.stderr) + if 'Switch' in srctree and srctree['Switch']: + srcswp = get_switched_path(srctree['Switch'], router, popt) + else: + srctree = None + print("Warning: Could not find source switch data in NetDB:", src, file=sys.stderr) # Find Switched Path from Router to Destination if dsttree: router = n2tree['_child001']['Router'] if 'StandbyRouter' in n2tree['_child001']: router = router + '|' + n2tree['_child001']['StandbyRouter'] - if routing: - if 'Switch' in dsttree and dsttree['Switch']: - dstswp = get_switched_path(router, dsttree['Switch'], popt) - else: - dsttree = None - print("Warning: Could not find destination switch data in NetDB", \ - file=sys.stderr) + if 'Switch' in dsttree and dsttree['Switch']: + dstswp = get_switched_path(router, dsttree['Switch'], popt) + else: + dsttree = None + print("Warning: Could not find destination switch data in NetDB", \ + dst, file=sys.stderr) # If only switching, update srcswp to show switched path - else: + if not routing and srctree and dsttree: srcswp = get_switched_path(srctree['Switch'], dsttree['Switch'], popt) # Same switch/vlan check @@ -141,9 +146,9 @@ def get_full_path(src, dst, popt, rtype="NGTREE"): ngtree["Traversal Type"] = 'All Paths' ## Add the SRC Data - if '_child002' in n2tree: + if '_child002' in n1tree: n1tree['_child002']['_type'] = "SRC" - if 'SwitchPort' in n2tree['_child002']: + if 'SwitchPort' in n1tree['_child002']: n1tree['_child002']['Name'] = src + ' ' + n1tree['_child002']['MAC'] \ + ' ' + str(n1tree['_child002']['Switch']) + '(' \ + str(n1tree['_child002']['SwitchPort']) \ diff --git a/test/testrand.py b/test/testrand.py new file mode 100755 index 0000000..f855811 --- /dev/null +++ b/test/testrand.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 +""" Test Random IP's for errors""" +import subprocess + +ip1 = open('/tmp/ip1.txt', 'r') +ip2 = open('/tmp/ip2.txt', 'r') + +verbose = True + +for e1, e2 in zip(ip1, ip2): + e1 = e1.strip() + e2 = e2.strip() + #print(e1, e2) + + cmd = './netgrph.py ' + e1 + ' ' + e2 + + proc = subprocess.Popen( + [cmd], + stdout=subprocess.PIPE, + shell=True, + universal_newlines=True) + + (out, err) = proc.communicate() + + if err: + print(cmd, err) + elif out and verbose: + print(out)