Skip to content

Commit

Permalink
Add Unit Test for portstat (#3564)
Browse files Browse the repository at this point in the history
added a unit test for scenarios where a LC's counter DB is N/A.
  • Loading branch information
BYGX-wcr authored Oct 2, 2024
1 parent 66b41e5 commit 008a078
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
68 changes: 68 additions & 0 deletions tests/portstat_db/on_sup_na/chassis_state_db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"CHASSIS_MODULE_HOSTNAME_TABLE|LINE-CARD0": {
"module_hostname": "sonic-lc1"
},
"CHASSIS_MODULE_HOSTNAME_TABLE|LINE-CARD1": {
"module_hostname": "sonic-lc2"
},
"CHASSIS_MODULE_HOSTNAME_TABLE|LINE-CARD2": {
"module_hostname": "sonic-lc3"
},
"LINECARD_PORT_STAT_MARK_TABLE|sonic-lc1": {
"timestamp": "2020-07-01 00:00:00"
},
"LINECARD_PORT_STAT_MARK_TABLE|sonic-lc3": {
"timestamp": "2020-07-01 00:00:00"
},
"LINECARD_PORT_STAT_TABLE|Ethernet1/1": {
"state": "U",
"rx_ok": 100,
"rx_bps": 10,
"rx_pps": 1,
"rx_util": 0,
"rx_err": 0,
"rx_drop": 0,
"rx_ovr": 0,
"tx_ok": 100,
"tx_bps": 10,
"tx_pps": 1,
"tx_util": 0,
"tx_err": 0,
"tx_drop": 0,
"tx_ovr": 0
},
"LINECARD_PORT_STAT_TABLE|Ethernet2/1": {
"state": "U",
"rx_ok": 100,
"rx_bps": 10,
"rx_pps": 1,
"rx_util": 0,
"rx_err": 0,
"rx_drop": 0,
"rx_ovr": 0,
"tx_ok": 100,
"tx_bps": 10,
"tx_pps": 1,
"tx_util": 0,
"tx_err": 0,
"tx_drop": 0,
"tx_ovr": 0
},
"LINECARD_PORT_STAT_TABLE|Ethernet11/1": {
"state": "N/A",
"rx_ok": "N/A",
"rx_bps": "N/A",
"rx_pps": "N/A",
"rx_util": "N/A",
"rx_err": "N/A",
"rx_drop": "N/A",
"rx_ovr": "N/A",
"tx_ok": "N/A",
"tx_bps": "N/A",
"tx_pps": "N/A",
"tx_util": "N/A",
"tx_err": "N/A",
"tx_drop": "N/A",
"tx_ovr": "N/A"
}
}
38 changes: 38 additions & 0 deletions tests/portstat_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,19 @@

intf_counters_on_sup_partial_lc = "Not all linecards have published their counter values.\n"

intf_counters_on_sup_na = """\
IFACE STATE RX_OK RX_BPS RX_UTIL RX_ERR RX_DRP RX_OVR TX_OK TX_BPS TX_UTIL\
TX_ERR TX_DRP TX_OVR
------------ ------- ------- --------- --------- -------- -------- -------- ------- --------- ---------\
-------- -------- --------
Ethernet1/1 U 100 10.00 B/s 0.00% 0 0 0 100 10.00 B/s 0.00%\
0 0 0
Ethernet2/1 U 100 10.00 B/s 0.00% 0 0 0 100 10.00 B/s 0.00%\
0 0 0
Ethernet11/1 N/A N/A N/A N/A N/A N/A N/A N/A N/A N/A\
N/A N/A N/A
"""

TEST_PERIOD = 3


Expand Down Expand Up @@ -512,6 +525,31 @@ def test_show_intf_counters_on_sup_partial_lc(self):
os.system("cp /tmp/chassis_state_db.json {}"
.format(os.path.join(test_path, "mock_tables/chassis_state_db.json")))

def test_show_intf_counters_on_sup_na(self):
remove_tmp_cnstat_file()
os.system("cp {} /tmp/".format(os.path.join(test_path, "mock_tables/chassis_state_db.json")))
os.system("cp {} {}".format(os.path.join(test_path, "portstat_db/on_sup_na/chassis_state_db.json"),
os.path.join(test_path, "mock_tables/chassis_state_db.json")))
os.environ["UTILITIES_UNIT_TESTING_IS_SUP"] = "1"

runner = CliRunner()
result = runner.invoke(
show.cli.commands["interfaces"].commands["counters"], [])
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == intf_counters_on_sup_na

return_code, result = get_result_and_return_code(['portstat'])
print("return_code: {}".format(return_code))
print("result = {}".format(result))
assert return_code == 0
assert result == intf_counters_on_sup_na

os.environ["UTILITIES_UNIT_TESTING_IS_SUP"] = "0"
os.system("cp /tmp/chassis_state_db.json {}"
.format(os.path.join(test_path, "mock_tables/chassis_state_db.json")))

@classmethod
def teardown_class(cls):
print("TEARDOWN")
Expand Down

0 comments on commit 008a078

Please sign in to comment.