Skip to content

Commit 77c856a

Browse files
authored
[202311][dhcp_server] Add support for customize string option include comma (#18811)
* [dhcp_server] Add support for customize comma-sperated string option
1 parent d1bd2d2 commit 77c856a

File tree

4 files changed

+68
-62
lines changed

4 files changed

+68
-62
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def _parse_customized_options(self, customized_options_ipv4):
9494
always_send = config["always_send"] if "always_send" in config else "true"
9595
customized_options[option_name] = {
9696
"id": config["id"],
97-
"value": config["value"],
97+
"value": config["value"].replace(",", "\\\\,") if option_type == "string" else config["value"],
9898
"type": option_type,
9999
"always_send": always_send
100100
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"key": "dummy_value\\,dummy_value"
3+
}

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

+50-60
Original file line numberDiff line numberDiff line change
@@ -209,61 +209,53 @@
209209
}
210210
}
211211
}
212-
tested_options_data = [
213-
{
214-
"data": {
215-
"option223": {
216-
"id": "223",
217-
"type": "string",
218-
"value": "dummy_value"
219-
}
212+
tested_options_data = {
213+
"data": {
214+
"option223": {
215+
"id": "223",
216+
"type": "string",
217+
"value": "dummy_value"
220218
},
221-
"res": True
222-
},
223-
{
224-
"data": {
225-
"option60": {
226-
"id": "60",
227-
"type": "string",
228-
"value": "dummy_value"
229-
}
219+
"option60": {
220+
"id": "60",
221+
"type": "string",
222+
"value": "dummy_value"
230223
},
231-
"res": False
232-
},
233-
{
234-
"data": {
235-
"option222": {
236-
"id": "222",
237-
"type": "text",
238-
"value": "dummy_value"
239-
}
224+
"option222": {
225+
"id": "222",
226+
"type": "text",
227+
"value": "dummy_value"
240228
},
241-
"res": False
242-
},
243-
{
244-
"data": {
245-
"option219": {
246-
"id": "219",
247-
"type": "uint8",
248-
"value": "259"
249-
}
229+
"option219": {
230+
"id": "219",
231+
"type": "uint8",
232+
"value": "259"
250233
},
251-
"res": False
252-
},
253-
{
254-
"data": {
255-
"option223": {
256-
"id": "223",
257-
"type": "string",
258-
"value": "long_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_value" +
259-
"long_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_value" +
260-
"long_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_value" +
261-
"long_valuelong_valuelong_valuelong_valuelong_value"
262-
}
234+
"option218": {
235+
"id": "218",
236+
"type": "string",
237+
"value": "long_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_value" +
238+
"long_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_value" +
239+
"long_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_value" +
240+
"long_valuelong_valuelong_valuelong_valuelong_value"
241+
},
242+
"option217": {
243+
"id": "217",
244+
"type": "string",
245+
"value": "dummy_value,dummy_value"
263246
},
264-
"res": False
247+
"option216": {
248+
"id": "216",
249+
"type": "uint8",
250+
"value": "8"
251+
}
252+
},
253+
"res": {
254+
"option223": "dummy_value",
255+
"option217": "dummy_value\\\\,dummy_value",
256+
"option216": "8"
265257
}
266-
]
258+
}
267259

268260

269261
def test_parse_port_alias(mock_swsscommon_dbconnector_init, mock_get_render_template):
@@ -397,21 +389,19 @@ def test_render_config(mock_swsscommon_dbconnector_init, mock_parse_port_map_ali
397389
assert json.loads(config) == expected_config if with_port_config else expected_config
398390

399391

400-
@pytest.mark.parametrize("tested_options_data", tested_options_data)
401392
def test_parse_customized_options(mock_swsscommon_dbconnector_init, mock_get_render_template,
402-
mock_parse_port_map_alias, tested_options_data):
393+
mock_parse_port_map_alias):
403394
dhcp_db_connector = DhcpDbConnector()
404395
dhcp_cfg_generator = DhcpServCfgGenerator(dhcp_db_connector)
405396
customized_options_ipv4 = tested_options_data["data"]
406397
customized_options = dhcp_cfg_generator._parse_customized_options(customized_options_ipv4)
407-
if tested_options_data["res"]:
408-
assert customized_options == {
409-
"option223": {
410-
"id": "223",
411-
"value": "dummy_value",
412-
"type": "string",
413-
"always_send": "true"
414-
}
398+
expected_res = {}
399+
for key, value in tested_options_data["res"].items():
400+
expected_res[key] = {
401+
"id": customized_options_ipv4[key]["id"],
402+
"value": value,
403+
"type": customized_options_ipv4[key]["type"],
404+
"always_send": "true"
415405
}
416406
else:
417-
assert customized_options == {}
407+
assert customized_options == expected_res

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
import json
23
import psutil
34
import signal
45
import sys
@@ -18,11 +19,18 @@
1819
"VlanMemberTableEventChecker"]
1920

2021

22+
tested_config = """
23+
{
24+
"key": "dummy_value\\\\,dummy_value"
25+
}
26+
"""
27+
28+
2129
@pytest.mark.parametrize("enabled_checker", [None, set(PORT_MODE_CHECKER)])
2230
def test_dump_dhcp4_config(mock_swsscommon_dbconnector_init, enabled_checker):
2331
new_enabled_checker = set(["VlanTableEventChecker"])
2432
with patch("dhcp_utilities.dhcpservd.dhcp_cfggen.DhcpServCfgGenerator.generate",
25-
return_value=("dummy_config", set(), set(), set(), new_enabled_checker)) as mock_generate, \
33+
return_value=(tested_config, set(), set(), set(), new_enabled_checker)) as mock_generate, \
2634
patch("dhcp_utilities.dhcpservd.dhcpservd.DhcpServd._notify_kea_dhcp4_proc",
2735
MagicMock()) as mock_notify_kea_dhcp4_proc, \
2836
patch.object(DhcpServd, "dhcp_servd_monitor", return_value=DhcpServdDbMonitor,
@@ -39,6 +47,11 @@ def test_dump_dhcp4_config(mock_swsscommon_dbconnector_init, enabled_checker):
3947
dhcpservd.dump_dhcp4_config()
4048
# Verfiy whether generate() func of dhcp_cfggen is called
4149
mock_generate.assert_called_once_with()
50+
with open("tests/test_data/test_kea_config.conf", "r") as file, \
51+
open("/tmp/kea-dhcp4.conf", "r") as output:
52+
expected_content = file.read()
53+
actual_content = output.read()
54+
assert json.loads(expected_content) == json.loads(actual_content)
4255
# Verify whether notify func of dhcpservd is called, which is expected to call after new config generated
4356
mock_notify_kea_dhcp4_proc.assert_called_once_with()
4457
if enabled_checker is None:

0 commit comments

Comments
 (0)