@@ -597,7 +597,7 @@ def test_config_vlan_add_member_of_portchannel(self):
597597 assert "Error: Ethernet32 is part of portchannel!" in result .output
598598
599599 @pytest .mark .parametrize ("ip_version" , ["ipv4" , "ipv6" ])
600- def test_config_add_del_vlan_dhcp_relay (self , ip_version , mock_restart_dhcp_relay_service ):
600+ def test_config_add_del_vlan_dhcp_relay_with_empty_entry (self , ip_version , mock_restart_dhcp_relay_service ):
601601 runner = CliRunner ()
602602 db = Db ()
603603
@@ -611,11 +611,103 @@ def test_config_add_del_vlan_dhcp_relay(self, ip_version, mock_restart_dhcp_rela
611611 assert db .cfgdb .get_entry (IP_VERSION_PARAMS_MAP [ip_version ]["table" ], "Vlan1001" ) == exp_output
612612
613613 # del vlan 1001
614- result = runner .invoke (config .config .commands ["vlan" ].commands ["del" ], ["1001" ], obj = db )
614+ with mock .patch ("utilities_common.dhcp_relay_util.handle_restart_dhcp_relay_service" ) as mock_handle_restart :
615+ result = runner .invoke (config .config .commands ["vlan" ].commands ["del" ], ["1001" ], obj = db )
616+ print (result .exit_code )
617+ print (result .output )
618+
619+ assert result .exit_code == 0
620+ assert "Vlan1001" not in db .cfgdb .get_keys (IP_VERSION_PARAMS_MAP [ip_version ]["table" ])
621+ assert "Restart service dhcp_relay failed with error" not in result .output
622+
623+ @pytest .mark .parametrize ("ip_version" , ["ipv4" , "ipv6" ])
624+ def test_config_add_del_vlan_dhcp_relay_with_non_empty_entry (self , ip_version , mock_restart_dhcp_relay_service ):
625+ runner = CliRunner ()
626+ db = Db ()
627+
628+ # add vlan 1001
629+ result = runner .invoke (config .config .commands ["vlan" ].commands ["add" ], ["1001" ], obj = db )
630+ print (result .exit_code )
631+ print (result .output )
632+ assert result .exit_code == 0
633+
634+ exp_output = {"vlanid" : "1001" } if ip_version == "ipv4" else {}
635+ assert db .cfgdb .get_entry (IP_VERSION_PARAMS_MAP [ip_version ]["table" ], "Vlan1001" ) == exp_output
636+ db .cfgdb .set_entry ("DHCP_RELAY" , "Vlan1001" , {"dhcpv6_servers" : ["fc02:2000::5" ]})
637+
638+ # del vlan 1001
639+ with mock .patch ("utilities_common.dhcp_relay_util.handle_restart_dhcp_relay_service" ) as mock_handle_restart :
640+ result = runner .invoke (config .config .commands ["vlan" ].commands ["del" ], ["1001" ], obj = db )
641+ print (result .exit_code )
642+ print (result .output )
643+
644+ assert result .exit_code == 0
645+ assert "Vlan1001" not in db .cfgdb .get_keys (IP_VERSION_PARAMS_MAP [ip_version ]["table" ])
646+ mock_handle_restart .assert_called_once ()
647+ assert "Restart service dhcp_relay failed with error" not in result .output
648+
649+ @pytest .mark .parametrize ("ip_version" , ["ipv4" , "ipv6" ])
650+ def test_config_add_del_vlan_with_dhcp_relay_not_running (self , ip_version ):
651+ runner = CliRunner ()
652+ db = Db ()
653+
654+ # add vlan 1001
655+ result = runner .invoke (config .config .commands ["vlan" ].commands ["add" ], ["1001" ], obj = db )
615656 print (result .exit_code )
616657 print (result .output )
658+ assert result .exit_code == 0
659+
660+ exp_output = {"vlanid" : "1001" } if ip_version == "ipv4" else {}
661+ assert db .cfgdb .get_entry (IP_VERSION_PARAMS_MAP [ip_version ]["table" ], "Vlan1001" ) == exp_output
662+
663+ # del vlan 1001
664+ with mock .patch ("utilities_common.dhcp_relay_util.handle_restart_dhcp_relay_service" ) \
665+ as mock_restart_dhcp_relay_service :
666+ result = runner .invoke (config .config .commands ["vlan" ].commands ["del" ], ["1001" ], obj = db )
667+ print (result .exit_code )
668+ print (result .output )
617669
618- assert "Vlan1001" not in db .cfgdb .get_keys (IP_VERSION_PARAMS_MAP [ip_version ]["table" ])
670+ assert result .exit_code == 0
671+ assert "Vlan1001" not in db .cfgdb .get_keys (IP_VERSION_PARAMS_MAP [ip_version ]["table" ])
672+ assert mock_restart_dhcp_relay_service .call_count == 0
673+ assert "Restarting DHCP relay service..." not in result .output
674+ assert "Restart service dhcp_relay failed with error" not in result .output
675+
676+ def test_config_add_del_vlan_with_not_restart_dhcp_relay_ipv6 (self ):
677+ runner = CliRunner ()
678+ db = Db ()
679+
680+ # add vlan 1001
681+ result = runner .invoke (config .config .commands ["vlan" ].commands ["add" ], ["1001" ], obj = db )
682+ print (result .exit_code )
683+ print (result .output )
684+ assert result .exit_code == 0
685+
686+ db .cfgdb .set_entry ("DHCP_RELAY" , "Vlan1001" , {"dhcpv6_servers" : ["fc02:2000::5" ]})
687+
688+ # del vlan 1001
689+ with mock .patch ("utilities_common.dhcp_relay_util.handle_restart_dhcp_relay_service" ) \
690+ as mock_restart_dhcp_relay_service :
691+ result = runner .invoke (config .config .commands ["vlan" ].commands ["del" ], ["1001" , "--no_restart_dhcp_relay" ],
692+ obj = db )
693+ print (result .exit_code )
694+ print (result .output )
695+
696+ assert result .exit_code != 0
697+ assert mock_restart_dhcp_relay_service .call_count == 0
698+ assert "Can't delete Vlan1001 because related DHCPv6 Relay config is exist" in result .output
699+
700+ db .cfgdb .set_entry ("DHCP_RELAY" , "Vlan1001" , None )
701+ # del vlan 1001
702+ with mock .patch ("utilities_common.dhcp_relay_util.handle_restart_dhcp_relay_service" ) \
703+ as mock_restart_dhcp_relay_service :
704+ result = runner .invoke (config .config .commands ["vlan" ].commands ["del" ], ["1001" , "--no_restart_dhcp_relay" ],
705+ obj = db )
706+ print (result .exit_code )
707+ print (result .output )
708+
709+ assert result .exit_code == 0
710+ assert mock_restart_dhcp_relay_service .call_count == 0
619711
620712 @pytest .mark .parametrize ("ip_version" , ["ipv6" ])
621713 def test_config_add_exist_vlan_dhcp_relay (self , ip_version ):
0 commit comments