[dhcp_server] Fix parse_dpus error (#17870)

This commit is contained in:
Yaqiang Zhu 2024-01-23 07:20:20 +08:00 committed by GitHub
parent 927dde73f1
commit ec31420329
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 157 additions and 11 deletions

View File

@ -70,7 +70,7 @@ class DhcpServCfgGenerator(object):
# Parse dpu
dpus_table = self.db_connector.get_config_db_table(DPUS)
mid_plane_table = self.db_connector.get_config_db_table(MID_PLANE_BRIDGE)
mid_plane, dpus = self._parse_dpu(dpus_table, mid_plane_table) if smart_switch else {}, {}
mid_plane, dpus = self._parse_dpu(dpus_table, mid_plane_table) if smart_switch else ({}, {})
dhcp_server_ipv4, customized_options_ipv4, range_ipv4, port_ipv4 = self._get_dhcp_ipv4_tables_from_db()
# Parse range table

View File

@ -8,7 +8,7 @@
"MID_PLANE_BRIDGE": {
"GLOBAL": {
"bridge": "bridge_midplane",
"address": "169.254.200.254/24"
"ip_prefix": "169.254.200.254/24"
}
},
"DHCP_SERVER_IPV4": {
@ -23,5 +23,41 @@
"netmask": "255.255.255.0",
"state": "enabled"
}
},
"DHCP_SERVER_IPV4_PORT": {
"bridge_midplane|dpu0": {
"ips": [
"169.254.200.1"
]
},
"bridge_midplane|dpu1": {
"ips": [
"169.254.200.2"
]
},
"bridge_midplane|dpu2": {
"ips": [
"169.254.200.3"
]
},
"bridge_midplane|dpu3": {
"ips": [
"169.254.200.4"
]
}
},
"DPUS": {
"dpu0": {
"midplane_interface": "dpu0"
},
"dpu1": {
"midplane_interface": "dpu1"
},
"dpu2": {
"midplane_interface": "dpu2"
},
"dpu3": {
"midplane_interface": "dpu3"
}
}
}

View File

@ -324,22 +324,19 @@ def test_parse_port(test_config_db, mock_swsscommon_dbconnector_init, mock_get_r
if test_config_db == "mock_config_db.json" else set())
@pytest.mark.parametrize("mid_plane", [{}, {"bridge": "mid_plane", "ip_prefix": "192.168.0.1/24"}])
@pytest.mark.parametrize("is_smart_switch", [True, False])
def test_generate(mock_swsscommon_dbconnector_init, mock_parse_port_map_alias, mock_get_render_template, mid_plane,
is_smart_switch):
def test_generate(mock_swsscommon_dbconnector_init, mock_parse_port_map_alias, mock_get_render_template):
with patch.object(DhcpServCfgGenerator, "_parse_hostname"), \
patch.object(DhcpServCfgGenerator, "_parse_vlan", return_value=({}, set(["Ethernet0"]))), \
patch.object(DhcpServCfgGenerator, "_get_dhcp_ipv4_tables_from_db", return_value=(None, None, None, None)), \
patch.object(DhcpServCfgGenerator, "_parse_range"), \
patch.object(DhcpServCfgGenerator, "_parse_port", return_value=(None, set(["range1"]))), \
patch.object(DhcpServCfgGenerator, "_parse_customized_options"), \
patch.object(DhcpServCfgGenerator, "_parse_dpu", side_effect=[mid_plane, set()]), \
patch.object(DhcpServCfgGenerator, "_parse_dpu", return_value=(set(), set())), \
patch.object(DhcpServCfgGenerator, "_construct_obj_for_template",
return_value=(None, set(["Vlan1000"]), set(["option1"]), set(["dummy"]))), \
patch.object(DhcpServCfgGenerator, "_render_config", return_value="dummy_config"), \
patch.object(DhcpDbConnector, "get_config_db_table", side_effect=mock_get_config_db_table), \
patch("dhcp_utilities.dhcpservd.dhcp_cfggen.is_smart_switch", return_value=is_smart_switch):
patch("dhcp_utilities.dhcpservd.dhcp_cfggen.is_smart_switch", return_value=False):
dhcp_db_connector = DhcpDbConnector()
dhcp_cfg_generator = DhcpServCfgGenerator(dhcp_db_connector)
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
assert enabled_dhcp_interfaces == set(["Vlan1000"])
assert used_options == set(["option1"])
expected_tables = set(["dummy"])
if is_smart_switch:
expected_tables |= set(["DpusTableEventChecker", "MidPlaneTableEventChecker"])
assert subscribe_table == expected_tables

