From 08666100fcde33bcb8025df8ae411b877ef78243 Mon Sep 17 00:00:00 2001 From: Xichen96 Date: Sat, 23 Dec 2023 01:07:24 +0800 Subject: [PATCH] [dhcp_server] add config dhcp server del (#17603) * add config dhcp server del --- .../cli-plugin-tests/conftest.py | 9 +++++++++ .../cli-plugin-tests/test_config_dhcp_server.py | 15 +++++++++++++++ .../cli/config/plugins/dhcp_server.py | 14 ++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/dockers/docker-dhcp-server/cli-plugin-tests/conftest.py b/dockers/docker-dhcp-server/cli-plugin-tests/conftest.py index 4dc078f769..560e25667a 100644 --- a/dockers/docker-dhcp-server/cli-plugin-tests/conftest.py +++ b/dockers/docker-dhcp-server/cli-plugin-tests/conftest.py @@ -62,10 +62,19 @@ def mock_db(): if table == "STATE_DB": return key in mock_state_db + def delete(table, key): + assert table == "CONFIG_DB" or table == "STATE_DB" + if table == "CONFIG_DB": + del mock_config_db[key] + if table == "STATE_DB": + del mock_state_db[key] + + db.keys = mock.Mock(side_effect=keys) db.get_all = mock.Mock(side_effect=get_all) db.get = mock.Mock(side_effect=get) db.hmset = mock.Mock(side_effect=hmset) db.exists = mock.Mock(side_effect=exists) + db.delete = mock.Mock(side_effect=delete) yield db diff --git a/dockers/docker-dhcp-server/cli-plugin-tests/test_config_dhcp_server.py b/dockers/docker-dhcp-server/cli-plugin-tests/test_config_dhcp_server.py index bc71154ccf..17add53a21 100644 --- a/dockers/docker-dhcp-server/cli-plugin-tests/test_config_dhcp_server.py +++ b/dockers/docker-dhcp-server/cli-plugin-tests/test_config_dhcp_server.py @@ -113,3 +113,18 @@ class TestConfigDHCPServer(object): ["Vlan100", "--mode=PORT", "--lease_time=1000", "--gateway=10.10.10.10", "--netmask=255.255.254.0"], obj=db) assert result.exit_code == 2, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info) + def test_config_dhcp_server_ipv4_del_already_exist(self, mock_db): + runner = CliRunner() + db = clicommon.Db() + db.db = mock_db + result = runner.invoke(dhcp_server.dhcp_server.commands["ipv4"].commands["del"], ["Vlan100"], obj=db) + assert result.exit_code == 0, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info) + assert mock_db.exists("CONFIG_DB", "DHCP_SERVER_IPV4|Vlan100") == False + + def test_config_dhcp_server_ipv4_del_does_not_exist(self, mock_db): + runner = CliRunner() + db = clicommon.Db() + db.db = mock_db + result = runner.invoke(dhcp_server.dhcp_server.commands["ipv4"].commands["del"], ["Vlan200"], obj=db) + assert result.exit_code == 2, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info) + diff --git a/dockers/docker-dhcp-server/cli/config/plugins/dhcp_server.py b/dockers/docker-dhcp-server/cli/config/plugins/dhcp_server.py index faf920b995..45475e3f6c 100644 --- a/dockers/docker-dhcp-server/cli/config/plugins/dhcp_server.py +++ b/dockers/docker-dhcp-server/cli/config/plugins/dhcp_server.py @@ -102,6 +102,20 @@ def dhcp_server_ipv4_add(db, mode, lease_time, dup_gw_nm, gateway, netmask, dhcp }) +@dhcp_server_ipv4.command(name="del") +@click.argument("dhcp_interface", required=True) +@clicommon.pass_db +def dhcp_server_ipv4_del(db, dhcp_interface): + ctx = click.get_current_context() + dbconn = db.db + key = "DHCP_SERVER_IPV4|" + dhcp_interface + if dbconn.exists("CONFIG_DB", key): + click.echo("Dhcp interface %s exists in config db, proceed to delete".format(dhcp_interface)) + dbconn.delete("CONFIG_DB", key) + else: + ctx.fail("Dhcp interface %s does not exist in config db".format(dhcp_interface)) + + def register(cli): # cli.add_command(dhcp_server) pass