Skip to content

Commit 103de86

Browse files
authored
[CLI][MPLS][Show] Fixed show and config mpls cli bug where invalid interfaces would pas… (sonic-net#1770)
* Added unit tests for both single and multi ASIC platform
1 parent 04cc047 commit 103de86

File tree

4 files changed

+171
-15
lines changed

4 files changed

+171
-15
lines changed

config/main.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -4039,7 +4039,9 @@ def add(ctx, interface_name):
40394039
if interface_name is None:
40404040
ctx.fail("'interface_name' is None!")
40414041

4042-
table_name = get_interface_table_name(interface_name)
4042+
table_name = get_interface_table_name(interface_name)
4043+
if not clicommon.is_interface_in_config_db(config_db, interface_name):
4044+
ctx.fail('interface {} doesn`t exist'.format(interface_name))
40434045
if table_name == "":
40444046
ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan]")
40454047
config_db.set_entry(table_name, interface_name, {"mpls": "enable"})
@@ -4059,7 +4061,9 @@ def remove(ctx, interface_name):
40594061
if interface_name is None:
40604062
ctx.fail("'interface_name' is None!")
40614063

4062-
table_name = get_interface_table_name(interface_name)
4064+
table_name = get_interface_table_name(interface_name)
4065+
if not clicommon.is_interface_in_config_db(config_db, interface_name):
4066+
ctx.fail('interface {} doesn`t exist'.format(interface_name))
40634067
if table_name == "":
40644068
ctx.fail("'interface_name' is not valid. Valid names [Ethernet/PortChannel/Vlan]")
40654069
config_db.set_entry(table_name, interface_name, {"mpls": "disable"})

show/interfaces/__init__.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,11 @@ def mpls(ctx, interfacename, namespace, display):
340340
print("Error: Invalid display option command for single asic")
341341
return
342342

343+
display = "all" if interfacename else display
343344
masic = multi_asic_util.MultiAsic(display_option=display, namespace_option=namespace)
344345
ns_list = masic.get_ns_list_based_on_options()
345346
intfs_data = {}
347+
intf_found = False
346348

347349
for ns in ns_list:
348350

@@ -360,11 +362,14 @@ def mpls(ctx, interfacename, namespace, display):
360362
if len(tokens) != 2:
361363
continue
362364

363-
if (interfacename is not None) and (interfacename != tokens[1]):
364-
continue
365+
if (interfacename is not None):
366+
if (interfacename != ifname):
367+
continue
368+
369+
intf_found = True
365370

366371
if (display != "all"):
367-
if ("Loopback" in tokens[1]):
372+
if ("Loopback" in ifname):
368373
continue
369374

370375
if ifname.startswith("Ethernet") and multi_asic.is_port_internal(ifname, ns):
@@ -377,9 +382,13 @@ def mpls(ctx, interfacename, namespace, display):
377382
mpls_intf = appl_db.get_all(appl_db.APPL_DB, key)
378383

379384
if 'mpls' not in mpls_intf or mpls_intf['mpls'] == 'disable':
380-
intfs_data.update({tokens[1]: 'disable'})
385+
intfs_data.update({ifname: 'disable'})
381386
else:
382-
intfs_data.update({tokens[1]: mpls_intf['mpls']})
387+
intfs_data.update({ifname: mpls_intf['mpls']})
388+
389+
# Check if interface is valid
390+
if (interfacename is not None and not intf_found):
391+
ctx.fail('interface {} doesn`t exist'.format(interfacename))
383392

384393
header = ['Interface', 'MPLS State']
385394
body = []

tests/mpls_test.py

