Skip to content

Commit

Permalink
Made changes to only allow a valid IPv4 or Ipv6 string for dhcp cli (s…
Browse files Browse the repository at this point in the history
…onic-net#2296)

What I did
Currently,

root@qa-eth-vt04-3-2700a0:/home/admin# config vlan dhcp_relay add 125 11
Added DHCP relay destination address 11 to Vlan125
Restarting DHCP relay service...
root@qa-eth-vt04-3-2700a0:/home/admin# config vlan dhcp_relay add 125 1100000
Added DHCP relay destination address 1100000 to Vlan125
Restarting DHCP relay service...
root@qa-eth-vt04-3-2700a0:/home/admin# show vla bri
+-----------+--------------+-----------+----------------+-----------------------+-------------+
|   VLAN ID | IP Address   | Ports     | Port Tagging   | DHCP Helper Address   | Proxy ARP   |
+===========+==============+===========+================+=======================+=============+
|       125 | 2000::1:1/64 | Ethernet4 | tagged         | 500                   | disabled    |
|           |              |           |                | 11                    |             |
|           |              |           |                | 1100000               |             |
|           |              |           |                | 2000:1::2             |             |
+-----------+--------------+-----------+----------------+-----------------------+-------------+
Made changes to take in a valid IP string.

After the change:

root@qa-eth-vt02-7-3420:/home/admin# config vlan dhcp_relay add 125 1100000
Error: 1100000 is invalid IP address

Signed-off-by: Vivek Reddy [email protected]
  • Loading branch information
vivekrnv authored Aug 11, 2022
1 parent b034f0c commit 6b9cdc9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion config/vlan.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import click
import ipaddress
import utilities_common.cli as clicommon

from time import sleep
Expand Down Expand Up @@ -198,7 +199,9 @@ def add_vlan_dhcp_relay_destination(db, vid, dhcp_relay_destination_ip):

ctx = click.get_current_context()

if not clicommon.is_ipaddress(dhcp_relay_destination_ip):
try:
ipaddress.ip_address(dhcp_relay_destination_ip)
except Exception:
ctx.fail('{} is invalid IP address'.format(dhcp_relay_destination_ip))

vlan_name = 'Vlan{}'.format(vid)
Expand Down
13 changes: 13 additions & 0 deletions tests/vlan_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,19 @@ def test_config_vlan_add_dhcp_relay_with_invalid_ipv6(self):
assert "Error: fe80:2030:31:24 is invalid IP address" in result.output
assert mock_run_command.call_count == 0

def test_config_vlan_add_dhcp_relay_with_invalid_ip_2(self):
runner = CliRunner()

with mock.patch('utilities_common.cli.run_command') as mock_run_command:
result = runner.invoke(config.config.commands["vlan"].commands["dhcp_relay"].commands["add"],
["1000", "110000"])
print(result.exit_code)
print(result.output)
# traceback.print_tb(result.exc_info[2])
assert result.exit_code != 0
assert "Error: 110000 is invalid IP address" in result.output
assert mock_run_command.call_count == 0

def test_config_vlan_add_dhcp_relay_with_exist_ip(self):
runner = CliRunner()

Expand Down

0 comments on commit 6b9cdc9

Please sign in to comment.