View File

@ -1,8 +1,106 @@
import json
import pytest
from common_utils import MockConfigDb, dhcprelayd_refresh_dhcrelay_test, dhcprelayd_proceed_with_check_res_test
from dhcp_utilities.dhcprelayd.dhcprelayd import DHCP_SERVER_CHECKER, MID_PLANE_CHECKER
from dhcp_utilities.dhcpservd.dhcp_cfggen import DhcpServCfgGenerator
from dhcp_utilities.common.utils import DhcpDbConnector
from unittest.mock import patch
MOCK_CONFIG_DB_PATH_SMART_SWITCH = "tests/test_data/mock_config_db_smart_switch.json"
expected_kea_config = {
"Dhcp4": {
"hooks-libraries": [
{
"library": "/usr/local/lib/kea/hooks/libdhcp_run_script.so",
"parameters": {
"name": "/etc/kea/lease_update.sh",
"sync": False
}
}
],
"interfaces-config": {
"interfaces": [
"eth0"
]
},
"control-socket": {
"socket-type": "unix",
"socket-name": "/run/kea/kea4-ctrl-socket"
},
"lease-database": {
"type": "memfile",
"persist": True,
"name": "/tmp/kea-lease.csv",
"lfc-interval": 3600
},
"subnet4": [
{
"subnet": "169.254.200.0/24",
"pools": [
{
"pool": "169.254.200.1 - 169.254.200.1",
"client-class": "sonic-host:dpu0"
},
{
"pool": "169.254.200.2 - 169.254.200.2",
"client-class": "sonic-host:dpu1"
},
{
"pool": "169.254.200.3 - 169.254.200.3",
"client-class": "sonic-host:dpu2"
},
{
"pool": "169.254.200.4 - 169.254.200.4",
"client-class": "sonic-host:dpu3"
}
],
"option-data": [
{
"name": "routers",
"data": "169.254.200.254"
},
{
"name": "dhcp-server-identifier",
"data": "169.254.200.254"
}
],
"valid-lifetime": 900,
"reservations": []
}
],
"loggers": [
{
"name": "kea-dhcp4",
"output_options": [
{
"output": "/var/log/kea-dhcp.log",
"pattern": "%-5p %m\n"
}
],
"severity": "INFO",
"debuglevel": 0
}
],
"client-classes": [
{
"name": "sonic-host:dpu0",
"test": "substring(relay4[1].hex, -15, 15) == 'sonic-host:dpu0'"
},
{
"name": "sonic-host:dpu1",
"test": "substring(relay4[1].hex, -15, 15) == 'sonic-host:dpu1'"
},
{
"name": "sonic-host:dpu2",
"test": "substring(relay4[1].hex, -15, 15) == 'sonic-host:dpu2'"
},
{
"name": "sonic-host:dpu3",
"test": "substring(relay4[1].hex, -15, 15) == 'sonic-host:dpu3'"
}
]
}
}
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
None, True, expected_checkers)
def test_dhcp_dhcp_cfggen_generate(mock_swsscommon_dbconnector_init, mock_parse_port_map_alias):
with patch.object(DhcpDbConnector, "get_config_db_table", side_effect=mock_get_config_db_table):
dhcp_db_connector = DhcpDbConnector()
dhcp_cfg_generator = DhcpServCfgGenerator(dhcp_db_connector,
kea_conf_template_path="tests/test_data/kea-dhcp4.conf.j2")
kea_dhcp4_config, used_ranges, enabled_dhcp_interfaces, used_options, subscribe_table = \
dhcp_cfg_generator.generate()
assert json.loads(kea_dhcp4_config) == expected_kea_config
assert used_ranges == set()
assert enabled_dhcp_interfaces == set(["bridge_midplane"])
assert used_options == set()
expected_tables = set(["DpusTableEventChecker", "MidPlaneTableEventChecker", "VlanTableEventChecker",
"VlanIntfTableEventChecker", "DhcpRangeTableEventChecker", "VlanMemberTableEventChecker",
"DhcpOptionTableEventChecker", "DhcpPortTableEventChecker",
"DhcpServerTableCfgChangeEventChecker"])
assert subscribe_table == expected_tables
def mock_get_config_db_table(table_name):
mock_config_db = MockConfigDb(MOCK_CONFIG_DB_PATH_SMART_SWITCH)
return mock_config_db.get_config_db_table(table_name)