def setUpClass(cls): print( "TestNetworkSetInterfaces::setup_class() before any methods in this class" ) rc, net_current_status = get_iface_list(admin_ip) cls.net_current_status = net_current_status
def test_get_iface_list(self): print ("Testing get_iface_list") (result, ifaces_list) = get_iface_list ("127.0.0.1") nose.tools.ok_ (result == True, "Error:get_iface_list: Can't get iface list") nose.tools.ok_ (type(ifaces_list).__name__ == 'dict', "Error:get_iface_list: Bad type") ifaces = [x.strip() for x in ifaces_list.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))
def network_status(system_id, no_cache=False): """ Return the network facts. Args: system_id (str): A valid uuid or local no_cache (bool): Not used, but we need it declared to make happy the @use_cache decorator Returns: A tuple (success, data) where *success* is a boolean informing the success (True) or failure (False) of the call the *data* member return the network facts as a dict. On error, a message about it is returned in the *data* field. """ success, system_ip = get_system_ip_from_system_id(system_id) if not success: return False, system_ip success, ifaces = get_iface_list(system_ip) if success: # Get the iface disk # ifaces = setup_data['ansible_interfaces'] # ipv4default = setup_data['ansible_default_ipv4'] # Get the network_status_facts success, facts = ans_network_status(system_ip) if success: for iface in facts['interfaces'].keys(): if iface in ifaces: # iface_data = setup_data['ansible_' + iface] if ifaces[iface].get('ipv4', None) is not None: facts['interfaces'][iface]['ipv4'] = ifaces[iface][ 'ipv4'] facts['interfaces'][iface]['role'] = ifaces[iface]['role'] # Add the a "UP" flags # if iface_data['active'] is True: # facts.data['interfaces'][iface]['status'] = 'UP' # else: # facts.data['interfaces'][iface]['status'] = 'DOWN' # Check gateway # if ipv4default.get('interface', None) == iface and ipv4default.get('gateway', None) is not None: # facts.data['gateway'] = ipv4default.get('gateway') pass return True, facts else: return False, facts else: return False, ifaces
def network_status(system_id, no_cache=False): """ Return the network facts. Args: system_id (str): A valid uuid or local no_cache (bool): Not used, but we need it declared to make happy the @use_cache decorator Returns: A tuple (success, data) where *success* is a boolean informing the success (True) or failure (False) of the call the *data* member return the network facts as a dict. On error, a message about it is returned in the *data* field. """ success, system_ip = get_system_ip_from_system_id(system_id) if not success: return False, system_ip success, ifaces = get_iface_list(system_ip) if success: # Get the iface disk # ifaces = setup_data['ansible_interfaces'] # ipv4default = setup_data['ansible_default_ipv4'] # Get the network_status_facts success, facts = ans_network_status(system_ip) if success: for iface in facts["interfaces"].keys(): if iface in ifaces: # iface_data = setup_data['ansible_' + iface] if ifaces[iface].get("ipv4", None) is not None: facts["interfaces"][iface]["ipv4"] = ifaces[iface]["ipv4"] facts["interfaces"][iface]["role"] = ifaces[iface]["role"] # Add the a "UP" flags # if iface_data['active'] is True: # facts.data['interfaces'][iface]['status'] = 'UP' # else: # facts.data['interfaces'][iface]['status'] = 'DOWN' # Check gateway # if ipv4default.get('interface', None) == iface and ipv4default.get('gateway', None) is not None: # facts.data['gateway'] = ipv4default.get('gateway') pass return True, facts else: return False, facts else: return False, ifaces
def test_0012(self): """Test 0012: valid configuration """ print('TestNetworkSetInterfaces:: test_valid_config()') system_ip = admin_ip roles = {'eth1': {'role': 'log_management', 'ipaddress': NET_IPS['eth1'], 'netmask': '255.255.255.0'}, 'eth2': {'role': 'log_management', 'ipaddress': NET_IPS['eth2'], 'netmask': '255.255.255.0'}, 'eth3': {'role': 'monitoring', 'ipaddress': NET_IPS['eth3'], 'netmask': '255.255.255.0'}, 'eth4': {'role': 'monitoring', 'ipaddress': NET_IPS['eth4'], 'netmask': '255.255.255.0'}, 'eth5': {'role': 'monitoring', 'ipaddress': NET_IPS['eth5'], 'netmask': '255.255.255.0'}} rc, data = set_interfaces_roles(system_ip, roles) self.assertTrue(rc) rc, data = get_iface_list(admin_ip) self.assertTrue(rc) self.assertTrue(self.check_request_and_response(roles, data))
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_0012(self): """Test 0012: valid configuration """ print('TestNetworkSetInterfaces:: test_valid_config()') system_ip = admin_ip roles = { 'eth1': { 'role': 'log_management', 'ipaddress': NET_IPS['eth1'], 'netmask': '255.255.255.0' }, 'eth2': { 'role': 'log_management', 'ipaddress': NET_IPS['eth2'], 'netmask': '255.255.255.0' }, 'eth3': { 'role': 'monitoring', 'ipaddress': NET_IPS['eth3'], 'netmask': '255.255.255.0' }, 'eth4': { 'role': 'monitoring', 'ipaddress': NET_IPS['eth4'], 'netmask': '255.255.255.0' }, 'eth5': { 'role': 'monitoring', 'ipaddress': NET_IPS['eth5'], 'netmask': '255.255.255.0' } } rc, data = set_interfaces_roles(system_ip, roles) self.assertTrue(rc) rc, data = get_iface_list(admin_ip) self.assertTrue(rc) self.assertTrue(self.check_request_and_response(roles, data))
def setUpClass(cls): print("TestNetworkSetInterfaces::setup_class() before any methods in this class") rc, net_current_status = get_iface_list(admin_ip) cls.net_current_status = net_current_status