1111 Added DHCP relay address [{}] to Vlan1000
1212Restarting DHCP relay service...
1313"""
14+ config_dhcp_relay_update_output = """\
15+ Updated DHCPv4 Servers as {} to Vlan1000
16+ """
1417config_dhcp_relay_del_output = """\
1518 Removed DHCP relay address [{}] from Vlan1000
1619Restarting DHCP relay service...
104107}
105108
106109
107- @pytest .fixture (scope = "module" , params = ["ipv4" , "ipv6" ])
108- def ip_version (request ):
110+ @pytest .fixture (scope = "module" , params = ["ipv4" , "ipv4_dhcp" , " ipv6" ])
111+ def version (request ):
109112 """
110113 Parametrize Ip version
111114
@@ -166,111 +169,131 @@ def test_config_dhcp_relay_del_with_nonexist_vlanid_ipv6(self):
166169 assert "Error: Vlan1001 doesn't exist" in result .output
167170 assert mock_run_command .call_count == 0
168171
169- def test_config_add_del_dhcp_relay_with_invalid_ip (self , ip_version , op ):
172+ def test_config_add_del_dhcp_relay_with_invalid_ip (self , mock_cfgdb , version , op ):
170173 runner = CliRunner ()
171- invalid_ip = IP_VER_TEST_PARAM_MAP [ip_version ]["invalid_ip" ]
172-
174+ db = Db ()
175+ db .cfgdb = mock_cfgdb
176+ invalid_ip = IP_VER_TEST_PARAM_MAP [version ]["invalid_ip" ]
177+ ip_version = "ipv6" if version == "ipv6" else "ipv4"
178+ if version == "ipv4_dhcp" :
179+ db .cfgdb .set_entry ('FEATURE' , 'dhcp_relay' , {'has_sonic_dhcpv4_relay' : 'True' })
173180 with mock .patch ("utilities_common.cli.run_command" ) as mock_run_command :
174181 result = runner .invoke (dhcp_relay .dhcp_relay .commands [ip_version ]
175- .commands [IP_VER_TEST_PARAM_MAP [ip_version ]["command" ]]
182+ .commands [IP_VER_TEST_PARAM_MAP [version ]["command" ]]
176183 .commands [op ], ["1000" , invalid_ip ])
177184 print (result .exit_code )
178185 print (result .output )
179186 assert result .exit_code != 0
180187 assert "Error: {} is invalid IP address" .format (invalid_ip ) in result .output
181188 assert mock_run_command .call_count == 0
182189
183- def test_config_add_dhcp_with_exist_ip (self , mock_cfgdb , ip_version ):
190+ def test_config_add_dhcp_with_exist_ip (self , mock_cfgdb , version ):
184191 runner = CliRunner ()
185192 db = Db ()
186193 db .cfgdb = mock_cfgdb
187- exist_ip = IP_VER_TEST_PARAM_MAP [ip_version ]["exist_ip" ]
188-
194+ exist_ip = IP_VER_TEST_PARAM_MAP [version ]["exist_ip" ]
195+ ip_version = "ipv6" if version == "ipv6" else "ipv4"
196+ if version == "ipv4_dhcp" :
197+ db .cfgdb .set_entry ('FEATURE' , 'dhcp_relay' , {'has_sonic_dhcpv4_relay' : 'True' })
198+ table = db .cfgdb .get_entry ("FEATURE" , "dhcp_relay" )
199+ if ('has_sonic_dhcpv4_relay' in table and table ['has_sonic_dhcpv4_relay' ] == 'True' ):
200+ print ("has_sonic_dhcpv4_relay is set" )
189201 with mock .patch ("utilities_common.cli.run_command" ) as mock_run_command :
190202 result = runner .invoke (dhcp_relay .dhcp_relay .commands [ip_version ]
191- .commands [IP_VER_TEST_PARAM_MAP [ip_version ]["command" ]]
203+ .commands [IP_VER_TEST_PARAM_MAP [version ]["command" ]]
192204 .commands ["add" ], ["1000" , exist_ip ], obj = db )
193205 print (result .exit_code )
194206 print (result .output )
195207 assert result .exit_code != 0
196- if ip_version == "ipv4 " :
197- assert "{} is already a DHCPv4 relay for Vlan1000" . format ( exist_ip ) in result .output
208+ if version == "ipv4_dhcp " :
209+ assert "DHCPv4 relay entry for Vlan1000 already exists. Use 'update' instead." in result .output
198210 else :
199211 assert "{} is already a DHCP relay for Vlan1000" .format (exist_ip ) in result .output
200212 assert mock_run_command .call_count == 0
201213 db .cfgdb .set_entry .reset_mock ()
202214
203- def test_config_del_nonexist_dhcp_relay (self , mock_cfgdb , ip_version ):
215+ def test_config_del_nonexist_dhcp_relay (self , mock_cfgdb , version ):
204216 runner = CliRunner ()
205217 db = Db ()
206218 db .cfgdb = mock_cfgdb
207- nonexist_ip = IP_VER_TEST_PARAM_MAP [ip_version ]["nonexist_ip" ]
219+ nonexist_ip = IP_VER_TEST_PARAM_MAP [version ]["nonexist_ip" ]
220+ ip_version = "ipv6" if version == "ipv6" else "ipv4"
221+ if version == "ipv4_dhcp" :
222+ db .cfgdb .set_entry ('FEATURE' , 'dhcp_relay' , {'has_sonic_dhcpv4_relay' : 'True' })
208223
209224 with mock .patch ("utilities_common.cli.run_command" ) as mock_run_command :
210225 result = runner .invoke (dhcp_relay .dhcp_relay .commands [ip_version ]
211- .commands [IP_VER_TEST_PARAM_MAP [ip_version ]["command" ]]
226+ .commands [IP_VER_TEST_PARAM_MAP [version ]["command" ]]
212227 .commands ["del" ], ["1000" , nonexist_ip ], obj = db )
213228 print (result .exit_code )
214229 print (result .output )
215230 assert result .exit_code != 0
216- if ip_version == "ipv4 " :
231+ if version == "ipv4_dhcp " :
217232 assert "Error: {} is not a DHCPv4 relay for Vlan1000" .format (nonexist_ip ) in result .output
218233 else :
219234 assert "Error: {} is not a DHCP relay for Vlan1000" .format (nonexist_ip ) in result .output
220235
221236 assert mock_run_command .call_count == 0
222237 db .cfgdb .set_entry .reset_mock ()
223238
224- def test_config_add_del_dhcp_relay (self , mock_cfgdb , ip_version ):
239+ def test_config_add_del_dhcp_relay (self , mock_cfgdb , version ):
225240 runner = CliRunner ()
226241 db = Db ()
227242 db .cfgdb = mock_cfgdb
228- test_ip = IP_VER_TEST_PARAM_MAP [ip_version ]["ips" ][0 ]
229- config_db_table = IP_VER_TEST_PARAM_MAP [ip_version ]["table" ]
243+ test_ip = IP_VER_TEST_PARAM_MAP [version ]["ips" ][0 ]
244+ ip_version = "ipv6" if version == "ipv6" else "ipv4"
245+ config_db_table = IP_VER_TEST_PARAM_MAP [version ]["table" ]
230246
231- if ip_version == "ipv4" :
247+ if version == "ipv4_dhcp" :
248+ db .cfgdb .set_entry ('FEATURE' , 'dhcp_relay' , {'has_sonic_dhcpv4_relay' : 'True' })
232249 dhcp4_config_db_table = IP_VER_TEST_PARAM_MAP ["ipv4_dhcp" ]["table" ]
250+ op = "update"
251+ else :
252+ op = "add"
233253
234254 with mock .patch ("utilities_common.cli.run_command" ) as mock_run_command :
235255 # add new dhcp relay
236256 result = runner .invoke (dhcp_relay .dhcp_relay .commands [ip_version ]
237- .commands [IP_VER_TEST_PARAM_MAP [ip_version ]["command" ]]
238- .commands ["add" ], ["1000" , test_ip ], obj = db )
257+ .commands [IP_VER_TEST_PARAM_MAP [version ]["command" ]]
258+ .commands [op ], ["1000" , test_ip ], obj = db )
239259 print (result .exit_code )
240260 print (result .output )
241261 assert result .exit_code == 0
242- assert result . output == config_dhcp_relay_add_output . format ( test_ip )
243- if ip_version != "ipv4" :
262+ if version != "ipv4_dhcp" :
263+ assert result . output == config_dhcp_relay_add_output . format ( test_ip )
244264 assert db .cfgdb .get_entry (config_db_table , "Vlan1000" ) \
245- == expected_dhcp_relay_add_config_db_output [ip_version ]
246- assert mock_run_command .call_count == 3
265+ == expected_dhcp_relay_add_config_db_output [version ]
266+ assert mock_run_command .call_count == 3
247267
248- if ip_version == "ipv4" :
268+ if version == "ipv4_dhcp" :
269+ assert result .output == config_dhcp_relay_update_output .format (test_ip )
249270 expected_calls = [
271+ mock .call ('FEATURE' , 'dhcp_relay' , {'has_sonic_dhcpv4_relay' : 'True' }),
250272 mock .call (dhcp4_config_db_table , "Vlan1000" , expected_dhcp_relay_add_config_db_output ["ipv4_dhcp" ])
251273 ]
252274
253275 assert db .cfgdb .set_entry .call_args_list == expected_calls
254276 else :
255277 db .cfgdb .set_entry .assert_called_once_with (config_db_table , "Vlan1000" ,
256- expected_dhcp_relay_add_config_db_output [ip_version ])
278+ expected_dhcp_relay_add_config_db_output [version ])
257279
258280 db .cfgdb .set_entry .reset_mock ()
259281 # del dhcp relay
260282 with mock .patch ("utilities_common.cli.run_command" ) as mock_run_command :
261283 result = runner .invoke (dhcp_relay .dhcp_relay .commands [ip_version ]
262- .commands [IP_VER_TEST_PARAM_MAP [ip_version ]["command" ]]
284+ .commands [IP_VER_TEST_PARAM_MAP [version ]["command" ]]
263285 .commands ["del" ], ["1000" , test_ip ], obj = db )
264286 print (result .exit_code )
265287 print (result .output )
266288 assert result .exit_code == 0
267- assert result .output == config_dhcp_relay_del_output .format (test_ip )
268- assert mock_run_command .call_count == 3
269- if ip_version != "ipv4" :
289+ if version != "ipv4_dhcp" :
290+ assert result .output == config_dhcp_relay_del_output .format (test_ip )
270291 assert db .cfgdb .get_entry (config_db_table , "Vlan1000" ) \
271- == expected_dhcp_relay_del_config_db_output [ip_version ]
292+ == expected_dhcp_relay_del_config_db_output [version ]
293+ assert mock_run_command .call_count == 3
272294
273- if ip_version == "ipv4" :
295+ if version == "ipv4_dhcp" :
296+ assert "Removed DHCP relay address [{}] from Vlan1000" .format (test_ip ) in result .output
274297 expected_calls = [
275298 mock .call (dhcp4_config_db_table , "Vlan1000" , expected_dhcp_relay_del_config_db_output ["ipv4_dhcp" ])
276299 ]
@@ -279,7 +302,7 @@ def test_config_add_del_dhcp_relay(self, mock_cfgdb, ip_version):
279302
280303 else :
281304 db .cfgdb .set_entry .assert_called_once_with (config_db_table , "Vlan1000" ,
282- expected_dhcp_relay_del_config_db_output [ip_version ])
305+ expected_dhcp_relay_del_config_db_output [version ])
283306
284307 def test_config_add_del_dhcp_relay_with_enable_dhcp_server (self , mock_cfgdb ):
285308 runner = CliRunner ()
@@ -311,81 +334,96 @@ def test_config_add_del_dhcp_relay_with_enable_dhcp_server(self, mock_cfgdb):
311334 assert result .exit_code == 0
312335 assert "Cannot change ipv4 dhcp_relay configuration when dhcp_server feature is enabled" in result .output
313336
314- def test_config_add_del_multiple_dhcp_relay (self , mock_cfgdb , ip_version ):
337+ def test_config_add_del_multiple_dhcp_relay (self , mock_cfgdb , version ):
315338 runner = CliRunner ()
316339 db = Db ()
317340 db .cfgdb = mock_cfgdb
318- test_ips = IP_VER_TEST_PARAM_MAP [ip_version ]["ips" ]
319- config_db_table = IP_VER_TEST_PARAM_MAP [ip_version ]["table" ]
341+ test_ips = IP_VER_TEST_PARAM_MAP [version ]["ips" ]
342+ config_db_table = IP_VER_TEST_PARAM_MAP [version ]["table" ]
343+ ip_version = "ipv6" if version == "ipv6" else "ipv4"
320344
321- if ip_version == "ipv4" :
345+ if version == "ipv4_dhcp" :
346+ db .cfgdb .set_entry ('FEATURE' , 'dhcp_relay' , {'has_sonic_dhcpv4_relay' : 'True' })
322347 dhcp4_config_db_table = IP_VER_TEST_PARAM_MAP ["ipv4_dhcp" ]["table" ]
348+ op = "update"
349+ else :
350+ op = "add"
323351
324352 with mock .patch ("utilities_common.cli.run_command" ) as mock_run_command :
325353 # add new dhcp relay
326354 result = runner .invoke (dhcp_relay .dhcp_relay .commands [ip_version ]
327- .commands [IP_VER_TEST_PARAM_MAP [ip_version ]["command" ]]
328- .commands ["add" ], ["1000" ] + test_ips , obj = db )
355+ .commands [IP_VER_TEST_PARAM_MAP [version ]["command" ]]
356+ .commands [op ], ["1000" ] + test_ips , obj = db )
329357 print (result .exit_code )
330358 print (result .output )
331359 assert result .exit_code == 0
332- assert result . output == config_dhcp_relay_add_output . format ( "," . join ( test_ips ))
333- if ip_version != "ipv4" :
360+ if version != "ipv4_dhcp" :
361+ assert result . output == config_dhcp_relay_add_output . format ( "," . join ( test_ips ))
334362 assert db .cfgdb .get_entry (config_db_table , "Vlan1000" ) \
335- == expected_dhcp_relay_add_multi_config_db_output [ip_version ]
336- assert mock_run_command .call_count == 3
363+ == expected_dhcp_relay_add_multi_config_db_output [version ]
364+ assert mock_run_command .call_count == 3
337365
338- if ip_version == "ipv4" :
366+ if version == "ipv4_dhcp" :
367+ assert result .output == config_dhcp_relay_update_output .format ("," .join (test_ips ))
339368 expected_calls = [
369+ mock .call ('FEATURE' , 'dhcp_relay' , {'has_sonic_dhcpv4_relay' : 'True' }),
340370 mock .call (dhcp4_config_db_table , "Vlan1000" , expected_dhcp_relay_add_multi_config_db_output ["ipv4_dhcp" ]),
341371 ]
342372
343373 assert db .cfgdb .set_entry .call_args_list == expected_calls
344374 else :
345375 db .cfgdb .set_entry .assert_called_once_with (config_db_table , "Vlan1000" ,
346- expected_dhcp_relay_add_multi_config_db_output [ip_version ])
376+ expected_dhcp_relay_add_multi_config_db_output [version ])
347377
348378 db .cfgdb .set_entry .reset_mock ()
349379 # del dhcp relay
350380 with mock .patch ("utilities_common.cli.run_command" ) as mock_run_command :
351381 result = runner .invoke (dhcp_relay .dhcp_relay .commands [ip_version ]
352- .commands [IP_VER_TEST_PARAM_MAP [ip_version ]["command" ]]
382+ .commands [IP_VER_TEST_PARAM_MAP [version ]["command" ]]
353383 .commands ["del" ], ["1000" ] + test_ips , obj = db )
354384 print (result .exit_code )
355385 print (result .output )
356386 assert result .exit_code == 0
357- assert result . output == config_dhcp_relay_del_output . format ( "," . join ( test_ips ))
358- assert mock_run_command . call_count == 3
359- if ip_version != "ipv4" :
387+ if version != "ipv4_dhcp" :
388+ assert result . output == config_dhcp_relay_del_output . format ( "," . join ( test_ips ))
389+ assert mock_run_command . call_count == 3
360390 assert db .cfgdb .get_entry (config_db_table , "Vlan1000" ) \
361- == expected_dhcp_relay_del_config_db_output [ip_version ]
362- if ip_version == "ipv4" :
391+ == expected_dhcp_relay_del_config_db_output [version ]
392+ if ip_version == "ipv4_dhcp" :
393+ assert "Removed DHCP relay address [{}] from Vlan1000" .format ("," .join (test_ip )) in result .output
363394 expected_calls = [
364395 mock .call (dhcp4_config_db_table , "Vlan1000" , expected_dhcp_relay_del_config_db_output ["ipv4_dhcp" ]),
365396 ]
366397
367398 assert db .cfgdb .set_entry .call_args_list == expected_calls
368399 else :
369400 db .cfgdb .set_entry .assert_called_once_with (config_db_table , "Vlan1000" ,
370- expected_dhcp_relay_del_config_db_output [ip_version ])
401+ expected_dhcp_relay_del_config_db_output [version ])
371402
372403 db .cfgdb .set_entry .reset_mock ()
373404
374- def test_config_add_del_duplicate_dhcp_relay (self , mock_cfgdb , ip_version , op ):
405+ def test_config_add_del_duplicate_dhcp_relay (self , mock_cfgdb , version , op ):
375406 runner = CliRunner ()
376407 db = Db ()
377408 db .cfgdb = mock_cfgdb
378- test_ip = IP_VER_TEST_PARAM_MAP [ip_version ]["ips" ][0 ] if op == "add" \
379- else IP_VER_TEST_PARAM_MAP [ip_version ]["exist_ip" ]
409+ test_ip = IP_VER_TEST_PARAM_MAP [version ]["ips" ][0 ] if op == "add" \
410+ else IP_VER_TEST_PARAM_MAP [version ]["exist_ip" ]
411+ ip_version = "ipv6" if version == "ipv6" else "ipv4"
412+
413+ if version == "ipv4_dhcp" :
414+ db .cfgdb .set_entry ('FEATURE' , 'dhcp_relay' , {'has_sonic_dhcpv4_relay' : 'True' })
380415
381416 with mock .patch ("utilities_common.cli.run_command" ) as mock_run_command :
382417 result = runner .invoke (dhcp_relay .dhcp_relay .commands [ip_version ]
383- .commands [IP_VER_TEST_PARAM_MAP [ip_version ]["command" ]]
418+ .commands [IP_VER_TEST_PARAM_MAP [version ]["command" ]]
384419 .commands [op ], ["1000" , test_ip , test_ip ], obj = db )
385420 print (result .exit_code )
386421 print (result .output )
387422 assert result .exit_code != 0
388- assert "Error: Find duplicate DHCP relay ip {} in {} list" .format (test_ip , op ) in result .output
423+ if version == "ipv4_dhcp" and op == "add" :
424+ assert "Error: DHCPv4 relay entry for Vlan1000 already exists. Use 'update' instead." in result .output
425+ else :
426+ assert "Error: Find duplicate DHCP relay ip {} in {} list" .format (test_ip , op ) in result .output
389427 assert mock_run_command .call_count == 0
390428
391429 def test_config_add_dhcp_relay_ipv6_with_non_entry (self , mock_cfgdb ):
0 commit comments