Skip to content

Commit cd09284

Browse files
authored
fix missing parameter and add UT (#18501)
1 parent a56cf79 commit cd09284

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

dockers/docker-dhcp-server/cli-plugin-tests/test_config_dhcp_server.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@ def test_plugin_registration(self):
3333
def test_validate_str_type(self, type, value, result):
3434
assert dhcp_server.validate_str_type(type, value) == result
3535

36+
@pytest.mark.parametrize("state", ["disabled", "enabled"])
37+
def test_config_dhcp_server_feature_state_checking(self, mock_db, state):
38+
runner = CliRunner()
39+
db = clicommon.Db()
40+
db.db = mock_db
41+
mock_db.set("CONFIG_DB", "FEATURE|dhcp_server", "state", state)
42+
result = runner.invoke(dhcp_server.dhcp_server, obj=db)
43+
if state == "disabled":
44+
assert result.exit_code == 2, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info)
45+
assert "Feature dhcp_server is not enabled" in result.output
46+
elif state == "enabled":
47+
assert result.exit_code == 0, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info)
48+
assert "Usage: dhcp_server [OPTIONS] COMMAND [ARGS]" in result.output
49+
else:
50+
assert False
51+
3652
def test_config_dhcp_server_ipv4_add(self, mock_db):
3753
expected_value = {
3854
"gateway": "10.10.10.10",

dockers/docker-dhcp-server/cli-plugin-tests/test_show_dhcp_server.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
import sys
23
from unittest import mock
34

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

18+
@pytest.mark.parametrize("state", ["disabled", "enabled"])
19+
def test_show_dhcp_server_feature_state_checking(self, mock_db, state):
20+
runner = CliRunner()
21+
db = clicommon.Db()
22+
db.db = mock_db
23+
mock_db.set("CONFIG_DB", "FEATURE|dhcp_server", "state", state)
24+
result = runner.invoke(show_dhcp_server.dhcp_server, obj=db)
25+
if state == "disabled":
26+
assert result.exit_code == 2, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info)
27+
assert "Feature dhcp_server is not enabled" in result.output
28+
elif state == "enabled":
29+
assert result.exit_code == 0, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info)
30+
assert "Usage: dhcp_server [OPTIONS] COMMAND [ARGS]" in result.output
31+
else:
32+
assert False
33+
1734
def test_show_dhcp_server_ipv4_lease_without_dhcpintf(self, mock_db):
1835
expected_stdout = """\
1936
+---------------------+-------------------+-------------+---------------------+---------------------+

dockers/docker-dhcp-server/cli/config/plugins/dhcp_server.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,17 @@ def validate_str_type(type_, value):
4545
return False
4646

4747

48-
@click.group(cls=clicommon.AbbreviationGroup, name="dhcp_server")
48+
@click.group(cls=clicommon.AbbreviationGroup, name="dhcp_server", invoke_without_command=True)
4949
@clicommon.pass_db
50-
def dhcp_server():
50+
def dhcp_server(db):
5151
"""config DHCP Server information"""
5252
ctx = click.get_current_context()
5353
dbconn = db.db
5454
if dbconn.get("CONFIG_DB", "FEATURE|dhcp_server", "state") != "enabled":
5555
ctx.fail("Feature dhcp_server is not enabled")
56+
if ctx.invoked_subcommand is None:
57+
click.echo(ctx.get_help())
58+
ctx.exit()
5659

5760

5861
@dhcp_server.group(cls=clicommon.AliasedGroup, name="ipv4")

dockers/docker-dhcp-server/cli/show/plugins/show_dhcp_server.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@ def ts_to_str(ts):
1313
return datetime.fromtimestamp(int(ts)).strftime("%Y-%m-%d %H:%M:%S")
1414

1515

16-
@click.group(cls=clicommon.AbbreviationGroup, name="dhcp_server")
16+
@click.group(cls=clicommon.AbbreviationGroup, name="dhcp_server", invoke_without_command=True)
1717
@clicommon.pass_db
1818
def dhcp_server(db):
1919
"""Show dhcp_server related info"""
2020
ctx = click.get_current_context()
2121
dbconn = db.db
2222
if dbconn.get("CONFIG_DB", "FEATURE|dhcp_server", "state") != "enabled":
2323
ctx.fail("Feature dhcp_server is not enabled")
24+
if ctx.invoked_subcommand is None:
25+
click.echo(ctx.get_help())
26+
ctx.exit()
2427

2528

2629
@dhcp_server.group(cls=clicommon.AliasedGroup)

0 commit comments

Comments
 (0)