From 77b725cade0c1f1ce9297c03cbd26ee6fb957b2b Mon Sep 17 00:00:00 2001 From: abdosi <58047199+abdosi@users.noreply.github.com> Date: Fri, 26 May 2023 10:11:11 -0700 Subject: [PATCH 1/2] Fix the show interface counters throwing exception on device with no external interfaces (#2851) Cherry-pick of PR:https://github.com/sonic-net/sonic-utilities/pull/2703/ Signed-off-by: Abhishek Dosi --- scripts/portstat | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/scripts/portstat b/scripts/portstat index 975db159f..21a42774d 100755 --- a/scripts/portstat +++ b/scripts/portstat @@ -320,13 +320,13 @@ class Portstat(object): format_number_with_comma(data.tx_err), format_number_with_comma(data.tx_drop), format_number_with_comma(data.tx_ovr))) - - if use_json: - print(table_as_json(table, header)) - else: - print(tabulate(table, header, tablefmt='simple', stralign='right')) - if multi_asic.is_multi_asic() or device_info.is_chassis(): - print("\nReminder: Please execute 'show interface counters -d all' to include internal links\n") + if table: + if use_json: + print(table_as_json(table, header)) + else: + print(tabulate(table, header, tablefmt='simple', stralign='right')) + if multi_asic.is_multi_asic() or device_info.is_chassis() and not use_json: + print("\nReminder: Please execute 'show interface counters -d all' to include internal links\n") def cnstat_intf_diff_print(self, cnstat_new_dict, cnstat_old_dict, intf_list): """ @@ -522,13 +522,13 @@ class Portstat(object): format_number_with_comma(cntr.tx_err), format_number_with_comma(cntr.tx_drop), format_number_with_comma(cntr.tx_ovr))) - - if use_json: - print(table_as_json(table, header)) - else: - print(tabulate(table, header, tablefmt='simple', stralign='right')) - if multi_asic.is_multi_asic() or device_info.is_chassis(): - print("\nReminder: Please execute 'show interface counters -d all' to include internal links\n") + if table: + if use_json: + print(table_as_json(table, header)) + else: + print(tabulate(table, header, tablefmt='simple', stralign='right')) + if multi_asic.is_multi_asic() or device_info.is_chassis() and not use_json: + print("\nReminder: Please execute 'show interface counters -d all' to include internal links\n") def main(): parser = argparse.ArgumentParser(description='Display the ports state and counters', From 4b96bd7e354ecc2cacf524f9b1690f43d538b15d Mon Sep 17 00:00:00 2001 From: cytsao1 <111393130+cytsao1@users.noreply.github.com> Date: Fri, 19 May 2023 19:17:32 -0700 Subject: [PATCH 2/2] Update pcieutil error message on loading common pcie module (#2786) * Update pcieutil load module error message * Add pcieutil test for load module warning to not print to output * Update pcieutil import test * Update pcieutil import test * Fix pcieutil import test --- pcieutil/main.py | 2 +- tests/pcieutil_test.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pcieutil/main.py b/pcieutil/main.py index ad6a1ebfa..0fef19359 100644 --- a/pcieutil/main.py +++ b/pcieutil/main.py @@ -54,7 +54,7 @@ def load_platform_pcieutil(): from sonic_platform.pcie import Pcie platform_pcieutil = Pcie(platform_path) except ImportError as e: - log.log_warning("Failed to load platform Pcie module. Error : {}, fallback to load Pcie common utility.".format(str(e)), True) + log.log_warning("Failed to load platform Pcie module. Warning : {}, fallback to load Pcie common utility.".format(str(e))) try: from sonic_platform_base.sonic_pcie.pcie_common import PcieUtil platform_pcieutil = PcieUtil(platform_path) diff --git a/tests/pcieutil_test.py b/tests/pcieutil_test.py index cee1feec8..acac60f8d 100644 --- a/tests/pcieutil_test.py +++ b/tests/pcieutil_test.py @@ -3,6 +3,7 @@ from unittest import mock from click.testing import CliRunner +from io import StringIO test_path = os.path.dirname(os.path.abspath(__file__)) modules_path = os.path.dirname(test_path) @@ -156,6 +157,8 @@ +---------------------+-----------+ """ +pcieutil_load_module_warning_msg = "Failed to load platform Pcie module. Warning : No module named 'sonic_platform.pcie', fallback to load Pcie common utility." + class TestPcieUtil(object): @classmethod def setup_class(cls): @@ -199,6 +202,16 @@ def test_aer_option_device(self): result = runner.invoke(pcieutil.cli.commands["pcie-aer"].commands["correctable"], ["-d", "0:1.0"]) assert result.output == pcieutil_pcie_aer_correctable_dev_output + def test_load_pcie_module_warning(self): + stdout = sys.stdout + sys.stdout = result = StringIO() + try: + pcieutil.load_platform_pcieutil() + except ImportError: + pass + sys.stdout = stdout + assert pcieutil_load_module_warning_msg not in result.getvalue() + @classmethod def teardown_class(cls): print("TEARDOWN")