Skip to content

Commit a5eb26b

Browse files
[show][muxcable] Catch port Value error exception (#2076) (#2486)
<!-- Please make sure you've read and understood our contributing guidelines: https://github.com/Azure/SONiC/blob/gh-pages/CONTRIBUTING.md ** Make sure all your commits include a signature generated with `git commit -s` ** If this is a bug fix, make sure your description includes "closes #xxxx", "fixes #xxxx" or "resolves #xxxx" so that GitHub automatically closes the related issue when the PR is merged. If you are adding/modifying/removing any command or utility script, please also make sure to add/modify/remove any unit tests from the tests directory as appropriate. If you are modifying or removing an existing 'show', 'config' or 'sonic-clear' subcommand, or you are adding a new subcommand, please make sure you also update the Command Line Reference Guide (doc/Command-Reference.md) to reflect your changes. Please provide the following information: --> #### What I did Catch the exception if a user passes in an invalid port name. #### How I did it Added a try/catch to catch the ValueError. #### How to verify it Run "show muxcable cableinfo [invalid port]". #### Previous command output (if the output of a command-line utility has changed) ``` crystalnet@ibr01:~$ show muxcable cableinfo all Traceback (most recent call last): File "/usr/local/bin/show", line 8, in <module> sys.exit(cli()) File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 764, in __call__ return self.main(*args, **kwargs) File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 717, in main rv = self.invoke(ctx) File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 1137, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 1137, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 956, in invoke return ctx.invoke(self.callback, **ctx.params) File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 555, in invoke return callback(*args, **kwargs) File "/usr/local/lib/python3.7/dist-packages/click/decorators.py", line 64, in new_func return ctx.invoke(f, obj, *args, **kwargs) File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 555, in invoke return callback(*args, **kwargs) File "/usr/local/lib/python3.7/dist-packages/show/muxcable.py", line 626, in cableinfo physical_port_list = platform_sfputil_helper.logical_port_name_to_physical_port_list(port) File "/usr/local/lib/python3.7/dist-packages/utilities_common/platform_sfputil_helper.py", line 53, in logical_port_name_to_physical_port_list return [int(port_name)] ValueError: invalid literal for int() with base 10: 'all' crystalnet@ibr01:~$ ``` #### New command output (if the output of a command-line utility has changed) ``` crystalnet@ibr01:~$ show muxcable cableinfo all Invalid port 'all' ERR: Unable to get a port on muxcable port crystalnet@ibr01:~$ ```
1 parent 97a340b commit a5eb26b

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

tests/muxcable_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,11 @@
384384
Credo CACL1X321P2PA1M
385385
"""
386386

387+
expected_muxcable_cableinfo_invalid_port_output = """\
388+
Invalid port 'abc'
389+
ERR: Unable to get a port on muxcable port
390+
"""
391+
387392
show_muxcable_hwmode_muxdirection_active_expected_output = """\
388393
Port Direction Presence
389394
---------- ----------- ----------
@@ -1169,6 +1174,21 @@ def test_show_muxcable_cableinfo_incorrect_logical_port_return_value(self):
11691174
["Ethernet0"], obj=db)
11701175
assert result.exit_code == 1
11711176

1177+
@mock.patch('sonic_y_cable.y_cable.get_part_number', mock.MagicMock(return_value=(False)))
1178+
@mock.patch('sonic_y_cable.y_cable.get_vendor', mock.MagicMock(return_value=(False)))
1179+
@mock.patch('show.muxcable.platform_sfputil', mock.MagicMock(return_value=1))
1180+
def test_show_muxcable_cableinfo_invalid_port(self):
1181+
runner = CliRunner()
1182+
db = Db()
1183+
1184+
result = runner.invoke(show.cli.commands["muxcable"].commands["cableinfo"],
1185+
["Ethernet0"], obj=db)
1186+
assert result.exit_code == 1
1187+
1188+
result = runner.invoke(show.cli.commands["muxcable"].commands["cableinfo"],
1189+
["abc"], obj=db)
1190+
assert result.exit_code == 1
1191+
assert result.output == expected_muxcable_cableinfo_invalid_port_output
11721192

11731193
@mock.patch('show.muxcable.delete_all_keys_in_db_table', mock.MagicMock(return_value=0))
11741194
@mock.patch('show.muxcable.update_and_get_response_for_xcvr_cmd', mock.MagicMock(return_value={0: 0,

utilities_common/platform_sfputil_helper.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,17 @@ def platform_sfputil_read_porttab_mappings():
5353

5454

5555
def logical_port_name_to_physical_port_list(port_name):
56-
if port_name.startswith("Ethernet"):
57-
if platform_sfputil.is_logical_port(port_name):
58-
return platform_sfputil.get_logical_to_physical(port_name)
56+
try:
57+
if port_name.startswith("Ethernet"):
58+
if platform_sfputil.is_logical_port(port_name):
59+
return platform_sfputil.get_logical_to_physical(port_name)
5960
else:
60-
click.echo("Invalid port '{}'".format(port_name))
61-
return None
62-
else:
63-
return [int(port_name)]
61+
return [int(port_name)]
62+
except ValueError:
63+
pass
64+
65+
click.echo("Invalid port '{}'".format(port_name))
66+
return None
6467

6568

6669
def get_logical_list():

0 commit comments

Comments
 (0)