From 553936b61a677b95a45a797c0e3ccdaf015cce94 Mon Sep 17 00:00:00 2001 From: Renuka Manavalan <47282725+renukamanavalan@users.noreply.github.com> Date: Tue, 23 Mar 2021 17:01:32 -0700 Subject: [PATCH] route_check: Fix hanging & logging level (#1520) Fix for hanging: Enforce max len on string to be printed to stdout. With huge mismatch, the message to print can be too long to hang python print statement. Lower logging level: The log for "details of route mismatch" is lowered to WARNING. The caller of the route_check may log error, as it sees it fit. The failure of route_check is indicated to caller via exit code. How to verify it Simulate error (route-mismatch), and verify a) message len does not exceed set MAX b) log level is WARNING --- scripts/route_check.py | 22 +++++++++++++++------- tests/route_check_test.py | 15 +++++++++++++-- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/scripts/route_check.py b/scripts/route_check.py index fbc223b6d976..72ed3201bfd6 100755 --- a/scripts/route_check.py +++ b/scripts/route_check.py @@ -68,6 +68,8 @@ MIN_SCAN_INTERVAL = 10 # Every 10 seconds MAX_SCAN_INTERVAL = 3600 # An hour +PRINT_MSG_LEN_MAX = 1000 + class Level(Enum): ERR = 'ERR' INFO = 'INFO' @@ -77,7 +79,7 @@ def __str__(self): return self.value -report_level = syslog.LOG_ERR +report_level = syslog.LOG_WARNING write_to_syslog = False def handler(signum, frame): @@ -113,14 +115,20 @@ def print_message(lvl, *args): :param args: message as list of strings or convertible to string :return None """ + msg = "" if (lvl <= report_level): - msg = "" for arg in args: - msg += " " + str(arg) + rem_len = PRINT_MSG_LEN_MAX - len(msg) + if rem_len <= 0: + break + msg += str(arg)[0:rem_len] + print(msg) if write_to_syslog: syslog.syslog(lvl, msg) + return msg + def add_prefix(ip): """ @@ -421,10 +429,10 @@ def check_routes(): results["Unaccounted_ROUTE_ENTRY_TABLE_entries"] = rt_asic_miss if results: - print_message(syslog.LOG_ERR, "Failure results: {", json.dumps(results, indent=4), "}") - print_message(syslog.LOG_ERR, "Failed. Look at reported mismatches above") - print_message(syslog.LOG_ERR, "add: {", json.dumps(adds, indent=4), "}") - print_message(syslog.LOG_ERR, "del: {", json.dumps(deletes, indent=4), "}") + print_message(syslog.LOG_WARNING, "Failure results: {", json.dumps(results, indent=4), "}") + print_message(syslog.LOG_WARNING, "Failed. Look at reported mismatches above") + print_message(syslog.LOG_WARNING, "add: {", json.dumps(adds, indent=4), "}") + print_message(syslog.LOG_WARNING, "del: {", json.dumps(deletes, indent=4), "}") return -1, results else: print_message(syslog.LOG_INFO, "All good!") diff --git a/tests/route_check_test.py b/tests/route_check_test.py index 460fdd16d2fb..cf271f666956 100644 --- a/tests/route_check_test.py +++ b/tests/route_check_test.py @@ -2,6 +2,7 @@ import json import os import sys +import syslog import time from unittest.mock import MagicMock, patch @@ -442,8 +443,18 @@ def test_server(self, mock_subs, mock_sel, mock_table, mock_conn): assert ex_str == expect, "{} != {}".format(ex_str, expect) assert ex_raised, "Exception expected" - - + # Test print_msg + route_check.PRINT_MSG_LEN_MAX = 5 + msg = route_check.print_message(syslog.LOG_ERR, "abcdefghi") + assert len(msg) == 5 + msg = route_check.print_message(syslog.LOG_ERR, "ab") + assert len(msg) == 2 + msg = route_check.print_message(syslog.LOG_ERR, "abcde") + assert len(msg) == 5 + msg = route_check.print_message(syslog.LOG_ERR, "a", "b", "c", "d", "e", "f") + assert len(msg) == 5 + +