Skip to content

Commit

Permalink
Add UT for checking for 'pfc_enabled' entry
Browse files Browse the repository at this point in the history
  • Loading branch information
ayurkiv committed Mar 10, 2021
1 parent 2f83dad commit 46392e4
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 24 deletions.
46 changes: 22 additions & 24 deletions pfcwd/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
from tabulate import tabulate
from utilities_common import multi_asic as multi_asic_util
from utilities_common import constants
from sonic_py_common import logger

SYSLOG_IDENTIFIER = "config"

log = logger.Logger(SYSLOG_IDENTIFIER)

# mock the redis for unit test purposes #
try:
Expand Down Expand Up @@ -242,9 +247,20 @@ def start(self, action, restoration_time, ports, detection_time):
exit()
self.start_cmd(action, restoration_time, ports, detection_time)

def get_port_pfc_status(self, port):
status = self.config_db.get_entry(PORT_QOS_MAP, port).get('pfc_enable')
return status

def verify_pfc_enable_status_per_port(self, port):
pfc_status = self.config_db.get_entry(PORT_QOS_MAP, port).get('pfc_enable')

if pfc_status == None:
log.log_warning("SKIPPED: PFC is not enabled on port: {}".format(port), also_print_to_console=True)
return

self.config_db.mod_entry(
CONFIG_DB_PFC_WD_TABLE_NAME, port, None
)
self.config_db.mod_entry(
CONFIG_DB_PFC_WD_TABLE_NAME, port, pfcwd_info
)

@multi_asic_util.run_on_multi_asic
def start_cmd(self, action, restoration_time, ports, detection_time):
Expand Down Expand Up @@ -276,29 +292,11 @@ def start_cmd(self, action, restoration_time, ports, detection_time):
for port in ports:
if port == "all":
for p in all_ports:
pfc_status = self.get_port_pfc_status(p)
if pfc_status == None:
print("SKIPPED: PFC is not enabled on port: {}".format(p))
continue

self.config_db.mod_entry(
CONFIG_DB_PFC_WD_TABLE_NAME, p, None
)
self.config_db.mod_entry(
CONFIG_DB_PFC_WD_TABLE_NAME, p, pfcwd_info
)
self.verify_pfc_enable_status_per_port(p)
else:
pfc_status = self.get_port_pfc_status(port)
if port not in all_ports or pfc_status == None:
print("SKIPPED: PFC is not enabled on port: {}".format(port))
if port not in all_ports:
continue

self.config_db.mod_entry(
CONFIG_DB_PFC_WD_TABLE_NAME, port, None
)
self.config_db.mod_entry(
CONFIG_DB_PFC_WD_TABLE_NAME, port, pfcwd_info
)
self.verify_pfc_enable_status_per_port(port)

@multi_asic_util.run_on_multi_asic
def interval(self, poll_interval):
Expand Down
21 changes: 21 additions & 0 deletions tests/mock_tables/asic0/config_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,27 @@
"BIG_RED_SWITCH": "enable",
"POLL_INTERVAL": "199"
},
"PORT_QOS_MAP|Ethernet0": {
"pfc_enable": "3,4"
},
"PORT_QOS_MAP|Ethernet4": {
"pfc_enable": "3,4"
},
"PORT_QOS_MAP|Ethernet8": {
"pfc_enable": "3,4"
},
"PORT_QOS_MAP|Ethernet-BP0": {
"pfc_enable": "3,4"
},
"PORT_QOS_MAP|Ethernet-BP4": {
"pfc_enable": "3,4"
},
"PORT_QOS_MAP|Ethernet-BP256": {
"pfc_enable": "3,4"
},
"PORT_QOS_MAP|Ethernet-BP260": {
"pfc_enable": "3,4"
},
"CRM|Config": {
"acl_table_threshold_type": "percentage",
"nexthop_group_threshold_type": "percentage",
Expand Down
21 changes: 21 additions & 0 deletions tests/mock_tables/asic1/config_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@
"BIG_RED_SWITCH": "enable",
"POLL_INTERVAL": "199"
},
"PORT_QOS_MAP|Ethernet0": {
"pfc_enable": "3,4"
},
"PORT_QOS_MAP|Ethernet4": {
"pfc_enable": "3,4"
},
"PORT_QOS_MAP|Ethernet8": {
"pfc_enable": "3,4"
},
"PORT_QOS_MAP|Ethernet-BP0": {
"pfc_enable": "3,4"
},
"PORT_QOS_MAP|Ethernet-BP4": {
"pfc_enable": "3,4"
},
"PORT_QOS_MAP|Ethernet-BP256": {
"pfc_enable": "3,4"
},
"PORT_QOS_MAP|Ethernet-BP260": {
"pfc_enable": "3,4"
},
"CRM|Config": {
"acl_table_threshold_type": "percentage",
"nexthop_group_threshold_type": "percentage",
Expand Down
6 changes: 6 additions & 0 deletions tests/mock_tables/config_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,12 @@
"PORT_QOS_MAP|Ethernet0": {
"pfc_enable": "3,4"
},
"PORT_QOS_MAP|Ethernet4": {
"pfc_enable": "3,4"
},
"PORT_QOS_MAP|Ethernet8": {
"pfc_enable": "3,4"
},
"DEFAULT_LOSSLESS_BUFFER_PARAMETER|AZURE": {
"default_dynamic_th": "0",
"over_subscribe_ratio": "2"
Expand Down
2 changes: 2 additions & 0 deletions tests/pfcwd_input/pfcwd_test_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@
------- -------- ------------------------- ------------ ------------ ----------------- -----------------
"""

pfc_is_not_enabled = "SKIPPED: PFC is not enabled on port: Ethernet0\n"

testData = {
'pfcwd_show_config' : [ {'cmd' : ['show', 'config'],
'args': [],
Expand Down
31 changes: 31 additions & 0 deletions tests/pfcwd_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,37 @@ def test_pfcwd_start_actions(self, mock_os):
assert result.exit_code == 0
assert result.output == pfcwd_show_start_action_drop_output

@patch('pfcwd.main.os')
def test_pfcwd_pfc_not_enabled(self, mock_os):
import pfcwd.main as pfcwd
runner = CliRunner()
db = Db()

# get initial config
result = runner.invoke(
pfcwd.cli.commands["show"].commands["config"],
obj=db
)
print(result.output)
assert result.output == pfcwd_show_config_output

mock_os.geteuid.return_value = 0
# remove 'pfc_enabled' entry from Ethernet0 node
db.cfgdb.mod_entry("PORT_QOS_MAP", "Ethernet0", None)

result = runner.invoke(
pfcwd.cli.commands["start"],
[
"--action", "drop", "--restoration-time", "601",
"Ethernet0", "602"
],
obj=db
)
print(result.output)
assert result.exit_code == 0
assert pfc_is_not_enabled == result.output


def test_pfcwd_start_ports_invalid(self):
# pfcwd start --action drop --restoration-time 200 Ethernet0 200
import pfcwd.main as pfcwd
Expand Down

0 comments on commit 46392e4

Please sign in to comment.