diff --git a/dockers/docker-dhcp-server/cli-plugin-tests/conftest.py b/dockers/docker-dhcp-server/cli-plugin-tests/conftest.py index 560e25667a..ece7d576dd 100644 --- a/dockers/docker-dhcp-server/cli-plugin-tests/conftest.py +++ b/dockers/docker-dhcp-server/cli-plugin-tests/conftest.py @@ -69,6 +69,12 @@ def mock_db(): if table == "STATE_DB": del mock_state_db[key] + def set_(table, key, k, v): + assert table == "CONFIG_DB" or table == "STATE_DB" + if table == "CONFIG_DB": + mock_config_db[key][k] = v + if table == "STATE_DB": + mock_state_db[key][k] = v db.keys = mock.Mock(side_effect=keys) db.get_all = mock.Mock(side_effect=get_all) @@ -76,5 +82,6 @@ def mock_db(): db.hmset = mock.Mock(side_effect=hmset) db.exists = mock.Mock(side_effect=exists) db.delete = mock.Mock(side_effect=delete) + db.set = mock.Mock(side_effect=set_) yield db diff --git a/dockers/docker-dhcp-server/cli-plugin-tests/mock_config_db.json b/dockers/docker-dhcp-server/cli-plugin-tests/mock_config_db.json index 751f716f66..fcc088e8c0 100644 --- a/dockers/docker-dhcp-server/cli-plugin-tests/mock_config_db.json +++ b/dockers/docker-dhcp-server/cli-plugin-tests/mock_config_db.json @@ -20,6 +20,14 @@ "customized_options": "option60", "state": "enabled" }, + "DHCP_SERVER_IPV4|Vlan300": { + "gateway": "100.1.1.1", + "lease_time": "3600", + "mode": "PORT", + "netmask": "255.255.255.0", + "customized_options": "option60", + "state": "disabled" + }, "DHCP_SERVER_IPV4_CUSTOMIZED_OPTIONS|option60": { "id": "60", "type": "string", 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 17add53a21..57ed08161a 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 @@ -128,3 +128,18 @@ class TestConfigDHCPServer(object): 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) + def test_config_dhcp_server_ipv4_enable_already_exist(self, mock_db): + runner = CliRunner() + db = clicommon.Db() + db.db = mock_db + result = runner.invoke(dhcp_server.dhcp_server.commands["ipv4"].commands["enable"], ["Vlan300"], obj=db) + assert result.exit_code == 0, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info) + assert mock_db.get("CONFIG_DB", "DHCP_SERVER_IPV4|Vlan300", "state") == "enabled" + + def test_config_dhcp_server_ipv4_enable_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["enable"], ["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-plugin-tests/test_show_dhcp_server.py b/dockers/docker-dhcp-server/cli-plugin-tests/test_show_dhcp_server.py index 55ab4cbdbd..348e7f0edc 100644 --- a/dockers/docker-dhcp-server/cli-plugin-tests/test_show_dhcp_server.py +++ b/dockers/docker-dhcp-server/cli-plugin-tests/test_show_dhcp_server.py @@ -113,8 +113,9 @@ range3 100.1.1.10 100.1.1.10 1 def test_show_dhcp_server_ipv4_info_without_intf(self, mock_db): expected_stdout = """\ Interface Mode Gateway Netmask Lease Time(s) State ------------ ------ --------- ------------- --------------- ------- +----------- ------ --------- ------------- --------------- -------- Vlan100 PORT 100.1.1.1 255.255.255.0 3600 enabled +Vlan300 PORT 100.1.1.1 255.255.255.0 3600 disabled """ runner = CliRunner() db = clicommon.Db() 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 45475e3f6c..ae60611ae6 100644 --- a/dockers/docker-dhcp-server/cli/config/plugins/dhcp_server.py +++ b/dockers/docker-dhcp-server/cli/config/plugins/dhcp_server.py @@ -116,6 +116,19 @@ def dhcp_server_ipv4_del(db, dhcp_interface): ctx.fail("Dhcp interface %s does not exist in config db".format(dhcp_interface)) +@dhcp_server_ipv4.command(name="enable") +@click.argument("dhcp_interface", required=True) +@clicommon.pass_db +def dhcp_server_ipv4_enable(db, dhcp_interface): + ctx = click.get_current_context() + dbconn = db.db + key = "DHCP_SERVER_IPV4|" + dhcp_interface + if dbconn.exists("CONFIG_DB", key): + dbconn.set("CONFIG_DB", key, "state", "enabled") + else: + ctx.fail("Failed to enable, dhcp interface %s does not exist".format(dhcp_interface)) + + def register(cli): # cli.add_command(dhcp_server) pass