From cd09284ac8551080141d20be08e5db42477f1265 Mon Sep 17 00:00:00 2001 From: Wenda Chu <32250288+w1nda@users.noreply.github.com> Date: Mon, 1 Apr 2024 09:20:22 -0700 Subject: [PATCH] fix missing parameter and add UT (#18501) --- .../cli-plugin-tests/test_config_dhcp_server.py | 16 ++++++++++++++++ .../cli-plugin-tests/test_show_dhcp_server.py | 17 +++++++++++++++++ .../cli/config/plugins/dhcp_server.py | 7 +++++-- .../cli/show/plugins/show_dhcp_server.py | 5 ++++- 4 files changed, 42 insertions(+), 3 deletions(-) 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 bc91dfe980ee..2871b7c06042 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 @@ -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", 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 bacf911b37a5..7dbcfc0e359d 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 @@ -1,3 +1,4 @@ +import pytest import sys from unittest import mock @@ -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 = """\ +---------------------+-------------------+-------------+---------------------+---------------------+ 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 7f94045e6c23..0aabf1c29ec3 100644 --- a/dockers/docker-dhcp-server/cli/config/plugins/dhcp_server.py +++ b/dockers/docker-dhcp-server/cli/config/plugins/dhcp_server.py @@ -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") diff --git a/dockers/docker-dhcp-server/cli/show/plugins/show_dhcp_server.py b/dockers/docker-dhcp-server/cli/show/plugins/show_dhcp_server.py index eb5e276c343e..5388b46177e7 100644 --- a/dockers/docker-dhcp-server/cli/show/plugins/show_dhcp_server.py +++ b/dockers/docker-dhcp-server/cli/show/plugins/show_dhcp_server.py @@ -13,7 +13,7 @@ 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""" @@ -21,6 +21,9 @@ def dhcp_server(db): 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)