[dhcp_server] Add field not exist checks in dhcp_cfggen (#17645)

* [dhcp_server] Add field not exist checks in dhcp_cfggen
This commit is contained in:
Yaqiang Zhu 2024-01-03 01:04:14 +08:00 committed by mssonicbld
parent a110e625a2
commit f97b53322f
4 changed files with 47 additions and 7 deletions

View File

@ -134,6 +134,7 @@ class DhcpServCfgGenerator(object):
client_classes = []
enabled_dhcp_interfaces = set()
used_options = set()
customized_option_keys = customized_options.keys()
# Different mode would subscribe different table, always subscribe DHCP_SERVER_IPV4
subscribe_table = set(["DhcpServerTableCfgChangeEventChecker"])
for dhcp_interface_name, dhcp_config in dhcp_server_ipv4.items():
@ -147,8 +148,12 @@ class DhcpServCfgGenerator(object):
.format(dhcp_interface_name))
continue
curr_options = {}
for option in dhcp_config["customized_options"]:
if option in customized_options.keys():
if "customized_options" in dhcp_config:
for option in dhcp_config["customized_options"]:
if option not in customized_option_keys:
syslog.syslog(syslog.LOG_WARNING, "Customized option {} configured for {} is not defined"
.format(option, dhcp_interface_name))
continue
curr_options[option] = {
"always_send": customized_options[option]["always_send"],
"value": customized_options[option]["value"]
@ -352,6 +357,9 @@ class DhcpServCfgGenerator(object):
syslog.syslog(syslog.LOG_WARNING, f"Cannot find {splits[1]} in port_alias_map")
continue
port = self.port_alias_map[splits[1]]
if dhcp_interface_name not in vlan_interfaces:
syslog.syslog(syslog.LOG_WARNING, f"Interface {dhcp_interface_name} doesn't have IPv4 address")
continue
if dhcp_interface_name not in port_ips:
port_ips[dhcp_interface_name] = {}
# Get ip information of Vlan

View File

@ -35,7 +35,8 @@ def mock_get_render_template():
def mock_parse_port_map_alias(scope="function"):
with patch("dhcp_utilities.dhcpservd.dhcp_cfggen.DhcpServCfgGenerator._parse_port_map_alias",
return_value=None) as mock_map, \
patch.object(DhcpServCfgGenerator, "port_alias_map", return_value={"Ethernet24": "etp7", "Ethernet28": "etp8"},
patch.object(DhcpServCfgGenerator, "port_alias_map", return_value={"Ethernet24": "etp7", "Ethernet28": "etp8",
"Ethernet44": "etp12"},
new_callable=PropertyMock), \
patch.object(DhcpServCfgGenerator, "lease_update_script_path", return_value="/etc/kea/lease_update.sh",
new_callable=PropertyMock), \

View File

@ -6,7 +6,8 @@
},
"VLAN": {
"Vlan1000": {},
"Vlan2000": {}
"Vlan2000": {},
"Vlan3000": {}
},
"VLAN_INTERFACE": {
"Vlan1000|192.168.0.1/21": {
@ -26,6 +27,9 @@
},
"Vlan2000": {
"NULL": "NULL"
},
"Vlan3000": {
"NULL": "NULL"
}
},
"VLAN_MEMBER": {
@ -37,6 +41,9 @@
},
"Vlan1000|Ethernet40": {
"tagging_mode": "untagged"
},
"Vlan3000|Ethernet44": {
"tagging_mode": "untagged"
}
},
"DHCP_SERVER_IPV4": {
@ -92,6 +99,13 @@
"mode": "PORT",
"netmask": "255.255.255.0",
"state": "disabled"
},
"Vlan6000": {
"gateway": "192.168.2.1",
"lease_time": "900",
"mode": "PORT",
"netmask": "255.255.255.0",
"state": "enabled"
}
},
"DHCP_SERVER_IPV4_RANGE": {
@ -160,6 +174,11 @@
"ips": [
"192.168.0.10"
]
},
"Vlan3000|Ethernet44": {
"ips": [
"192.168.0.10"
]
}
},
"PORT": {

View File

@ -303,7 +303,8 @@ def test_parse_vlan(mock_swsscommon_dbconnector_init, mock_parse_port_map_alias,
vlan_interfaces, vlan_members = dhcp_cfg_generator._parse_vlan(mock_config_db.config_db.get("VLAN_INTERFACE"),
mock_config_db.config_db.get("VLAN_MEMBER"))
assert vlan_interfaces == expected_vlan_ipv4_interface
assert list(vlan_members) == ["Vlan1000|Ethernet24", "Vlan1000|Ethernet28", "Vlan1000|Ethernet40"]
expeceted_members = ["Vlan1000|Ethernet24", "Vlan1000|Ethernet28", "Vlan1000|Ethernet40", "Vlan3000|Ethernet44"]
assert list(vlan_members) == expeceted_members
@pytest.mark.parametrize("test_config_db", ["mock_config_db.json", "mock_config_db_without_port_config.json"])
@ -352,11 +353,22 @@ def test_construct_obj_for_template(mock_swsscommon_dbconnector_init, mock_parse
customized_options = {"option223": {"id": "223", "value": "dummy_value", "type": "string", "always_send": "true"}}
dhcp_cfg_generator = DhcpServCfgGenerator(dhcp_db_connector)
tested_hostname = "sonic-host"
port_ips = {
"Vlan1000": {
"192.168.0.1/21": {
"etp8": [["192.168.0.2", "192.168.0.6"], ["192.168.0.10", "192.168.0.10"]],
"etp7": [["192.168.0.7", "192.168.0.7"]],
"etp9": []
}
},
"Vlan6000": {
}
}
render_obj, enabled_dhcp_interfaces, used_options, subscribe_table = \
dhcp_cfg_generator._construct_obj_for_template(mock_config_db.config_db.get("DHCP_SERVER_IPV4"),
tested_parsed_port, tested_hostname, customized_options)
port_ips, tested_hostname, customized_options)
assert render_obj == expected_render_obj
assert enabled_dhcp_interfaces == {"Vlan1000", "Vlan4000", "Vlan3000"}
assert enabled_dhcp_interfaces == {"Vlan1000", "Vlan4000", "Vlan3000", "Vlan6000"}
assert used_options == set(["option223"])
assert subscribe_table == set(PORT_MODE_CHECKER)