Skip to content

Commit 5a7facb

Browse files
yaqiangzrajib-dutta1
authored andcommitted
[dhcp_server] Fix parse_dpus error (#17870)
1 parent 18318e4 commit 5a7facb

File tree

4 files changed

+157
-11
lines changed

4 files changed

+157
-11
lines changed

src/sonic-dhcp-utilities/dhcp_utilities/dhcpservd/dhcp_cfggen.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def generate(self):
7070
# Parse dpu
7171
dpus_table = self.db_connector.get_config_db_table(DPUS)
7272
mid_plane_table = self.db_connector.get_config_db_table(MID_PLANE_BRIDGE)
73-
mid_plane, dpus = self._parse_dpu(dpus_table, mid_plane_table) if smart_switch else {}, {}
73+
mid_plane, dpus = self._parse_dpu(dpus_table, mid_plane_table) if smart_switch else ({}, {})
7474

7575
dhcp_server_ipv4, customized_options_ipv4, range_ipv4, port_ipv4 = self._get_dhcp_ipv4_tables_from_db()
7676
# Parse range table

src/sonic-dhcp-utilities/tests/test_data/mock_config_db_smart_switch.json

+37-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"MID_PLANE_BRIDGE": {
99
"GLOBAL": {
1010
"bridge": "bridge_midplane",
11-
"address": "169.254.200.254/24"
11+
"ip_prefix": "169.254.200.254/24"
1212
}
1313
},
1414
"DHCP_SERVER_IPV4": {
@@ -23,5 +23,41 @@
2323
"netmask": "255.255.255.0",
2424
"state": "enabled"
2525
}
26+
},
27+
"DHCP_SERVER_IPV4_PORT": {
28+
"bridge_midplane|dpu0": {
29+
"ips": [
30+
"169.254.200.1"
31+
]
32+
},
33+
"bridge_midplane|dpu1": {
34+
"ips": [
35+
"169.254.200.2"
36+
]
37+
},
38+
"bridge_midplane|dpu2": {
39+
"ips": [
40+
"169.254.200.3"
41+
]
42+
},
43+
"bridge_midplane|dpu3": {
44+
"ips": [
45+
"169.254.200.4"
46+
]
47+
}
48+
},
49+
"DPUS": {
50+
"dpu0": {
51+
"midplane_interface": "dpu0"
52+
},
53+
"dpu1": {
54+
"midplane_interface": "dpu1"
55+
},
56+
"dpu2": {
57+
"midplane_interface": "dpu2"
58+
},
59+
"dpu3": {
60+
"midplane_interface": "dpu3"
61+
}
2662
}
2763
}

src/sonic-dhcp-utilities/tests/test_dhcp_cfggen.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -324,22 +324,19 @@ def test_parse_port(test_config_db, mock_swsscommon_dbconnector_init, mock_get_r
324324
if test_config_db == "mock_config_db.json" else set())
325325

326326

