@@ -847,7 +847,7 @@ def test_config_vlan_add_member_of_portchannel(self):
847847 assert "Error: Ethernet32 is part of portchannel!" in result .output
848848
849849 @pytest .mark .parametrize ("ip_version" , ["ipv4" , "ipv6" ])
850- def test_config_add_del_vlan_dhcp_relay (self , ip_version , mock_restart_dhcp_relay_service ):
850+ def test_config_add_del_vlan_dhcp_relay_with_empty_entry (self , ip_version , mock_restart_dhcp_relay_service ):
851851 runner = CliRunner ()
852852 db = Db ()
853853
@@ -861,11 +861,103 @@ def test_config_add_del_vlan_dhcp_relay(self, ip_version, mock_restart_dhcp_rela
861861 assert db .cfgdb .get_entry (IP_VERSION_PARAMS_MAP [ip_version ]["table" ], "Vlan1001" ) == exp_output
862862
863863 # del vlan 1001
864- result = runner .invoke (config .config .commands ["vlan" ].commands ["del" ], ["1001" ], obj = db )
864+ with mock .patch ("utilities_common.dhcp_relay_util.handle_restart_dhcp_relay_service" ) as mock_handle_restart :
865+ result = runner .invoke (config .config .commands ["vlan" ].commands ["del" ], ["1001" ], obj = db )
866+ print (result .exit_code )
867+ print (result .output )
868+
869+ assert result .exit_code == 0
870+ assert "Vlan1001" not in db .cfgdb .get_keys (IP_VERSION_PARAMS_MAP [ip_version ]["table" ])
871+ assert "Restart service dhcp_relay failed with error" not in result .output
872+
873+ @pytest .mark .parametrize ("ip_version" , ["ipv4" , "ipv6" ])
874+ def test_config_add_del_vlan_dhcp_relay_with_non_empty_entry (self , ip_version , mock_restart_dhcp_relay_service ):
875+ runner = CliRunner ()
876+ db = Db ()
877+
878+ # add vlan 1001
879+ result = runner .invoke (config .config .commands ["vlan" ].commands ["add" ], ["1001" ], obj = db )
880+ print (result .exit_code )
881+ print (result .output )
882+ assert result .exit_code == 0
883+
884+ exp_output = {"vlanid" : "1001" } if ip_version == "ipv4" else {}
885+ assert db .cfgdb .get_entry (IP_VERSION_PARAMS_MAP [ip_version ]["table" ], "Vlan1001" ) == exp_output
886+ db .cfgdb .set_entry ("DHCP_RELAY" , "Vlan1001" , {"dhcpv6_servers" : ["fc02:2000::5" ]})
887+
888+ # del vlan 1001
889+ with mock .patch ("utilities_common.dhcp_relay_util.handle_restart_dhcp_relay_service" ) as mock_handle_restart :
890+ result = runner .invoke (config .config .commands ["vlan" ].commands ["del" ], ["1001" ], obj = db )
891+ print (result .exit_code )
892+ print (result .output )
893+
894+ assert result .exit_code == 0
895+ assert "Vlan1001" not in db .cfgdb .get_keys (IP_VERSION_PARAMS_MAP [ip_version ]["table" ])
896+ mock_handle_restart .assert_called_once ()
897+ assert "Restart service dhcp_relay failed with error" not in result .output
898+
899+ @pytest .mark .parametrize ("ip_version" , ["ipv4" , "ipv6" ])
900+ def test_config_add_del_vlan_with_dhcp_relay_not_running (self , ip_version ):
901+ runner = CliRunner ()
902+ db = Db ()
903+
904+ # add vlan 1001
905+ result = runner .invoke (config .config .commands ["vlan" ].commands ["add" ], ["1001" ], obj = db )
865906 print (result .exit_code )
866907 print (result .output )
908+ assert result .exit_code == 0
909+
910+ exp_output = {"vlanid" : "1001" } if ip_version == "ipv4" else {}
911+ assert db .cfgdb .get_entry (IP_VERSION_PARAMS_MAP [ip_version ]["table" ], "Vlan1001" ) == exp_output
912+
913+ # del vlan 1001
914+ with mock .patch ("utilities_common.dhcp_relay_util.handle_restart_dhcp_relay_service" ) \
915+ as mock_restart_dhcp_relay_service :
916+ result = runner .invoke (config .config .commands ["vlan" ].commands ["del" ], ["1001" ], obj = db )
917+ print (result .exit_code )
918+ print (result .output )
867919
868- assert "Vlan1001" not in db .cfgdb .get_keys (IP_VERSION_PARAMS_MAP [ip_version ]["table" ])
920+ assert result .exit_code == 0
921+ assert "Vlan1001" not in db .cfgdb .get_keys (IP_VERSION_PARAMS_MAP [ip_version ]["table" ])
922+ assert mock_restart_dhcp_relay_service .call_count == 0
923+ assert "Restarting DHCP relay service..." not in result .output
924+ assert "Restart service dhcp_relay failed with error" not in result .output
925+
926+ def test_config_add_del_vlan_with_not_restart_dhcp_relay_ipv6 (self ):
927+ runner = CliRunner ()
928+ db = Db ()
929+
930+ # add vlan 1001
931+ result = runner .invoke (config .config .commands ["vlan" ].commands ["add" ], ["1001" ], obj = db )
932+ print (result .exit_code )
933+ print (result .output )
934+ assert result .exit_code == 0
935+
936+ db .cfgdb .set_entry ("DHCP_RELAY" , "Vlan1001" , {"dhcpv6_servers" : ["fc02:2000::5" ]})
937+
938+ # del vlan 1001
939+ with mock .patch ("utilities_common.dhcp_relay_util.handle_restart_dhcp_relay_service" ) \
940+ as mock_restart_dhcp_relay_service :
941+ result = runner .invoke (config .config .commands ["vlan" ].commands ["del" ], ["1001" , "--no_restart_dhcp_relay" ],
942+ obj = db )
943+ print (result .exit_code )
944+ print (result .output )
945+
946+ assert result .exit_code != 0
947+ assert mock_restart_dhcp_relay_service .call_count == 0
948+ assert "Can't delete Vlan1001 because related DHCPv6 Relay config is exist" in result .output
949+
950+ db .cfgdb .set_entry ("DHCP_RELAY" , "Vlan1001" , None )
951+ # del vlan 1001
952+ with mock .patch ("utilities_common.dhcp_relay_util.handle_restart_dhcp_relay_service" ) \
953+ as mock_restart_dhcp_relay_service :
954+ result = runner .invoke (config .config .commands ["vlan" ].commands ["del" ], ["1001" , "--no_restart_dhcp_relay" ],
955+ obj = db )
956+ print (result .exit_code )
957+ print (result .output )
958+
959+ assert result .exit_code == 0
960+ assert mock_restart_dhcp_relay_service .call_count == 0
869961
870962 @pytest .mark .parametrize ("ip_version" , ["ipv6" ])
871963 def test_config_add_exist_vlan_dhcp_relay (self , ip_version ):
0 commit comments