From 92f8e7b3896339916ea5bce57cdc5f229189c0f2 Mon Sep 17 00:00:00 2001 From: Yaqiang Zhu Date: Wed, 20 Sep 2023 17:20:33 +0800 Subject: [PATCH] [vlan][dhcp_relay] Fix error while delete vlan (#2987) Why I did Use static str to run_command would encounter error because change in this PR: #2718. How I did it Change command from static str to list. How to verify it UT passed. Build whl and install in testbed. Signed-off-by: Yaqiang Zhu --- config/vlan.py | 3 ++- tests/mock_tables/config_db.json | 5 +++++ tests/vlan_test.py | 34 +++++++++++++++++++++++++++++ utilities_common/dhcp_relay_util.py | 6 ++--- 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/config/vlan.py b/config/vlan.py index fd70b027cbf..2f99f3ccfe7 100644 --- a/config/vlan.py +++ b/config/vlan.py @@ -30,7 +30,8 @@ def set_dhcp_relay_table(table, config_db, vlan_name, value): def is_dhcp_relay_running(): - out, _ = clicommon.run_command("systemctl show dhcp_relay.service --property ActiveState --value", return_cmd=True) + out, _ = clicommon.run_command(["systemctl", "show", "dhcp_relay.service", "--property", "ActiveState", "--value"], + return_cmd=True) return out.strip() == "active" def is_dhcpv6_relay_config_exist(db, vlan_name): diff --git a/tests/mock_tables/config_db.json b/tests/mock_tables/config_db.json index 07fc66db9e3..f622184e26a 100644 --- a/tests/mock_tables/config_db.json +++ b/tests/mock_tables/config_db.json @@ -2723,5 +2723,10 @@ "alias": "Fabric2", "isolateStatus": "False", "lanes": "2" + }, + "DHCP_RELAY|Vlan1000": { + "dhcpv6_servers": [ + "fc02:2000::1" + ] } } diff --git a/tests/vlan_test.py b/tests/vlan_test.py index 456bb5dd116..5212a7b0263 100644 --- a/tests/vlan_test.py +++ b/tests/vlan_test.py @@ -1377,3 +1377,37 @@ def teardown_class(cls): os.environ['UTILITIES_UNIT_TESTING'] = "0" bgp_util.run_bgp_command = cls._old_run_bgp_command print("TEARDOWN") + + def test_config_vlan_del_dhcp_relay_restart(self): + runner = CliRunner() + db = Db() + obj = {"config_db": db.cfgdb} + + # remove vlan IP`s + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Vlan1000", "192.168.0.1/21"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code != 0 + + result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["remove"], + ["Vlan1000", "fc02:1000::1/64"], obj=obj) + print(result.exit_code, result.output) + assert result.exit_code != 0 + + # remove vlan members + vlan_member = db.cfgdb.get_table("VLAN_MEMBER") + keys = [(k, v) for k, v in vlan_member if k == "Vlan{}".format(1000)] + for _, v in keys: + result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["del"], ["1000", v], obj=db) + print(result.exit_code) + print(result.output) + assert result.exit_code == 0 + + origin_run_command_func = config.vlan.clicommon.run_command + config.vlan.clicommon.run_command = mock.MagicMock(return_value=("active", 0)) + result = runner.invoke(config.config.commands["vlan"].commands["del"], ["1000"], obj=db) + print(result.exit_code) + print(result.output) + assert result.exit_code == 0 + + config.vlan.clicommon.run_command = origin_run_command_func diff --git a/utilities_common/dhcp_relay_util.py b/utilities_common/dhcp_relay_util.py index b9c0b4e20f1..4a0ab5a2e4b 100644 --- a/utilities_common/dhcp_relay_util.py +++ b/utilities_common/dhcp_relay_util.py @@ -7,9 +7,9 @@ def restart_dhcp_relay_service(): Restart dhcp_relay service """ click.echo("Restarting DHCP relay service...") - clicommon.run_command("systemctl stop dhcp_relay", display_cmd=False) - clicommon.run_command("systemctl reset-failed dhcp_relay", display_cmd=False) - clicommon.run_command("systemctl start dhcp_relay", display_cmd=False) + clicommon.run_command(["systemctl", "stop", "dhcp_relay"], display_cmd=False) + clicommon.run_command(["systemctl", "reset-failed", "dhcp_relay"], display_cmd=False) + clicommon.run_command(["systemctl", "start", "dhcp_relay"], display_cmd=False) def handle_restart_dhcp_relay_service():