327-
@pytest.mark.parametrize("mid_plane", [{}, {"bridge": "mid_plane", "ip_prefix": "192.168.0.1/24"}])
328-
@pytest.mark.parametrize("is_smart_switch", [True, False])
329-
def test_generate(mock_swsscommon_dbconnector_init, mock_parse_port_map_alias, mock_get_render_template, mid_plane,
330-
is_smart_switch):
327+
def test_generate(mock_swsscommon_dbconnector_init, mock_parse_port_map_alias, mock_get_render_template):
331328
with patch.object(DhcpServCfgGenerator, "_parse_hostname"), \
332329
patch.object(DhcpServCfgGenerator, "_parse_vlan", return_value=({}, set(["Ethernet0"]))), \
333330
patch.object(DhcpServCfgGenerator, "_get_dhcp_ipv4_tables_from_db", return_value=(None, None, None, None)), \
334331
patch.object(DhcpServCfgGenerator, "_parse_range"), \
335332
patch.object(DhcpServCfgGenerator, "_parse_port", return_value=(None, set(["range1"]))), \
336333
patch.object(DhcpServCfgGenerator, "_parse_customized_options"), \
337-
patch.object(DhcpServCfgGenerator, "_parse_dpu", side_effect=[mid_plane, set()]), \
334+
patch.object(DhcpServCfgGenerator, "_parse_dpu", return_value=(set(), set())), \
338335
patch.object(DhcpServCfgGenerator, "_construct_obj_for_template",
339336
return_value=(None, set(["Vlan1000"]), set(["option1"]), set(["dummy"]))), \
340337
patch.object(DhcpServCfgGenerator, "_render_config", return_value="dummy_config"), \
341338
patch.object(DhcpDbConnector, "get_config_db_table", side_effect=mock_get_config_db_table), \
342-
patch("dhcp_utilities.dhcpservd.dhcp_cfggen.is_smart_switch", return_value=is_smart_switch):
339+
patch("dhcp_utilities.dhcpservd.dhcp_cfggen.is_smart_switch", return_value=False):
343340
dhcp_db_connector = DhcpDbConnector()
344341
dhcp_cfg_generator = DhcpServCfgGenerator(dhcp_db_connector)
345342
kea_dhcp4_config, used_ranges, enabled_dhcp_interfaces, used_options, subscribe_table = \
@@ -349,9 +346,6 @@ def test_generate(mock_swsscommon_dbconnector_init, mock_parse_port_map_alias, m
349346
assert enabled_dhcp_interfaces == set(["Vlan1000"])
350347
assert used_options == set(["option1"])
351348
expected_tables = set(["dummy"])
352-
if is_smart_switch:
353-
expected_tables |= set(["DpusTableEventChecker", "MidPlaneTableEventChecker"])
354-
355349
assert subscribe_table == expected_tables
356350

357351

src/sonic-dhcp-utilities/tests/test_smart_switch.py

+116
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,106 @@
1+
import json
12
import pytest
23
from common_utils import MockConfigDb, dhcprelayd_refresh_dhcrelay_test, dhcprelayd_proceed_with_check_res_test
34
from dhcp_utilities.dhcprelayd.dhcprelayd import DHCP_SERVER_CHECKER, MID_PLANE_CHECKER
5+
from dhcp_utilities.dhcpservd.dhcp_cfggen import DhcpServCfgGenerator
6+
from dhcp_utilities.common.utils import DhcpDbConnector
7+
from unittest.mock import patch
48

59
MOCK_CONFIG_DB_PATH_SMART_SWITCH = "tests/test_data/mock_config_db_smart_switch.json"
10+
expected_kea_config = {
11+
"Dhcp4": {
12+
"hooks-libraries": [
13+
{
14+
"library": "/usr/local/lib/kea/hooks/libdhcp_run_script.so",
15+
"parameters": {
16+
"name": "/etc/kea/lease_update.sh",
17+
"sync": False
18+
}
19+
}
20+
],
21+
"interfaces-config": {
22+
"interfaces": [
23+
"eth0"
24+
]
25+
},
26+
"control-socket": {
27+
"socket-type": "unix",
28+
"socket-name": "/run/kea/kea4-ctrl-socket"
29+
},
30+
"lease-database": {
31+
"type": "memfile",
32+
"persist": True,
33+
"name": "/tmp/kea-lease.csv",
34+
"lfc-interval": 3600
35+
},
36+
"subnet4": [
37+
{
38+
"subnet": "169.254.200.0/24",
39+
"pools": [
40+
{
41+
"pool": "169.254.200.1 - 169.254.200.1",
42+
"client-class": "sonic-host:dpu0"
43+
},
44+
{
45+
"pool": "169.254.200.2 - 169.254.200.2",
46+
"client-class": "sonic-host:dpu1"
47+
},
48+
{
49+
"pool": "169.254.200.3 - 169.254.200.3",
50+
"client-class": "sonic-host:dpu2"
51+
},
52+
{
53+
"pool": "169.254.200.4 - 169.254.200.4",
54+
"client-class": "sonic-host:dpu3"
55+
}
56+
],
57+
"option-data": [
58+
{
59+
"name": "routers",
60+
"data": "169.254.200.254"
61+
},
62+
{
63+
"name": "dhcp-server-identifier",
64+
"data": "169.254.200.254"
65+
}
66+
],
67+
"valid-lifetime": 900,
68+
"reservations": []
69+
}
70+
],
71+
"loggers": [
72+
{
73+
"name": "kea-dhcp4",
74+
"output_options": [
75+
{
76+
"output": "/var/log/kea-dhcp.log",
77+
"pattern": "%-5p %m\n"
78+
}
79+
],
80+
"severity": "INFO",
81+
"debuglevel": 0
82+
}
83+
],
84+
"client-classes": [
85+
{
86+
"name": "sonic-host:dpu0",
87+
"test": "substring(relay4[1].hex, -15, 15) == 'sonic-host:dpu0'"
88+
},
89+
{
90+
"name": "sonic-host:dpu1",
91+
"test": "substring(relay4[1].hex, -15, 15) == 'sonic-host:dpu1'"
92+
},
93+
{
94+
"name": "sonic-host:dpu2",
95+
"test": "substring(relay4[1].hex, -15, 15) == 'sonic-host:dpu2'"
96+
},
97+
{
98+
"name": "sonic-host:dpu3",
99+
"test": "substring(relay4[1].hex, -15, 15) == 'sonic-host:dpu3'"
100+
}
101+
]
102+
}
103+
}
6104

7105

8106
def test_dhcprelayd_refresh_dhcrelay(mock_swsscommon_dbconnector_init):
@@ -21,6 +119,24 @@ def test_dhcprelayd_proceed_with_check_res(mock_swsscommon_dbconnector_init, moc
21119
None, True, expected_checkers)
22120

23121

122+
def test_dhcp_dhcp_cfggen_generate(mock_swsscommon_dbconnector_init, mock_parse_port_map_alias):
123+
with patch.object(DhcpDbConnector, "get_config_db_table", side_effect=mock_get_config_db_table):
124+
dhcp_db_connector = DhcpDbConnector()
125+
dhcp_cfg_generator = DhcpServCfgGenerator(dhcp_db_connector,
126+
kea_conf_template_path="tests/test_data/kea-dhcp4.conf.j2")
127+
kea_dhcp4_config, used_ranges, enabled_dhcp_interfaces, used_options, subscribe_table = \
128+
dhcp_cfg_generator.generate()
129+
assert json.loads(kea_dhcp4_config) == expected_kea_config
130+
assert used_ranges == set()
131+
assert enabled_dhcp_interfaces == set(["bridge_midplane"])
132+
assert used_options == set()
133+
expected_tables = set(["DpusTableEventChecker", "MidPlaneTableEventChecker", "VlanTableEventChecker",
134+
"VlanIntfTableEventChecker", "DhcpRangeTableEventChecker", "VlanMemberTableEventChecker",
135+
"DhcpOptionTableEventChecker", "DhcpPortTableEventChecker",
136+
"DhcpServerTableCfgChangeEventChecker"])
137+
assert subscribe_table == expected_tables
138+
139+
24140
def mock_get_config_db_table(table_name):
25141
mock_config_db = MockConfigDb(MOCK_CONFIG_DB_PATH_SMART_SWITCH)
26142
return mock_config_db.get_config_db_table(table_name)

0 commit comments

Comments
 (0)