Skip to content

Commit e45b47a

Browse files
authored
[VRF]Adding CLI checks to ensure Vrf is valid in interface bind and static route commands (sonic-net#2333)
- What I did Added CLI checks to verify if Vrf is created and valid when used in interface bind and static route commands - How I did it Check for presence of Vrf in VRF table or in case if it is mgmt vrf, verify if mgmt vrf is enabled/ - How to verify it Added UT to verify it.
1 parent f82835e commit e45b47a

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

config/main.py

+21
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,19 @@ def get_interface_ipaddresses(config_db, interface_name):
369369

370370
return ipaddresses
371371

372+
def is_vrf_exists(config_db, vrf_name):
373+
"""Check if VRF exists
374+
"""
375+
keys = config_db.get_keys("VRF")
376+
if vrf_name in keys:
377+
return True
378+
elif vrf_name == "mgmt":
379+
entry = config_db.get_entry("MGMT_VRF_CONFIG", "vrf_global")
380+
if entry and entry.get("mgmtVrfEnabled") == "true":
381+
return True
382+
383+
return False
384+
372385
def is_interface_bind_to_vrf(config_db, interface_name):
373386
"""Get interface if bind to vrf or not
374387
"""
@@ -986,6 +999,7 @@ def cli_sroute_to_config(ctx, command_str, strict_nh = True):
986999
nexthop_str = None
9871000
config_entry = {}
9881001
vrf_name = ""
1002+
config_db = ctx.obj['config_db']
9891003

9901004
if "nexthop" in command_str:
9911005
idx = command_str.index("nexthop")
@@ -998,6 +1012,8 @@ def cli_sroute_to_config(ctx, command_str, strict_nh = True):
9981012
if 'prefix' in prefix_str and 'vrf' in prefix_str:
9991013
# prefix_str: ['prefix', 'vrf', Vrf-name, ip]
10001014
vrf_name = prefix_str[2]
1015+
if not is_vrf_exists(config_db, vrf_name):
1016+
ctx.fail("VRF %s does not exist!"%(vrf_name))
10011017
ip_prefix = prefix_str[3]
10021018
elif 'prefix' in prefix_str:
10031019
# prefix_str: ['prefix', ip]
@@ -1009,6 +1025,8 @@ def cli_sroute_to_config(ctx, command_str, strict_nh = True):
10091025
if 'nexthop' in nexthop_str and 'vrf' in nexthop_str:
10101026
# nexthop_str: ['nexthop', 'vrf', Vrf-name, ip]
10111027
config_entry["nexthop"] = nexthop_str[3]
1028+
if not is_vrf_exists(config_db, nexthop_str[2]):
1029+
ctx.fail("VRF %s does not exist!"%(nexthop_str[2]))
10121030
config_entry["nexthop-vrf"] = nexthop_str[2]
10131031
elif 'nexthop' in nexthop_str and 'dev' in nexthop_str:
10141032
# nexthop_str: ['nexthop', 'dev', ifname]
@@ -4883,6 +4901,9 @@ def bind(ctx, interface_name, vrf_name):
48834901
if interface_name is None:
48844902
ctx.fail("'interface_name' is None!")
48854903

4904+
if not is_vrf_exists(config_db, vrf_name):
4905+
ctx.fail("VRF %s does not exist!"%(vrf_name))
4906+
48864907
table_name = get_interface_table_name(interface_name)
48874908
if table_name == "":
48884909
ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan/Loopback]")

tests/ip_config_test.py

+35
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@
1515

1616
ERROR_MSG = "Error: IP address is not valid"
1717

18+
INVALID_VRF_MSG ="""\
19+
Usage: bind [OPTIONS] <interface_name> <vrf_name>
20+
Try "bind --help" for help.
21+
22+
Error: VRF Vrf2 does not exist!
23+
"""
24+
25+
INVALID_MGMT_VRF_MSG ="""\
26+
Usage: bind [OPTIONS] <interface_name> <vrf_name>
27+
Try "bind --help" for help.
28+
29+
Error: VRF mgmt does not exist!
30+
"""
31+
1832
class TestConfigIP(object):
1933
@classmethod
2034
def setup_class(cls):
@@ -190,6 +204,27 @@ def test_intf_vrf_bind_unbind(self):
190204
print(result.exit_code, result.output)
191205
assert result.exit_code == 0
192206

207+
def test_intf_unknown_vrf_bind(self):
208+
runner = CliRunner()
209+
db = Db()
210+
obj = {'config_db':db.cfgdb, 'namespace':db.db.namespace}
211+
212+
result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["bind"], ["Ethernet64", "Vrf2"], obj=obj)
213+
print(result.exit_code, result.output)
214+
assert result.exit_code != 0
215+
assert result.output == INVALID_VRF_MSG
216+
217+
result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["bind"], ["Ethernet64", "mgmt"], obj=obj)
218+
print(result.exit_code, result.output)
219+
assert result.exit_code != 0
220+
assert result.output == INVALID_MGMT_VRF_MSG
221+
222+
result = runner.invoke(config.config.commands["vrf"].commands["add"], ["mgmt"], obj=obj)
223+
print(result.exit_code, result.output)
224+
result = runner.invoke(config.config.commands["interface"].commands["vrf"].commands["bind"], ["Ethernet64", "mgmt"], obj=obj)
225+
print(result.exit_code, result.output)
226+
assert result.exit_code == 0
227+
193228
@classmethod
194229
def teardown_class(cls):
195230
os.environ['UTILITIES_UNIT_TESTING'] = "0"

