Skip to content

Commit

Permalink
Review fixes - Vlan exists
Browse files Browse the repository at this point in the history
Test that vlan exists
Added an optional param (option) --ignore-vlan which will test only
format

~

Signed-off-by: matiAlfaro <[email protected]>
  • Loading branch information
matiAlfaro committed Feb 8, 2024
1 parent e9cacc9 commit 041f3ec
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
19 changes: 15 additions & 4 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4516,6 +4516,11 @@ def ip(ctx):
def validate_vlan_format(text):
pattern = re.compile(r'^Vlan([1-9]\d{0,2}|[1-3]\d{3}|40[0-8][0-9]|409[0-4])$')
return pattern.fullmatch(text) is not None

def validate_vlan_exists(db,text):
data = db.get_table('VLAN')
keys = list(data.keys())
return text in keys
#
# 'add' subcommand
#
Expand All @@ -4524,8 +4529,9 @@ def validate_vlan_format(text):
@click.argument('interface_name', metavar='<interface_name>', required=True)
@click.argument("ip_addr", metavar="<ip_addr>", required=True)
@click.argument('gw', metavar='<default gateway IP address>', required=False)
@click.option('-i','--ignore-vlan',is_flag=True, required=False, help="Allow configuration on not created Vlan")
@click.pass_context
def add(ctx, interface_name, ip_addr, gw):
def add(ctx, interface_name, ip_addr, gw, ignore_vlan):
"""Add an IP address towards the interface"""
# Get the config_db connector
config_db = ValidatedConfigDBConnector(ctx.obj['config_db'])
Expand Down Expand Up @@ -4581,9 +4587,14 @@ def add(ctx, interface_name, ip_addr, gw):
ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan/Loopback]")

if table_name == "VLAN_INTERFACE":
if not validate_vlan_format(interface_name):
ctx.fail(f"Error: {interface_name} is not a valid Vlan name")
return
if ignore_vlan:
if not validate_vlan_format(interface_name):
ctx.fail(f"Error: {interface_name} is not a valid Vlan name")
return
else:
if not validate_vlan_exists(config_db, interface_name):
ctx.fail(f"Error: {interface_name} does not exist")
return

interface_entry = config_db.get_entry(table_name, interface_name)
if len(interface_entry) == 0:
Expand Down
23 changes: 16 additions & 7 deletions tests/ip_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import utilities_common.bgp_util as bgp_util

ERROR_MSG = "Error: IP address is not valid"
VLAN_ERROR_MSG = "not a valid Vlan name"
ILLEGAL_VLAN_ERROR_MSG = "not a valid Vlan name"
NOT_EXIST_VLAN_ERROR_MSG ="does not exist"

INVALID_VRF_MSG ="""\
Usage: bind [OPTIONS] <interface_name> <vrf_name>
Expand Down Expand Up @@ -49,33 +50,41 @@ def test_add_vlan_interface_ipv4(self):
runner = CliRunner()
obj = {'config_db':db.cfgdb}

# config int ip add Vlan100 1.1.1.1/24
# config int ip add Vlan100 1.1.1.1/24
result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], ["Vlan100", "1.1.1.1/24"], obj=obj)
print(result.exit_code, result.output)
assert result.exit_code != 0
assert NOT_EXIST_VLAN_ERROR_MSG in result.output

# config int ip add Vlan101 1.1.1.1/24 --ignore-vlan
result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], [ "Vlan101", "1.1.1.1/24" , "--ignore-vlan"], obj=obj)
print(result.exit_code, result.output)
assert result.exit_code == 0

# create vlan 4093
result = runner.invoke(config.config.commands["vlan"].commands["add"], ["4093"], obj=db)
# config int ip add Vlan4093 1.1.1.1/24
result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], ["Vlan4093", "1.1.1.1/24"], obj=obj)
print(result.exit_code, result.output)
assert result.exit_code == 0

# config int ip add Vlan000000000000003 1.1.1.1/24
result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], ["Vlan000000000000003", "1.1.1.1/24"], obj=obj)
result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], ["Vlan000000000000003", "1.1.1.1/24" , "--ignore-vlan"], obj=obj)
print(result.exit_code, result.output)
assert result.exit_code != 0
assert VLAN_ERROR_MSG in result.output
assert ILLEGAL_VLAN_ERROR_MSG in result.output

# config int ip add Vlan01 1.1.1.1/24
result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], ["Vlan01", "1.1.1.1/24"], obj=obj)
print(result.exit_code, result.output)
assert result.exit_code != 0
assert VLAN_ERROR_MSG in result.output
assert NOT_EXIST_VLAN_ERROR_MSG in result.output

# config int ip add Vlan1.2 1.1.1.1/24
result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], ["Vlan1.2", "1.1.1.1/24"], obj=obj)
result = runner.invoke(config.config.commands["interface"].commands["ip"].commands["add"], ["Vlan1.2", "1.1.1.1/24", "--ignore-vlan"], obj=obj)
print(result.exit_code, result.output)
assert result.exit_code != 0
assert VLAN_ERROR_MSG in result.output
assert ILLEGAL_VLAN_ERROR_MSG in result.output


def test_add_del_interface_valid_ipv4(self):
Expand Down

0 comments on commit 041f3ec

Please sign in to comment.