Skip to content

Commit

Permalink
Update ut
Browse files Browse the repository at this point in the history
  • Loading branch information
yaqiangz committed Jan 20, 2025
1 parent ced356c commit c94c6fd
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/sonic-dhcp-utilities/tests/test_data/kea-dhcp4.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@
"option60",
"option223"
],
"gateway": "192.168.3.1",
"lease_time": "900",
"mode": "PORT",
"netmask": "255.255.255.0",
Expand Down
80 changes: 65 additions & 15 deletions src/sonic-dhcp-utilities/tests/test_dhcp_cfggen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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": {
}
}
Expand All @@ -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,
Expand Down

0 comments on commit c94c6fd

Please sign in to comment.