From 3fa4ad5076def7b2f5c0ab02201b5b3762678910 Mon Sep 17 00:00:00 2001 From: Xichen Lin Date: Sun, 10 Dec 2023 17:20:07 +0000 Subject: [PATCH 1/5] add show dhcp server info --- .../cli-plugin-tests/test_show_dhcp_server.py | 39 +++++++++++++++++++ .../cli/show/plugins/show_dhcp_server.py | 21 ++++++++++ 2 files changed, 60 insertions(+) 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 a41650584ce4..7c5408dd1bb8 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 @@ -110,3 +110,42 @@ def test_show_dhcp_server_ipv4_range_single_ip(self, mock_db): assert result.exit_code == 0, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info) assert result.stdout == expected_stdout + def test_show_dhcp_server_ipv4_info_without_intf(self, mock_db): + expected_stdout = """\ +Range IP Start IP End IP Count +------- ---------- ---------- ---------- +range3 100.1.1.10 100.1.1.10 1 +""" + runner = CliRunner() + db = clicommon.Db() + db.db = mock_db + result = runner.invoke(show_dhcp_server.dhcp_server.commands["ipv4"].commands["info"], [], obj=db) + assert result.exit_code == 0, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info) + assert result.stdout == expected_stdout + + def test_show_dhcp_server_ipv4_info_with_intf(self, mock_db): + expected_stdout = """\ +Range IP Start IP End IP Count +------- ---------- ---------- ---------- +range3 100.1.1.10 100.1.1.10 1 +""" + runner = CliRunner() + db = clicommon.Db() + db.db = mock_db + result = runner.invoke(show_dhcp_server.dhcp_server.commands["ipv4"].commands["info"], ["Vlan100"], obj=db) + assert result.exit_code == 0, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info) + assert result.stdout == expected_stdout + + def test_show_dhcp_server_ipv4_info_with_customized_options(self, mock_db): + expected_stdout = """\ +Range IP Start IP End IP Count +------- ---------- ---------- ---------- +range3 100.1.1.10 100.1.1.10 1 +""" + runner = CliRunner() + db = clicommon.Db() + db.db = mock_db + result = runner.invoke(show_dhcp_server.dhcp_server.commands["ipv4"].commands["info"], ["Vlan100", "--with_customized_options"], obj=db) + assert result.exit_code == 0, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info) + assert result.stdout == expected_stdout + 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 3032168552af..05aca39f726f 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 @@ -79,5 +79,26 @@ def range(db, range_name): click.echo(tabulate(table, headers=headers)) +@ipv4.command() +@click.argument('dhcp_interface', required=False) +@click.option('--with_customized_options', default=False, is_flag=True) +@clicommon.pass_db +def info(db, dhcp_interface, with_customized_options): + if not dhcp_interface: + dhcp_interface = "*" + headers = ["Interface", "Mode", "Gateway", "Netmask", "Lease Time(s)", "IP Bind"] + if with_customized_options: + headers.append("Customized Options") + table = [] + dbconn = db.db + for key in dbconn.keys("CONFIG_DB", "DHCP_SERVER_IPV4|" + dhcp_interface): + entry = dbconn.get_all("CONFIG_DB", key) + interface = key.split("|")[1] + table.append([interface, entry["mode"], entry["gateway"], entry["netmask"], entry["lease_time"], entry["state"]]) + if with_customized_options: + table[-1].append(entry["customized_options"]) + click.echo(tabulate(table, headers=headers)) + + def register(cli): cli.add_command(dhcp_server) From 6445818bfe8a9edd4433324ff41286e959a2e31b Mon Sep 17 00:00:00 2001 From: Xichen Lin Date: Sun, 10 Dec 2023 18:04:44 +0000 Subject: [PATCH 2/5] fix bug --- .../cli-plugin-tests/mock_config_db.json | 4 +--- .../cli-plugin-tests/test_show_dhcp_server.py | 12 ++++++------ 2 files changed, 7 insertions(+), 9 deletions(-) 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 c8f42d4c39cf..908c1143544e 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 @@ -7,9 +7,7 @@ "lease_time": "3600", "mode": "PORT", "netmask": "255.255.255.0", - "customized_options": [ - "option60" - ], + "customized_options": "option60", "state": "enabled" }, "DHCP_SERVER_IPV4_CUSTOMIZED_OPTIONS|option60": { 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 7c5408dd1bb8..fbd5d461477b 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 @@ -112,9 +112,9 @@ def test_show_dhcp_server_ipv4_range_single_ip(self, mock_db): def test_show_dhcp_server_ipv4_info_without_intf(self, mock_db): expected_stdout = """\ -Range IP Start IP End IP Count -------- ---------- ---------- ---------- -range3 100.1.1.10 100.1.1.10 1 +Interface Mode Gateway Netmask Lease Time(s) IP Bind +----------- ------ --------- ------------- --------------- --------- +Vlan100 PORT 100.1.1.1 255.255.255.0 3600 enabled """ runner = CliRunner() db = clicommon.Db() @@ -125,9 +125,9 @@ def test_show_dhcp_server_ipv4_info_without_intf(self, mock_db): def test_show_dhcp_server_ipv4_info_with_intf(self, mock_db): expected_stdout = """\ -Range IP Start IP End IP Count -------- ---------- ---------- ---------- -range3 100.1.1.10 100.1.1.10 1 +Interface Mode Gateway Netmask Lease Time(s) IP Bind Customized Options +----------- ------ --------- ------------- --------------- --------- -------------------- +Vlan100 PORT 100.1.1.1 255.255.255.0 3600 enabled ['option60'] """ runner = CliRunner() db = clicommon.Db() From 376b36ca014a4d0952f49e4f11234f0e1a24d286 Mon Sep 17 00:00:00 2001 From: Xichen Lin Date: Sun, 10 Dec 2023 18:09:57 +0000 Subject: [PATCH 3/5] fix bug --- .../cli-plugin-tests/test_show_dhcp_server.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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 fbd5d461477b..7f50930cb9e0 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 @@ -125,9 +125,9 @@ def test_show_dhcp_server_ipv4_info_without_intf(self, mock_db): def test_show_dhcp_server_ipv4_info_with_intf(self, mock_db): expected_stdout = """\ -Interface Mode Gateway Netmask Lease Time(s) IP Bind Customized Options ------------ ------ --------- ------------- --------------- --------- -------------------- -Vlan100 PORT 100.1.1.1 255.255.255.0 3600 enabled ['option60'] +Interface Mode Gateway Netmask Lease Time(s) IP Bind +----------- ------ --------- ------------- --------------- --------- +Vlan100 PORT 100.1.1.1 255.255.255.0 3600 enabled """ runner = CliRunner() db = clicommon.Db() @@ -138,9 +138,9 @@ def test_show_dhcp_server_ipv4_info_with_intf(self, mock_db): def test_show_dhcp_server_ipv4_info_with_customized_options(self, mock_db): expected_stdout = """\ -Range IP Start IP End IP Count -------- ---------- ---------- ---------- -range3 100.1.1.10 100.1.1.10 1 +Interface Mode Gateway Netmask Lease Time(s) IP Bind Customized Options +----------- ------ --------- ------------- --------------- --------- -------------------- +Vlan100 PORT 100.1.1.1 255.255.255.0 3600 enabled option60 """ runner = CliRunner() db = clicommon.Db() From a7e69a9e90cc649c2811b7481a1369633fa43ca5 Mon Sep 17 00:00:00 2001 From: Xichen Lin Date: Tue, 12 Dec 2023 07:00:31 +0000 Subject: [PATCH 4/5] fix bug --- .../cli-plugin-tests/test_show_dhcp_server.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) 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 7f50930cb9e0..e461396ef665 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 @@ -112,8 +112,8 @@ def test_show_dhcp_server_ipv4_range_single_ip(self, mock_db): def test_show_dhcp_server_ipv4_info_without_intf(self, mock_db): expected_stdout = """\ -Interface Mode Gateway Netmask Lease Time(s) IP Bind ------------ ------ --------- ------------- --------------- --------- +Interface Mode Gateway Netmask Lease Time(s) State +----------- ------ --------- ------------- --------------- ------- Vlan100 PORT 100.1.1.1 255.255.255.0 3600 enabled """ runner = CliRunner() @@ -125,8 +125,8 @@ def test_show_dhcp_server_ipv4_info_without_intf(self, mock_db): def test_show_dhcp_server_ipv4_info_with_intf(self, mock_db): expected_stdout = """\ -Interface Mode Gateway Netmask Lease Time(s) IP Bind ------------ ------ --------- ------------- --------------- --------- +Interface Mode Gateway Netmask Lease Time(s) State +----------- ------ --------- ------------- --------------- ------- Vlan100 PORT 100.1.1.1 255.255.255.0 3600 enabled """ runner = CliRunner() @@ -138,9 +138,9 @@ def test_show_dhcp_server_ipv4_info_with_intf(self, mock_db): def test_show_dhcp_server_ipv4_info_with_customized_options(self, mock_db): expected_stdout = """\ -Interface Mode Gateway Netmask Lease Time(s) IP Bind Customized Options ------------ ------ --------- ------------- --------------- --------- -------------------- -Vlan100 PORT 100.1.1.1 255.255.255.0 3600 enabled option60 +Interface Mode Gateway Netmask Lease Time(s) State Customized Options +----------- ------ --------- ------------- --------------- ------- -------------------- +Vlan100 PORT 100.1.1.1 255.255.255.0 3600 enabled option60 """ runner = CliRunner() db = clicommon.Db() From fdd72dd03c14e723203dbaf5b84f679b9827b0e8 Mon Sep 17 00:00:00 2001 From: Xichen Lin Date: Tue, 12 Dec 2023 09:38:42 +0000 Subject: [PATCH 5/5] fix bug --- dockers/docker-dhcp-server/cli/show/plugins/show_dhcp_server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 05aca39f726f..93e38af920ad 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 @@ -86,7 +86,7 @@ def range(db, range_name): def info(db, dhcp_interface, with_customized_options): if not dhcp_interface: dhcp_interface = "*" - headers = ["Interface", "Mode", "Gateway", "Netmask", "Lease Time(s)", "IP Bind"] + headers = ["Interface", "Mode", "Gateway", "Netmask", "Lease Time(s)", "State"] if with_customized_options: headers.append("Customized Options") table = []