Skip to content

Commit 91b6642

Browse files
authored
Added detailed reason for assert failure for bgp (#19587)
Summary: Enhanced assertion messages in selected test cases to provide clearer failure context. This improves debuggability and speeds up issue resolution when tests fail. Added detailed assertion failure messages in test scripts to make it easier to understand why tests fails. What is the motivation for this PR? The motivation is to improve test debuggability by adding detailed failure reasons in assertions for selected test cases. This enhancement makes it easier to identify the root cause of test failures in logs, thereby reducing triage time and simplifying test maintenance. How did you do it? I updated the assertion statements in the following test files to include descriptive error messages: tests/bgp/test_bgp_speaker.py tests/bgp/test_bgp_router_id.py How did you verify/test it? Verified that the updated assertion messages are correctly reflected in the test code by reviewing the changes locally.
1 parent e2f12f3 commit 91b6642

2 files changed

Lines changed: 79 additions & 21 deletions

File tree

tests/bgp/test_bgp_router_id.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ def verify_bgp(enum_asic_index, duthost, expected_bgp_router_id, neighbor_type,
2323
# Verify router id from DUT itself
2424
pattern = r"BGP router identifier (\d+\.\d+\.\d+\.\d+)"
2525
match = re.search(pattern, output)
26-
pytest_assert(match, "Cannot get actual BGP router id from [{}]".format(output))
27-
pytest_assert(match.group(1) == expected_bgp_router_id,
28-
"BGP router id unexpected, expected: {}, actual: {}".format(expected_bgp_router_id, match.group(1)))
26+
pytest_assert(match, (
27+
"Cannot get actual BGP router id from [{}]. "
28+
).format(output))
29+
30+
pytest_assert(match.group(1) == expected_bgp_router_id, (
31+
"BGP router id unexpected, expected: {}, actual: {}. "
32+
).format(expected_bgp_router_id, match.group(1)))
2933

3034
# Verify BGP sessions are established
3135
run_bgp_facts(duthost, enum_asic_index)
@@ -40,18 +44,25 @@ def verify_bgp(enum_asic_index, duthost, expected_bgp_router_id, neighbor_type,
4044
local_ip_map[item["name"]] = item["local_addr"]
4145

4246
for neighbor_name, nbrhost in nbrhosts.items():
43-
pytest_assert(neighbor_name in local_ip_map, "Cannot find local ip for {}".format(neighbor_name))
47+
pytest_assert(neighbor_name in local_ip_map, (
48+
"Cannot find local ip for {}. "
49+
"Local IP map: {}"
50+
).format(neighbor_name, local_ip_map))
51+
4452
if neighbor_type == "sonic":
4553
cmd = "show ip neighbors {}".format(local_ip_map[neighbor_name])
4654
elif neighbor_type == "eos":
4755
cmd = "/usr/bin/Cli -c \"show ip bgp neighbors {}\"".format(local_ip_map[neighbor_name])
4856
output = nbrhost["host"].shell(cmd, module_ignore_errors=True)['stdout']
4957
pattern = r"BGP version 4, remote router ID (\d+\.\d+\.\d+\.\d+)"
5058
match = re.search(pattern, output)
51-
pytest_assert(match, "Cannot get remote BGP router id from [{}]".format(output))
52-
pytest_assert(match.group(1) == expected_bgp_router_id,
53-
"BGP router id is unexpected, local: {}, fetch from remote: {}"
54-
.format(expected_bgp_router_id, match.group(1)))
59+
pytest_assert(match, (
60+
"Cannot get remote BGP router id from [{}]. "
61+
).format(output))
62+
63+
pytest_assert(match.group(1) == expected_bgp_router_id, (
64+
"BGP router id is unexpected, local: {}, fetch from remote: {}. "
65+
).format(expected_bgp_router_id, match.group(1)))
5566

5667

5768
@pytest.fixture()
@@ -129,8 +140,17 @@ def test_bgp_router_id_set(duthosts, enum_frontend_dut_hostname, enum_asic_index
129140
continue
130141
output = duthost.shell("show ip bgp neighbor {} advertised-routes| grep {}".format(remote_ip, loopback_ip),
131142
module_ignore_errors=True)
132-
pytest_assert(output["rc"] == 0, "Failed to check whether Loopback ipv4 address has been advertised")
133-
pytest_assert(loopback_ip in output["stdout"], "Router advertised unexpected: {}".format(output["stdout"]))
143+
pytest_assert(output["rc"] == 0, (
144+
"Failed to check whether Loopback ipv4 address has been advertised. "
145+
"Return code: {} "
146+
"Output: {}"
147+
).format(output["rc"], output))
148+
149+
pytest_assert(loopback_ip in output["stdout"], (
150+
"Router advertised unexpected. "
151+
"Expected loopback IP: {} "
152+
"Actual output: {}"
153+
).format(loopback_ip, output["stdout"]))
134154

135155

136156
def test_bgp_router_id_set_without_loopback(duthosts, enum_frontend_dut_hostname, enum_asic_index, nbrhosts, request,

tests/bgp/test_bgp_speaker.py

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ def change_route(operation, ptfip, neighbor, route, nexthop, port):
6161
url = "http://%s:%d" % (ptfip, port)
6262
data = {"command": "neighbor %s %s route %s next-hop %s" % (neighbor, operation, route, nexthop)}
6363
r = requests.post(url, data=data, proxies={"http": None, "https": None})
64-
assert r.status_code == 200
64+
assert r.status_code == 200, (
65+
"Request failed with status code {}. Expected 200. "
66+
).format(r.status_code)
6567

6668

6769
@pytest.fixture(scope="module", autouse=True)
@@ -205,14 +207,27 @@ def test_bgp_speaker_bgp_sessions(common_setup_teardown, duthosts, rand_one_dut_
205207
"""
206208
duthost = duthosts[rand_one_dut_hostname]
207209
ptfip, mg_facts, interface_facts, vlan_ips, _, _, speaker_ips, port_num, http_ready = common_setup_teardown
208-
assert http_ready
210+
assert http_ready, (
211+
"HTTP is not ready. "
212+
"HTTP Ready: {}"
213+
).format(http_ready)
209214

210215
logger.info("Wait some time to verify that bgp sessions are established")
211216
time.sleep(20)
212217
bgp_facts = duthost.bgp_facts()['ansible_facts']
213-
assert all([v["state"] == "established" for _, v in list(bgp_facts["bgp_neighbors"].items())]), \
214-
"Not all bgp sessions are established"
215-
assert str(speaker_ips[2].ip) in bgp_facts["bgp_neighbors"], "No bgp session with PTF"
218+
assert all([v["state"] == "established" for _, v in list(bgp_facts["bgp_neighbors"].items())]), (
219+
"Not all BGP sessions are established. "
220+
"Current state: {} "
221+
"BGP neighbors items: {} "
222+
).format(
223+
(v["state"] for _, v in list(bgp_facts["bgp_neighbors"].items())),
224+
list(bgp_facts["bgp_neighbors"].items())
225+
)
226+
227+
assert str(speaker_ips[2].ip) in bgp_facts["bgp_neighbors"], (
228+
"No BGP session with PTF. "
229+
"Speaker IP: {} "
230+
).format(str(speaker_ips[2].ip))
216231

217232

218233
# For dualtor
@@ -270,7 +285,11 @@ def bgp_speaker_announce_routes_common(common_setup_teardown, tbinfo, duthost,
270285
"""
271286
ptfip, mg_facts, interface_facts, vlan_ips, _, vlan_if_name, \
272287
speaker_ips, port_num, http_ready = common_setup_teardown
273-
assert http_ready
288+
assert http_ready, (
289+
"HTTP is not ready. "
290+
"HTTP Ready: {}"
291+
).format(http_ready)
292+
274293
asic_type = duthost.facts["asic_type"]
275294

276295
logger.info("announce route")
@@ -283,16 +302,35 @@ def bgp_speaker_announce_routes_common(common_setup_teardown, tbinfo, duthost,
283302
announce_route(ptfip, lo_addr, peer_range, vlan_ips[0].ip, port_num[2])
284303

285304
logger.info("Wait some time to make sure routes announced to dynamic bgp neighbors")
286-
assert wait_until(120, 10, 0, is_all_neighbors_learned, duthost, speaker_ips), \
287-
"Not all dynamic neighbors were learned"
305+
assert wait_until(120, 10, 0, is_all_neighbors_learned, duthost, speaker_ips), (
306+
"Not all dynamic neighbors were learned. "
307+
"DUT Host: {}"
308+
"Speaker IPs: {}"
309+
).format(duthost, speaker_ips)
288310

289311
logger.info("Verify nexthops and nexthop interfaces for accepted prefixes of the dynamic neighbors")
290312
rtinfo = duthost.get_ip_route_info(ipaddress.ip_network(prefix.encode().decode()))
291313
nexthops_ip_set = {str(nexthop.ip) for nexthop in nexthop_ips}
292-
assert len(rtinfo["nexthops"]) == 2
314+
assert len(rtinfo["nexthops"]) == 2, (
315+
"Number of nexthops is not 2. "
316+
"Actual number of nexthops: {}"
317+
"Nexthops: {}"
318+
).format(len(rtinfo["nexthops"]), rtinfo["nexthops"])
319+
293320
for i in [0, 1]:
294-
assert str(rtinfo["nexthops"][i][0]) in nexthops_ip_set
295-
assert rtinfo["nexthops"][i][1] == vlan_if_name.encode().decode()
321+
assert str(rtinfo["nexthops"][i][0]) in nexthops_ip_set, (
322+
"Nexthop IP not found in expected set. "
323+
"Actual nexthop IP: {}"
324+
"Expected nexthop IP set: {}"
325+
"Nexthops: {}"
326+
).format(str(rtinfo["nexthops"][i][0]), nexthops_ip_set, rtinfo["nexthops"])
327+
328+
assert rtinfo["nexthops"][i][1] == vlan_if_name.encode().decode(), (
329+
"Nexthop interface name does not match expected value. "
330+
"Actual nexthop interface name: {}"
331+
"Expected nexthop interface name: {}"
332+
"Nexthops: {}"
333+
).format(rtinfo["nexthops"][i][1], vlan_if_name.encode().decode(), rtinfo["nexthops"])
296334

297335
logger.info("Generate route-port map information")
298336
extra_vars = {'announce_prefix': prefix,

0 commit comments

Comments
 (0)