def check_traffic_get_rx_stats(sensor_id, ifaces, delay): # First I need the sensor ip (result, admin_ip) = get_sensor_ip_from_sensor_id(sensor_id) dresult = {} msg = "Internal Error" r = False if result == True: (r, data) = get_iface_list(admin_ip) if r == True: if ifaces == []: l = data.keys() else: l = ifaces # Now check traffic (r, statsfirst) = get_iface_stats(admin_ip) if r == True: time.sleep(delay) (r, statslast) = get_iface_stats(admin_ip) # Now, check each result: for iface in l: first = statsfirst.get(iface) last = statslast.get(iface) if first is not None and last is not None: if ((last['RX'] - first['RX']) > 0): dresult[iface] = 'yes' else: dresult[iface] = 'no' msg = "OK" r = True else: r = False msg = "Error obtains iface stats" else: r = False msg = "Can't obtain iface admin ip => " + str(admin_ip) else: r = False msg = "No admin ip for uuid " + sensor_id return (r, msg, dresult)
def check_traffic_get_rx_stats (sensor_id,ifaces,delay): # First I need the sensor ip (result,admin_ip) = get_sensor_ip_from_sensor_id (sensor_id) dresult = {} msg = "Internal Error" r = False if result == True: (r,data) = get_iface_list (admin_ip) if r == True: if ifaces == []: l = data.keys() else: l = ifaces # Now check traffic (r,statsfirst) = get_iface_stats (admin_ip) if r == True: time.sleep (delay) (r,statslast ) = get_iface_stats (admin_ip) # Now, check each result: for iface in l: first = statsfirst.get(iface) last = statslast.get(iface) if first is not None and last is not None: if ((last['RX']-first['RX'])> 0): dresult[iface] = 'yes' else: dresult[iface] = 'no' msg = "OK" r = True else: r = False msg = "Error obtains iface stats" else: r = False msg = "Can't obtain iface admin ip => " + str(admin_ip) else: r = False msg = "No admin ip for uuid " + sensor_id return (r, msg,dresult)
def test_get_iface_stats (self): print ("Testing get_iface_stats") # This difficult to test, because the dynamic nature of the counter # I'm only test if the function return all the ifaces in the system (result, dstats) = get_iface_stats ("127.0.0.1") nose.tools.ok_ (result == True, msg="Error:get_iface_list: Can't get iface list") nose.tools.ok_ (type(dstats).__name__ == 'dict', msg="Error:get_iface_list: Bad type") ifaces = [x.strip() for x in dstats.keys() if x != 'lo'] sys_ifaces = [iface.name for iface in netinterfaces.get_network_interfaces() if iface.name !='lo'] nose.tools.ok_ (len (sys_ifaces) == len (ifaces), msg="Number of interfaces mismatch") sys_set = set(sys_ifaces) ansible_set = set(ifaces) nose.tools.ok_(ansible_set.issubset(sys_set)) # verify each result #print d for value in dstats.values(): #print v,type(v).__name__ nose.tools.ok_ (type(value).__name__ == 'dict', msg="Error:get_iface_stats: Bad type") nose.tools.ok_ (len(value) ==2, msg="Error:get_iface_stats: Bad response in key " + str(value)) nose.tools.ok_ (value.get('TX') != None, msg="Error:get_iface_stats: No TX key in response" + str(value)) nose.tools.ok_ (value.get('RX') != None, msg="Error:get_iface_stats: No RX key in response" + str(value))