def select_host_vars(hostname: str, groups: list, protocol: str): if truth_vars_exists() is False: if verbose_mode(user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET), needed_value=LEVEL4): printline() print(f"{HEADER} Truth_vars doesn't exists") return {} if host_vars_exists(hostname, protocol): if verbose_mode(user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET), needed_value=LEVEL4): printline() print(f"{HEADER} Select hosts variables") return open_file(path=f"truth_vars/hosts/{hostname}/{protocol}.yml") if group_vars_exists(groups[0], protocol): if verbose_mode(user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET), needed_value=LEVEL4): printline() print(f"{HEADER} Select groups variables") return open_file(path=f"truth_vars/groups/{groups[0]}/{protocol}.yml") if all_vars_exists(protocol): if verbose_mode(user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET), needed_value=LEVEL4): printline() print(f"{HEADER} Select all variables") return open_file(path=f"truth_vars/all/{protocol}.yml")
def create_an_vlan_object_from_a_arista_output_command(context) -> None: """ Create an VLAN object from a Arista output :param context: :return None: """ outputs_dict = dict() outputs_dict[VLAN_GET_L2] = open_file( path=f"{FEATURES_OUTPUT_PATH}arista_show_vlan.json" ) outputs_dict[VLAN_GET_L3] = open_file( path=f"{FEATURES_OUTPUT_PATH}arista_show_ip_interface_vlan_1_4094.json" ) outputs_dict[VLAN_GET_INT] = open_file( path=f"{FEATURES_OUTPUT_PATH}arista_show_interfaces_vlan_1_4094.json" ) context.object_03 = _arista_vlan_ssh_converter( cmd_output=outputs_dict )
def create_a_mtu_object_from_a_json(context) -> None: """ JSON file data structure must be as follow: spine01: global_mtu: 1500 interfaces: lo0: 1514 :param context: :param mtu_data: Data to create a MTU object :param hostname: Device hostname :return: """ mtu_data = open_file(path=f"{FEATURES_SRC_PATH}mtu_tests.yml") hostname = list(mtu_data.keys())[0] interface_mtu_lst = ListInterfaceMTU(list()) if hostname in mtu_data.keys(): if MTU_INTER_YAML_KEY in mtu_data.get(hostname): for interface_name in mtu_data.get(hostname).get( MTU_INTER_YAML_KEY): interface_mtu_lst.interface_mtu_lst.append( InterfaceMTU( interface_name=_mapping_interface_name(interface_name), mtu_size=mtu_data.get(hostname).get( MTU_INTER_YAML_KEY).get(interface_name, NOT_SET))) context.object_02 = MTU(hostname=hostname, mtu_global=mtu_data.get(hostname).get( MTU_GLOBAL_YAML_KEY, NOT_SET), interface_mtu_lst=interface_mtu_lst)
def step_impl(context): dict_output = open_file( path=(f"{FEATURES_SRC_PATH}outputs/bgp/iosxr/netconf/" "iosxr_nc_get_bgp_one_vrf.xml")) context.o0423 = _iosxr_bgp_netconf_converter(hostname="spine03", cmd_output=dict_output, options={})
def step_impl(context): context.o0303 = _ios_facts_netconf_converter( hostname="leaf05", cmd_output=open_file( path=(f"{FEATURES_SRC_PATH}outputs/facts/ios/netconf/" "cisco_ios_nc_get_facts.xml")), options={})
def run_vlan(nr: Nornir, test_to_execute: dict) -> bool: exit_value = True if TEST_TO_EXC_VLAN_KEY in test_to_execute.keys(): if test_to_execute.get(TEST_TO_EXC_VLAN_KEY).get("test", False): get_vlan( nr=nr, filters=test_to_execute.get(TEST_TO_EXC_VLAN_KEY).get( "filters", dict({})), ) same = compare_vlan( nr=nr, vlan_yaml_data=open_file( f"{PATH_TO_VERITY_FILES}{VLAN_SRC_FILENAME}"), ) if (test_to_execute[TEST_TO_EXC_VLAN_KEY] and same is False): exit_value = False print( f"{HEADER} VLAN defined in" f"{PATH_TO_VERITY_FILES}{VLAN_SRC_FILENAME} work = {same} !!") else: print(f"{HEADER} VLAN have not been executed !!") else: print(f"{HEADER} VLAN key is not defined in" f"{PATH_TO_VERITY_FILES}{TEST_TO_EXECUTE_FILENAME} !!") return exit_value
def run_bond(nr: Nornir, test_to_execute: dict): exit_value = True if TEST_TO_EXC_BOND_KEY in test_to_execute.keys(): if test_to_execute[TEST_TO_EXC_BOND_KEY].get("test"): get_bond( nr=nr, filters=test_to_execute.get(TEST_TO_EXC_BOND_KEY).get( "filters", dict({})), ) bond_yaml_data = open_file( path=f"{PATH_TO_VERITY_FILES}{BOND_SRC_FILENAME}") same = compare_bond(nr=nr, bond_yaml_data=bond_yaml_data) if (test_to_execute[TEST_TO_EXC_BOND_KEY] and same is False): exit_value = False print( f"{HEADER} BOND defined in" f"{PATH_TO_VERITY_FILES}{BOND_SRC_FILENAME} work = {same} !!") else: print(f"{HEADER} BOND have not been executed !!") else: if verbose_mode(user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET), needed_value=LEVEL1): print(f"{HEADER} BOND key not found in" f"{PATH_TO_VERITY_FILES}{TEST_TO_EXECUTE_FILENAME} !!") return exit_value
def run_ipv4(nr: Nornir, test_to_execute: dict) -> bool: exit_value = True if TEST_TO_EXC_IPV4_KEY in test_to_execute.keys(): if test_to_execute[TEST_TO_EXC_IPV4_KEY].get("test", False): get_ipv4( nr=nr, filters=test_to_execute.get(TEST_TO_EXC_IPV4_KEY).get( "filters", dict({})), ) ipv4_yaml_data = open_file( path=f"{PATH_TO_VERITY_FILES}{IPV4_SRC_FILENAME}") same = compare_ipv4(nr, ipv4_yaml_data) if (test_to_execute[TEST_TO_EXC_IPV4_KEY] and same is False): exit_value = False print( f"{HEADER} IPv4 addresses defined in" f"{PATH_TO_VERITY_FILES}{IPV4_SRC_FILENAME} work = {same} !!") else: print(f"{HEADER} IPv4 addresses have not been executed !!") else: if verbose_mode(user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET), needed_value=LEVEL1): print(f"{HEADER} IPv4 addresses key is not defined in" f"{PATH_TO_VERITY_FILES}{TEST_TO_EXECUTE_FILENAME} !!") return exit_value
def step_impl(context): dict_output = open_file( path=(f"{FEATURES_SRC_PATH}outputs/bgp/ios/netconf/" "ios_nc_get_bgp.xml")) context.o0303 = _ios_bgp_netconf_converter(hostname="leaf05", cmd_output=dict_output, options={})
def run_ospf(nr: Nornir, test_to_execute: dict) -> bool: exit_value = True if TEST_TO_EXC_OSPF_KEY in test_to_execute.keys(): if test_to_execute[TEST_TO_EXC_OSPF_KEY].get("test", False): get_ospf(nr) ospf_data = open_file( path=f"{PATH_TO_VERITY_FILES}{OSPF_SRC_FILENAME}") works = compare_ospf( nr=nr, ospf_data=ospf_data, level_test=get_level_test(level_value=test_to_execute.get( TEST_TO_EXC_OSPF_KEY).get("level", NOT_SET)), ) if (test_to_execute[TEST_TO_EXC_OSPF_KEY] and works is False): exit_value = False print( f"{HEADER} OSPF sessions defined in" f"{PATH_TO_VERITY_FILES}{OSPF_SRC_FILENAME} work = {works} !!") else: print(f"{HEADER} OSPF have not been executed !!") else: print(f"{HEADER} OSPF sessions key is not defined in" f"{PATH_TO_VERITY_FILES}{TEST_TO_EXECUTE_FILENAME} !!") return exit_value
def create_an_ospf_object_from_a_juniper_output_command(context) -> None: """ Create an OSPF object from a Juniper output :param context: :return None: """ outputs_dict = dict() outputs_dict['default'] = dict() outputs_dict['default'][OSPF_NEI_KEY] = open_file( path=f"{FEATURES_OUTPUT_PATH}juniper_show_ospf_neighbor_detail.json") outputs_dict['default'][OSPF_RIB_KEY] = open_file( path=f"{FEATURES_OUTPUT_PATH}juniper_show_ospf_overview.json") context.object_04 = _juniper_ospf_converter(hostname="leaf04", cmd_outputs=outputs_dict)
def create_an_lldp_object_from_a_yaml_corresponding_to_cumulus( context) -> None: """ Retrieve data from a YAML file to compare with a LLDP object :param context: :return None: """ context.object_02 = open_file(path=f"{FEATURES_SRC_PATH}lldp_tests.yml")
def create_an_ipv4_object_from_a_nexus_output_command(context) -> None: """ Create an IPV4 object from a Cisco Nexus output :param context: :return None: """ outputs_lst = list() outputs_lst.append( open_file(path=f"{FEATURES_OUTPUT_PATH}nexus_show_ip_interface.json")) data = open_file( path=f"{FEATURES_OUTPUT_PATH}nexus_show_ip_interface_vrf_client_00.json" ) if data != "" and data is not None: outputs_lst.append(data) outputs_lst.append( open_file( path= f"{FEATURES_OUTPUT_PATH}nexus_show_ip_interface_vrf_management.json" )) outputs_lst.append( open_file( path= f"{FEATURES_OUTPUT_PATH}nexus_show_ip_interface_vrf_test_dylan.json" )) context.object_04 = _nexus_ipv4_converter(hostname="leaf01", plateform="nxos", cmd_outputs=outputs_lst, filters={ "get_loopback": False, "get_physical": False, "get_vlan": True, "get_peerlink": False, "get_vni": False })
def step_impl(context): cmd_output = dict() cmd_output[FACTS_SYS_DICT_KEY] = open_json_file( path=(f"{FEATURES_SRC_PATH}outputs/facts/juniper/netconf/" "juniper_nc_get_facts.json")) cmd_output[FACTS_INT_DICT_KEY] = open_file( path=(f"{FEATURES_SRC_PATH}outputs/facts/juniper/netconf/" "juniper_nc_get_interfaces_terse.xml")) context.o0503 = _juniper_facts_netconf_converter(hostname="leaf04", cmd_output=cmd_output, options={})
def create_a_static_object_from_a_json(context) -> None: """ Retrieve data from a YAML file to compare with a Static object :param context: :return None: """ yaml_content = open_file(path=f"{FEATURES_SRC_PATH}static_tests.yml") context.object_02 = yaml_content
def create_a_mtu_object_from_a_nexus_output_command(context) -> None: """ Create a MTU object from a Nexus output :param context: :return None: """ context.object_06 = _cumulus_mtu_converter( hostname="spine01", cmd_output=open_file( path=f"{FEATURES_OUTPUT_PATH}nexus_show_interface.json"))
def create_an_lldp_object_from_a_juniper_output_command(context) -> None: """ Create an LLDP object from a Juniper output :param context: :return None: """ context.object_03 = _juniper_lldp_converter( hostname="leaf04", cmd_output=open_file( path=f"{FEATURES_OUTPUT_PATH}juniper_show_lldp_neighbors.json"))
def main(netest_config_file, inventory_config_file, check_connectivity, devices, devices_number, devices_group, inventory, nornir_groups_file, nornir_defaults_file, netbox_url, netbox_token, netbox_ssl, reports, terminal, verbose, num_workers, ansible_inventory, netbox_inventory, nornir_inventory, init_data): urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) t = open_file(path=netest_config_file) os.environ["NETESTS_VERBOSE"] = f"{verbose}" # Create Nornir object try: nr = init_nornir( log_file="./nornir/nornir.log", log_level="debug", ansible_inventory=ansible_inventory, nornir_inventory=nornir_inventory, netbox_inventory=netbox_inventory, num_workers=num_workers, inventory_config_file=inventory_config_file, inventory=inventory, nornir_groups_file=nornir_groups_file, nornir_defaults_file=nornir_defaults_file, netbox_url=netbox_url, netbox_token=netbox_token, netbox_ssl=netbox_ssl, ) except FileNotFoundError as e: print(f"{HEADER} Inventory file not found ...") print(f"{HEADER} {e}") exit(EXIT_FAILURE) if terminal: netests_cli(nr) exit(EXIT_SUCCESS) p(comment="Devices selected", json_to_print=nr.inventory.hosts) if check_connectivity: if check_devices_connectivity(nr): exit(EXIT_SUCCESS) else: exit(EXIT_FAILURE) exit_value = True for k, v in t.get('config').get('protocols').items(): if (run_base(nr=nr, protocol=k, parameters=v, init_data=init_data) is False and exit_value is True): exit_value = False return exit_value
def create_a_static_object_from_a_arista_output_command(context) -> None: """ Create a Static object from a Arista output :param context: :return None: """ cmd_outputs = list() cmd_outputs.append( open_file( path= f"{FEATURES_OUTPUT_PATH}arista_show_ip_route_static_default.json")) cmd_outputs.append( open_file( path=f"{FEATURES_OUTPUT_PATH}arista_show_ip_route_static_mgmt.json" )) context.object_03 = _arista_static_converter(hostname="leaf03", cmd_outputs=cmd_outputs)
def create_an_vlan_object_from_a_cumulus_output_command(context) -> None: """ Create an VLAN object from a Cumulus output :param context: :return None: """ outputs_dict = dict() vlan_data = open_txt_file( path=f"{FEATURES_OUTPUT_PATH}cumulus_net_show_vrf_list.output" ) if vlan_data != "": template = open( f"{TEXTFSM_PATH}cumulus_net_show_vrf_list.textfsm") results_template = textfsm.TextFSM(template) outputs_dict[VLAN_VRF_LIST_KEY] = results_template.ParseText(vlan_data) outputs_dict[VLAN_VRF_DETAIL_KEY] = open_file( path=f"{FEATURES_OUTPUT_PATH}cumulus_net_show_interface.json" ) outputs_dict[VLAN_VRF_MEMBERS_KEY] = open_file( path=f"{FEATURES_OUTPUT_PATH}cumulus_net_show_bridge_vlan.json" ) context.object_04 = _cumulus_vlan_converter( bond_lst=["bond02"], cmd_output=outputs_dict, filters={ "get_default": False, "get_bridge": False, "get_vni": False, "get_peerlink": False, "get_lag": True } )
def create_an_lldp_object_from_a_cumulus_output_command(context) -> None: """ Create an LLDP object from a Juniper output :param context: :return None: """ context.object_04 = _cumulus_lldp_converter( hostname="spine01", cmd_output=open_file( path=f"{FEATURES_OUTPUT_PATH}cumulus_net_show_lldp_neighbors.json") )
def create_a_static_object_from_a_juniper_output_command(context) -> None: """ Create a Static object from a Arista output :param context: :return None: """ cmd_outputs = open_file( path=f"{FEATURES_OUTPUT_PATH}juniper_show_route_protocol_static.json") context.object_04 = _juniper_static_converter(hostname="leaf04", cmd_outputs=cmd_outputs)
def create_an_mlag_object_from_a_cumulus_output_command(context) -> None: """ Create an MLAG object from a Cumulus output :param context: :return None: """ context.object_03 = _cumulus_mlag_converter( hostname="leaf01", cmd_output=open_file( path=f"{FEATURES_OUTPUT_PATH}cumulus_net_show_clag.json" ) )
def create_an_ospf_object_from_a_cumulus_output_command(context) -> None: """ Create an OSPF object from a Cumulus output :param context: :return None: """ outputs_lst = list() # VRF - Default data = dict() data['rid'] = open_file( path=f"{FEATURES_OUTPUT_PATH}cumulus_net_show_ospf.json") data['data'] = open_file( path=f"{FEATURES_OUTPUT_PATH}cumulus_net_show_ospf_neighbor_detail.json" ) outputs_lst.append(data) # VRF - mgmt data = dict() data['rid'] = open_file( path=f"{FEATURES_OUTPUT_PATH}cumulus_net_show_ospf_vrf.json") data['data'] = open_file( path= f"{FEATURES_OUTPUT_PATH}cumulus_net_show_ospf_vrf_neighbor_detail.json" ) outputs_lst.append(data) context.object_03 = _cumulus_ospf_converter(hostname="spine01", cmd_outputs=outputs_lst)
def compare_mtu_object_01_and_json_object(context) -> None: """ Compate object based on a verity file :param context: :param mtu_data: :param hostname: :return: """ mtu_data = open_file(path=f"{FEATURES_SRC_PATH}mtu_tests_validate.yml") hostname = list(mtu_data.keys())[0] assert _compare_mtu(hostname=hostname, mtu_host_data=context.object_03, mtu_yaml_data=mtu_data)
def _compare_facts(host_keys, hostname: str, groups: list, facts_host_data: Facts, test=False, options={}, task=Task) -> bool: if ('own_vars' in options.keys() and options.get('own_vars') is not None and 'enable' in options.get('own_vars').keys() and options.get('own_vars').get('enable') is True): raise NetestsOverideTruthVarsKeyUnsupported() else: if test: facts_yaml_data = open_file( path="tests/features/src/facts_tests.yml").get(hostname) else: facts_yaml_data = select_host_vars(hostname=hostname, groups=groups, protocol="facts") if (FACTS_DATA_HOST_KEY in host_keys and facts_yaml_data is not None): verity_facts = Facts( hostname=hostname, domain=facts_yaml_data.get('domain', NOT_SET), version=facts_yaml_data.get('version', NOT_SET), build=facts_yaml_data.get('build', NOT_SET), serial=facts_yaml_data.get('serial', NOT_SET), base_mac=facts_yaml_data.get('serial', NOT_SET), memory=facts_yaml_data.get('memory', NOT_SET), vendor=facts_yaml_data.get('vendor', NOT_SET), model=facts_yaml_data.get('model', NOT_SET), interfaces_lst=facts_yaml_data.get('interfaces', list()), options=facts_host_data.options) else: print(f"{HEADER} Key {FACTS_DATA_HOST_KEY} is missing" f"for {hostname} or no Facts data has been found.") return False if verbose_mode(user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET), needed_value=LEVEL2): print(f"{HEADER} Return value for host {hostname}" f"is {verity_facts == facts_host_data}") return verity_facts == facts_host_data
def _compare_vrf(host_keys, hostname: str, groups: list, vrf_host_data: ListVRF, test=False, options={}, task=Task) -> bool: verity_vrf = ListVRF(list()) if ('own_vars' in options.keys() and options.get('own_vars') is not None and 'enable' in options.get('own_vars').keys() and options.get('own_vars').get('enable') is True): raise NetestsOverideTruthVarsKeyUnsupported() else: if test: vrf_yaml_data = open_file( path="tests/features/src/vrf_tests.yml").get(hostname) else: vrf_yaml_data = select_host_vars(hostname=hostname, groups=groups, protocol="vrf") if (VRF_DATA_KEY in host_keys and vrf_yaml_data is not None): if vrf_yaml_data is not None: for vrf in vrf_yaml_data: verity_vrf.vrf_lst.append( VRF(vrf_name=vrf.get('vrf_name', NOT_SET), vrf_id=vrf.get('vrf_id', NOT_SET), l3_vni=vrf.get('l3_vni', NOT_SET), rd=vrf.get('rd', NOT_SET), rt_imp=vrf.get('rt_imp', NOT_SET), rt_exp=vrf.get('rt_exp', NOT_SET), imp_targ=vrf.get('imp_targ', NOT_SET), exp_targ=vrf.get('exp_targ', NOT_SET))) else: print(f"{HEADER} Key {VRF_DATA_KEY} is missing" f"for {hostname} or no VRF data has been found.") if verbose_mode(user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET), needed_value=LEVEL2): print(f"{HEADER} Return value for host {hostname}" f"is {verity_vrf == vrf_host_data}") return verity_vrf == vrf_host_data
def run_cdp(nr: Nornir, test_to_execute: dict) -> bool: exit_value = True if TEST_TO_EXC_CDP_KEY in test_to_execute.keys(): if test_to_execute[TEST_TO_EXC_CDP_KEY] is True: get_cdp(nr) cdp_data = open_file(f"{PATH_TO_VERITY_FILES}{CDP_SRC_FILENAME}") same = compare_cdp(nr, cdp_data) if (test_to_execute[TEST_TO_EXC_CDP_KEY] and same is False): exit_value = False print(f"{HEADER} CDP sessions are the same that defined in" f"{PATH_TO_VERITY_FILES}{CDP_SRC_FILENAME} = {same} !!") else: print(f"{HEADER} CDP sessions tests are not executed !!") else: if verbose_mode(user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET), needed_value=LEVEL1): print(f"{HEADER} CDP sessions key is not defined in" f"{PATH_TO_VERITY_FILES}{TEST_TO_EXECUTE_FILENAME} !!") return exit_value
def create_an_ipv4_object_from_a_napalm_output_command(context) -> None: """ Create an IPV4 object from a NAPALM output :param context: :return None: """ context.object_09 = _napalm_ipv4_converter( hostname="napalm01", plateform="eos", cmd_output=open_file( path=f"{FEATURES_OUTPUT_PATH}napalm_get_interfaces.json"), filters={ "get_loopback": True, "get_physical": True, "get_vlan": False, "get_peerlink": False, "get_vni": False })
def create_an_ipv6_object_from_a_cumulus_output_command(context) -> None: """ Create an IPV6 object from a Cumulus output :param context: :return None: """ context.object_04 = _cumulus_ipv6_converter( hostname="leaf03", plateform="linux", cmd_output=open_file( path=f"{FEATURES_OUTPUT_PATH}cumulus_net_show_interface.json"), filters={ "get_loopback": True, "get_physical": True, "get_vlan": True, "get_peerlink": False, "get_vni": False })