[dhcp_server] Add support for only configures 1 ip in dhcp_server range (#17280)

How I did it
Add support for only configures 1 ip in dhcp_server range.
Treat range with value out of order as invalid range.
This commit is contained in:
Yaqiang Zhu 2023-11-28 00:44:41 -05:00 committed by GitHub
parent 90ff72c885
commit be95d49db6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 10 deletions

View File

@ -251,15 +251,17 @@ class DhcpServCfgGenerator(object):
ranges = {}
for range in list(range_ipv4.keys()):
curr_range = range_ipv4.get(range, {}).get("range", {})
if len(curr_range) != 2:
syslog.syslog(syslog.LOG_WARNING, f"Length of {curr_range} != 2")
list_length = len(curr_range)
if list_length == 0 or list_length > 2:
syslog.syslog(syslog.LOG_WARNING, f"Length of {curr_range} is {list_length}, which is invalid!")
continue
address_1 = ipaddress.ip_address(curr_range[0])
address_2 = ipaddress.ip_address(curr_range[1])
address_start = ipaddress.ip_address(curr_range[0])
address_end = ipaddress.ip_address(curr_range[1] if list_length == 2 else curr_range[0])
# To make sure order of range is correct
range_start = address_1 if address_1 < address_2 else address_2
range_end = address_2 if address_1 < address_2 else address_1
ranges[range] = [range_start, range_end]
if address_start > address_end:
syslog.syslog(syslog.LOG_WARNING, f"Start of {curr_range} is greater than end, skip it")
continue
ranges[range] = [address_start, address_end]
return ranges

View File

@ -123,6 +123,9 @@
"192.168.8.2",
"192.168.8.3"
]
},
"range5": {
"range": []
}
},
"DHCP_SERVER_IPV4_PORT": {

View File

@ -140,7 +140,7 @@ expected_dhcp_config_without_port_config = {
}
}
expected_parsed_range = {
"range2": [ipaddress.IPv4Address("192.168.0.3"), ipaddress.IPv4Address("192.168.0.6")],
"range4": [ipaddress.IPv4Address("192.168.0.1"), ipaddress.IPv4Address("192.168.0.1")],
"range3": [ipaddress.IPv4Address("192.168.0.10"), ipaddress.IPv4Address("192.168.0.10")],
"range1": [ipaddress.IPv4Address("192.168.0.2"), ipaddress.IPv4Address("192.168.0.5")],
"range0": [ipaddress.IPv4Address("192.168.8.2"), ipaddress.IPv4Address("192.168.8.3")]
@ -164,7 +164,7 @@ expected_vlan_ipv4_interface = {
expected_parsed_port = {
"Vlan1000": {
"192.168.0.1/21": {
"etp8": [["192.168.0.2", "192.168.0.6"], ["192.168.0.10", "192.168.0.10"]],
"etp8": [["192.168.0.2", "192.168.0.5"], ["192.168.0.10", "192.168.0.10"]],
"etp7": [["192.168.0.7", "192.168.0.7"]]
}
}
@ -318,7 +318,7 @@ def test_parse_port(test_config_db, mock_swsscommon_dbconnector_init, mock_get_r
parsed_port, used_ranges = dhcp_cfg_generator._parse_port(ipv4_port, tested_vlan_interfaces, vlan_members,
tested_ranges)
assert parsed_port == (expected_parsed_port if test_config_db == "mock_config_db.json" else {})
assert used_ranges == ({"range2", "range1", "range0", "range3"}
assert used_ranges == ({"range1", "range0", "range3"}
if test_config_db == "mock_config_db.json" else set())