1
+ import json
1
2
import pytest
2
3
from common_utils import MockConfigDb , dhcprelayd_refresh_dhcrelay_test , dhcprelayd_proceed_with_check_res_test
3
4
from dhcp_utilities .dhcprelayd .dhcprelayd import DHCP_SERVER_CHECKER , MID_PLANE_CHECKER
5
+ from dhcp_utilities .dhcpservd .dhcp_cfggen import DhcpServCfgGenerator
6
+ from dhcp_utilities .common .utils import DhcpDbConnector
7
+ from unittest .mock import patch
4
8
5
9
MOCK_CONFIG_DB_PATH_SMART_SWITCH = "tests/test_data/mock_config_db_smart_switch.json"
10
+ expected_kea_config = {
11
+ "Dhcp4" : {
12
+ "hooks-libraries" : [
13
+ {
14
+ "library" : "/usr/local/lib/kea/hooks/libdhcp_run_script.so" ,
15
+ "parameters" : {
16
+ "name" : "/etc/kea/lease_update.sh" ,
17
+ "sync" : False
18
+ }
19
+ }
20
+ ],
21
+ "interfaces-config" : {
22
+ "interfaces" : [
23
+ "eth0"
24
+ ]
25
+ },
26
+ "control-socket" : {
27
+ "socket-type" : "unix" ,
28
+ "socket-name" : "/run/kea/kea4-ctrl-socket"
29
+ },
30
+ "lease-database" : {
31
+ "type" : "memfile" ,
32
+ "persist" : True ,
33
+ "name" : "/tmp/kea-lease.csv" ,
34
+ "lfc-interval" : 3600
35
+ },
36
+ "subnet4" : [
37
+ {
38
+ "subnet" : "169.254.200.0/24" ,
39
+ "pools" : [
40
+ {
41
+ "pool" : "169.254.200.1 - 169.254.200.1" ,
42
+ "client-class" : "sonic-host:dpu0"
43
+ },
44
+ {
45
+ "pool" : "169.254.200.2 - 169.254.200.2" ,
46
+ "client-class" : "sonic-host:dpu1"
47
+ },
48
+ {
49
+ "pool" : "169.254.200.3 - 169.254.200.3" ,
50
+ "client-class" : "sonic-host:dpu2"
51
+ },
52
+ {
53
+ "pool" : "169.254.200.4 - 169.254.200.4" ,
54
+ "client-class" : "sonic-host:dpu3"
55
+ }
56
+ ],
57
+ "option-data" : [
58
+ {
59
+ "name" : "routers" ,
60
+ "data" : "169.254.200.254"
61
+ },
62
+ {
63
+ "name" : "dhcp-server-identifier" ,
64
+ "data" : "169.254.200.254"
65
+ }
66
+ ],
67
+ "valid-lifetime" : 900 ,
68
+ "reservations" : []
69
+ }
70
+ ],
71
+ "loggers" : [
72
+ {
73
+ "name" : "kea-dhcp4" ,
74
+ "output_options" : [
75
+ {
76
+ "output" : "/var/log/kea-dhcp.log" ,
77
+ "pattern" : "%-5p %m\n "
78
+ }
79
+ ],
80
+ "severity" : "INFO" ,
81
+ "debuglevel" : 0
82
+ }
83
+ ],
84
+ "client-classes" : [
85
+ {
86
+ "name" : "sonic-host:dpu0" ,
87
+ "test" : "substring(relay4[1].hex, -15, 15) == 'sonic-host:dpu0'"
88
+ },
89
+ {
90
+ "name" : "sonic-host:dpu1" ,
91
+ "test" : "substring(relay4[1].hex, -15, 15) == 'sonic-host:dpu1'"
92
+ },
93
+ {
94
+ "name" : "sonic-host:dpu2" ,
95
+ "test" : "substring(relay4[1].hex, -15, 15) == 'sonic-host:dpu2'"
96
+ },
97
+ {
98
+ "name" : "sonic-host:dpu3" ,
99
+ "test" : "substring(relay4[1].hex, -15, 15) == 'sonic-host:dpu3'"
100
+ }
101
+ ]
102
+ }
103
+ }
6
104
7
105
8
106
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
21
119
None , True , expected_checkers )
22
120
23
121
122
+ def test_dhcp_dhcp_cfggen_generate (mock_swsscommon_dbconnector_init , mock_parse_port_map_alias ):
123
+ with patch .object (DhcpDbConnector , "get_config_db_table" , side_effect = mock_get_config_db_table ):
124
+ dhcp_db_connector = DhcpDbConnector ()
125
+ dhcp_cfg_generator = DhcpServCfgGenerator (dhcp_db_connector ,
126
+ kea_conf_template_path = "tests/test_data/kea-dhcp4.conf.j2" )
127
+ kea_dhcp4_config , used_ranges , enabled_dhcp_interfaces , used_options , subscribe_table = \
128
+ dhcp_cfg_generator .generate ()
129
+ assert json .loads (kea_dhcp4_config ) == expected_kea_config
130
+ assert used_ranges == set ()
131
+ assert enabled_dhcp_interfaces == set (["bridge_midplane" ])
132
+ assert used_options == set ()
133
+ expected_tables = set (["DpusTableEventChecker" , "MidPlaneTableEventChecker" , "VlanTableEventChecker" ,
134
+ "VlanIntfTableEventChecker" , "DhcpRangeTableEventChecker" , "VlanMemberTableEventChecker" ,
135
+ "DhcpOptionTableEventChecker" , "DhcpPortTableEventChecker" ,
136
+ "DhcpServerTableCfgChangeEventChecker" ])
137
+ assert subscribe_table == expected_tables
138
+
139
+
24
140
def mock_get_config_db_table (table_name ):
25
141
mock_config_db = MockConfigDb (MOCK_CONFIG_DB_PATH_SMART_SWITCH )
26
142
return mock_config_db .get_config_db_table (table_name )
0 commit comments