Skip to content

Commit

Permalink
[202205]Fixes #12170: Delete subinterface and recreate the subinterfa…
Browse files Browse the repository at this point in the history
…ce in default-vrf (#2564)

* Fixes #12170: Delete subinterface and recreate the subinterface  in default-vrf while unbinding subinterface from user defined vrf.
  • Loading branch information
dgsudharsan authored Dec 20, 2022
1 parent 93172c4 commit 67cbb15
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
16 changes: 16 additions & 0 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4956,6 +4956,22 @@ def unbind(ctx, interface_name):
for ipaddress in interface_ipaddresses:
remove_router_interface_ip_address(config_db, interface_name, ipaddress)
if table_name == "VLAN_SUB_INTERFACE":
# First delete subinterface, once subinterface deletion successful,
# recreate same with same config on default vrf
if 'state_db' not in ctx.obj:
if ctx.obj['namespace'] is DEFAULT_NAMESPACE:
state_db = SonicV2Connector(use_unix_socket_path=True)
else:
state_db = SonicV2Connector(use_unix_socket_path=True, namespace=ctx.obj['namespace'])
state_db.connect(state_db.STATE_DB, False)
else:
state_db = ctx.obj['state_db']

config_db.set_entry(table_name, interface_name, None)
_hash = '{}{}'.format('INTERFACE_TABLE|', interface_name)
while state_db.exists(state_db.STATE_DB, _hash):
time.sleep(0.01)
state_db.close(state_db.STATE_DB)
config_db.set_entry(table_name, interface_name, subintf_entry)
else:
config_db.set_entry(table_name, interface_name, None)
Expand Down
53 changes: 43 additions & 10 deletions tests/show_vrf_test.py → tests/vrf_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

import config.main as config
import show.main as show
import threading

DEFAULT_NAMESPACE = ''
test_path = os.path.dirname(os.path.abspath(__file__))
mock_db_path = os.path.join(test_path, "vrf_input")

Expand All @@ -16,6 +18,11 @@ def setup_class(cls):
print("SETUP")
os.environ["UTILITIES_UNIT_TESTING"] = "1"

def update_statedb(self, db, db_name, key):
import time
time.sleep(0.5)
db.delete(db_name, key)

def test_vrf_show(self):
from .mock_tables import dbconnector
jsonfile_config = os.path.join(mock_db_path, "config_db")
Expand Down Expand Up @@ -64,43 +71,69 @@ def test_vrf_bind_unbind(self):
assert result.exit_code == 0
assert result.output == expected_output

obj = {'config_db':db.cfgdb}

result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["unbind"], ["Ethernet4"], obj=obj)
vrf_obj = {'config_db':db.cfgdb, 'namespace':db.db.namespace}

result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["unbind"], ["Ethernet4"], obj=vrf_obj)

print(result.exit_code, result.output)
assert result.exit_code == 0
assert 'Ethernet4' not in db.cfgdb.get_table('INTERFACE')

result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["unbind"], ["Loopback0"], obj=obj)
result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["unbind"], ["Loopback0"], obj=vrf_obj)

print(result.exit_code, result.output)
assert result.exit_code == 0
assert 'Loopback0' not in db.cfgdb.get_table('LOOPBACK_INTERFACE')

result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["unbind"], ["Vlan40"], obj=obj)


result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["unbind"], ["Vlan40"], obj=vrf_obj)

print(result.exit_code, result.output)
assert result.exit_code == 0
assert 'Vlan40' not in db.cfgdb.get_table('VLAN_INTERFACE')

result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["unbind"], ["PortChannel0002"], obj=obj)

result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["unbind"], ["PortChannel0002"], obj=vrf_obj)

print(result.exit_code, result.output)
assert result.exit_code == 0
assert 'PortChannel002' not in db.cfgdb.get_table('PORTCHANNEL_INTERFACE')

result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["unbind"], ["Eth36.10"], obj=obj)
vrf_obj = {'config_db':db.cfgdb, 'namespace':DEFAULT_NAMESPACE}
state_db = SonicV2Connector(use_unix_socket_path=True, namespace='')
state_db.connect(state_db.STATE_DB, False)
_hash = "INTERFACE_TABLE|Eth36.10"
state_db.set(db.db.STATE_DB, _hash, "state", "ok")
vrf_obj['state_db'] = state_db

T1 = threading.Thread( target = self.update_statedb, args = (state_db, db.db.STATE_DB, _hash))
T1.start()
result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["unbind"], ["Eth36.10"], obj=vrf_obj)
T1.join()
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']

result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["unbind"], ["Ethernet0.10"], obj=obj)
vrf_obj = {'config_db':db.cfgdb, 'namespace':DEFAULT_NAMESPACE}


result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["unbind"], ["Ethernet0.10"], obj=vrf_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']

result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["unbind"], ["Po0002.101"], obj=obj)

result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["unbind"], ["Po0002.101"], obj=vrf_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']

result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["bind"], ["Ethernet0", "Vrf1"], obj=vrf_obj)
assert result.exit_code == 0
assert ('Vrf1') in db.cfgdb.get_table('INTERFACE')['Ethernet0']['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
Expand Down

0 comments on commit 67cbb15

Please sign in to comment.