Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IP remove warnings for VRF commands #2351

Merged
merged 4 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5017,6 +5017,7 @@ def bind(ctx, interface_name, vrf_name):
else:
config_db.set_entry(table_name, interface_name, {"vrf_name": vrf_name})

click.echo("Interface {} IP disabled and address(es) removed due to binding VRF {}.".format(interface_name, vrf_name))
#
# 'unbind' subcommand
#
Expand Down Expand Up @@ -5052,7 +5053,8 @@ def unbind(ctx, interface_name):
config_db.set_entry(table_name, interface_name, subintf_entry)
else:
config_db.set_entry(table_name, interface_name, None)


click.echo("Interface {} IP disabled and address(es) removed due to unbinding VRF.".format(interface_name))
#
# 'ipv6' subgroup ('config interface ipv6 ...')
#
Expand Down Expand Up @@ -5226,7 +5228,7 @@ def del_vrf(ctx, vrf_name):
else:
del_interface_bind_to_vrf(config_db, vrf_name)
config_db.set_entry('VRF', vrf_name, None)

click.echo("VRF {} deleted and all associated IP addresses removed.".format(vrf_name))

@vrf.command('add_vrf_vni_map')
@click.argument('vrfname', metavar='<vrf-name>', required=True, type=str)
Expand Down
92 changes: 88 additions & 4 deletions tests/show_vrf_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,44 +66,99 @@ def test_vrf_bind_unbind(self):

obj = {'config_db':db.cfgdb}

expected_output_unbind = "Interface Ethernet4 IP disabled and address(es) removed due to unbinding VRF.\n"
result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["unbind"], ["Ethernet4"], obj=obj)
print(result.exit_code, result.output)
assert result.exit_code == 0
assert 'Ethernet4' not in db.cfgdb.get_table('INTERFACE')
assert result.output == expected_output_unbind

expected_output_unbind = "Interface Loopback0 IP disabled and address(es) removed due to unbinding VRF.\n"
result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["unbind"], ["Loopback0"], obj=obj)
print(result.exit_code, result.output)
assert result.exit_code == 0
assert 'Loopback0' not in db.cfgdb.get_table('LOOPBACK_INTERFACE')

assert result.output == expected_output_unbind

expected_output_unbind = "Interface Vlan40 IP disabled and address(es) removed due to unbinding VRF.\n"
result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["unbind"], ["Vlan40"], obj=obj)
print(result.exit_code, result.output)
assert result.exit_code == 0
assert 'Vlan40' not in db.cfgdb.get_table('VLAN_INTERFACE')

assert result.output == expected_output_unbind

expected_output_unbind = "Interface PortChannel0002 IP disabled and address(es) removed due to unbinding VRF.\n"
result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["unbind"], ["PortChannel0002"], obj=obj)
print(result.exit_code, result.output)
assert result.exit_code == 0
assert 'PortChannel002' not in db.cfgdb.get_table('PORTCHANNEL_INTERFACE')
assert result.output == expected_output_unbind

expected_output_unbind = "Interface Eth36.10 IP disabled and address(es) removed due to unbinding VRF.\n"
result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["unbind"], ["Eth36.10"], obj=obj)
print(result.exit_code, result.output)
assert result.exit_code == 0
assert ('vrf_name', 'Vrf102') not in db.cfgdb.get_table('VLAN_SUB_INTERFACE')['Eth36.10']
assert result.output == expected_output_unbind

expected_output_unbind = "Interface Ethernet0.10 IP disabled and address(es) removed due to unbinding VRF.\n"
result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["unbind"], ["Ethernet0.10"], obj=obj)
print(result.exit_code, result.output)
assert result.exit_code == 0
assert ('vrf_name', 'Vrf101') not in db.cfgdb.get_table('VLAN_SUB_INTERFACE')['Ethernet0.10']
assert result.output == expected_output_unbind

expected_output_unbind = "Interface Po0002.101 IP disabled and address(es) removed due to unbinding VRF.\n"
result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["unbind"], ["Po0002.101"], obj=obj)
print(result.exit_code, result.output)
assert result.exit_code == 0
assert ('vrf_name', 'Vrf103') not in db.cfgdb.get_table('VLAN_SUB_INTERFACE')['Po0002.101']
assert result.output == expected_output_unbind

vrf_obj = {'config_db':db.cfgdb, 'namespace':db.db.namespace}

