Skip to content

Commit

Permalink
[test] Update unit test coverage for command 'show mac' (sonic-net#1504)
Browse files Browse the repository at this point in the history
* Adding unit tests for CLI command 'show mac'. Testing
  both CLI command and 'fdbshow' script.

Signed-off-by: Maksym Belei <Maksym_Belei@jabil.com>
  • Loading branch information
maksymbelei95 authored Mar 18, 2021
1 parent 4a0c010 commit 3a482ac
Show file tree
Hide file tree
Showing 8 changed files with 446 additions and 9 deletions.
41 changes: 32 additions & 9 deletions scripts/fdbshow
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@
import argparse
import json
import sys
import os

# mock the redis for unit test purposes #
try: # pragma: no cover
if os.environ["UTILITIES_UNIT_TESTING"] == "1":
modules_path = os.path.join(os.path.dirname(__file__), "..")
test_path = os.path.join(modules_path, "tests")
sys.path.insert(0, modules_path)
sys.path.insert(0, test_path)
import mock_tables.dbconnector
mock_variants = { "1": 'asic_db',
"2": 'asic_db_def_vlan',
"3": 'asic_db_no_fdb',
"4": 'asic_db_no_bridge',
"5": 'asic_db_fetch_except'}
mock_db_path = os.path.join(test_path, "fdbshow_input")
file_name = mock_variants[os.environ["FDBSHOW_MOCK"]]
jsonfile_asic = os.path.join(mock_db_path, file_name)
mock_tables.dbconnector.dedicated_dbs['ASIC_DB'] = jsonfile_asic
jsonfile_counters = os.path.join(mock_db_path, 'counters_db')
mock_tables.dbconnector.dedicated_dbs['COUNTERS_DB'] = jsonfile_counters
except KeyError: # pragma: no cover
pass

from natsort import natsorted
from swsssdk import port_util
Expand Down Expand Up @@ -56,14 +79,14 @@ class FdbShow(object):
"""
self.db.connect(self.db.ASIC_DB)
self.bridge_mac_list = []

if not self.if_br_oid_map:
return

fdb_str = self.db.keys('ASIC_DB', "ASIC_STATE:SAI_OBJECT_TYPE_FDB_ENTRY:*")
if not fdb_str:
return

if self.if_br_oid_map is None:
return

oid_pfx = len("oid:0x")
for s in fdb_str:
fdb_entry = s
Expand All @@ -84,7 +107,10 @@ class FdbShow(object):
if_name = port_id
if 'vlan' in fdb:
vlan_id = fdb["vlan"]
elif 'bvid' in fdb:
else:
if 'bvid' not in fdb:
# no possibility to find the Vlan id. skip the FDB entry
continue
try:
vlan_id = port_util.get_vlan_id_from_bvid(self.db, fdb["bvid"])
if vlan_id is None:
Expand All @@ -100,16 +126,13 @@ class FdbShow(object):
return


def get_iter_index(self, key_value=0, pos=0):
def get_iter_index(self, key_value, pos=0):
"""
Get the starting index of matched entry
"""
if pos != 0:
self.bridge_mac_list = natsorted(self.bridge_mac_list, key = lambda x: x[pos])

if key_value == 0:
return 0

keys = [r[pos] for r in self.bridge_mac_list]
return keys.index(key_value)

Expand Down Expand Up @@ -154,5 +177,5 @@ def main():
print(str(e))
sys.exit(1)

if __name__ == "__main__":
if __name__ == "__main__": # pragma: no cover
main()
73 changes: 73 additions & 0 deletions tests/fdbshow_input/asic_db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"ASIC_STATE:SAI_OBJECT_TYPE_SWITCH:oid:0x21000000000000": {
"SAI_SWITCH_ATTR_INIT_SWITCH": "true",
"SAI_SWITCH_ATTR_SRC_MAC_ADDRESS": "DE:AD:BE:EF:CA:FE"
},
"ASIC_STATE:SAI_OBJECT_TYPE_BRIDGE_PORT:oid:0x3a0000000005cb": {
"SAI_BRIDGE_PORT_ATTR_TYPE": "SAI_BRIDGE_PORT_TYPE_PORT",
"SAI_BRIDGE_PORT_ATTR_PORT_ID": "oid:0x1000000000528",
"SAI_BRIDGE_PORT_ATTR_ADMIN_STATE": "true",
"SAI_BRIDGE_PORT_ATTR_FDB_LEARNING_MODE": "SAI_BRIDGE_PORT_FDB_LEARNING_MODE_HW"
},
"ASIC_STATE:SAI_OBJECT_TYPE_BRIDGE_PORT:oid:0x3a0000000006cd": {
"SAI_BRIDGE_PORT_ATTR_TYPE": "SAI_BRIDGE_PORT_TYPE_PORT",
"SAI_BRIDGE_PORT_ATTR_PORT_ID": "oid:0x1000000000549",
"SAI_BRIDGE_PORT_ATTR_ADMIN_STATE": "true",
"SAI_BRIDGE_PORT_ATTR_FDB_LEARNING_MODE": "SAI_BRIDGE_PORT_FDB_LEARNING_MODE_HW"
},
"ASIC_STATE:SAI_OBJECT_TYPE_BRIDGE_PORT:oid:0x3a0000000007ef": {
"SAI_BRIDGE_PORT_ATTR_TYPE": "SAI_BRIDGE_PORT_TYPE_PORT",
"SAI_BRIDGE_PORT_ATTR_PORT_ID": "oid:0x1000000000fff",
"SAI_BRIDGE_PORT_ATTR_ADMIN_STATE": "true",
"SAI_BRIDGE_PORT_ATTR_FDB_LEARNING_MODE": "SAI_BRIDGE_PORT_FDB_LEARNING_MODE_HW"
},
"ASIC_STATE:SAI_OBJECT_TYPE_FDB_ENTRY:{\"bvid\":\"oid:0x260000000005c5\",\"mac\":\"11:22:33:44:55:66\",\"switch_id\":\"oid:0x21000000000000\"}": {
"SAI_FDB_ENTRY_ATTR_BRIDGE_PORT_ID": "oid:0x3a0000000005cb",
"SAI_FDB_ENTRY_ATTR_TYPE": "SAI_FDB_ENTRY_TYPE_DYNAMIC",
"SAI_FDB_ENTRY_ATTR_PACKET_ACTION": "SAI_PACKET_ACTION_FORWARD"
},
"ASIC_STATE:SAI_OBJECT_TYPE_FDB_ENTRY:{\"bvid\":\"oid:0x260000000006c6\",\"mac\":\"11:22:33:66:55:44\",\"switch_id\":\"oid:0x21000000000000\"}": {
"SAI_FDB_ENTRY_ATTR_BRIDGE_PORT_ID": "oid:0x3a0000000006cd",
"SAI_FDB_ENTRY_ATTR_TYPE": "SAI_FDB_ENTRY_TYPE_STATIC",
"SAI_FDB_ENTRY_ATTR_PACKET_ACTION": "SAI_PACKET_ACTION_FORWARD"
},
"ASIC_STATE:SAI_OBJECT_TYPE_FDB_ENTRY:{\"bvid\":\"oid:0x260000000007c7\",\"mac\":\"66:55:44:33:22:11\",\"switch_id\":\"oid:0x21000000000000\"}": {
"SAI_FDB_ENTRY_ATTR_BRIDGE_PORT_ID": "oid:0x3a0000000005cb",
"SAI_FDB_ENTRY_ATTR_TYPE": "SAI_FDB_ENTRY_TYPE_DYNAMIC",
"SAI_FDB_ENTRY_ATTR_PACKET_ACTION": "SAI_PACKET_ACTION_FORWARD"
},
"ASIC_STATE:SAI_OBJECT_TYPE_FDB_ENTRY:{}": {
"SAI_FDB_ENTRY_ATTR_BRIDGE_PORT_ID": "oid:0x3a0000000005ff",
"SAI_FDB_ENTRY_ATTR_TYPE": "SAI_FDB_ENTRY_TYPE_DYNAMIC",
"SAI_FDB_ENTRY_ATTR_PACKET_ACTION": "SAI_PACKET_ACTION_FORWARD"
},
"ASIC_STATE:SAI_OBJECT_TYPE_FDB_ENTRY:{\"bvid\":\"oid:0x260000000007c7\",\"mac\":\"77:55:44:33:22:11\",\"switch_id\":\"oid:0x21000000000000\"}": {
"SAI_FDB_ENTRY_ATTR_BRIDGE_PORT_ID": "oid:0x3a000000000fff",
"SAI_FDB_ENTRY_ATTR_TYPE": "SAI_FDB_ENTRY_TYPE_DYNAMIC",
"SAI_FDB_ENTRY_ATTR_PACKET_ACTION": "SAI_PACKET_ACTION_FORWARD"
},
"ASIC_STATE:SAI_OBJECT_TYPE_FDB_ENTRY:{\"bvid\":\"oid:0x260000000007c7\",\"mac\":\"77:66:44:33:22:11\",\"switch_id\":\"oid:0x21000000000000\"}": {
"SAI_FDB_ENTRY_ATTR_BRIDGE_PORT_ID": "oid:0x3a0000000007ef",
"SAI_FDB_ENTRY_ATTR_TYPE": "SAI_FDB_ENTRY_TYPE_DYNAMIC",
"SAI_FDB_ENTRY_ATTR_PACKET_ACTION": "SAI_PACKET_ACTION_FORWARD"
},
"ASIC_STATE:SAI_OBJECT_TYPE_FDB_ENTRY:{\"vlan\":\"5\",\"mac\":\"77:66:55:44:22:11\",\"switch_id\":\"oid:0x21000000000000\"}": {
"SAI_FDB_ENTRY_ATTR_BRIDGE_PORT_ID": "oid:0x3a0000000006cd",
"SAI_FDB_ENTRY_ATTR_TYPE": "SAI_FDB_ENTRY_TYPE_DYNAMIC",
"SAI_FDB_ENTRY_ATTR_PACKET_ACTION": "SAI_PACKET_ACTION_FORWARD"
},
"ASIC_STATE:SAI_OBJECT_TYPE_FDB_ENTRY:{\"mac\":\"77:66:55:33:22:11\",\"switch_id\":\"oid:0x21000000000000\"}": {
"SAI_FDB_ENTRY_ATTR_BRIDGE_PORT_ID": "oid:0x3a0000000007ef",
"SAI_FDB_ENTRY_ATTR_TYPE": "SAI_FDB_ENTRY_TYPE_DYNAMIC",
"SAI_FDB_ENTRY_ATTR_PACKET_ACTION": "SAI_PACKET_ACTION_FORWARD"
},
"ASIC_STATE:SAI_OBJECT_TYPE_VLAN:oid:0x260000000005c5": {
"SAI_VLAN_ATTR_VLAN_ID": "2"
},
"ASIC_STATE:SAI_OBJECT_TYPE_VLAN:oid:0x260000000006c6": {
"SAI_VLAN_ATTR_VLAN_ID": "3"
},
"ASIC_STATE:SAI_OBJECT_TYPE_VLAN:oid:0x260000000007c7": {
"SAI_VLAN_ATTR_VLAN_ID": "4"
}
}
42 changes: 42 additions & 0 deletions tests/fdbshow_input/asic_db_def_vlan.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"ASIC_STATE:SAI_OBJECT_TYPE_SWITCH:oid:0x21000000000000": {
"SAI_SWITCH_ATTR_INIT_SWITCH": "true",
"SAI_SWITCH_ATTR_SRC_MAC_ADDRESS": "DE:AD:BE:EF:CA:FE"
},
"ASIC_STATE:SAI_OBJECT_TYPE_BRIDGE_PORT:oid:0x3a0000000005cb": {
"SAI_BRIDGE_PORT_ATTR_TYPE": "SAI_BRIDGE_PORT_TYPE_PORT",
"SAI_BRIDGE_PORT_ATTR_PORT_ID": "oid:0x1000000000528",
"SAI_BRIDGE_PORT_ATTR_ADMIN_STATE": "true",
"SAI_BRIDGE_PORT_ATTR_FDB_LEARNING_MODE": "SAI_BRIDGE_PORT_FDB_LEARNING_MODE_HW"
},
"ASIC_STATE:SAI_OBJECT_TYPE_BRIDGE_PORT:oid:0x3a0000000006cd": {
"SAI_BRIDGE_PORT_ATTR_TYPE": "SAI_BRIDGE_PORT_TYPE_PORT",
"SAI_BRIDGE_PORT_ATTR_PORT_ID": "oid:0x1000000000549",
"SAI_BRIDGE_PORT_ATTR_ADMIN_STATE": "true",
"SAI_BRIDGE_PORT_ATTR_FDB_LEARNING_MODE": "SAI_BRIDGE_PORT_FDB_LEARNING_MODE_HW"
},
"ASIC_STATE:SAI_OBJECT_TYPE_FDB_ENTRY:{\"bvid\":\"oid:0x260000000005c5\",\"mac\":\"11:22:33:44:55:66\",\"switch_id\":\"oid:0x21000000000000\"}": {
"SAI_FDB_ENTRY_ATTR_BRIDGE_PORT_ID": "oid:0x3a0000000005cb",
"SAI_FDB_ENTRY_ATTR_TYPE": "SAI_FDB_ENTRY_TYPE_DYNAMIC",
"SAI_FDB_ENTRY_ATTR_PACKET_ACTION": "SAI_PACKET_ACTION_FORWARD"
},
"ASIC_STATE:SAI_OBJECT_TYPE_FDB_ENTRY:{\"bvid\":\"oid:0x260000000006c6\",\"mac\":\"11:22:33:66:55:44\",\"switch_id\":\"oid:0x21000000000000\"}": {
"SAI_FDB_ENTRY_ATTR_BRIDGE_PORT_ID": "oid:0x3a0000000006cd",
"SAI_FDB_ENTRY_ATTR_TYPE": "SAI_FDB_ENTRY_TYPE_STATIC",
"SAI_FDB_ENTRY_ATTR_PACKET_ACTION": "SAI_PACKET_ACTION_FORWARD"
},
"ASIC_STATE:SAI_OBJECT_TYPE_FDB_ENTRY:{\"bvid\":\"oid:0x26000000000013\",\"mac\":\"66:55:44:33:22:11\",\"switch_id\":\"oid:0x21000000000000\"}": {
"SAI_FDB_ENTRY_ATTR_BRIDGE_PORT_ID": "oid:0x3a0000000005cb",
"SAI_FDB_ENTRY_ATTR_TYPE": "SAI_FDB_ENTRY_TYPE_DYNAMIC",
"SAI_FDB_ENTRY_ATTR_PACKET_ACTION": "SAI_PACKET_ACTION_FORWARD"
},
"ASIC_STATE:SAI_OBJECT_TYPE_VLAN:oid:0x260000000005c5": {
"SAI_VLAN_ATTR_VLAN_ID": "2"
},
"ASIC_STATE:SAI_OBJECT_TYPE_VLAN:oid:0x260000000006c6": {
"SAI_VLAN_ATTR_VLAN_ID": "3"
},
"ASIC_STATE:SAI_OBJECT_TYPE_VLAN:oid:0x26000000000013": {
"NULL": "NULL"
}
}
50 changes: 50 additions & 0 deletions tests/fdbshow_input/asic_db_fetch_except.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"ASIC_STATE:SAI_OBJECT_TYPE_SWITCH:oid:0x21000000000000": {
"SAI_SWITCH_ATTR_INIT_SWITCH": "true",
"SAI_SWITCH_ATTR_SRC_MAC_ADDRESS": "DE:AD:BE:EF:CA:FE"
},
"ASIC_STATE:SAI_OBJECT_TYPE_BRIDGE_PORT:oid:0x3a0000000005cb": {
"SAI_BRIDGE_PORT_ATTR_TYPE": "SAI_BRIDGE_PORT_TYPE_PORT",
"SAI_BRIDGE_PORT_ATTR_PORT_ID": "oid:0x1000000000528",
"SAI_BRIDGE_PORT_ATTR_ADMIN_STATE": "true",
"SAI_BRIDGE_PORT_ATTR_FDB_LEARNING_MODE": "SAI_BRIDGE_PORT_FDB_LEARNING_MODE_HW"
},
"ASIC_STATE:SAI_OBJECT_TYPE_BRIDGE_PORT:oid:0x3a0000000006cd": {
"SAI_BRIDGE_PORT_ATTR_TYPE": "SAI_BRIDGE_PORT_TYPE_PORT",
"SAI_BRIDGE_PORT_ATTR_PORT_ID": "oid:0x1000000000549",
"SAI_BRIDGE_PORT_ATTR_ADMIN_STATE": "true",
"SAI_BRIDGE_PORT_ATTR_FDB_LEARNING_MODE": "SAI_BRIDGE_PORT_FDB_LEARNING_MODE_HW"
},
"ASIC_STATE:SAI_OBJECT_TYPE_FDB_ENTRY:{\"bvid\":\"oid:0x260000000005c5\",\"mac\":\"11:22:33:44:55:66\",\"switch_id\":\"oid:0x21000000000000\"}": {
"SAI_FDB_ENTRY_ATTR_BRIDGE_PORT_ID": "oid:0x3a0000000005cb",
"SAI_FDB_ENTRY_ATTR_TYPE": "SAI_FDB_ENTRY_TYPE_DYNAMIC",
"SAI_FDB_ENTRY_ATTR_PACKET_ACTION": "SAI_PACKET_ACTION_FORWARD"
},
"ASIC_STATE:SAI_OBJECT_TYPE_FDB_ENTRY:{\"bvid\":\"oid:0x260000000006c6\",\"mac\":\"11:22:33:66:55:44\",\"switch_id\":\"oid:0x21000000000000\"}": {
"SAI_FDB_ENTRY_ATTR_BRIDGE_PORT_ID": "oid:0x3a0000000006cd",
"SAI_FDB_ENTRY_ATTR_TYPE": "SAI_FDB_ENTRY_TYPE_STATIC",
"SAI_FDB_ENTRY_ATTR_PACKET_ACTION": "SAI_PACKET_ACTION_FORWARD"
},
"ASIC_STATE:SAI_OBJECT_TYPE_FDB_ENTRY:{\"bvid\":\"oid:0x260000000007c7\",\"mac\":\"66:55:44:33:22:11\",\"switch_id\":\"oid:0x21000000000000\"}": {
"SAI_FDB_ENTRY_ATTR_BRIDGE_PORT_ID": "oid:0x3a0000000005cb",
"SAI_FDB_ENTRY_ATTR_TYPE": "SAI_FDB_ENTRY_TYPE_DYNAMIC",
"SAI_FDB_ENTRY_ATTR_PACKET_ACTION": "SAI_PACKET_ACTION_FORWARD"
},
"ASIC_STATE:SAI_OBJECT_TYPE_FDB_ENTRY:{}": {
"SAI_FDB_ENTRY_ATTR_BRIDGE_PORT_ID": "oid:0x3a0000000005ff",
"SAI_FDB_ENTRY_ATTR_TYPE": "SAI_FDB_ENTRY_TYPE_DYNAMIC",
"SAI_FDB_ENTRY_ATTR_PACKET_ACTION": "SAI_PACKET_ACTION_FORWARD"
},
"ASIC_STATE:SAI_OBJECT_TYPE_FDB_ENTRY:{\"bvid\":\"oid:0x260000000007c7\",\"mac\":\"77:55:44:33:22:11\",\"switch_id\":\"oid:0x21000000000000\"}": {
"SAI_FDB_ENTRY_ATTR_BRIDGE_PORT_ID": "oid:0x3a000000000fff",
"SAI_FDB_ENTRY_ATTR_TYPE": "SAI_FDB_ENTRY_TYPE_DYNAMIC",
"SAI_FDB_ENTRY_ATTR_PACKET_ACTION": "SAI_PACKET_ACTION_FORWARD"
},
"ASIC_STATE:SAI_OBJECT_TYPE_VLAN:oid:0x260000000005c5": {
"SAI_VLAN_ATTR_VLAN_ID": "2"
},
"ASIC_STATE:SAI_OBJECT_TYPE_VLAN:oid:0x260000000006c6": {
"SAI_VLAN_ATTR_VLAN_ID": "3"
},
"ASIC_STATE:SAI_OBJECT_TYPE_VLAN:oid:0x260000000007c7": {}
}
15 changes: 15 additions & 0 deletions tests/fdbshow_input/asic_db_no_bridge.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"ASIC_STATE:SAI_OBJECT_TYPE_SWITCH:oid:0x21000000000000": {
"SAI_SWITCH_ATTR_INIT_SWITCH": "true",
"SAI_SWITCH_ATTR_SRC_MAC_ADDRESS": "DE:AD:BE:EF:CA:FE"
},
"ASIC_STATE:SAI_OBJECT_TYPE_VLAN:oid:0x260000000005c5": {
"SAI_VLAN_ATTR_VLAN_ID": "2"
},
"ASIC_STATE:SAI_OBJECT_TYPE_VLAN:oid:0x260000000006c6": {
"SAI_VLAN_ATTR_VLAN_ID": "3"
},
"ASIC_STATE:SAI_OBJECT_TYPE_VLAN:oid:0x260000000007c7": {
"SAI_VLAN_ATTR_VLAN_ID": "4"
}
}
27 changes: 27 additions & 0 deletions tests/fdbshow_input/asic_db_no_fdb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"ASIC_STATE:SAI_OBJECT_TYPE_SWITCH:oid:0x21000000000000": {
"SAI_SWITCH_ATTR_INIT_SWITCH": "true",
"SAI_SWITCH_ATTR_SRC_MAC_ADDRESS": "DE:AD:BE:EF:CA:FE"
},
"ASIC_STATE:SAI_OBJECT_TYPE_BRIDGE_PORT:oid:0x3a0000000005cb": {
"SAI_BRIDGE_PORT_ATTR_TYPE": "SAI_BRIDGE_PORT_TYPE_PORT",
"SAI_BRIDGE_PORT_ATTR_PORT_ID": "oid:0x1000000000528",
"SAI_BRIDGE_PORT_ATTR_ADMIN_STATE": "true",
"SAI_BRIDGE_PORT_ATTR_FDB_LEARNING_MODE": "SAI_BRIDGE_PORT_FDB_LEARNING_MODE_HW"
},
"ASIC_STATE:SAI_OBJECT_TYPE_BRIDGE_PORT:oid:0x3a0000000006cd": {
"SAI_BRIDGE_PORT_ATTR_TYPE": "SAI_BRIDGE_PORT_TYPE_PORT",
"SAI_BRIDGE_PORT_ATTR_PORT_ID": "oid:0x1000000000549",
"SAI_BRIDGE_PORT_ATTR_ADMIN_STATE": "true",
"SAI_BRIDGE_PORT_ATTR_FDB_LEARNING_MODE": "SAI_BRIDGE_PORT_FDB_LEARNING_MODE_HW"
},
"ASIC_STATE:SAI_OBJECT_TYPE_VLAN:oid:0x260000000005c5": {
"SAI_VLAN_ATTR_VLAN_ID": "2"
},
"ASIC_STATE:SAI_OBJECT_TYPE_VLAN:oid:0x260000000006c6": {
"SAI_VLAN_ATTR_VLAN_ID": "3"
},
"ASIC_STATE:SAI_OBJECT_TYPE_VLAN:oid:0x260000000007c7": {
"SAI_VLAN_ATTR_VLAN_ID": "4"
}
}
9 changes: 9 additions & 0 deletions tests/fdbshow_input/counters_db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"COUNTERS_PORT_NAME_MAP": {
"Ethernet0": "oid:0x1000000000528",
"Ethernet4": "oid:0x1000000000549"
},
"COUNTERS_LAG_NAME_MAP": {
"": ""
}
}
Loading

0 comments on commit 3a482ac

Please sign in to comment.