From 76ce6c493eb389118e4b12e3f58198230f3b02ff Mon Sep 17 00:00:00 2001 From: Kenneth Cheung Date: Wed, 3 Jul 2024 20:23:18 -0700 Subject: [PATCH] Add UT for single ASIC Also some minor test renaming and cleanup for clarity --- scripts/dropstat | 2 + tests/multi_asic_dropstat_test.py | 12 ++--- tests/single_asic_dropstat_test.py | 72 ++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 tests/single_asic_dropstat_test.py diff --git a/scripts/dropstat b/scripts/dropstat index 9758f0e6eb..219ad2b494 100755 --- a/scripts/dropstat +++ b/scripts/dropstat @@ -37,6 +37,8 @@ try: if os.environ["UTILITIES_UNIT_TESTING_TOPOLOGY"] == "multi_asic": import tests.mock_tables.mock_multi_asic dbconnector.load_namespace_config() + else: + dbconnector.load_database_config() except KeyError: pass diff --git a/tests/multi_asic_dropstat_test.py b/tests/multi_asic_dropstat_test.py index 7681edf503..6ee7e4ec54 100644 --- a/tests/multi_asic_dropstat_test.py +++ b/tests/multi_asic_dropstat_test.py @@ -9,8 +9,6 @@ sys.path.insert(0, test_path) sys.path.insert(0, modules_path) -dropstat_path = "/tmp/dropstat-27" - dropstat_masic_result_asic0 = """\ IFACE STATE RX_ERR RX_DROPS TX_ERR TX_DROPS DEBUG_0 DEBUG_2 ------------ ------- -------- ---------- -------- ---------- --------- --------- @@ -60,14 +58,12 @@ class TestMultiAsicDropstat(object): @classmethod def setup_class(cls): - if os.path.exists(dropstat_path): - shutil.rmtree(dropstat_path) os.environ["PATH"] += os.pathsep + scripts_path os.environ["UTILITIES_UNIT_TESTING"] = "1" os.environ["UTILITIES_UNIT_TESTING_TOPOLOGY"] = "multi_asic" print("SETUP") - def test_show_pg_drop_masic_asic0(self): + def test_show_dropcount_masic_asic0(self): os.environ["UTILITIES_UNIT_TESTING_DROPSTAT_CLEAN_CACHE"] = "1" return_code, result = get_result_and_return_code([ 'dropstat', '-c', 'show', '-n', 'asic0' @@ -77,7 +73,7 @@ def test_show_pg_drop_masic_asic0(self): print("result = {}".format(result)) assert result == dropstat_masic_result_asic0 and return_code == 0 - def test_show_pg_drop_masic_all_and_clear(self): + def test_show_dropcount_masic_all_and_clear(self): os.environ["UTILITIES_UNIT_TESTING_DROPSTAT_CLEAN_CACHE"] = "1" return_code, result = get_result_and_return_code([ 'dropstat', '-c', 'show' @@ -102,7 +98,7 @@ def test_show_pg_drop_masic_all_and_clear(self): print("result = {}".format(result)) assert result == dropstat_masic_result_clear_all and return_code == 0 - def test_show_pg_drop_masic_invalid_ns(self): + def test_show_dropcount_masic_invalid_ns(self): return_code, result = get_result_and_return_code([ 'dropstat', '-c', 'show', '-n', 'asic5' ]) @@ -111,7 +107,7 @@ def test_show_pg_drop_masic_invalid_ns(self): assert return_code == 2 assert "invalid choice: asic5" in result - def test_show_pg_drop_version(self): + def test_show_dropcount_version(self): return_code, result = get_result_and_return_code([ 'dropstat', '--version' ]) diff --git a/tests/single_asic_dropstat_test.py b/tests/single_asic_dropstat_test.py new file mode 100644 index 0000000000..c521bcfa60 --- /dev/null +++ b/tests/single_asic_dropstat_test.py @@ -0,0 +1,72 @@ +import os +import sys +from .utils import get_result_and_return_code + +test_path = os.path.dirname(os.path.abspath(__file__)) +modules_path = os.path.dirname(test_path) +scripts_path = os.path.join(modules_path, "scripts") +sys.path.insert(0, test_path) +sys.path.insert(0, modules_path) + +dropstat_result = """\ + IFACE STATE RX_ERR RX_DROPS TX_ERR TX_DROPS DEBUG_0 DEBUG_2 +--------- ------- -------- ---------- -------- ---------- --------- --------- +Ethernet0 D 10 100 0 0 80 20 +Ethernet4 N/A 0 1000 0 0 800 100 +Ethernet8 N/A 100 10 0 0 10 0 + + DEVICE SWITCH_DROPS lowercase_counter +---------------- -------------- ------------------- +sonic_drops_test 1000 0 +""" + +dropstat_result_clear_all = """\ + IFACE STATE RX_ERR RX_DROPS TX_ERR TX_DROPS DEBUG_0 DEBUG_2 +--------- ------- -------- ---------- -------- ---------- --------- --------- +Ethernet0 D 0 0 0 0 0 0 +Ethernet4 N/A 0 0 0 0 0 0 +Ethernet8 N/A 0 0 0 0 0 0 + + DEVICE SWITCH_DROPS lowercase_counter +---------------- -------------- ------------------- +sonic_drops_test 0 0 +""" + + +class TestMultiAsicDropstat(object): + @classmethod + def setup_class(cls): + os.environ["PATH"] += os.pathsep + scripts_path + os.environ["UTILITIES_UNIT_TESTING"] = "1" + print("SETUP") + + def test_show_dropcount_and_clear(self): + os.environ["UTILITIES_UNIT_TESTING_DROPSTAT_CLEAN_CACHE"] = "1" + return_code, result = get_result_and_return_code([ + 'dropstat', '-c', 'show' + ]) + os.environ.pop("UTILITIES_UNIT_TESTING_DROPSTAT_CLEAN_CACHE") + print("return_code: {}".format(return_code)) + print("result = {}".format(result)) + assert result == dropstat_result + assert return_code == 0 + + return_code, result = get_result_and_return_code([ + 'dropstat', '-c', 'clear' + ]) + print("return_code: {}".format(return_code)) + print("result = {}".format(result)) + assert result == 'Cleared drop counters\n' and return_code == 0 + + return_code, result = get_result_and_return_code([ + 'dropstat', '-c', 'show' + ]) + print("return_code: {}".format(return_code)) + print("result = {}".format(result)) + assert result == dropstat_result_clear_all and return_code == 0 + + @classmethod + def teardown_class(cls): + os.environ["PATH"] = os.pathsep.join(os.environ["PATH"].split(os.pathsep)[:-1]) + os.environ.pop("UTILITIES_UNIT_TESTING") + print("TEARDOWN")