Skip to content

Commit 263810b

Browse files
authored
Update vrf add, del commands for duplicate/non-existing VRFs (sonic-net#2467)
*[VRF] Update vrf add, del commands for duplicate/non-existing VRFs sonic-net#2467 *What I did Throw an error in case user wants to add a duplicate VRF. Throw an error if user wants to delete a VRF that does not exist. Update is_vrf_exists function to use vrf_name == management as a valid vrf name as well. Update grammar for vrf_name is invalid message. Renamed tests/show_vrf_test.py -> tests/vrf_test.py to correctly represent the file contents. Add test cases for the added/modified lines.
1 parent addae73 commit 263810b

File tree

2 files changed

+56
-22
lines changed

2 files changed

+56
-22
lines changed

config/main.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ def is_vrf_exists(config_db, vrf_name):
379379
keys = config_db.get_keys("VRF")
380380
if vrf_name in keys:
381381
return True
382-
elif vrf_name == "mgmt":
382+
elif vrf_name == "mgmt" or vrf_name == "management":
383383
entry = config_db.get_entry("MGMT_VRF_CONFIG", "vrf_global")
384384
if entry and entry.get("mgmtVrfEnabled") == "true":
385385
return True
@@ -5213,10 +5213,12 @@ def add_vrf(ctx, vrf_name):
52135213
"""Add vrf"""
52145214
config_db = ctx.obj['config_db']
52155215
if not vrf_name.startswith("Vrf") and not (vrf_name == 'mgmt') and not (vrf_name == 'management'):
5216-
ctx.fail("'vrf_name' is not start with Vrf, mgmt or management!")
5216+
ctx.fail("'vrf_name' must begin with 'Vrf' or named 'mgmt'/'management' in case of ManagementVRF.")
52175217
if len(vrf_name) > 15:
52185218
ctx.fail("'vrf_name' is too long!")
5219-
if (vrf_name == 'mgmt' or vrf_name == 'management'):
5219+
if is_vrf_exists(config_db, vrf_name):
5220+
ctx.fail("VRF {} already exists!".format(vrf_name))
5221+
elif (vrf_name == 'mgmt' or vrf_name == 'management'):
52205222
vrf_add_management_vrf(config_db)
52215223
else:
52225224
config_db.set_entry('VRF', vrf_name, {"NULL": "NULL"})
@@ -5228,7 +5230,7 @@ def del_vrf(ctx, vrf_name):
52285230
"""Del vrf"""
52295231
config_db = ctx.obj['config_db']
52305232
if not vrf_name.startswith("Vrf") and not (vrf_name == 'mgmt') and not (vrf_name == 'management'):
5231-
ctx.fail("'vrf_name' is not start with Vrf, mgmt or management!")
5233+
ctx.fail("'vrf_name' must begin with 'Vrf' or named 'mgmt'/'management' in case of ManagementVRF.")
52325234
if len(vrf_name) > 15:
52335235
ctx.fail("'vrf_name' is too long!")
52345236
syslog_table = config_db.get_table("SYSLOG_SERVER")
@@ -5237,7 +5239,9 @@ def del_vrf(ctx, vrf_name):
52375239
syslog_vrf = syslog_data.get("vrf")
52385240
if syslog_vrf == syslog_vrf_dev:
52395241
ctx.fail("Failed to remove VRF device: {} is in use by SYSLOG_SERVER|{}".format(syslog_vrf, syslog_entry))
5240-
if (vrf_name == 'mgmt' or vrf_name == 'management'):
5242+
if not is_vrf_exists(config_db, vrf_name):
5243+
ctx.fail("VRF {} does not exist!".format(vrf_name))
5244+
elif (vrf_name == 'mgmt' or vrf_name == 'management'):
52415245
vrf_delete_management_vrf(config_db)
52425246
else:
52435247
del_interface_bind_to_vrf(config_db, vrf_name)

tests/show_vrf_test.py renamed to tests/vrf_test.py

+47-17
Original file line numberDiff line numberDiff line change
@@ -180,31 +180,61 @@ def test_vrf_bind_unbind(self):
180180
assert result.exit_code == 0
181181
assert result.output == expected_output
182182

183-
def test_vrf_del(self):
183+
def test_vrf_add_del(self):
184184
runner = CliRunner()
185185
db = Db()
186186
vrf_obj = {'config_db':db.cfgdb, 'namespace':db.db.namespace}
187187

188+
result = runner.invoke(config.config.commands["vrf"].commands["add"], ["Vrf100"], obj=vrf_obj)
189+
assert ('Vrf100') in db.cfgdb.get_table('VRF')
190+
assert result.exit_code == 0
191+
192+
result = runner.invoke(config.config.commands["vrf"].commands["add"], ["Vrf1"], obj=vrf_obj)
193+
assert "VRF Vrf1 already exists!" in result.output
194+
assert ('Vrf1') in db.cfgdb.get_table('VRF')
195+
assert result.exit_code != 0
196+
188197
expected_output_del = "VRF Vrf1 deleted and all associated IP addresses removed.\n"
189198
result = runner.invoke(config.config.commands["vrf"].commands["del"], ["Vrf1"], obj=vrf_obj)
190199
assert result.exit_code == 0
191200
assert result.output == expected_output_del
192201
assert ('Vrf1') not in db.cfgdb.get_table('VRF')
193202

194-
expected_output_del = "VRF Vrf101 deleted and all associated IP addresses removed.\n"
195-
result = runner.invoke(config.config.commands["vrf"].commands["del"], ["Vrf101"], obj=vrf_obj)
196-
assert result.exit_code == 0
197-
assert result.output == expected_output_del
198-
assert ('Vrf101') not in db.cfgdb.get_table('VRF')
199-
200-
expected_output_del = "VRF Vrf102 deleted and all associated IP addresses removed.\n"
201-
result = runner.invoke(config.config.commands["vrf"].commands["del"], ["Vrf102"], obj=vrf_obj)
202-
assert result.exit_code == 0
203-
assert result.output == expected_output_del
204-
assert ('Vrf102') not in db.cfgdb.get_table('VRF')
203+
result = runner.invoke(config.config.commands["vrf"].commands["del"], ["Vrf200"], obj=vrf_obj)
204+
assert result.exit_code != 0
205+
assert ('Vrf200') not in db.cfgdb.get_table('VRF')
206+
assert "VRF Vrf200 does not exist!" in result.output
205207

206-
expected_output_del = "VRF Vrf103 deleted and all associated IP addresses removed.\n"
207-
result = runner.invoke(config.config.commands["vrf"].commands["del"], ["Vrf103"], obj=vrf_obj)
208-
assert result.exit_code == 0
209-
assert result.output == expected_output_del
210-
assert ('Vrf103') not in db.cfgdb.get_table('VRF')
208+
def test_invalid_vrf_name(self):
209+
db = Db()
210+
runner = CliRunner()
211+
obj = {'config_db':db.cfgdb}
212+
expected_output = """\
213+
Error: 'vrf_name' must begin with 'Vrf' or named 'mgmt'/'management' in case of ManagementVRF.
214+
"""
215+
result = runner.invoke(config.config.commands["vrf"].commands["add"], ["vrf-blue"], obj=obj)
216+
assert result.exit_code != 0
217+
assert ('vrf-blue') not in db.cfgdb.get_table('VRF')
218+
assert expected_output in result.output
219+
220+
result = runner.invoke(config.config.commands["vrf"].commands["add"], ["VRF2"], obj=obj)
221+
assert result.exit_code != 0
222+
assert ('VRF2') not in db.cfgdb.get_table('VRF')
223+
assert expected_output in result.output
224+
225+
result = runner.invoke(config.config.commands["vrf"].commands["add"], ["VrF10"], obj=obj)
226+
assert result.exit_code != 0
227+
assert ('VrF10') not in db.cfgdb.get_table('VRF')
228+
assert expected_output in result.output
229+
230+
result = runner.invoke(config.config.commands["vrf"].commands["del"], ["vrf-blue"], obj=obj)
231+
assert result.exit_code != 0
232+
assert expected_output in result.output
233+
234+
result = runner.invoke(config.config.commands["vrf"].commands["del"], ["VRF2"], obj=obj)
235+
assert result.exit_code != 0
236+
assert expected_output in result.output
237+
238+
result = runner.invoke(config.config.commands["vrf"].commands["del"], ["VrF10"], obj=obj)
239+
assert result.exit_code != 0
240+
assert expected_output in result.output

0 commit comments

Comments
 (0)