diff --git a/README.md b/README.md index df68464..7487b4a 100644 --- a/README.md +++ b/README.md @@ -457,7 +457,8 @@ tool that is reliable enough. NetGrph was developed on Ubuntu 14.04 LTS, tested on 16.04, and should be compatible with other versions of linux. I highly recommend using Ubuntu at this early stage for better support. I also plan to create an ansible build script in -the next few months. +the next few months. It also works just find on MacOS but there are no Ansible or +install instructions for that OS. ## Installation diff --git a/docs/TODO b/docs/TODO index 978da36..cccbc66 100644 --- a/docs/TODO +++ b/docs/TODO @@ -1,24 +1,14 @@ -NetDB: -- Add CIDR scope usage reporting -- Add more extensive device reporting - -Reporting: - - No name, no root, no router report Other: - Add VRF to -rpath -- Add master -path to netgrph -- Add tabular output option +- Add distance option for traversals - built-in paging support -- Group report takes up entire console Maintenance: - Add switch skip on certain neighbors -- Rewrite nglib.net_update using Bolt as Pythonic - Emulate dict with ngtree - Rewrite py2neo cypher queries with py2neo v3.0 - Change py2neo to use bolt driver by default - diff --git a/netgrph.py b/netgrph.py index 0992a17..a1c5f6a 100755 --- a/netgrph.py +++ b/netgrph.py @@ -120,6 +120,7 @@ def check_path(singlepath): singlepath = True return singlepath + # Alternate Config File if args.conf: config_file = args.conf @@ -155,37 +156,37 @@ def check_path(singlepath): ## Pathfinding if args.fpath: - nglib.query.path.get_fw_path(args.fpath, args.search) + nglib.query.path.get_fw_path(args.fpath, args.search, dict()) # Quick Path elif args.qpath: rtype = "QTREE" if args.output: rtype = args.output - nglib.query.path.get_full_path(args.search, args.qpath, rtype=rtype, \ - onepath=check_path(False)) + nglib.query.path.get_full_path(args.search, args.qpath, \ + {"onepath": check_path(False)}, rtype=rtype) elif args.spath: rtype = "TREE" if args.output: rtype = args.output - nglib.query.path.get_switched_path(args.spath, args.search, rtype=rtype, \ - onepath=check_path(False)) + nglib.query.path.get_switched_path(args.spath, args.search, \ + {"onepath": check_path(False)}, rtype=rtype) elif args.rpath: rtype = "TREE" if args.output: rtype = args.output - nglib.query.path.get_routed_path(args.rpath, args.search, rtype=rtype, \ - onepath=check_path(False)) + nglib.query.path.get_routed_path(args.rpath, args.search, \ + {"onepath":check_path(False)}, rtype=rtype) elif args.path: rtype = "TREE" if args.output: rtype = args.output - nglib.query.path.get_full_path(args.path, args.search, rtype=rtype, \ - onepath=check_path(True)) + nglib.query.path.get_full_path(args.path, args.search, \ + {"onepath": check_path(False)}, rtype=rtype) ## Individual Queries elif args.dev: diff --git a/nglib/ngtree/export.py b/nglib/ngtree/export.py index ced69e2..cc95f07 100644 --- a/nglib/ngtree/export.py +++ b/nglib/ngtree/export.py @@ -124,4 +124,5 @@ def strip_ngtree(ngtree, top=True): elif top: newtree[en] = ngtree[en] - return newtree \ No newline at end of file + return newtree + \ No newline at end of file diff --git a/nglib/query/__init__.py b/nglib/query/__init__.py index 3c07d48..78670c2 100644 --- a/nglib/query/__init__.py +++ b/nglib/query/__init__.py @@ -177,7 +177,7 @@ def get_filter_dict(group=None, nFilter=None): gFilter = nFilter else: raise Exception("Must pass in group or filter") - + # Split all VRFs by spaces vrfFilters = gFilter.rsplit() diff --git a/nglib/query/net.py b/nglib/query/net.py index 57ec099..3ff98f0 100644 --- a/nglib/query/net.py +++ b/nglib/query/net.py @@ -41,12 +41,13 @@ logger = logging.getLogger(__name__) -def get_net(ip, rtype="TREE", days=7): +def get_net(ip, rtype="TREE", days=7, verbose=True): """Find a network and return text output""" rtypes = ('TREE', 'JSON', 'YAML', 'NGTREE') - logger.info("Query: Looking up %s for %s", ip, nglib.user) + if verbose: + logger.info("Query: Looking up %s for %s", ip, nglib.user) if rtype in rtypes: net = nglib.query.net.find_cidr(ip) @@ -294,8 +295,8 @@ def find_cidr(ip): if len(networks) > 1: for r in networks.records: if ipaddress.ip_address(ip) in ipaddress.ip_network(r.cidr): - if nglib.verbose: - print(ip + " in " + r.cidr) + if nglib.verbose>1: + print("find_cidr", ip + " in " + r.cidr) mostSpecific = compare_cidr(mostSpecific, r.cidr) diff --git a/nglib/query/path.py b/nglib/query/path.py index 4c83195..ef243cb 100644 --- a/nglib/query/path.py +++ b/nglib/query/path.py @@ -42,7 +42,7 @@ logger = logging.getLogger(__name__) -def get_full_path(src, dst, rtype="NGTREE", onepath=True): +def get_full_path(src, dst, popt, rtype="NGTREE"): """ Gets the full path (switch->rt->VRF->rt->switch) Required NetDB for switchpath @@ -52,6 +52,15 @@ def get_full_path(src, dst, rtype="NGTREE", onepath=True): if rtype in rtypes: + if 'onepath' not in popt: + popt['onepath'] = True + if 'l2path' not in popt: + popt['l2path'] = True + if 'verbose' not in popt: + popt['verbose'] = False + if nglib.verbose: + popt['verbose'] = True + logger.info("Query: Finding Full Path (%s --> %s) for %s", src, dst, nglib.user) @@ -60,17 +69,24 @@ def get_full_path(src, dst, rtype="NGTREE", onepath=True): # Translate IPs to CIDRs if re.search(r'^\d+\.\d+\.\d+\.\d+$', net1): - n1tree = nglib.query.net.get_net(net1, rtype="NGTREE") + n1tree = nglib.query.net.get_net(net1, rtype="NGTREE", verbose=popt['verbose']) if n1tree: net1 = n1tree['_child001']['Name'] if re.search(r'^\d+\.\d+\.\d+\.\d+$', net2): - n2tree = nglib.query.net.get_net(net2, rtype="NGTREE") + n2tree = nglib.query.net.get_net(net2, rtype="NGTREE", verbose=popt['verbose']) if n2tree: net2 = n2tree['_child001']['Name'] 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']: @@ -79,36 +95,33 @@ def get_full_path(src, dst, rtype="NGTREE", onepath=True): if nglib.use_netdb: srctree = nglib.netdb.ip.get_netdb_ip(src) dsttree = nglib.netdb.ip.get_netdb_ip(dst) - + # Find Switched Path from Source to Router if srctree: router = n1tree['_child001']['Router'] if 'StandbyRouter' in n1tree['_child001']: router = router + '|' + n1tree['_child001']['StandbyRouter'] - if routing: - if 'Switch' in srctree: - srcswp = get_switched_path(srctree['Switch'], router, \ - verbose=False, onepath=onepath) - 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: - dstswp = get_switched_path(router, dsttree['Switch'], \ - verbose=False, onepath=onepath) - 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: - srcswp = get_switched_path(srctree['Switch'], dsttree['Switch'], verbose=False, onepath=onepath) + if not routing and srctree and dsttree: + srcswp = get_switched_path(srctree['Switch'], dsttree['Switch'], popt) # Same switch/vlan check switching = True @@ -127,15 +140,15 @@ def get_full_path(src, dst, rtype="NGTREE", onepath=True): if srctree and dsttree: ngtree["L2 Path"] = srctree['Switch'] + " (" + srctree['SwitchPort'] \ + ") -> " + dsttree['Switch'] + " (" + dsttree['SwitchPort'] + ")" - if onepath: + if popt['onepath']: ngtree["Traversal Type"] = 'Single Path' else: 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']) \ @@ -163,7 +176,7 @@ def get_full_path(src, dst, rtype="NGTREE", onepath=True): nglib.ngtree.add_child_ngtree(ngtree, n1tree['_child001']) ## Check for routed paths (inter/intra VRF) - rtree = get_full_routed_path(src, dst, rtype="NGTREE", l2path=True, onepath=onepath) + rtree = get_full_routed_path(src, dst, popt) if rtree and 'PATH' in rtree['_type']: # Breakdown L4 Path @@ -189,7 +202,6 @@ def get_full_path(src, dst, rtype="NGTREE", onepath=True): # Destination Switch Data if switching and dstswp: - #dstswp['Name'] = "DST Switched Path" nglib.ngtree.add_child_ngtree(ngtree, dstswp) # NetDB Destination Data @@ -206,7 +218,7 @@ def get_full_path(src, dst, rtype="NGTREE", onepath=True): ngtree = nglib.query.exp_ngtree(ngtree, rtype) return ngtree -def get_full_routed_path(src, dst, rtype="NGTREE", l2path=False, onepath=True): +def get_full_routed_path(src, dst, popt, rtype="NGTREE"): """ Gets the full L3 Path between src -> dst IPs including inter-vrf routing """ @@ -214,22 +226,29 @@ def get_full_routed_path(src, dst, rtype="NGTREE", l2path=False, onepath=True): if rtype in rtypes: + if 'l2path' not in popt: + popt['l2path'] = False + if 'onepath' not in popt: + popt['onepath'] = True + if 'VRF' not in popt: + popt['VRF'] = "default" + srct, dstt, ngtree = None, None, None # Translate IPs to CIDRs if re.search(r'^\d+\.\d+\.\d+\.\d+$', src): - srct = nglib.query.net.get_net(src, rtype="NGTREE") + srct = nglib.query.net.get_net(src, rtype="NGTREE", verbose=popt['verbose']) if re.search(r'^\d+\.\d+\.\d+\.\d+$', dst): - dstt = nglib.query.net.get_net(dst, rtype="NGTREE") - + dstt = nglib.query.net.get_net(dst, rtype="NGTREE", verbose=popt['verbose']) + # Intra VRF if srct['_child001']['VRF'] == dstt['_child001']['VRF']: - ngtree = get_routed_path(src, dst, verbose=False, onepath=onepath) - + ngtree = get_routed_path(src, dst, popt) + # Inter VRF else: - secpath = get_fw_path(src, dst, rtype="NGTREE") + secpath = get_fw_path(src, dst, popt, rtype="NGTREE") if secpath: ngtree = nglib.ngtree.get_ngtree(secpath['Name'], tree_type="L4-PATH") @@ -242,8 +261,9 @@ def get_full_routed_path(src, dst, rtype="NGTREE", l2path=False, onepath=True): # First Entry gets a route check if first: - rtree = get_routed_path(src, secpath[key]['gateway'], \ - vrf=srct['_child001']['VRF'], l2path=l2path, onepath=onepath) + npopt = popt.copy() + npopt['VRF'] = srct['_child001']['VRF'] + rtree = get_routed_path(src, secpath[key]['gateway'], popt) if rtree: nglib.ngtree.add_child_ngtree(ngtree, rtree) first = False @@ -252,15 +272,16 @@ def get_full_routed_path(src, dst, rtype="NGTREE", l2path=False, onepath=True): # Last entry gets a route check if last: - rtree = get_routed_path(secpath[last]['gateway'], dst, \ - vrf=dstt['_child001']['VRF'], l2path=l2path, onepath=onepath) + npopt = popt.copy() + npopt['VRF'] = srct['_child001']['VRF'] + rtree = get_routed_path(secpath[last]['gateway'], dst, popt) if rtree: nglib.ngtree.add_child_ngtree(ngtree, rtree) return ngtree -def get_routed_path(net1, net2, rtype="NGTREE", vrf="default", verbose=True, l2path=True, onepath=True): +def get_routed_path(net1, net2, popt, rtype="NGTREE"): """ Find the routed path between two CIDRs and return all interfaces and devices between the two. This query need optimization. @@ -275,21 +296,31 @@ def get_routed_path(net1, net2, rtype="NGTREE", vrf="default", verbose=True, l2p if rtype in rtypes: - logger.info("Query: Finding Routed Paths (%s --> %s) for %s", - net1, net2, nglib.user) + if 'l2path' not in popt: + popt['l2path'] = False + if 'onepath' not in popt: + popt['onepath'] = True + if 'VRF' not in popt: + popt['VRF'] = "default" + if 'verbose' not in popt: + popt['verbose'] = True + + if popt['verbose']: + logger.info("Query: Finding Routed Paths (%s --> %s) for %s", + net1, net2, nglib.user) hopSet = set() # Translate IPs to CIDRs if re.search(r'^\d+\.\d+\.\d+\.\d+$', net1): - n1tree = nglib.query.net.get_net(net1, rtype="NGTREE") + n1tree = nglib.query.net.get_net(net1, rtype="NGTREE", verbose=popt['verbose']) net1 = n1tree['_child001']['Name'] if re.search(r'^\d+\.\d+\.\d+\.\d+$', net2): - n2tree = nglib.query.net.get_net(net2, rtype="NGTREE") + n2tree = nglib.query.net.get_net(net2, rtype="NGTREE", verbose=popt['verbose']) if n2tree: net2 = n2tree['_child001']['Name'] - + ngtree = nglib.ngtree.get_ngtree("Path", tree_type="L3-PATH") ngtree["Path"] = net1 + " -> " + net2 ngtree['Name'] = ngtree['Path'] @@ -325,7 +356,7 @@ def get_routed_path(net1, net2, rtype="NGTREE", vrf="default", verbose=True, l2p + 'r2.name AS r2name, l2.gateway as r2ip, v.vid AS vid, ' + 'LENGTH(shortestPath((sn)<-[:ROUTED|ROUTED_BY|ROUTED_STANDBY*0..12]->(r1))) ' + 'AS distance ORDER BY distance', - {"net1": net1, "net2": net2, "vrf": vrf}) + {"net1": net1, "net2": net2, "vrf": popt['VRF']}) allpaths = dict() # Load all paths into tuples with distance value @@ -375,8 +406,8 @@ def get_routed_path(net1, net2, rtype="NGTREE", vrf="default", verbose=True, l2p rtree['Name'] = rtree['Name'] + ' [vid:' + str(rtree['VLAN']) + ']' # Add Switchpath if requested - if l2path: - spath = get_switched_path(rec['r1name'], rec['r2name'], verbose=False) + if popt['l2path']: + spath = get_switched_path(rec['r1name'], rec['r2name'], popt) if spath: for sp in spath: if '_child' in sp and '_rvlans' in spath[sp]: @@ -387,7 +418,8 @@ def get_routed_path(net1, net2, rtype="NGTREE", vrf="default", verbose=True, l2p # print(spath[sp]['_rvlans']) # nglib.ngtree.add_child_ngtree(rtree, spath[sp]) - if not onepath or distance not in hopSet: + # Single / Multi-path + if not popt['onepath'] or distance not in hopSet: hopSet.add(distance) nglib.ngtree.add_child_ngtree(ngtree, rtree) pathList.append(rtree) @@ -397,9 +429,9 @@ def get_routed_path(net1, net2, rtype="NGTREE", vrf="default", verbose=True, l2p ngtree['Hops'] = len(pathList) ngtree['Distance'] = max([s['distance'] for s in pathList]) - ngtree['VRF'] = vrf + ngtree['VRF'] = popt['VRF'] - if onepath: + if popt['onepath']: ngtree['Traversal Type'] = 'Single Path' ngtree['Traversal Coverage'] = path_coverage(ngtree['Distance'], ngtree['Hops']) else: @@ -414,11 +446,12 @@ def get_routed_path(net1, net2, rtype="NGTREE", vrf="default", verbose=True, l2p # Export NGTree ngtree = nglib.query.exp_ngtree(ngtree, rtype) return ngtree - elif verbose: - print("No results found for path between {:} and {:}".format(net1, net2), file=sys.stderr) + elif popt['verbose']: + print("No results found for path between {:} and {:}".format(net1, net2), \ + file=sys.stderr) -def get_switched_path(switch1, switch2, rtype="NGTREE", verbose=True, onepath=True): +def get_switched_path(switch1, switch2, popt, rtype="NGTREE"): """ Find the path between two switches and return all interfaces and devices between the two. @@ -435,13 +468,20 @@ def get_switched_path(switch1, switch2, rtype="NGTREE", verbose=True, onepath=Tr if rtype in rtypes: - logger.info("Query: Finding Switched Paths (%s --> %s) for %s", - switch1, switch2, nglib.user) + + if 'onepath' not in popt: + popt['onepath'] = False + if 'verbose' not in popt: + popt['verbose'] = True + + if popt['verbose']: + logger.info("Query: Finding Switched Paths (%s --> %s) for %s", + switch1, switch2, nglib.user) pathList = [] hopSet = set() ngtree = nglib.ngtree.get_ngtree("Switched Paths", tree_type="L2-PATH") - ngtree["Name"] = switch1 + " -> " + switch2 + ngtree["Name"] = str(switch1) + " -> " + str(switch2) swp = nglib.py2neo_ses.cypher.execute( 'MATCH (ss:Switch), (ds:Switch), ' @@ -501,10 +541,10 @@ def get_switched_path(switch1, switch2, rtype="NGTREE", verbose=True, onepath=Tr swptree = spath_direction(swptree) # Save data structures - if not onepath or swptree['distance'] not in hopSet: + if not popt['onepath'] or swptree['distance'] not in hopSet: hopSet.add(swptree['distance']) nglib.ngtree.add_child_ngtree(ngtree, swptree) - + # App Paths go in pathlist pathList.append(swptree) @@ -513,7 +553,7 @@ def get_switched_path(switch1, switch2, rtype="NGTREE", verbose=True, onepath=Tr ngtree['Links'] = len(pathList) ngtree['Distance'] = max([s['distance'] for s in pathList]) - if onepath: + if popt['onepath']: ngtree['Traversal Type'] = 'Single Path' ngtree['Traversal Coverage'] = path_coverage(ngtree['Distance'], ngtree['Links']) else: @@ -528,14 +568,14 @@ def get_switched_path(switch1, switch2, rtype="NGTREE", verbose=True, onepath=Tr # Export NGTree ngtree = nglib.query.exp_ngtree(ngtree, rtype) return ngtree - elif verbose: + elif popt['verbose']: print("No results found for path between {:} and {:}".format(switch1, switch2)) return def spath_direction(swp): - """ Adds directionality to spath queries + """ Adds directionality to spath queries All spath queries traverse the directed paths from the core out Examine _reverse value for reversal of From -> To """ @@ -557,9 +597,9 @@ def spath_direction(swp): nswp[nen] = swp[en] else: nen = en.replace('Parent', 'From') - nswp[nen] = swp[en] + nswp[nen] = swp[en] else: - nswp[en] = swp[en] + nswp[en] = swp[en] # Update Name nswp['Name'] = '#' + str(swp['distance']) + ' ' + \ @@ -580,20 +620,27 @@ def path_coverage(inc, total): return percent[0] + '%' -def get_fw_path(src, dst, rtype="TEXT", verbose=True): +def get_fw_path(src, dst, popt, rtype="TEXT"): """Discover the Firewall Path between two IP addresses""" rtypes = ('TEXT', 'TREE', 'JSON', 'YAML', 'NGTREE') if rtype in rtypes: + if 'onepath' not in popt: + popt['onepath'] = False + if 'verbose' not in popt: + popt['verbose'] = True + + if popt['verbose']: + logger.info("Query: Security Path %s -> %s for %s", src, dst, nglib.user) + logcmd = nglib.config['nglib']['logcmd'] logurl = nglib.config['nglib']['logurl'] srcnet = nglib.query.net.find_cidr(src) dstnet = nglib.query.net.find_cidr(dst) - logger.info("Query: Security Path %s -> %s for %s", src, dst, nglib.user) if nglib.verbose: print("\nFinding security path from {:} -> {:}:\n".format(srcnet, dstnet)) @@ -644,10 +691,11 @@ def get_fw_path(src, dst, rtype="TEXT", verbose=True): # Add properties #router = get_router(nProp) if 'vid' and 'vrf' in nProp: - hop['Name'] = hop['Name'] + ' [rtr:' + get_router(nProp) + ' vid:' + nProp['vid'] \ + hop['Name'] = hop['Name'] + ' [rtr:' + get_router(nProp) \ + + ' vid:' + nProp['vid'] \ + ' vrf:' + nProp['vrf'] + ']' #hop['Name'] = hop['Name'] + ' VRF:' + nProp['vrf'] - + for prop in nProp: hop[prop] = nProp[prop] nglib.ngtree.add_child_ngtree(ngtree, hop) @@ -673,7 +721,7 @@ def get_fw_path(src, dst, rtype="TEXT", verbose=True): print("\n{:} (15min): {:}{:}".format(fw, logurl, query)) - if verbose: + if popt['verbose']: print(cmd) proc = subprocess.Popen( @@ -689,8 +737,8 @@ def get_fw_path(src, dst, rtype="TEXT", verbose=True): elif out: print(out) - # Space out - print() + # Space out + print() # Export NGTree ngtree = nglib.query.exp_ngtree(ngtree, rtype) @@ -704,4 +752,4 @@ def get_router(ngtree): router = ngtree['Router'] if 'StandbyRouter' in ngtree: router = router + '|' + ngtree['StandbyRouter'] - return router \ No newline at end of file + return router diff --git a/ngtest.py b/ngtest.py index eca132b..6fbce2e 100755 --- a/ngtest.py +++ b/ngtest.py @@ -32,11 +32,8 @@ """ Test Database functions for errors """ -import os import re import sys -import argparse -import pytest import subprocess ngcmd = './netgrph.py' + ' ' @@ -79,15 +76,16 @@ # NetDB #ptest['-p 10.26.72.142 10.28.6.27'] = 'Link rVLANs' -qtest = ptest - rtest = dict() -rtest['-vlans -empty -vrange 200'] = 'v6test' -#rtest['-vlans -vrange 120 -o yaml'] = 'Root: abc4mdf' -#rtest['-v -dev "xyz.*" -o json'] = '"parent_switch": "core1"' +#rtest['-vlans -empty -vrange 200'] = 'v6test' +rtest['-vlans -vrange 120 -o yaml'] = 'Root: abc4mdf' +rtest['-v -dev "xyz.*" -o json'] = '"parent_switch": "core1"' + +prtest = dict() +prtest['-vlans -empty -vrange 200'] = 'v6test' # Update Tests (check for errors) -utest = ('-ivrf', '-id', '-ind', '-inet', '-isnet', '-iasa', '-ivlan', '-uvlan') +utest = ('-ivrf', '-id', '-ind', '-inet', '-isnet', '-iasa', '-ivlan', '-uvlan', '-ild') def run_query(cmd, option, result): """Run Queries for testing""" @@ -107,8 +105,14 @@ def run_query(cmd, option, result): else: return False +def test_dev_updates(): + print("Testing Update Routines") + + for ut in utest: + print(ut) + assert(run_query(upngcmd, ut, None)) == True -def test_queries(): +def test_dev_queries(): """netgrph query testing""" print("Testing Query Routines") @@ -121,10 +125,15 @@ def test_queries(): print(key) assert(run_query(ngrepcmd, key, rtest[key])) == True +def test_prod_updates(): + test_dev_updates() + +def test_prod_queries(): + global qtest + global rtest + qtest = ptest + rtest = prtest + + test_dev_queries() -def test_updates(): - print("Testing Update Routines") - for ut in utest: - print(ut) - assert(run_query(upngcmd, ut, None)) == True diff --git a/test/test_full.sh b/test/test_full.sh index c39472e..933bbe5 100755 --- a/test/test_full.sh +++ b/test/test_full.sh @@ -8,4 +8,9 @@ if ! type "$pytestcmd" 2> /dev/null; then export pytestcmd="py.test" fi -$pytestcmd --resultlog=/tmp/pytest.log ngtest.py +if [ "$1" = "prod" ]; then + $pytestcmd --resultlog=/tmp/pytest.log -k test_prod ngtest.py +else + $pytestcmd --resultlog=/tmp/pytest.log -k test_dev ngtest.py +fi + diff --git a/test/test_queries.sh b/test/test_queries.sh index 7989218..dff1af7 100755 --- a/test/test_queries.sh +++ b/test/test_queries.sh @@ -6,4 +6,8 @@ if ! type "$pytestcmd" 2> /dev/null; then export pytestcmd="py.test" fi -$pytestcmd --resultlog=/tmp/pytest.log ngtest.py::test_queries +if [ "$1" = "prod" ]; then + $pytestcmd --resultlog=/tmp/pytest.log ngtest.py::test_prod_queries +else + $pytestcmd --resultlog=/tmp/pytest.log ngtest.py::test_dev_queries +fi 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)