def apply_route_config_for_port(request, duthost, ptfhost, define_sub_ports_configuration, apply_config_on_the_dut, apply_config_on_the_ptf): """ Apply route configuration on the PTF and remove after tests Args: request: pytest request object duthost: DUT host object ptfhost: PTF host object define_sub_ports_configuration: Dictonary of parameters for configuration DUT apply_config_on_the_dut: fixture for applying sub-ports configuration on the DUT apply_config_on_the_ptf: fixture for applying sub-ports configuration on the PTF Yields: Dictonary of parameters for configuration DUT and PTF host """ port_map = {} vlan_id = 999 sub_ports = define_sub_ports_configuration['sub_ports'] dut_ports = define_sub_ports_configuration['dut_ports'] port_type = define_sub_ports_configuration['port_type'] subnet = define_sub_ports_configuration['subnet'] # Get additional port for configuration of SVI port or L3 RIF if 'svi' in request.param: interface_num = 1 else: interface_num = 2 dut_ports, ptf_ports = get_port(duthost, ptfhost, interface_num, port_type, dut_ports.values(), exclude_sub_interface_ports=True) # Get additional IP addresses for configuration of RIF on the DUT and PTF subnet = ipaddress.ip_network(str(subnet.broadcast_address + 1) + u'/24') subnets = [i for i, _ in zip(subnet.subnets(new_prefix=30), dut_ports)] sub_ports_keys = sub_ports.copy() for dut_port, ptf_port, subnet in zip(dut_ports.values(), ptf_ports, subnets): dut_port_ip, ptf_port_ip = ('{}/{}'.format(host, 30) for host in subnet.hosts()) remove_ip_from_port(duthost, dut_port) if 'svi' in request.param: # Configure SVI port on the DUT ptf_port = '{}.{}'.format(ptf_port, vlan_id) remove_member_from_vlan(duthost, vlan_id, dut_port) setup_vlan(duthost, vlan_id) add_member_to_vlan(duthost, vlan_id, dut_port) add_ip_to_dut_port(duthost, 'Vlan{}'.format(vlan_id), dut_port_ip) # Configure additional sub-port for connection between SVI port of the DUT and PTF create_sub_port_on_ptf(ptfhost, ptf_port, ptf_port_ip) else: # should remove port from Vlan1000 first to configure L3 RIF remove_member_from_vlan(duthost, '1000', dut_port) # Configure L3 RIF on the DUT add_ip_to_dut_port(duthost, dut_port, dut_port_ip) # Configure L3 RIF on the PTF add_ip_to_ptf_port(ptfhost, ptf_port, ptf_port_ip) # Get two random sub-ports which are not part of the selected DUT interface sub_ports_on_port = random.sample([ sub_port for sub_port in sub_ports_keys if dut_port + '.' not in sub_port ], 2) for sub_port in sub_ports_on_port: sub_ports_keys.pop(sub_port) port_map[ptf_port] = { 'dut_port': dut_port, 'ip': ptf_port_ip, 'neighbor_ip': dut_port_ip, 'dst_ports': [] } # Configure static route between selected sub-ports and selected interfaces on the PTF for next_hop_sub_port in sub_ports_on_port: name_of_namespace = 'vnet_for_{}'.format(next_hop_sub_port) dst_port_network = ipaddress.ip_network(unicode( sub_ports[next_hop_sub_port]['neighbor_ip']), strict=False) # Add selected sub-port to namespace on the PTF add_port_to_namespace( ptfhost, name_of_namespace, sub_ports[next_hop_sub_port]['neighbor_port'], sub_ports[next_hop_sub_port]['neighbor_ip']) # Add static route from sub-port to selected interface on the PTF add_static_route_to_ptf(ptfhost, subnet, sub_ports[next_hop_sub_port]['ip'], name_of_namespace) # Add static route from selected interface to sub-port on the PTF add_static_route_to_ptf(ptfhost, dst_port_network, dut_port_ip) port_map[ptf_port]['dst_ports'].append( (next_hop_sub_port, name_of_namespace)) yield {'port_map': port_map, 'sub_ports': sub_ports} # Teardown for src_port, next_hop_sub_ports in port_map.items(): src_port_network = ipaddress.ip_network(unicode( next_hop_sub_ports['ip']), strict=False) # Remove static route between selected sub-ports and selected interfaces from the PTF for sub_port, name_of_namespace in next_hop_sub_ports['dst_ports']: dst_port_network = ipaddress.ip_network(unicode( sub_ports[sub_port]['ip']), strict=False) remove_static_route_from_ptf(ptfhost, src_port_network, sub_ports[sub_port]['ip'], name_of_namespace) remove_static_route_from_ptf(ptfhost, dst_port_network, next_hop_sub_ports['neighbor_ip']) remove_namespace(ptfhost, name_of_namespace) if 'svi' in request.param: # Remove SVI port from the DUT remove_member_from_vlan(duthost, vlan_id, next_hop_sub_ports['dut_port']) remove_ip_from_port(duthost, 'Vlan{}'.format(vlan_id), ip=next_hop_sub_ports['neighbor_ip']) # Remove additional sub-port from the PTF remove_sub_port_from_ptf(ptfhost, src_port, next_hop_sub_ports['ip']) if 'port_in_lag' in port_type: bond_port = src_port.split('.')[0] cfg_facts = duthost.config_facts( host=duthost.hostname, source="running")['ansible_facts'] remove_lag_port(duthost, cfg_facts, next_hop_sub_ports['dut_port']) remove_bond_port(ptfhost, bond_port, ptf_ports[bond_port]) else: # Remove L3 RIF from the DUT remove_ip_from_port(duthost, next_hop_sub_ports['dut_port'], ip=next_hop_sub_ports['neighbor_ip']) # Remove L3 RIF from the PTF remove_ip_from_ptf_port(ptfhost, src_port, next_hop_sub_ports['ip']) if 'port_in_lag' in port_type: bond_port = src_port cfg_facts = duthost.config_facts( host=duthost.hostname, source="running")['ansible_facts'] remove_lag_port(duthost, cfg_facts, next_hop_sub_ports['dut_port']) remove_bond_port(ptfhost, bond_port, ptf_ports[bond_port]) if 'svi' in request.param: remove_vlan(duthost, vlan_id)
def apply_route_config(request, ptfhost, define_sub_ports_configuration, apply_config_on_the_dut, apply_config_on_the_ptf): """ Apply route configuration on the PTF and remove after tests Args: request: pytest request object ptfhost: PTF host object define_sub_ports_configuration: Dictonary of parameters for configuration DUT apply_config_on_the_dut: fixture for applying sub-ports configuration on the DUT apply_config_on_the_ptf: fixture for applying sub-ports configuration on the PTF Yields: Dictonary of parameters for configuration DUT and PTF host """ new_sub_ports = {} sub_ports = define_sub_ports_configuration['sub_ports'] dut_ports = define_sub_ports_configuration['dut_ports'] sub_ports_keys = sub_ports.copy() for port in dut_ports.values(): if 'same' in request.param: sub_ports_on_port = random.sample([ sub_port for sub_port in sub_ports_keys if port + '.' in sub_port ], 2) else: sub_ports_on_port = [ random.choice([ sub_port for sub_port in sub_ports_keys if port + '.' in sub_port ]), random.choice([ sub_port for sub_port in sub_ports_keys if port + '.' not in sub_port ]) ] for sub_port in sub_ports_on_port: sub_ports_keys.pop(sub_port) src_port = sub_ports_on_port.pop(0) new_sub_ports[src_port] = [] src_port_network = ipaddress.ip_network(unicode( sub_ports[src_port]['ip']), strict=False) for next_hop_sub_port in sub_ports_on_port: name_of_namespace = 'vnet_for_{}'.format(next_hop_sub_port) dst_port_network = ipaddress.ip_network(unicode( sub_ports[next_hop_sub_port]['neighbor_ip']), strict=False) add_port_to_namespace( ptfhost, name_of_namespace, sub_ports[next_hop_sub_port]['neighbor_port'], sub_ports[next_hop_sub_port]['neighbor_ip']) if 'tunneling' not in request.node.name: add_static_route_to_ptf(ptfhost, src_port_network, sub_ports[next_hop_sub_port]['ip'], name_of_namespace) add_static_route_to_ptf(ptfhost, dst_port_network, sub_ports[src_port]['ip']) new_sub_ports[src_port].append( (next_hop_sub_port, name_of_namespace)) yield {'new_sub_ports': new_sub_ports, 'sub_ports': sub_ports} for src_port, next_hop_sub_ports in new_sub_ports.items(): src_port_network = ipaddress.ip_network(unicode( sub_ports[src_port]['ip']), strict=False) for next_hop_sub_port in next_hop_sub_ports: sub_port, name_of_namespace = next_hop_sub_port dst_port_network = ipaddress.ip_network(unicode( sub_ports[sub_port]['ip']), strict=False) if 'tunneling' not in request.node.name: remove_static_route_from_ptf(ptfhost, src_port_network, sub_ports[sub_port]['ip'], name_of_namespace) remove_static_route_from_ptf(ptfhost, dst_port_network, sub_ports[src_port]['ip']) remove_namespace(ptfhost, name_of_namespace)