+140-8
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,39 @@
5050
Ethernet-BP4 disable
5151
"""
5252

53+
show_interfaces_mpls_output_interface="""\
54+
Interface MPLS State
55+
----------- ------------
56+
Ethernet4 enable
57+
"""
58+
59+
show_interfaces_mpls_masic_output_interface="""\
60+
Interface MPLS State
61+
----------- ------------
62+
Ethernet4 disable
63+
"""
64+
65+
invalid_interface_remove_output = """\
66+
Usage: remove [OPTIONS] <interface_name>
67+
Try "remove --help" for help.
68+
69+
Error: interface Ethernet8 doesn`t exist
70+
"""
5371

72+
invalid_interface_add_output = """\
73+
Usage: add [OPTIONS] <interface_name>
74+
Try "add --help" for help.
75+
76+
Error: interface Ethernet8 doesn`t exist
77+
"""
78+
79+
invalid_interface_show_output = """\
80+
Usage: mpls [OPTIONS] [INTERFACENAME]
81+
Try "mpls --help" for help.
82+
83+
Error: interface Ethernet100 doesn`t exist
84+
"""
85+
5486
modules_path = os.path.join(os.path.dirname(__file__), "..")
5587
test_path = os.path.join(modules_path, "tests")
5688
scripts_path = os.path.join(modules_path, "scripts")
@@ -72,12 +104,27 @@ def test_config_mpls_add(self):
72104

73105
result = runner.invoke(
74106
config.config.commands["interface"].commands["mpls"].commands["add"],
75-
["Ethernet8"], obj=obj
107+
["Ethernet0"], obj=obj
76108
)
77109
print(result.exit_code)
78110
print(result.output)
79111
assert result.exit_code == 0
80-
assert db.cfgdb.get_entry("INTERFACE", "Ethernet8") == {"mpls": "enable"}
112+
assert db.cfgdb.get_entry("INTERFACE", "Ethernet0") == {"mpls": "enable"}
113+
114+
def test_config_mpls_invalid_interface_add(self):
115+
runner = CliRunner()
116+
db = Db()
117+
obj = {'config_db':db.cfgdb}
118+
119+
result = runner.invoke(
120+
config.config.commands["interface"].commands["mpls"].commands["add"],
121+
["Ethernet8"], obj=obj
122+
)
123+
print(result.exit_code)
124+
print(result.output)
125+
assert result.exit_code == 2
126+
assert result.output == invalid_interface_add_output
127+
81128

82129
def test_show_interfaces_mpls_frontend(self):
83130

@@ -111,19 +158,54 @@ def test_show_interfaces_mpls_dall(self):
111158
assert result.exit_code == 0
112159
assert result.output == show_interfaces_mpls_output_frontend
113160

161+
def test_show_interfaces_mpls_asic_interface(self):
162+
runner = CliRunner()
163+
result = runner.invoke(
164+
show.cli.commands["interfaces"].commands["mpls"],
165+
["Ethernet4"]
166+
)
167+
print(result.output)
168+
assert result.exit_code == 0
169+
assert result.output == show_interfaces_mpls_output_interface
170+
171+
def test_show_interfaces_mpls_asic_invalid_interface(self):
172+
runner = CliRunner()
173+
result = runner.invoke(
174+
show.cli.commands["interfaces"].commands["mpls"],
175+
["Ethernet100"]
176+
)
177+
print(result.output)
178+
assert result.exit_code == 2
179+
assert result.output == invalid_interface_show_output
180+
114181
def test_config_mpls_remove(self):
115182
runner = CliRunner()
116183
db = Db()
117184
obj = {'config_db':db.cfgdb}
118185

119186
result = runner.invoke(
120187
config.config.commands["interface"].commands["mpls"].commands["remove"],
121-
["Ethernet8"], obj=obj
188+
["Ethernet0"], obj=obj
122189
)
123190
print(result.exit_code)
124191
print(result.output)
125192
assert result.exit_code == 0
126-
assert db.cfgdb.get_entry("INTERFACE", "Ethernet8") == {"mpls": "disable"}
193+
assert db.cfgdb.get_entry("INTERFACE", "Ethernet0") == {"mpls": "disable"}
194+
195+
def test_config_mpls_invalid_interface_remove(self):
196+
runner = CliRunner()
197+
db = Db()
198+
obj = {'config_db':db.cfgdb}
199+
200+
result = runner.invoke(
201+
config.config.commands["interface"].commands["mpls"].commands["remove"],
202+
["Ethernet8"], obj=obj
203+
)
204+
print(result.exit_code)
205+
print(result.output)
206+
assert result.exit_code == 2
207+
assert result.output == invalid_interface_remove_output
208+
127209

128210
@classmethod
129211
def teardown_class(cls):
@@ -152,12 +234,27 @@ def test_config_mpls_masic_add(self):
152234

153235
result = runner.invoke(
154236
config.config.commands["interface"].commands["mpls"].commands["add"],
155-
["Ethernet8"], obj=obj
237+
["Ethernet0"], obj=obj
156238
)
157239
print(result.exit_code)
158240
print(result.output)
159241
assert result.exit_code == 0
160-
assert db.cfgdb.get_entry("INTERFACE", "Ethernet8") == {"mpls": "enable"}
242+
assert db.cfgdb.get_entry("INTERFACE", "Ethernet0") == {"mpls": "enable"}
243+
244+
245+
def test_config_mpls_masic_invalid_interface_add(self):
246+
runner = CliRunner()
247+
db = Db()
248+
obj = {'config_db':db.cfgdb, 'namespace':'asic0'}
249+
250+
result = runner.invoke(
251+
config.config.commands["interface"].commands["mpls"].commands["add"],
252+
["Ethernet8"], obj=obj
253+
)
254+
print(result.exit_code)
255+
print(result.output)
256+
assert result.exit_code == 2
257+
assert result.output == invalid_interface_add_output
161258

162259

163260
def test_show_interfaces_mpls_masic_frontend(self):
@@ -202,19 +299,54 @@ def test_show_interfaces_mpls_masic_asic_all(self):
202299
assert result.exit_code == 0
203300
assert result.output == show_interfaces_mpls_masic_output_asic_all
204301

302+
def test_show_interfaces_mpls_masic_asic_interface(self):
303+
runner = CliRunner()
304+
result = runner.invoke(
305+
show.cli.commands["interfaces"].commands["mpls"],
306+
["Ethernet4"]
307+
)
308+
print(result.output)
309+
assert result.exit_code == 0
310+
assert result.output == show_interfaces_mpls_masic_output_interface
311+
312+
def test_show_interfaces_mpls_masic_asic_invalid_interface(self):
313+
runner = CliRunner()
314+
result = runner.invoke(
315+
show.cli.commands["interfaces"].commands["mpls"],
316+
["Ethernet100"]
317+
)
318+
print(result.output)
319+
assert result.exit_code == 2
320+
assert result.output == invalid_interface_show_output
321+
205322
def test_config_mpls_masic_remove(self):
206323
runner = CliRunner()
207324
db = Db()
208325
obj = {'config_db':db.cfgdb, 'namespace':'asic0'}
209326

210327
result = runner.invoke(
211328
config.config.commands["interface"].commands["mpls"].commands["remove"],
212-
["Ethernet8"], obj=obj
329+
["Ethernet0"], obj=obj
213330
)
214331
print(result.exit_code)
215332
print(result.output)
216333
assert result.exit_code == 0
217-
assert db.cfgdb.get_entry("INTERFACE", "Ethernet8") == {"mpls": "disable"}
334+
assert db.cfgdb.get_entry("INTERFACE", "Ethernet0") == {"mpls": "disable"}
335+
336+
def test_config_mpls_masic_invalid_interface_remove(self):
337+
runner = CliRunner()
338+
db = Db()
339+
obj = {'config_db':db.cfgdb, 'namespace':'asic0'}
340+
341+
result = runner.invoke(
342+
config.config.commands["interface"].commands["mpls"].commands["remove"],
343+
["Ethernet8"], obj=obj
344+
)
345+
print(result.exit_code)
346+
print(result.output)
347+
assert result.exit_code == 2
348+
assert result.output == invalid_interface_remove_output
349+
218350

219351
@classmethod
220352
def teardown_class(cls):

utilities_common/cli.py

+11
Original file line numberDiff line numberDiff line change
@@ -576,3 +576,14 @@ def interface_is_untagged_member(db, interface_name):
576576
if (val['tagging_mode'] == 'untagged'):
577577
return True
578578
return False
579+
580+
def is_interface_in_config_db(config_db, interface_name):
581+
""" Check if an interface is in CONFIG DB """
582+
if (not interface_name in config_db.get_keys('VLAN_INTERFACE') and
583+
not interface_name in config_db.get_keys('INTERFACE') and
584+
not interface_name in config_db.get_keys('PORTCHANNEL_INTERFACE') and
585+
not interface_name == 'null'):
586+
return False
587+
588+
return True
589+

0 commit comments

Comments
 (0)