From 3f80da010a73b8d53e75f8b71bffeaf1e57695b1 Mon Sep 17 00:00:00 2001 From: bktsim Date: Fri, 22 Dec 2023 09:52:01 -0800 Subject: [PATCH] add tests --- pfc/main.py | 16 +++++- tests/pfc_input/assert_show_output.py | 83 +++++++++++++++++++++++++++ tests/pfc_test.py | 79 +++++++++++++++++++++++++ 3 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 tests/pfc_input/assert_show_output.py create mode 100644 tests/pfc_test.py diff --git a/pfc/main.py b/pfc/main.py index dd4ed537ff..fc023f0cce 100644 --- a/pfc/main.py +++ b/pfc/main.py @@ -1,13 +1,25 @@ #!/usr/bin/env python3 - +import os +import sys import click from swsscommon.swsscommon import ConfigDBConnector from tabulate import tabulate from natsort import natsorted +from importlib import reload ALL_PRIORITIES = [str(x) for x in range(8)] PRIORITY_STATUS = ['on', 'off'] +# mock the redis for unit test purposes # +try: + if os.environ["UTILITIES_UNIT_TESTING"] == "2": + modules_path = os.path.join(os.path.dirname(__file__), "..") + tests_path = os.path.join(modules_path, "tests") + sys.path.insert(0, modules_path) + sys.path.insert(0, tests_path) +except KeyError: + pass + class Pfc(object): def configPfcAsym(self, interface, pfc_asym): """ @@ -18,6 +30,8 @@ def configPfcAsym(self, interface, pfc_asym): configdb.mod_entry("PORT", interface, {'pfc_asym': pfc_asym}) + if "UTILITIES_UNIT_TESTING" in os.environ and os.environ["UTILITIES_UNIT_TESTING"] == "2": + self.showPfcAsym(interface) def showPfcAsym(self, interface): """ diff --git a/tests/pfc_input/assert_show_output.py b/tests/pfc_input/assert_show_output.py new file mode 100644 index 0000000000..017e179282 --- /dev/null +++ b/tests/pfc_input/assert_show_output.py @@ -0,0 +1,83 @@ +pfc_cannot_find_intf = """\ +Cannot find interface Ethernet1234 +""" + +pfc_show_asymmetric_all = """\ + +Interface Asymmetric +----------- ------------ +Ethernet0 off +Ethernet4 off +Ethernet8 off +Ethernet12 off +Ethernet16 off +Ethernet20 off +Ethernet24 off +Ethernet28 off +Ethernet32 off +Ethernet36 off +Ethernet40 off +Ethernet44 off +Ethernet48 off +Ethernet52 off +Ethernet56 off +Ethernet60 off +Ethernet64 off +Ethernet68 off +Ethernet72 off +Ethernet76 off +Ethernet80 off +Ethernet84 off +Ethernet88 off +Ethernet92 off +Ethernet96 off +Ethernet100 off +Ethernet104 off +Ethernet108 off +Ethernet112 off +Ethernet116 off +Ethernet120 off +Ethernet124 off + +""" + +pfc_show_asymmetric_intf = """\ + +Interface Asymmetric +----------- ------------ +Ethernet0 off + +""" + +pfc_show_priority_all = """\ + +Interface Lossless priorities +----------- --------------------- +Ethernet0 3,4 +Ethernet4 3,4 + +""" + +pfc_show_priority_intf = """\ + +Interface Lossless priorities +----------- --------------------- +Ethernet0 3,4 + +""" + +pfc_config_asymmetric = """\ + +Interface Asymmetric +----------- ------------ +Ethernet0 on + +""" + +pfc_config_priority_on = """\ + +Interface Lossless priorities +----------- --------------------- +Ethernet0 3,4,5 + +""" diff --git a/tests/pfc_test.py b/tests/pfc_test.py new file mode 100644 index 0000000000..dea309adbc --- /dev/null +++ b/tests/pfc_test.py @@ -0,0 +1,79 @@ +import os +import sys +import pfc.main as pfc +from pfc_input.assert_show_output import * + +from click.testing import CliRunner +from importlib import reload + +test_path = os.path.dirname(os.path.abspath(__file__)) +modules_path = os.path.dirname(test_path) +scripts_path = os.path.join(modules_path, "pfc") +sys.path.insert(0, test_path) +sys.path.insert(0, modules_path) + +class TestPfcBase(object): + @classmethod + def setup_class(cls): + print("SETUP") + os.environ["PATH"] += os.pathsep + scripts_path + os.environ['UTILITIES_UNIT_TESTING'] = "2" + + def executor(self, command, args, expected_rc=0, expected_output=None, runner=CliRunner()): + result = runner.invoke(command, args) + print(result.exit_code) + print(result.output) + + if result.exit_code != expected_rc: + print(result.exception) + + assert result.exit_code == expected_rc + if expected_output: + assert result.output == expected_output + + @classmethod + def teardown_class(cls): + os.environ["PATH"] = os.pathsep.join(os.environ["PATH"].split(os.pathsep)[:-1]) + os.environ["UTILITIES_UNIT_TESTING"] = "0" + print("TEARDOWN") + +class TestPfc(TestPfcBase): + @classmethod + def setup_class(cls): + super().setup_class() + from mock_tables import dbconnector + from mock_tables import mock_single_asic + reload(mock_single_asic) + dbconnector.load_namespace_config() + + def test_pfc_show_asymmetric_all(self): + self.executor(pfc.cli, ['show', 'asymmetric'], + expected_output=pfc_show_asymmetric_all) + + def test_pfc_show_asymmetric_intf(self): + self.executor(pfc.cli, ['show', 'asymmetric', 'Ethernet0'], + expected_output=pfc_show_asymmetric_intf) + + def test_pfc_show_asymmetric_intf_fake(self): + self.executor(pfc.cli, ['show', 'asymmetric', 'Ethernet1234'], + expected_output=pfc_cannot_find_intf) + + def test_pfc_show_priority_all(self): + self.executor(pfc.cli, ['show', 'priority'], + expected_output=pfc_show_priority_all) + + def test_pfc_show_priority_intf(self): + self.executor(pfc.cli, ['show', 'priority', 'Ethernet0'], + expected_output=pfc_show_priority_intf) + + def test_pfc_show_asymmetric_intf_fake(self): + self.executor(pfc.cli, ['show', 'priority', 'Ethernet1234'], + expected_output=pfc_cannot_find_intf) + + def test_pfc_config_asymmetric(self): + self.executor(pfc.cli, ['config', 'asymmetric', 'on', 'Ethernet0'], + expected_output=pfc_config_asymmetric) + + def test_pfc_config_priority(self): + self.executor(pfc.cli, ['config', 'priority', 'on', 'Ethernet0', '5'], + expected_output=pfc_config_priority_on) \ No newline at end of file