Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions tests/ipfwd/test_dip_sip.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@
]


def get_asic_for_nexthop(duthost, nexthop_ip, is_ipv6=False):
"""Find ASIC instance can reach the given nexthop IP."""
ip_cmd = "ip -6" if is_ipv6 else "ip"
for asic in duthost.asics:
try:
res = asic.command(f"{ip_cmd} route get {nexthop_ip}")
if res['rc'] == 0 and "unreachable" not in res.get('stdout', ''):
return asic
except Exception:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since gather_facts gets facts for chosen random frontend asic we do not need this condition if you use enum_rand_one_frontend_asic_index like in PR #19931

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reviewing the PR. I agree using enum_rand_one_frontend_asic_index would be better in this case as then we would not need the for loop and condition.

I see PR #19931 is approved and merge ready. I am okay if we wanna go with that solution.

# If the IP is unreachable, an exception is raised.
# This means the route isn't here, ignore the error and check the next ASIC.
continue

# Fallback to the default asic
return duthost.asics[0]


@pytest.fixture(scope="module", autouse="True")
def lldp_setup(duthosts, enum_rand_one_per_hwsku_frontend_hostname, patch_lldpctl, unpatch_lldpctl, localhost):
duthost = duthosts[enum_rand_one_per_hwsku_frontend_hostname]
Expand Down Expand Up @@ -50,9 +67,13 @@ def setup_static_route(duthosts, enum_rand_one_per_hwsku_frontend_hostname, gath
"""
duthost = duthosts[enum_rand_one_per_hwsku_frontend_hostname]

# Find the correct ASIC for the nexthops
asic_v4 = get_asic_for_nexthop(duthost, gather_facts['dst_host_ipv4'], is_ipv6=False)
asic_v6 = get_asic_for_nexthop(duthost, gather_facts['dst_host_ipv6'], is_ipv6=True)

# Configure IPv4 static route
try:
result = duthost.command("ip route add {} via {}".format(STATIC_ROUTE, gather_facts['dst_host_ipv4']))
result = asic_v4.command("ip route add {} via {}".format(STATIC_ROUTE, gather_facts['dst_host_ipv4']))
if result['rc'] != 0:
raise Exception("Failed to add IPv4 static route: {}".format(result['stderr']))
except Exception as e:
Expand All @@ -61,26 +82,25 @@ def setup_static_route(duthosts, enum_rand_one_per_hwsku_frontend_hostname, gath

# Configure IPv6 static route
try:
result = duthost.command("ip -6 route add {} via {}".format(STATIC_ROUTE_IPV6, gather_facts['dst_host_ipv6']))
result = asic_v6.command("ip -6 route add {} via {}".format(STATIC_ROUTE_IPV6, gather_facts['dst_host_ipv6']))
if result['rc'] != 0:
raise Exception("Failed to add IPv6 static route: {}".format(result['stderr']))
except Exception as e:
logger.error("Error occurred while adding IPv6 static route: %s", str(e))
pytest.fail("IPv6 static route addition failed")

# Verify IPv4 route is in the routing table

try:
result = duthost.command("ip route show {}".format(STATIC_ROUTE))
result = asic_v4.command("ip route show {}".format(STATIC_ROUTE))
assert result['rc'] == 0, "Failed to show IPv4 static route: {}".format(result['stderr'])
assert "via " + gather_facts['dst_host_ipv4'] in result["stdout"], "IPv4 static route verification failed"
except Exception as e:
logger.error("Error occurred while verifying IPv4 static route: %s", str(e))
pytest.fail("IPv4 static route verification failed")

# # Verify IPv6 route is in the routing table
# Verify IPv6 route is in the routing table
try:
result = duthost.command("ip -6 route show {}".format(STATIC_ROUTE_IPV6))
result = asic_v6.command("ip -6 route show {}".format(STATIC_ROUTE_IPV6))
assert result['rc'] == 0, "Failed to show IPv6 static route: {}".format(result['stderr'])
assert "via " + gather_facts['dst_host_ipv6'] in result["stdout"], "IPv6 static route verification failed"
except Exception as e:
Expand Down Expand Up @@ -124,13 +144,14 @@ def delete_ipv4_static_route(duthosts, enum_rand_one_per_hwsku_frontend_hostname
pytest.fail: If any step in removing or verifying route fails
"""
duthost = duthosts[enum_rand_one_per_hwsku_frontend_hostname]
asic_v4 = get_asic_for_nexthop(duthost, gather_facts['dst_host_ipv4'], is_ipv6=False)

# Check if IPv4 route exists before deleting
try:
check_result = duthost.command("ip route show {}".format(STATIC_ROUTE))
check_result = asic_v4.command("ip route show {}".format(STATIC_ROUTE))
if check_result['rc'] == 0 and check_result['stdout'].strip():
# Route exists, delete it
result = duthost.command("ip route del {}".format(STATIC_ROUTE))
result = asic_v4.command("ip route del {}".format(STATIC_ROUTE))
if result['rc'] != 0:
raise Exception("Failed to delete IPv4 static route: {}".format(result['stderr']))
logger.info("Successfully deleted IPv4 static route: {}".format(STATIC_ROUTE))
Expand All @@ -142,7 +163,7 @@ def delete_ipv4_static_route(duthosts, enum_rand_one_per_hwsku_frontend_hostname

# Verify IPv4 route is removed from the routing table
try:
result = duthost.command("ip route show {}".format(STATIC_ROUTE))
result = asic_v4.command("ip route show {}".format(STATIC_ROUTE))
assert "No such process" in result['stderr'] or result['stdout'].strip() == "", \
"IPv4 static route still exists in routing table"
except Exception as e:
Expand All @@ -168,13 +189,14 @@ def delete_ipv6_static_route(duthosts, enum_rand_one_per_hwsku_frontend_hostname
pytest.fail: If any step in removing or verifying route fails
"""
duthost = duthosts[enum_rand_one_per_hwsku_frontend_hostname]
asic_v6 = get_asic_for_nexthop(duthost, gather_facts['dst_host_ipv6'], is_ipv6=True)

# Check if IPv6 route exists before deleting
try:
check_result = duthost.command("ip -6 route show {}".format(STATIC_ROUTE_IPV6))
check_result = asic_v6.command("ip -6 route show {}".format(STATIC_ROUTE_IPV6))
if check_result['rc'] == 0 and check_result['stdout'].strip():
# Route exists, delete it
result = duthost.command("ip -6 route del {}".format(STATIC_ROUTE_IPV6))
result = asic_v6.command("ip -6 route del {}".format(STATIC_ROUTE_IPV6))
if result['rc'] != 0:
raise Exception("Failed to delete IPv6 static route: {}".format(result['stderr']))
logger.info("Successfully deleted IPv6 static route: {}".format(STATIC_ROUTE_IPV6))
Expand All @@ -186,7 +208,7 @@ def delete_ipv6_static_route(duthosts, enum_rand_one_per_hwsku_frontend_hostname

# Verify IPv6 route is removed from the routing table
try:
result = duthost.command("ip -6 route show {}".format(STATIC_ROUTE_IPV6))
result = asic_v6.command("ip -6 route show {}".format(STATIC_ROUTE_IPV6))
assert "No such process" in result['stderr'] or result['stdout'].strip() == "", \
"IPv6 static route still exists in routing table"
except Exception as e:
Expand Down
Loading