Skip to content

Commit

Permalink
added more tests for vlan range and fdb config commands
Browse files Browse the repository at this point in the history
added more tests for vlan range and fdb config commands
  • Loading branch information
anilkpandey committed Dec 17, 2020
1 parent 2a9ab88 commit a8c651e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 21 deletions.
38 changes: 17 additions & 21 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1954,60 +1954,56 @@ def mac_address_is_valid(mac):
@click.argument('mac', metavar='<mac-address as xx:xx:xx:xx:xx:xx>', required=True)
@click.argument('vlan', metavar='<vlan>', required=True, type=int)
@click.argument('interface_name', metavar='<interface name>', required=True)
@click.pass_context
def add_mac(ctx, mac, vlan, interface_name):
db = ctx.obj['db']

@clicommon.pass_db
def add_mac(db, mac, vlan, interface_name):
mac_valid = bool(mac_address_is_valid(mac))
if mac_valid == False:
ctx.fail("Incorrect mac-address format!!")
click.echo("Incorrect mac-address format!!")

mac_valid = not bool(re.match('^' + '[\:\-]'.join(['([00]{2})']*6) + '$', mac.lower()))
if mac_valid == False:
ctx.fail("Invalid (Zero) mac-address!!")
click.echo("Invalid (Zero) mac-address!!")

mac_valid = not bool(re.match('^' + '[\:\-]'.join(['([ff]{2})']*6) + '$', mac.lower()))
if mac_valid == False:
ctx.fail("Invalid (Bcast) mac-address!!")
click.echo("Invalid (Bcast) mac-address!!")

mac_is_multicast = int(mac[:2]) & 1;
if mac_is_multicast == True:
ctx.fail("Invalid (Multicast) mac-address!!")
click.echo("Invalid (Multicast) mac-address!!")

vlan_valid = bool(vlan_id_is_valid(vlan))
if vlan_valid == False:
ctx.fail("Invalid VlanId!!")
click.echo("Invalid VlanId!!")

vlan_name = 'Vlan{}'.format(vlan)

if clicommon.get_interface_naming_mode() == "alias":
interface_name = interface_alias_to_name(db, interface_name)
interface_name = interface_alias_to_name(db.cfgdb, interface_name)
if interface_name is None:
ctx.fail("'interface_name' is None!")
click.echo("'interface_name' is None!")

if interface_name_is_valid(db, interface_name) is False:
ctx.fail("Interface name is invalid!!")
if interface_name_is_valid(db.cfgdb, interface_name) is False:
click.echo("Interface name is invalid!!")

db.set_entry('FDB', (vlan_name, mac), {'port': interface_name })
db.cfgdb.set_entry('FDB', (vlan_name, mac), {'port': interface_name })

@mac.command('del')
@click.argument('mac', metavar='<mac-address as xx:xx:xx:xx:xx:xx>', required=True)
@click.argument('vlan', metavar='<vlan>', required=True, type=int)
@click.pass_context
def del_mac(ctx, mac, vlan):
db = ctx.obj['db']

@clicommon.pass_db
def del_mac(db, mac, vlan):
mac_valid = bool(mac_address_is_valid(mac))
if mac_valid == False:
ctx.fail("Incorrect mac-address format!!")
click.echo("Incorrect mac-address format!!")

vlan_valid = bool(vlan_id_is_valid(vlan))
if vlan_valid == False:
ctx.fail("Invalid VlanId!!")
click.echo("Invalid VlanId!!")

vlan_name = 'Vlan{}'.format(vlan)

db.set_entry('FDB', (vlan_name, mac), None)
db.cfgdb.set_entry('FDB', (vlan_name, mac), None)

#
# 'bgp' group ('config bgp ...')
Expand Down
22 changes: 22 additions & 0 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,28 @@ def count():
output += ('%s \n' % (str(fdb_count)))
click.echo(output)

@mac.command()
@clicommon.pass_db
def config(db):
data = db.cfgdb.get_table('FDB')
keys = list(data.keys())

def tablelize(keys, data):
table = []

for k in natsorted(keys):
entry = db.cfgdb.get_entry('FDB', k)
r = []
r.append(k[0])
r.append(k[1])
r.append(data[k]['port'])
table.append(r)

return table

header = ['Vlan', 'MAC', 'Port']
click.echo(tabulate(tablelize(keys, data), header))

#
# 'show route-map' command ("show route-map")
#
Expand Down
35 changes: 35 additions & 0 deletions tests/fdb_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@
"""

show_mac_config_add="""\
Vlan MAC Port
------- ----------------- ---------
Vlan100 00:00:00:00:00:01 Ethernet4
"""

show_mac_config_del="""\
Vlan MAC Port
------ ----- ------
"""

class TestFdb(object):
@classmethod
def setup_class(cls):
Expand All @@ -32,6 +43,30 @@ def test_fdb_aging_time(self):
assert result.exit_code == 0
assert result.output == show_mac_aging_time

def test_fdb_mac_add(self):
runner = CliRunner()
db = Db()
result = runner.invoke(config.config.commands["vlan"].commands["add"], ["100"], obj=db)
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["add"], ["100", "Ethernet4"], obj=db)
result = runner.invoke(config.config.commands["mac"].commands["add"], ["00:00:00:00:00:01", "100", "Ethernet4"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
result = runner.invoke(show.cli.commands["mac"].commands["config"], [], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == show_mac_config_add
result = runner.invoke(config.config.commands["mac"].commands["del"], ["00:00:00:00:00:01", "100"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
result = runner.invoke(show.cli.commands["mac"].commands["config"], [], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == show_mac_config_del

@classmethod
def teardown_class(cls):
os.environ['UTILITIES_UNIT_TESTING'] = "0"
Expand Down
13 changes: 13 additions & 0 deletions tests/vlan_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,19 @@ def test_vlan_config_range(self):
print(result.output)
assert result.exit_code == 0
assert result.output == show_vlan_config_output_range
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["range"].commands["del"], ["3001", "3003", "Ethernet4"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
result = runner.invoke(config.config.commands["vlan"].commands["member"].commands["range"].commands["del"], ["3001", "3003", "Ethernet8"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
result = runner.invoke(show.cli.commands["vlan"].commands["config"], [], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code == 0
assert result.output == show_vlan_config_output

def test_config_vlan_remove_nonexist_dhcp_relay_dest(self):
runner = CliRunner()
Expand Down

0 comments on commit a8c651e

Please sign in to comment.