From c94c6fd4a454898f9421ac5d044a57581f4d5d91 Mon Sep 17 00:00:00 2001 From: yaqiangz Date: Mon, 20 Jan 2025 00:06:57 -0500 Subject: [PATCH] Update ut --- .../tests/test_data/kea-dhcp4.conf.j2 | 4 +- .../tests/test_data/mock_config_db.json | 1 - .../tests/test_dhcp_cfggen.py | 80 +++++++++++++++---- 3 files changed, 67 insertions(+), 18 deletions(-) diff --git a/src/sonic-dhcp-utilities/tests/test_data/kea-dhcp4.conf.j2 b/src/sonic-dhcp-utilities/tests/test_data/kea-dhcp4.conf.j2 index 2c013faba4919..9c98dc01d496f 100644 --- a/src/sonic-dhcp-utilities/tests/test_data/kea-dhcp4.conf.j2 +++ b/src/sonic-dhcp-utilities/tests/test_data/kea-dhcp4.conf.j2 @@ -63,12 +63,12 @@ "always-send": {{ config["always_send"] }} }, {%- endfor %} - {%- if "gateway" in subnet_info -%} + {%- if "gateway" in subnet_info %} { "name": "routers", "data": "{{ subnet_info["gateway"] }}" }, - {%- endif -%} + {%- endif %} { "name": "dhcp-server-identifier", "data": "{{ subnet_info["server_id"] }}" diff --git a/src/sonic-dhcp-utilities/tests/test_data/mock_config_db.json b/src/sonic-dhcp-utilities/tests/test_data/mock_config_db.json index ca7974673a34d..b2025d9a2a065 100644 --- a/src/sonic-dhcp-utilities/tests/test_data/mock_config_db.json +++ b/src/sonic-dhcp-utilities/tests/test_data/mock_config_db.json @@ -83,7 +83,6 @@ "option60", "option223" ], - "gateway": "192.168.3.1", "lease_time": "900", "mode": "PORT", "netmask": "255.255.255.0", diff --git a/src/sonic-dhcp-utilities/tests/test_dhcp_cfggen.py b/src/sonic-dhcp-utilities/tests/test_dhcp_cfggen.py index 4e8b0504b01cf..5ce21ce698dc9 100644 --- a/src/sonic-dhcp-utilities/tests/test_dhcp_cfggen.py +++ b/src/sonic-dhcp-utilities/tests/test_dhcp_cfggen.py @@ -70,6 +70,24 @@ ], "valid-lifetime": 900, "reservations": [] + }, + { + "id": 4000, + "subnet": "192.168.3.0/24", + "pools": [ + { + "pool": "192.168.3.2 - 192.168.3.3", + "client-class": "sonic-host:etp11" + } + ], + "option-data": [ + { + "name": "dhcp-server-identifier", + "data": "192.168.3.1" + } + ], + "valid-lifetime": 900, + "reservations": [] } ], "loggers": [ @@ -93,12 +111,23 @@ { "name": "sonic-host:etp7", "test": "substring(relay4[1].hex, -15, 15) == 'sonic-host:etp7'" + }, + { + "name": "sonic-host:etp11", + "test": "substring(relay4[1].hex, -16, 16) == 'sonic-host:etp11'" } ] } } expected_dhcp_config_without_port_config = { "Dhcp4": { + "option-def": [ + { + "name": "option223", + "code": 223, + "type": "string" + } + ], "hooks-libraries": [ { "library": "/usr/local/lib/kea/hooks/libdhcp_run_script.so", @@ -194,11 +223,23 @@ "value": "dummy_value" } } + }, + { + "subnet": "192.168.3.0/24", 'id': '4000', + "pools": [{"range": "192.168.3.2 - 192.168.3.3", "client_class": "sonic-host:etp11"}], + "server_id": "192.168.3.1", "lease_time": "900", + "customized_options": { + "option223": { + "always_send": "true", + "value": "dummy_value" + } + } } ], "client_classes": [ {"name": "sonic-host:etp8", "condition": "substring(relay4[1].hex, -15, 15) == 'sonic-host:etp8'"}, - {"name": "sonic-host:etp7", "condition": "substring(relay4[1].hex, -15, 15) == 'sonic-host:etp7'"} + {"name": "sonic-host:etp7", "condition": "substring(relay4[1].hex, -15, 15) == 'sonic-host:etp7'"}, + {"name": "sonic-host:etp11", "condition": "substring(relay4[1].hex, -16, 16) == 'sonic-host:etp11'"} ], "lease_update_script_path": "/etc/kea/lease_update.sh", "lease_path": "/tmp/kea-lease.csv", @@ -238,9 +279,9 @@ "id": "218", "type": "string", "value": "long_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_value" + - "long_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_value" + - "long_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_value" + - "long_valuelong_valuelong_valuelong_valuelong_value" + "long_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_value" + + "long_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_valuelong_value" + + "long_valuelong_valuelong_valuelong_valuelong_value" }, "option217": { "id": "217", @@ -359,6 +400,11 @@ def test_construct_obj_for_template(mock_swsscommon_dbconnector_init, mock_parse "etp9": [] } }, + "Vlan4000": { + "192.168.3.1/24": { + "etp11": [["192.168.3.2", "192.168.3.3"]] + } + }, "Vlan6000": { } } @@ -374,25 +420,29 @@ def test_construct_obj_for_template(mock_swsscommon_dbconnector_init, mock_parse @pytest.mark.parametrize("with_port_config", [True, False]) @pytest.mark.parametrize("with_option_config", [True, False]) def test_render_config(mock_swsscommon_dbconnector_init, mock_parse_port_map_alias, with_port_config, - with_option_config): + with_option_config, request): dhcp_db_connector = DhcpDbConnector() dhcp_cfg_generator = DhcpServCfgGenerator(dhcp_db_connector, "/usr/local/lib/kea/hooks/libdhcp_run_script.so", kea_conf_template_path="tests/test_data/kea-dhcp4.conf.j2") render_obj = copy.deepcopy(expected_render_obj) - expected_config = copy.deepcopy(expected_dhcp_config) if not with_port_config: render_obj["client_classes"] = [] render_obj["subnets"] = [] - elif not with_option_config: - render_obj["subnets"][0]["customized_options"] = {} + expected_config = copy.deepcopy(expected_dhcp_config_without_port_config) + else: + expected_config = copy.deepcopy(expected_dhcp_config) + if not with_option_config: + for i in range(len(expected_config["Dhcp4"]["subnet4"])): + render_obj["subnets"][i]["customized_options"] = {} + else: + for i in range(len(expected_config["Dhcp4"]["subnet4"])): + expected_config["Dhcp4"]["subnet4"][i]["option-data"].insert(0, { + "name": "option223", + "data": "dummy_value", + "always-send": True + }) config = dhcp_cfg_generator._render_config(render_obj) - if with_option_config: - expected_config["Dhcp4"]["subnet4"][0]["option-data"].insert(0, { - "name": "option223", - "data": "dummy_value", - "always-send": True - }) - assert json.loads(config) == expected_config if with_port_config else expected_config + assert json.loads(config) == expected_config def test_parse_customized_options(mock_swsscommon_dbconnector_init, mock_get_render_template,