Skip to content

Commit cdaae03

Browse files
authored
[route_check]: Add truncation indicator and mismatch summary counts (#4587)
* [route_check]: Add truncation indicator and mismatch summary counts * Add PRINT_MSG_TRUNCATION_SUFFIX constant and truncation marker within PRINT_MSG_LEN_MAX budget * Extract summarize_results() helper for per-namespace mismatch counts * Add test_logging_truncation_indicator with coverage for suffix, short msg, and edge cases Signed-off-by: Bojun-Feng <bojundf@gmail.com> * [route_check]: Fix flake8 E501 line length violations * Break long print_message calls to fit within 120 char limit Signed-off-by: Bojun-Feng <bojundf@gmail.com> * [route_check]: Fix flake8 E128 continuation line indent * Align continuation lines to opening parenthesis Signed-off-by: Bojun-Feng <bojundf@gmail.com> --------- Signed-off-by: Bojun-Feng <bojundf@gmail.com>
1 parent 23fb9f3 commit cdaae03

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

scripts/route_check.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
MAX_SCAN_INTERVAL = 3600 # An hour
7777

7878
PRINT_MSG_LEN_MAX = 1000
79+
PRINT_MSG_TRUNCATION_SUFFIX = " ... (truncated)"
7980

8081
FRR_CHECK_RETRIES = 3
8182
FRR_WAIT_TIME = 15
@@ -129,15 +130,23 @@ def print_message(lvl, *args, write_to_stdout=True):
129130
:param lvl: Log level for this message as ERR/INFO/DEBUG
130131
:param args: message as list of strings or convertible to string
131132
:param write_to_stdout: print the message to stdout if set to true
132-
:return None
133+
:return msg string (may be truncated)
133134
"""
134135
msg = ""
136+
truncated = False
135137
if (lvl <= report_level):
136138
for arg in args:
137139
rem_len = PRINT_MSG_LEN_MAX - len(msg)
138140
if rem_len <= 0:
141+
truncated = True
139142
break
140-
msg += str(arg)[0:rem_len]
143+
s = str(arg)
144+
if len(s) > rem_len:
145+
truncated = True
146+
msg += s[0:rem_len]
147+
148+
if truncated and PRINT_MSG_LEN_MAX > len(PRINT_MSG_TRUNCATION_SUFFIX):
149+
msg = msg[:PRINT_MSG_LEN_MAX - len(PRINT_MSG_TRUNCATION_SUFFIX)] + PRINT_MSG_TRUNCATION_SUFFIX
141150

142151
if write_to_stdout:
143152
print(msg)
@@ -966,6 +975,16 @@ def check_routes_for_namespace(namespace):
966975
return results, adds, deletes
967976

968977

978+
def summarize_results(results):
979+
"""
980+
Summarize mismatch results by counting entries per namespace/category.
981+
:param results: dict of {namespace: {category: [entries]}}
982+
:return dict of {namespace: {category: count}}
983+
"""
984+
return {ns: {k: len(v) for k, v in entries.items()}
985+
for ns, entries in results.items()}
986+
987+
969988
def check_routes(namespace):
970989
"""
971990
Main function to parallelize route checks across all namespaces.
@@ -1000,6 +1019,8 @@ def check_routes(namespace):
10001019
return -1, results
10011020

10021021
if results:
1022+
print_message(syslog.LOG_WARNING, "Route mismatch counts: ",
1023+
json.dumps(summarize_results(results), separators=(",", ":")))
10031024
print_message(syslog.LOG_WARNING, "Failure results: {", json.dumps(results, indent=4), "}")
10041025
print_message(syslog.LOG_WARNING, "Failed. Look at reported mismatches above")
10051026
print_message(syslog.LOG_WARNING, "add: ", json.dumps(all_adds, indent=4))
@@ -1070,6 +1091,8 @@ def check_sids(namespace):
10701091
return -1, results
10711092

10721093
if results:
1094+
print_message(syslog.LOG_WARNING, "SID mismatch counts: ",
1095+
json.dumps(summarize_results(results), separators=(",", ":")))
10731096
print_message(syslog.LOG_WARNING, "SIDs Check Failure results: {", json.dumps(results, indent=4), "}")
10741097
return -1, results
10751098
else:

tests/route_check_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,24 @@ def test_logging(self):
340340
msg = route_check.print_message(syslog.LOG_ERR, "a", "b", "c", "d", "e", "f")
341341
assert len(msg) == 5
342342

343+
def test_logging_truncation_indicator(self):
344+
# Test truncation indicator with realistic PRINT_MSG_LEN_MAX
345+
route_check.PRINT_MSG_LEN_MAX = 50
346+
msg = route_check.print_message(syslog.LOG_ERR, "x" * 100)
347+
assert len(msg) == 50
348+
assert msg.endswith(" ... (truncated)")
349+
350+
# Short message should not have truncation indicator
351+
msg = route_check.print_message(syslog.LOG_ERR, "short")
352+
assert msg == "short"
353+
assert " ... (truncated)" not in msg
354+
355+
# When PRINT_MSG_LEN_MAX is smaller than suffix, no suffix appended
356+
route_check.PRINT_MSG_LEN_MAX = 5
357+
msg = route_check.print_message(syslog.LOG_ERR, "abcdefghi")
358+
assert len(msg) == 5
359+
assert " ... (truncated)" not in msg
360+
343361
def test_mitigate_routes(self, mock_dbs):
344362
namespace = DEFAULTNS
345363
missed_frr_rt = [{'prefix': '192.168.0.1', 'protocol': 'bgp'}]

0 commit comments

Comments
 (0)