diff --git a/tests/arp/arp_utils.py b/tests/arp/arp_utils.py index afcafb85f15..4c29eb06af3 100644 --- a/tests/arp/arp_utils.py +++ b/tests/arp/arp_utils.py @@ -2,7 +2,7 @@ import logging from tests.common.helpers.assertions import pytest_assert from tests.common.utilities import wait_until -from ipaddress import ip_interface +from ipaddress import ip_interface, ip_network, IPv4Network logger = logging.getLogger(__name__) @@ -95,14 +95,21 @@ def fdb_has_mac(duthost, mac): return any(mac in line.lower() for line in duthost.command("show mac")["stdout_lines"]) -def get_first_vlan_ipv4(config_facts): +def get_vlan_last_ipv4(config_facts): """ - Get first VLAN interface and its IPv4 address + Return (vlan_intf_name, ipv4) for the first VLAN_INTERFACE with IPv4, + using the last IPv4 in that interface's address list. """ vlan_intfs = config_facts.get("VLAN_INTERFACE", {}) for intf, addrs in vlan_intfs.items(): + intf_ipv4 = None for addr in addrs: - if ":" in addr: + try: + if type(ip_network(addr, strict=False)) is IPv4Network: + iface = ip_interface(addr) + intf_ipv4 = (intf, iface.ip) + except ValueError: continue - return intf, ip_interface(addr).ip + if intf_ipv4 is not None: + return intf_ipv4 return None, None diff --git a/tests/arp/test_arp_update.py b/tests/arp/test_arp_update.py index d48bcdf4544..283cacf7cf4 100644 --- a/tests/arp/test_arp_update.py +++ b/tests/arp/test_arp_update.py @@ -5,7 +5,7 @@ import pytest import random -from tests.arp.arp_utils import clear_dut_arp_cache, fdb_cleanup, get_dut_mac, fdb_has_mac, get_first_vlan_ipv4 +from tests.arp.arp_utils import clear_dut_arp_cache, fdb_cleanup, get_dut_mac, fdb_has_mac, get_vlan_last_ipv4 from tests.common.dualtor.mux_simulator_control import toggle_all_simulator_ports_to_rand_selected_tor # noqa: F401 from tests.common.fixtures.ptfhost_utils import setup_vlan_arp_responder, run_icmp_responder # noqa: F401 from tests.common.helpers.assertions import pytest_assert as pt_assert @@ -61,7 +61,7 @@ def dut_interface_info(rand_selected_dut, config_facts, tbinfo): """ duthost = rand_selected_dut dut_mac = get_dut_mac(duthost, config_facts, tbinfo) - vlan_name, dut_ipv4 = get_first_vlan_ipv4(config_facts) + vlan_name, dut_ipv4 = get_vlan_last_ipv4(config_facts) return { 'host': duthost,