tests/static_routes_test.py

+13
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ def test_vrf_static_route(self):
9393
obj = {'config_db':db.cfgdb}
9494

9595
# config route add prefix vrf Vrf-BLUE 2.2.3.4/32 nexthop 30.0.0.6
96+
result = runner.invoke(config.config.commands["vrf"].commands["add"], ["Vrf-BLUE"], obj=obj)
97+
print(result.exit_code, result.output)
9698
result = runner.invoke(config.config.commands["route"].commands["add"], \
9799
["prefix", "vrf", "Vrf-BLUE", "2.2.3.4/32", "nexthop", "30.0.0.6"], obj=obj)
98100
print(result.exit_code, result.output)
@@ -111,9 +113,12 @@ def test_dest_vrf_static_route(self):
111113
obj = {'config_db':db.cfgdb}
112114

113115
# config route add prefix 3.2.3.4/32 nexthop vrf Vrf-RED 30.0.0.6
116+
result = runner.invoke(config.config.commands["vrf"].commands["add"], ["Vrf-RED"], obj=obj)
117+
print(result.exit_code, result.output)
114118
result = runner.invoke(config.config.commands["route"].commands["add"], \
115119
["prefix", "3.2.3.4/32", "nexthop", "vrf", "Vrf-RED", "30.0.0.6"], obj=obj)
116120
print(result.exit_code, result.output)
121+
print(db.cfgdb.get_table('STATIC_ROUTE'))
117122
assert ('3.2.3.4/32') in db.cfgdb.get_table('STATIC_ROUTE')
118123
assert db.cfgdb.get_entry('STATIC_ROUTE', '3.2.3.4/32') == {'nexthop': '30.0.0.6', 'nexthop-vrf': 'Vrf-RED', 'blackhole': 'false', 'distance': '0', 'ifname': ''}
119124

@@ -129,6 +134,8 @@ def test_multiple_nexthops_with_vrf_static_route(self):
129134
obj = {'config_db':db.cfgdb}
130135

131136
''' Add '''
137+
result = runner.invoke(config.config.commands["vrf"].commands["add"], ["Vrf-RED"], obj=obj)
138+
print(result.exit_code, result.output)
132139
# config route add prefix 6.2.3.4/32 nexthop vrf Vrf-RED "30.0.0.6,30.0.0.7"
133140
result = runner.invoke(config.config.commands["route"].commands["add"], \
134141
["prefix", "6.2.3.4/32", "nexthop", "vrf", "Vrf-RED", "30.0.0.6,30.0.0.7"], obj=obj)
@@ -251,13 +258,17 @@ def test_static_route_ECMP_nexthop_with_vrf(self):
251258
obj = {'config_db':db.cfgdb}
252259

253260
''' Add '''
261+
result = runner.invoke(config.config.commands["vrf"].commands["add"], ["Vrf-RED"], obj=obj)
262+
print(result.exit_code, result.output)
254263
# config route add prefix 11.2.3.4/32 nexthop vrf Vrf-RED 30.0.0.5
255264
result = runner.invoke(config.config.commands["route"].commands["add"], \
256265
["prefix", "11.2.3.4/32", "nexthop", "vrf", "Vrf-RED", "30.0.0.5"], obj=obj)
257266
print(result.exit_code, result.output)
258267
assert ('11.2.3.4/32') in db.cfgdb.get_table('STATIC_ROUTE')
259268
assert db.cfgdb.get_entry('STATIC_ROUTE', '11.2.3.4/32') == {'nexthop': '30.0.0.5', 'nexthop-vrf': 'Vrf-RED', 'blackhole': 'false', 'distance': '0', 'ifname': ''}
260269

270+
result = runner.invoke(config.config.commands["vrf"].commands["add"], ["Vrf-BLUE"], obj=obj)
271+
print(result.exit_code, result.output)
261272
# config route add prefix 11.2.3.4/32 nexthop vrf Vrf-BLUE 30.0.0.6
262273
result = runner.invoke(config.config.commands["route"].commands["add"], \
263274
["prefix", "11.2.3.4/32", "nexthop", "vrf", "Vrf-BLUE", "30.0.0.6"], obj=obj)
@@ -292,6 +303,8 @@ def test_static_route_ECMP_mixed_nextfop(self):
292303
assert ('12.2.3.4/32') in db.cfgdb.get_table('STATIC_ROUTE')
293304
assert db.cfgdb.get_entry('STATIC_ROUTE', '12.2.3.4/32') == {'nexthop': '30.0.0.6', 'blackhole': 'false', 'distance': '0', 'ifname': '', 'nexthop-vrf': ''}
294305

306+
result = runner.invoke(config.config.commands["vrf"].commands["add"], ["Vrf-RED"], obj=obj)
307+
print(result.exit_code, result.output)
295308
# config route add prefix 12.2.3.4/32 nexthop vrf Vrf-RED 30.0.0.7
296309
result = runner.invoke(config.config.commands["route"].commands["add"], \
297310
["prefix", "12.2.3.4/32", "nexthop", "vrf", "Vrf-RED", "30.0.0.7"], obj=obj)

0 commit comments

Comments
 (0)