Skip to content

Commit 3156f45

Browse files
authored
improve unit test coverage for fast-reboot-dump script (sonic-net#4154)
What I did enhances unit test coverage for scripts/fast-reboot-dump.py generate_default_route_entries generate_media_config generate_fdb_entries generate_neighbor_entries This partially fixes sonic-net#1175 by increasing coverage of the fast-reboot-dump, coverage improving from 48.5% to 70% How I did it How to verify it coverage after this unit test scripts/fast-reboot-dump.py 270 71 82 12 70%
1 parent 8012ba4 commit 3156f45

1 file changed

Lines changed: 126 additions & 2 deletions

File tree

tests/fast_reboot_dump_test.py

Lines changed: 126 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
from deepdiff import DeepDiff
44
from utilities_common.db import Db
55
import importlib
6+
import tempfile
7+
from unittest import mock
8+
from .mock_tables import dbconnector
69
fast_reboot_dump = importlib.import_module("scripts.fast-reboot-dump")
710

811
class TestFastRebootDump(object):
@@ -23,7 +26,7 @@ def setup_class(cls):
2326
cls.app_db = app_db
2427

2528
#Test fast-reboot-dump script to generate all required objects when there is a VLAN interface with a PortChannel member.
26-
def test_generate_fdb_entries_vlan_portcahnnel_member(self):
29+
def test_generate_fdb_entries_vlan_portchannel_member(self):
2730
vlan_ifaces = ['Vlan2']
2831

2932
fdb_entries, all_available_macs, map_mac_ip_per_vlan = fast_reboot_dump.generate_fdb_entries_logic(self.asic_db, self.app_db, vlan_ifaces)
@@ -36,7 +39,128 @@ def test_generate_fdb_entries_vlan_portcahnnel_member(self):
3639

3740
expectd_map_mac_ip_per_vlan = {'Vlan2': {'52:54:00:5d:fc:b7': 'PortChannel0001'}}
3841
assert not DeepDiff(map_mac_ip_per_vlan, expectd_map_mac_ip_per_vlan, ignore_order=True)
39-
42+
43+
@mock.patch.object(fast_reboot_dump.syslog, "syslog", return_value=None)
44+
@mock.patch.object(fast_reboot_dump, "SonicV2Connector")
45+
def test_generate_neighbor_entries(self, mock_sonicv2, _mock_syslog):
46+
conn = dbconnector.SonicV2Connector()
47+
mock_sonicv2.return_value = conn
48+
49+
conn.connect(conn.APPL_DB)
50+
51+
# Included: (Vlan2, mac) is present in all_available_macs
52+
key_included = 'NEIGH_TABLE:Vlan2:192.168.0.2'
53+
conn.set(conn.APPL_DB, key_included, 'neigh', '52:54:00:5D:FC:B7')
54+
55+
# Excluded: exists in DB, but (Vlan3, mac) is NOT present in all_available_macs
56+
# so generate_neighbor_entries() skip it
57+
key_excluded = 'NEIGH_TABLE:Vlan3:192.168.0.3'
58+
conn.set(conn.APPL_DB, key_excluded, 'neigh', 'aa:bb:cc:dd:ee:ff')
59+
60+
all_available_macs = {('Vlan2', '52:54:00:5d:fc:b7')}
61+
62+
with tempfile.TemporaryDirectory() as tmpdir:
63+
outfile = os.path.join(tmpdir, 'arp.json')
64+
65+
neighbor_entries = fast_reboot_dump.generate_neighbor_entries(
66+
outfile, all_available_macs
67+
)
68+
69+
assert neighbor_entries == [
70+
('Vlan2', '52:54:00:5d:fc:b7', '192.168.0.2')
71+
]
72+
73+
with open(outfile) as fp:
74+
data = json.load(fp)
75+
76+
assert len(data) == 1
77+
obj = data[0]
78+
assert key_included in obj
79+
assert obj[key_included]['neigh'] == '52:54:00:5D:FC:B7'
80+
assert obj['OP'] == 'SET'
81+
assert not any(key_excluded in item for item in data)
82+
83+
@mock.patch.object(fast_reboot_dump, "SonicV2Connector")
84+
def test_generate_default_route_entries(self, mock_sonicv2):
85+
conn = dbconnector.SonicV2Connector()
86+
mock_sonicv2.return_value = conn
87+
88+
conn.connect(conn.APPL_DB)
89+
conn.set(conn.APPL_DB, 'ROUTE_TABLE:0.0.0.0/0', 'nexthop', '10.0.0.1')
90+
conn.set(conn.APPL_DB, 'ROUTE_TABLE:0.0.0.0/0', 'ifname', 'Ethernet0')
91+
conn.set(conn.APPL_DB, 'ROUTE_TABLE::/0', 'nexthop', '2001:db8::1')
92+
conn.set(conn.APPL_DB, 'ROUTE_TABLE::/0', 'ifname', 'Ethernet0')
93+
94+
with tempfile.TemporaryDirectory() as tmpdir:
95+
outfile = os.path.join(tmpdir, 'default_routes.json')
96+
fast_reboot_dump.generate_default_route_entries(outfile)
97+
98+
with open(outfile) as fp:
99+
data = json.load(fp)
100+
101+
ipv4_obj = next(item for item in data if 'ROUTE_TABLE:0.0.0.0/0' in item)
102+
assert ipv4_obj['ROUTE_TABLE:0.0.0.0/0']['nexthop'] == '10.0.0.1'
103+
assert ipv4_obj['ROUTE_TABLE:0.0.0.0/0']['ifname'] == 'Ethernet0'
104+
assert ipv4_obj['OP'] == 'SET'
105+
106+
ipv6_candidates = [item for item in data if 'ROUTE_TABLE::/0' in item]
107+
if ipv6_candidates:
108+
ipv6_obj = ipv6_candidates[0]
109+
assert ipv6_obj['ROUTE_TABLE::/0']['nexthop'] == '2001:db8::1'
110+
assert ipv6_obj['ROUTE_TABLE::/0']['ifname'] == 'Ethernet0'
111+
112+
@mock.patch.object(fast_reboot_dump, "SonicV2Connector")
113+
def test_generate_media_config(self, mock_sonicv2):
114+
conn = dbconnector.SonicV2Connector()
115+
mock_sonicv2.return_value = conn
116+
117+
conn.connect(conn.APPL_DB)
118+
119+
key = 'PORT_TABLE:Ethernet0'
120+
conn.set(conn.APPL_DB, key, 'preemphasis', '1')
121+
conn.set(conn.APPL_DB, key, 'idriver', '2')
122+
conn.set(conn.APPL_DB, key, 'speed', '100000')
123+
124+
with tempfile.TemporaryDirectory() as tmpdir:
125+
outfile = os.path.join(tmpdir, 'media_config.json')
126+
media_config = fast_reboot_dump.generate_media_config(outfile)
127+
128+
with open(outfile) as fp:
129+
file_data = json.load(fp)
130+
131+
entry = next(item for item in media_config if key in item)
132+
attrs = entry[key]
133+
assert attrs == {
134+
'preemphasis': '1',
135+
'idriver': '2'
136+
}
137+
assert 'speed' not in attrs
138+
139+
file_entry = next(item for item in file_data if key in item)
140+
assert file_entry[key] == attrs
141+
assert entry['OP'] == 'SET'
142+
assert file_entry['OP'] == 'SET'
143+
144+
@mock.patch.object(fast_reboot_dump, "get_vlan_ifaces", return_value=[])
145+
@mock.patch.object(fast_reboot_dump, "SonicV2Connector")
146+
def test_generate_fdb_entries(self, mock_sonicv2, _mock_get_vlan_ifaces):
147+
conn = dbconnector.SonicV2Connector()
148+
mock_sonicv2.return_value = conn
149+
150+
with tempfile.TemporaryDirectory() as tmpdir:
151+
outfile = os.path.join(tmpdir, 'fdb.json')
152+
153+
all_available_macs, map_mac_ip_per_vlan = fast_reboot_dump.generate_fdb_entries(outfile)
154+
155+
assert all_available_macs == set()
156+
assert map_mac_ip_per_vlan == {}
157+
158+
with open(outfile) as fp:
159+
data = json.load(fp)
160+
161+
assert data == []
162+
163+
40164
@classmethod
41165
def teardown_class(cls):
42166
print("TEARDOWN")

0 commit comments

Comments
 (0)