Merge "Fix port-forwarding issue in pod restart tests"
diff --git a/TestON/drivers/common/api/controller/trexclientdriver.py b/TestON/drivers/common/api/controller/trexclientdriver.py
index f6dfffe..de65f3a 100644
--- a/TestON/drivers/common/api/controller/trexclientdriver.py
+++ b/TestON/drivers/common/api/controller/trexclientdriver.py
@@ -568,7 +568,8 @@
             # Assume whole the bucket experienced the range_end latency.
             all_latencies += [range_end] * val
         q = [50, 75, 90, 99, 99.9, 99.99, 99.999]
-        percentiles = np.percentile(all_latencies, q)
+        # Prevent exception if we have no latency histogram
+        percentiles = np.percentile(all_latencies, q) if len(all_latencies) > 0 else [sys.maxint] * len(q)
 
         ret = LatencyStats(
             pg_id=pg_id,
diff --git a/TestON/tests/USECASE/SegmentRouting/SRStaging/SRpairedLeaves/SRpairedLeaves.py b/TestON/tests/USECASE/SegmentRouting/SRStaging/SRpairedLeaves/SRpairedLeaves.py
index fe7c839..ecdb769 100644
--- a/TestON/tests/USECASE/SegmentRouting/SRStaging/SRpairedLeaves/SRpairedLeaves.py
+++ b/TestON/tests/USECASE/SegmentRouting/SRStaging/SRpairedLeaves/SRpairedLeaves.py
@@ -582,6 +582,7 @@
         """
         try:
             from tests.USECASE.SegmentRouting.SRStaging.dependencies.SRStagingTest import SRStagingTest
+            from tests.USECASE.SegmentRouting.dependencies.Testcaselib import Testcaselib as run
             import json
             import re
         except ImportError:
@@ -615,30 +616,9 @@
         # Add route in host to outside host via gateway ip
         srcIface = srcComponent.interfaces[0].get( 'name' )
         srcIp = srcComponent.getIPAddress( iface=srcIface )
-        hostsJson = json.loads( main.Cluster.active( 0 ).hosts() )
-        netcfgJson = json.loads( main.Cluster.active( 0 ).getNetCfg( subjectClass='ports') )
-        ips = []
-        fabricIntfIp = None
-        for obj in hostsJson:
-            if srcIp in obj['ipAddresses']:
-                for location in obj['locations']:
-                    main.log.debug( location )
-                    did = location['elementId'].encode( 'utf-8' )
-                    port = location['port'].encode( 'utf-8' )
-                    m = re.search( '\((\d+)\)', port )
-                    if m:
-                        port = m.group(1)
-                    portId = "%s/%s" % ( did, port )
-                    # Lookup ip assigned to this network port
-                    ips.extend( [ x.encode( 'utf-8' ) for x in netcfgJson[ portId ][ 'interfaces' ][0][ 'ips' ] ] )
-        ips = set( ips )
-        ipRE = r'(\d+\.\d+\.\d+\.\d+)/\d+|([\w,:]*)/\d+'
-        for ip in ips:
-            ipMatch = re.search( ipRE, ip )
-            if ipMatch:
-                fabricIntfIp = ipMatch.group(1)
-                main.log.debug( "Found %s as gateway ip for %s" % ( fabricIntfIp, srcComponent.shortName ) )
-                # FIXME: How to chose the correct one if there are multiple? look at subnets
+
+        fabricIntfIp = run.getFabricIntfIp( main, srcIp )
+
         addResult = srcComponent.addRouteToHost( route, fabricIntfIp, srcIface, sudoRequired=True, purgeOnDisconnect=True )
         failMsg = "Failed to add static route to host"
         utilities.assert_equals( expect=main.TRUE, actual=addResult,
diff --git a/TestON/tests/USECASE/SegmentRouting/dependencies/Testcaselib.py b/TestON/tests/USECASE/SegmentRouting/dependencies/Testcaselib.py
index 99327f7..5638949 100644
--- a/TestON/tests/USECASE/SegmentRouting/dependencies/Testcaselib.py
+++ b/TestON/tests/USECASE/SegmentRouting/dependencies/Testcaselib.py
@@ -793,6 +793,41 @@
                                         cliPort=main.Cluster.active(0).CLI.karafPort )
 
     @staticmethod
+    def getFabricIntfIp( main, hostIp, hostsJson=None, netcfgJson=None ):
+        '''
+        Get the fabric interface IP of a given host
+        '''
+        # NOTE: We pass in json instead of loading them here since finding all host's gateway ips
+        #       would require 2 rest calls per host
+        if not hostsJson:
+            hostsJson = json.loads( main.Cluster.active( 0 ).hosts() )
+        if not netcfgJson:
+            netcfgJson = json.loads( main.Cluster.active( 0 ).getNetCfg( subjectClass='ports') )
+        ips = []
+        fabricIntfIp = None
+        for obj in hostsJson:
+            if hostIp in obj['ipAddresses']:
+                for location in obj['locations']:
+                    main.log.debug( location )
+                    did = location['elementId'].encode( 'utf-8' )
+                    port = location['port'].encode( 'utf-8' )
+                    m = re.search( '\((\d+)\)', port )
+                    if m:
+                        port = m.group(1)
+                    portId = "%s/%s" % ( did, port )
+                    # Lookup ip assigned to this network port
+                    ips.extend( [ x.encode( 'utf-8' ) for x in netcfgJson[ portId ][ 'interfaces' ][0][ 'ips' ] ] )
+        ips = set( ips )
+        ipRE = r'(\d+\.\d+\.\d+\.\d+)/\d+|([\w,:]*)/\d+'
+        for ip in ips:
+            ipMatch = re.search( ipRE, ip )
+            if ipMatch:
+                fabricIntfIp = ipMatch.group(1)
+                main.log.debug( "Found %s as gateway ip for %s" % ( fabricIntfIp, hostIp ) )
+                # FIXME: How to chose the correct one if there are multiple? look at subnets
+        return fabricIntfIp
+
+    @staticmethod
     def pingAllFabricIntfs( main, srcList, tag="", dumpFlows=True, skipOnFail=False ):
         '''
         Verify connectivity between hosts and their fabric interfaces
@@ -801,8 +836,7 @@
         if tag == "":
             tag = 'CASE%d' % main.CurrentTestCaseNumber
         expect = main.TRUE
-        import json
-        import re
+
         hostsJson = json.loads( main.Cluster.active( 0 ).hosts() )
         netcfgJson = json.loads( main.Cluster.active( 0 ).getNetCfg( subjectClass='ports') )
         for hostname in srcList:
@@ -813,34 +847,15 @@
                 #Get host location, check netcfg for that port's ip
                 hostIp = hostComponent.getIPAddress( iface=srcIface )
                 main.log.warn( "Looking for %s" % hostIp )
-                ips = []
-                for obj in hostsJson:
-                    if hostIp in obj['ipAddresses']:
-                        for location in obj['locations']:
-                            main.log.debug( location )
-                            did = location['elementId'].encode( 'utf-8' )
-                            port = location['port'].encode( 'utf-8' )
-                            m = re.search( '\((\d+)\)', port )
-                            if m:
-                                port = m.group(1)
-                            portId = "%s/%s" % ( did, port )
-                            # Lookup ip assigned to this network port
-                            ips.extend( [ x.encode( 'utf-8' ) for x in netcfgJson[ portId ][ 'interfaces' ][0][ 'ips' ] ] )
-                ips = set( ips )
-                ipRE = r'(\d+\.\d+\.\d+\.\d+)/\d+|([\w,:]*)/\d+'
-                for ip in ips:
-                    ipMatch = re.search( ipRE, ip )
-                    if ipMatch:
-                        fabricIntfIp = ipMatch.group(1)
-                        main.log.debug( "Found %s as gateway ip for %s" % ( fabricIntfIp, hostname ) )
-                        pa = hostComponent.ping( fabricIntfIp, interface=srcIface )
-                        utilities.assert_equals( expect=expect, actual=pa,
-                                                 onpass="IP connectivity successfully tested",
-                                                 onfail="IP connectivity failed" )
-                        if pa != expect:
-                            if skipOnFail:
-                                Testcaselib.cleanup( main, copyKarafLog=False )
-                                main.skipCase()
+                fabricIntfIp = Testcaselib.getFabricIntfIp( main, hostIp, hostsJson, netcfgJson )
+                pa = hostComponent.ping( fabricIntfIp, interface=srcIface )
+                utilities.assert_equals( expect=expect, actual=pa,
+                                         onpass="IP connectivity successfully tested",
+                                         onfail="IP connectivity failed" )
+                if pa != expect:
+                    if skipOnFail:
+                        Testcaselib.cleanup( main, copyKarafLog=False )
+                        main.skipCase()
             except ValueError:
                 main.log.exception( "Could not get gateway ip for %s" % hostname )