sonic-buildimage/dockers/docker-dhcp-server/cli-plugin-tests/test_show_dhcp_server.py
Xichen96 1e92ba24ec
[dhcp_server] add show dhcp server info (#17468)
* add show dhcp server info
2023-12-20 09:07:32 -08:00

152 lines
7.5 KiB
Python

import sys
from unittest import mock
from click.testing import CliRunner
import utilities_common.cli as clicommon
sys.path.append('../cli/show/plugins/')
import show_dhcp_server
class TestShowDHCPServer(object):
def test_plugin_registration(self):
cli = mock.MagicMock()
show_dhcp_server.register(cli)
def test_show_dhcp_server_ipv4_lease_without_dhcpintf(self, mock_db):
expected_stdout = """\
Interface MAC Address IP Lease Start Lease End
------------------- ----------------- ----------- ------------------- -------------------
Vlan1000|Ethernet10 10:70:fd:b6:13:00 192.168.0.1 2023-03-01 03:16:21 2023-03-01 03:31:21
Vlan1000|Ethernet11 10:70:fd:b6:13:01 192.168.0.2 2023-03-01 03:16:21 2023-03-01 03:31:21
Vlan1001|<Unknown> 10:70:fd:b6:13:02 192.168.0.3 2023-03-01 03:16:21 2023-03-01 03:31:21
"""
runner = CliRunner()
db = clicommon.Db()
db.db = mock_db
result = runner.invoke(show_dhcp_server.dhcp_server.commands["ipv4"].commands["lease"], [], 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_lease_with_dhcpintf(self, mock_db):
expected_stdout = """\
Interface MAC Address IP Lease Start Lease End
------------------- ----------------- ----------- ------------------- -------------------
Vlan1000|Ethernet10 10:70:fd:b6:13:00 192.168.0.1 2023-03-01 03:16:21 2023-03-01 03:31:21
Vlan1000|Ethernet11 10:70:fd:b6:13:01 192.168.0.2 2023-03-01 03:16:21 2023-03-01 03:31:21
"""
runner = CliRunner()
db = clicommon.Db()
db.db = mock_db
result = runner.invoke(show_dhcp_server.dhcp_server.commands["ipv4"].commands["lease"], ["Vlan1000"], 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_lease_client_not_in_fdb(self, mock_db):
expected_stdout = """\
Interface MAC Address IP Lease Start Lease End
------------------ ----------------- ----------- ------------------- -------------------
Vlan1001|<Unknown> 10:70:fd:b6:13:02 192.168.0.3 2023-03-01 03:16:21 2023-03-01 03:31:21
"""
runner = CliRunner()
db = clicommon.Db()
db.db = mock_db
result = runner.invoke(show_dhcp_server.dhcp_server.commands["ipv4"].commands["lease"], ["Vlan1001"], 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_range_without_name(self, mock_db):
expected_stdout = """\
Range IP Start IP End IP Count
------- ---------- ---------- ----------------------
range1 100.1.1.3 100.1.1.5 3
range2 100.1.1.9 100.1.1.8 range value is illegal
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["range"], [], 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_range_with_name(self, mock_db):
expected_stdout = """\
Range IP Start IP End IP Count
------- ---------- --------- ----------
range1 100.1.1.3 100.1.1.5 3
"""
runner = CliRunner()
db = clicommon.Db()
db.db = mock_db
result = runner.invoke(show_dhcp_server.dhcp_server.commands["ipv4"].commands["range"], ["range1"], 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_range_wrong_data(self, mock_db):
expected_stdout = """\
Range IP Start IP End IP Count
------- ---------- --------- ----------------------
range2 100.1.1.9 100.1.1.8 range value is illegal
"""
runner = CliRunner()
db = clicommon.Db()
db.db = mock_db
result = runner.invoke(show_dhcp_server.dhcp_server.commands["ipv4"].commands["range"], ["range2"], 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_range_single_ip(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["range"], ["range3"], 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_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
"""
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 = """\
Interface Mode Gateway Netmask Lease Time(s) State
----------- ------ --------- ------------- --------------- -------
Vlan100 PORT 100.1.1.1 255.255.255.0 3600 enabled
"""
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 = """\
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()
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