Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bktsim-arista committed Dec 22, 2023
1 parent c463abd commit 001c9c9
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pfc/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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):
"""
Expand Down
83 changes: 83 additions & 0 deletions tests/pfc_input/assert_show_output.py
Original file line number Diff line number Diff line change
@@ -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
"""
79 changes: 79 additions & 0 deletions tests/pfc_test.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 001c9c9

Please sign in to comment.