expected_output_bind = "Interface Ethernet0 IP disabled and address(es) removed due to binding VRF Vrf1.\n"
result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["bind"], ["Ethernet0", "Vrf1"], obj=vrf_obj)
assert result.exit_code == 0
assert result.output == expected_output_bind
assert ('Vrf1') in db.cfgdb.get_table('INTERFACE')['Ethernet0']['vrf_name']

expected_output_bind = "Interface Loopback0 IP disabled and address(es) removed due to binding VRF Vrf101.\n"
result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["bind"], ["Loopback0", "Vrf101"], obj=vrf_obj)
assert result.exit_code == 0
assert result.output == expected_output_bind
assert ('Vrf101') in db.cfgdb.get_table('LOOPBACK_INTERFACE')['Loopback0']['vrf_name']

expected_output_bind = "Interface Vlan40 IP disabled and address(es) removed due to binding VRF Vrf101.\n"
result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["bind"], ["Vlan40", "Vrf101"], obj=vrf_obj)
assert result.exit_code == 0
assert result.output == expected_output_bind
assert ('Vrf101') in db.cfgdb.get_table('VLAN_INTERFACE')['Vlan40']['vrf_name']

expected_output_bind = "Interface PortChannel0002 IP disabled and address(es) removed due to binding VRF Vrf101.\n"
result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["bind"], ["PortChannel0002", "Vrf101"], obj=vrf_obj)
assert result.exit_code == 0
assert result.output == expected_output_bind
assert ('Vrf101') in db.cfgdb.get_table('PORTCHANNEL_INTERFACE')['PortChannel0002']['vrf_name']

expected_output_bind = "Interface Eth36.10 IP disabled and address(es) removed due to binding VRF Vrf102.\n"
result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["bind"], ["Eth36.10", "Vrf102"], obj=vrf_obj)
assert result.exit_code == 0
assert result.output == expected_output_bind
assert ('Vrf102') in db.cfgdb.get_table('VLAN_SUB_INTERFACE')['Eth36.10']['vrf_name']

expected_output_bind = "Interface Ethernet0.10 IP disabled and address(es) removed due to binding VRF Vrf103.\n"
result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["bind"], ["Ethernet0.10", "Vrf103"], obj=vrf_obj)
assert result.exit_code == 0
assert result.output == expected_output_bind
assert ('Vrf103') in db.cfgdb.get_table('VLAN_SUB_INTERFACE')['Ethernet0.10']['vrf_name']

expected_output_bind = "Interface Po0002.101 IP disabled and address(es) removed due to binding VRF Vrf1.\n"
result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["bind"], ["Po0002.101", "Vrf1"], obj=vrf_obj)
assert result.exit_code == 0
assert result.output == expected_output_bind
assert ('Vrf1') in db.cfgdb.get_table('VLAN_SUB_INTERFACE')['Po0002.101']['vrf_name']

#Bind click CLI cannot be tested as it tries to connecte to statedb
#for verification of all IP address delete before applying new vrf configuration
jsonfile_config = os.path.join(mock_db_path, "config_db")
dbconnector.dedicated_dbs['CONFIG_DB'] = jsonfile_config

Expand All @@ -124,3 +179,32 @@ def test_vrf_bind_unbind(self):
dbconnector.dedicated_dbs = {}
assert result.exit_code == 0
assert result.output == expected_output

def test_vrf_del(self):
runner = CliRunner()
db = Db()
vrf_obj = {'config_db':db.cfgdb, 'namespace':db.db.namespace}

expected_output_del = "VRF Vrf1 deleted and all associated IP addresses removed.\n"
result = runner.invoke(config.config.commands["vrf"].commands["del"], ["Vrf1"], obj=vrf_obj)
assert result.exit_code == 0
assert result.output == expected_output_del
assert ('Vrf1') not in db.cfgdb.get_table('VRF')

expected_output_del = "VRF Vrf101 deleted and all associated IP addresses removed.\n"
result = runner.invoke(config.config.commands["vrf"].commands["del"], ["Vrf101"], obj=vrf_obj)
assert result.exit_code == 0
assert result.output == expected_output_del
assert ('Vrf101') not in db.cfgdb.get_table('VRF')

expected_output_del = "VRF Vrf102 deleted and all associated IP addresses removed.\n"
result = runner.invoke(config.config.commands["vrf"].commands["del"], ["Vrf102"], obj=vrf_obj)
assert result.exit_code == 0
assert result.output == expected_output_del
assert ('Vrf102') not in db.cfgdb.get_table('VRF')

