[dhcp_server] add config dhcp server enable (#17605)

* add config dhcp_server enable

* fix bug

* fix bug

* fix bug

* fix bug
This commit is contained in:
Xichen96 2024-01-04 05:39:39 +08:00 committed by GitHub
parent 7011e00eba
commit ffe292a021
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 1 deletions

View File

@ -69,6 +69,12 @@ def mock_db():
if table == "STATE_DB":
del mock_state_db[key]
def set_(table, key, k, v):
assert table == "CONFIG_DB" or table == "STATE_DB"
if table == "CONFIG_DB":
mock_config_db[key][k] = v
if table == "STATE_DB":
mock_state_db[key][k] = v
db.keys = mock.Mock(side_effect=keys)
db.get_all = mock.Mock(side_effect=get_all)
@ -76,5 +82,6 @@ def mock_db():
db.hmset = mock.Mock(side_effect=hmset)
db.exists = mock.Mock(side_effect=exists)
db.delete = mock.Mock(side_effect=delete)
db.set = mock.Mock(side_effect=set_)
yield db

View File

@ -20,6 +20,14 @@
"customized_options": "option60",
"state": "enabled"
},
"DHCP_SERVER_IPV4|Vlan300": {
"gateway": "100.1.1.1",
"lease_time": "3600",
"mode": "PORT",
"netmask": "255.255.255.0",
"customized_options": "option60",
"state": "disabled"
},
"DHCP_SERVER_IPV4_CUSTOMIZED_OPTIONS|option60": {
"id": "60",
"type": "string",

View File

@ -128,3 +128,18 @@ class TestConfigDHCPServer(object):
result = runner.invoke(dhcp_server.dhcp_server.commands["ipv4"].commands["del"], ["Vlan200"], obj=db)
assert result.exit_code == 2, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info)
def test_config_dhcp_server_ipv4_enable_already_exist(self, mock_db):
runner = CliRunner()
db = clicommon.Db()
db.db = mock_db
result = runner.invoke(dhcp_server.dhcp_server.commands["ipv4"].commands["enable"], ["Vlan300"], obj=db)
assert result.exit_code == 0, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info)
assert mock_db.get("CONFIG_DB", "DHCP_SERVER_IPV4|Vlan300", "state") == "enabled"
def test_config_dhcp_server_ipv4_enable_does_not_exist(self, mock_db):
runner = CliRunner()
db = clicommon.Db()
db.db = mock_db
result = runner.invoke(dhcp_server.dhcp_server.commands["ipv4"].commands["enable"], ["Vlan200"], obj=db)
assert result.exit_code == 2, "exit code: {}, Exception: {}, Traceback: {}".format(result.exit_code, result.exception, result.exc_info)

View File

@ -113,8 +113,9 @@ range3 100.1.1.10 100.1.1.10 1
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
Vlan300 PORT 100.1.1.1 255.255.255.0 3600 disabled
"""
runner = CliRunner()
db = clicommon.Db()

View File

@ -116,6 +116,19 @@ def dhcp_server_ipv4_del(db, dhcp_interface):
ctx.fail("Dhcp interface %s does not exist in config db".format(dhcp_interface))
@dhcp_server_ipv4.command(name="enable")
@click.argument("dhcp_interface", required=True)
@clicommon.pass_db
def dhcp_server_ipv4_enable(db, dhcp_interface):
ctx = click.get_current_context()
dbconn = db.db
key = "DHCP_SERVER_IPV4|" + dhcp_interface
if dbconn.exists("CONFIG_DB", key):
dbconn.set("CONFIG_DB", key, "state", "enabled")
else:
ctx.fail("Failed to enable, dhcp interface %s does not exist".format(dhcp_interface))
def register(cli):
# cli.add_command(dhcp_server)
pass