Skip to content

Commit

Permalink
Validations checks while creating and deleting a Portchannel (sonic-n…
Browse files Browse the repository at this point in the history
…et#1326)

Added below validation checks when a portchannel is created by the user:
1 if a given portchannel name is valid or not
2 if a given portchannel name is already created by user (exists in CONFIG_DB)

Added below validation checks when a portchannel is attempted for deletion by the user:
1 if a given portchannel name is valid or not
2 if a given portchannel name exists in exists in CONFIG_DB or not (throw an error if it does not exist)

Signed-off-by: Akhilesh Samineni <akhilesh.samineni@broadcom.com>
  • Loading branch information
AkhileshSamineni authored Jan 20, 2021
1 parent 3df267e commit 8119ba2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
17 changes: 17 additions & 0 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1356,7 +1356,15 @@ def portchannel(ctx, namespace):
@click.pass_context
def add_portchannel(ctx, portchannel_name, min_links, fallback):
"""Add port channel"""
if is_portchannel_name_valid(portchannel_name) != True:
ctx.fail("{} is invalid!, name should have prefix '{}' and suffix '{}'"
.format(portchannel_name, CFG_PORTCHANNEL_PREFIX, CFG_PORTCHANNEL_NO))

db = ctx.obj['db']

if is_portchannel_present_in_db(db, portchannel_name):
ctx.fail("{} already exists!".format(portchannel_name))

fvs = {'admin_status': 'up',
'mtu': '9100'}
if min_links != 0:
Expand All @@ -1370,7 +1378,16 @@ def add_portchannel(ctx, portchannel_name, min_links, fallback):
@click.pass_context
def remove_portchannel(ctx, portchannel_name):
"""Remove port channel"""
if is_portchannel_name_valid(portchannel_name) != True:
ctx.fail("{} is invalid!, name should have prefix '{}' and suffix '{}'"
.format(portchannel_name, CFG_PORTCHANNEL_PREFIX, CFG_PORTCHANNEL_NO))

db = ctx.obj['db']

# Dont proceed if the port channel does not exist
if is_portchannel_present_in_db(db, portchannel_name) is False:
ctx.fail("{} is not present.".format(portchannel_name))

if len([(k, v) for k, v in db.get_table('PORTCHANNEL_MEMBER') if k == portchannel_name]) != 0:
click.echo("Error: Portchannel {} contains members. Remove members before deleting Portchannel!".format(portchannel_name))
else:
Expand Down
48 changes: 48 additions & 0 deletions tests/portchannel_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,54 @@ def setup_class(cls):
os.environ['UTILITIES_UNIT_TESTING'] = "1"
print("SETUP")

def test_add_portchannel_with_invalid_name(self):
runner = CliRunner()
db = Db()
obj = {'db':db.cfgdb}

# add a portchannel with invalid name
result = runner.invoke(config.config.commands["portchannel"].commands["add"], ["PortChan005"], obj=obj)
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "Error: PortChan005 is invalid!, name should have prefix 'PortChannel' and suffix '<0-9999>'" in result.output

def test_delete_portchannel_with_invalid_name(self):
runner = CliRunner()
db = Db()
obj = {'db':db.cfgdb}

# delete a portchannel with invalid name
result = runner.invoke(config.config.commands["portchannel"].commands["del"], ["PortChan005"], obj=obj)
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "Error: PortChan005 is invalid!, name should have prefix 'PortChannel' and suffix '<0-9999>'" in result.output

def test_add_existing_portchannel_again(self):
runner = CliRunner()
db = Db()
obj = {'db':db.cfgdb}

# add a portchannel which is already created
result = runner.invoke(config.config.commands["portchannel"].commands["add"], ["PortChannel0001"], obj=obj)
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "Error: PortChannel0001 already exists!" in result.output

def test_delete_non_existing_portchannel(self):
runner = CliRunner()
db = Db()
obj = {'db':db.cfgdb}

# delete a portchannel which is not present
result = runner.invoke(config.config.commands["portchannel"].commands["del"], ["PortChannel0005"], obj=obj)
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "Error: PortChannel0005 is not present." in result.output

def test_add_portchannel_member_with_invalid_name(self):
runner = CliRunner()
db = Db()
Expand Down

0 comments on commit 8119ba2

Please sign in to comment.