Skip to content

Commit

Permalink
fix missing parameter and add UT (#18501)
Browse files Browse the repository at this point in the history
  • Loading branch information
w1nda authored Apr 1, 2024
1 parent a56cf79 commit cd09284
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ def test_plugin_registration(self):
def test_validate_str_type(self, type, value, result):
assert dhcp_server.validate_str_type(type, value) == result

@pytest.mark.parametrize("state", ["disabled", "enabled"])
def test_config_dhcp_server_feature_state_checking(self, mock_db, state):
runner = CliRunner()
db = clicommon.Db()
db.db = mock_db
mock_db.set("CONFIG_DB", "FEATURE|dhcp_server", "state", state)
result = runner.invoke(dhcp_server.dhcp_server, obj=db)
if state == "disabled":
assert result.exit_code == 2, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info)
assert "Feature dhcp_server is not enabled" in result.output
elif state == "enabled":
assert result.exit_code == 0, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info)
assert "Usage: dhcp_server [OPTIONS] COMMAND [ARGS]" in result.output
else:
assert False

def test_config_dhcp_server_ipv4_add(self, mock_db):
expected_value = {
"gateway": "10.10.10.10",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
import sys
from unittest import mock

Expand All @@ -14,6 +15,22 @@ def test_plugin_registration(self):
cli = mock.MagicMock()
show_dhcp_server.register(cli)

@pytest.mark.parametrize("state", ["disabled", "enabled"])
def test_show_dhcp_server_feature_state_checking(self, mock_db, state):
runner = CliRunner()
db = clicommon.Db()
db.db = mock_db
mock_db.set("CONFIG_DB", "FEATURE|dhcp_server", "state", state)
result = runner.invoke(show_dhcp_server.dhcp_server, obj=db)
if state == "disabled":
assert result.exit_code == 2, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info)
assert "Feature dhcp_server is not enabled" in result.output
elif state == "enabled":
assert result.exit_code == 0, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info)
assert "Usage: dhcp_server [OPTIONS] COMMAND [ARGS]" in result.output
else:
assert False

def test_show_dhcp_server_ipv4_lease_without_dhcpintf(self, mock_db):
expected_stdout = """\
+---------------------+-------------------+-------------+---------------------+---------------------+
Expand Down
7 changes: 5 additions & 2 deletions dockers/docker-dhcp-server/cli/config/plugins/dhcp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,17 @@ def validate_str_type(type_, value):
return False


@click.group(cls=clicommon.AbbreviationGroup, name="dhcp_server")
@click.group(cls=clicommon.AbbreviationGroup, name="dhcp_server", invoke_without_command=True)
@clicommon.pass_db
def dhcp_server():
def dhcp_server(db):
"""config DHCP Server information"""
ctx = click.get_current_context()
dbconn = db.db
if dbconn.get("CONFIG_DB", "FEATURE|dhcp_server", "state") != "enabled":
ctx.fail("Feature dhcp_server is not enabled")
if ctx.invoked_subcommand is None:
click.echo(ctx.get_help())
ctx.exit()


@dhcp_server.group(cls=clicommon.AliasedGroup, name="ipv4")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ def ts_to_str(ts):
return datetime.fromtimestamp(int(ts)).strftime("%Y-%m-%d %H:%M:%S")


@click.group(cls=clicommon.AbbreviationGroup, name="dhcp_server")
@click.group(cls=clicommon.AbbreviationGroup, name="dhcp_server", invoke_without_command=True)
@clicommon.pass_db
def dhcp_server(db):
"""Show dhcp_server related info"""
ctx = click.get_current_context()
dbconn = db.db
if dbconn.get("CONFIG_DB", "FEATURE|dhcp_server", "state") != "enabled":
ctx.fail("Feature dhcp_server is not enabled")
if ctx.invoked_subcommand is None:
click.echo(ctx.get_help())
ctx.exit()


@dhcp_server.group(cls=clicommon.AliasedGroup)
Expand Down

0 comments on commit cd09284

Please sign in to comment.