expected_output_del = "VRF Vrf103 deleted and all associated IP addresses removed.\n"
result = runner.invoke(config.config.commands["vrf"].commands["del"], ["Vrf103"], obj=vrf_obj)
assert result.exit_code == 0
assert result.output == expected_output_del
assert ('Vrf103') not in db.cfgdb.get_table('VRF')
12 changes: 12 additions & 0 deletions tests/subintf_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,18 @@ def test_subintf_vrf_bind_unbind(self):
assert ('Ethernet0.102') in db.cfgdb.get_table('VLAN_SUB_INTERFACE')
assert db.cfgdb.get_table('VLAN_SUB_INTERFACE')['Ethernet0.102']['admin_status'] == 'up'

expected_output_bind = "Interface Ethernet0.102 IP disabled and address(es) removed due to binding VRF Vrf1.\n"
vrf_obj = {'config_db':db.cfgdb, 'namespace':db.db.namespace}
result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["bind"], ["Ethernet0.102", "Vrf1"], obj=vrf_obj)
assert result.exit_code == 0
assert ('Vrf1') in db.cfgdb.get_table('VLAN_SUB_INTERFACE')['Ethernet0.102']['vrf_name']
assert result.output == expected_output_bind

expected_output_unbind = "Interface Ethernet0.102 IP disabled and address(es) removed due to unbinding VRF.\n"
result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["unbind"], ["Ethernet0.102"], obj=vrf_obj)
assert result.exit_code == 0
assert ('vrf_name') not in db.cfgdb.get_table('VLAN_SUB_INTERFACE')['Ethernet0.102']
assert result.output == expected_output_unbind

result = runner.invoke(config.config.commands["subinterface"].commands["del"], ["Ethernet0.102"], obj=obj)
print(result.exit_code, result.output)
Expand All @@ -193,13 +197,17 @@ def test_subintf_vrf_bind_unbind(self):
assert result.exit_code == 0
assert ('Eth0.1002') in db.cfgdb.get_table('VLAN_SUB_INTERFACE')

expected_output_bind = "Interface Eth0.1002 IP disabled and address(es) removed due to binding VRF Vrf1.\n"
result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["bind"], ["Eth0.1002", "Vrf1"], obj=vrf_obj)
assert result.exit_code == 0
assert ('Vrf1') in db.cfgdb.get_table('VLAN_SUB_INTERFACE')['Eth0.1002']['vrf_name']
assert result.output == expected_output_bind

expected_output_unbind = "Interface Eth0.1002 IP disabled and address(es) removed due to unbinding VRF.\n"
result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["unbind"], ["Eth0.1002"], obj=vrf_obj)
assert result.exit_code == 0
assert ('vrf_name') not in db.cfgdb.get_table('VLAN_SUB_INTERFACE')['Eth0.1002']
assert result.output == expected_output_unbind

result = runner.invoke(config.config.commands["subinterface"].commands["del"], ["Eth0.1002"], obj=obj)
print(result.exit_code, result.output)
Expand All @@ -212,13 +220,17 @@ def test_subintf_vrf_bind_unbind(self):
assert result.exit_code == 0
assert ('Po0004.1004') in db.cfgdb.get_table('VLAN_SUB_INTERFACE')

expected_output_bind = "Interface Po0004.1004 IP disabled and address(es) removed due to binding VRF Vrf1.\n"
result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["bind"], ["Po0004.1004", "Vrf1"], obj=vrf_obj)
assert result.exit_code == 0
assert ('Vrf1') in db.cfgdb.get_table('VLAN_SUB_INTERFACE')['Po0004.1004']['vrf_name']
assert result.output == expected_output_bind

expected_output_unbind = "Interface Po0004.1004 IP disabled and address(es) removed due to unbinding VRF.\n"
result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["unbind"], ["Po0004.1004"], obj=vrf_obj)
assert result.exit_code == 0
assert ('vrf_name') not in db.cfgdb.get_table('VLAN_SUB_INTERFACE')['Po0004.1004']
assert result.output == expected_output_unbind

result = runner.invoke(config.config.commands["subinterface"].commands["del"], ["Po0004.1004"], obj=obj)
print(result.exit_code, result.output)
Expand Down