Skip to content

Commit

Permalink
[VxLAN]Fix Vxlan delete command to throw error when there are referen…
Browse files Browse the repository at this point in the history
…ces (sonic-net#2404)

*Check for VNET references for vxlan before deleting vxlan object
  • Loading branch information
dgsudharsan authored and EdenGri committed Oct 12, 2022
1 parent 2b4d727 commit d975c36
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
10 changes: 10 additions & 0 deletions config/vxlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def del_vxlan(db, vxlan_name):
"""Del VXLAN"""
ctx = click.get_current_context()

vxlan_keys = db.cfgdb.get_keys('VXLAN_TUNNEL')
if vxlan_name not in vxlan_keys:
ctx.fail("Vxlan tunnel {} does not exist".format(vxlan_name))

vxlan_keys = db.cfgdb.get_keys('VXLAN_EVPN_NVO')
if not vxlan_keys:
vxlan_count = 0
Expand All @@ -56,6 +60,12 @@ def del_vxlan(db, vxlan_name):
if(vxlan_count > 0):
ctx.fail("Please delete all VLAN VNI mappings.")

vnet_table = db.cfgdb.get_table('VNET')
vnet_keys = vnet_table.keys()
for vnet_key in vnet_keys:
if ('vxlan_tunnel' in vnet_table[vnet_key] and vnet_table[vnet_key]['vxlan_tunnel'] == vxlan_name):
ctx.fail("Please delete all VNET configuration referencing the tunnel " + vxlan_name)

db.cfgdb.set_entry('VXLAN_TUNNEL', vxlan_name, None)

@vxlan.group('evpn_nvo')
Expand Down
10 changes: 10 additions & 0 deletions tests/vnet_input/config_db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"VNET|Vnet_2000": {
"peer_list": "",
"vni": "2000",
"vxlan_tunnel": "tunnel1"
},
"VXLAN_TUNNEL|tunnel1": {
"src_ip": "10.10.10.10"
}
}
22 changes: 22 additions & 0 deletions tests/vxlan_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import config.main as config
import show.main as show
from utilities_common.db import Db
from .mock_tables import dbconnector

test_path = os.path.dirname(os.path.abspath(__file__))
mock_db_path = os.path.join(test_path, "vnet_input")

show_vxlan_interface_output="""\
VTEP Information:
Expand Down Expand Up @@ -247,6 +251,24 @@ def test_config_vxlan_add(self):
assert result.exit_code == 0
assert result.output == show_vxlan_vlanvnimap_output

def test_config_vxlan_del(self):
dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'config_db')
db = Db()
runner = CliRunner()

result = runner.invoke(config.config.commands["vxlan"].commands["del"], ["tunnel_invalid"], obj=db)
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "Error: Vxlan tunnel tunnel_invalid does not exist" in result.output

result = runner.invoke(config.config.commands["vxlan"].commands["del"], ["tunnel1"], obj=db)
dbconnector.dedicated_dbs = {}
print(result.exit_code)
print(result.output)
assert result.exit_code != 0
assert "Please delete all VNET configuration referencing the tunnel" in result.output

@classmethod
def teardown_class(cls):
os.environ['UTILITIES_UNIT_TESTING'] = "0"
Expand Down

0 comments on commit d975c36

Please sign in to comment.