Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[show] cli support for show muxcable cableinfo #1448

Merged
merged 7 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions show/muxcable.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,36 @@ def eyeinfo(port, target):
lane_data.append(res)
click.echo(tabulate(lane_data, headers=headers))
sys.exit(EXIT_SUCCESS)

@muxcable.command()
@click.argument('port', required=True, default=None)
def cableinfo(port):
"""Show muxcable cable information"""

if platform_sfputil is not None:
physical_port_list = platform_sfputil_helper.logical_port_name_to_physical_port_list(port)

if not isinstance(physical_port_list, list):
click.echo("ERR: Unable to get a port on muxcable port")
sys.exit(EXIT_FAIL)
if len(physical_port_list) != 1:
click.echo("ERR: Unable to get a single port on muxcable")
sys.exit(EXIT_FAIL)

physical_port = physical_port_list[0]
import sonic_y_cable.y_cable
res = sonic_y_cable.y_cable.get_pn_number_and_vendor_name(physical_port)
if res == False or res == -1:
click.echo("ERR: Unable to get cable info")
sys.exit(EXIT_FAIL)
headers = ['pin_number', 'vendor_name']
jleveque marked this conversation as resolved.
Show resolved Hide resolved
lane_data = []
pin = str(res[0].decode())
vendor = str(res[1].decode())
jleveque marked this conversation as resolved.
Show resolved Hide resolved
temp_list = []

temp_list.append(pin)
temp_list.append(vendor)
lane_data.append(temp_list)
click.echo(tabulate(lane_data, headers=headers))
sys.exit(EXIT_SUCCESS)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we've changed the success code to 0, there's no need to call sys.exit(EXIT_SUCCESS) anywhere, because Click will exit 0 by default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please plan to open a cleanup PR to remove all other sys.exit(EXIT_SUCCESS) calls

48 changes: 48 additions & 0 deletions tests/muxcable_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
sys.modules['sonic_y_cable'] = mock.Mock()
sys.modules['y_cable'] = mock.Mock()
sys.modules['sonic_y_cable.y_cable'] = mock.Mock()
sys.modules['platform_sfputil'] = mock.Mock()
sys.modules['platform_sfputil_helper'] = mock.Mock()
sys.modules['utilities_common.platform_sfputil_helper'] = mock.Mock()
#sys.modules['os'] = mock.Mock()
#sys.modules['os.geteuid'] = mock.Mock()
#sys.modules['platform_sfputil'] = mock.Mock()
Expand Down Expand Up @@ -487,6 +490,51 @@ def test_config_muxcable_disable_loopback(self):

assert result.exit_code == 100

@mock.patch('sonic_y_cable.y_cable.get_pn_number_and_vendor_name', mock.MagicMock(return_value=(bytearray(b'CACL1X321P2PA1M'), bytearray(b'Credo '))))
@mock.patch('show.muxcable.platform_sfputil', mock.MagicMock(return_value=1))
@mock.patch('utilities_common.platform_sfputil_helper.logical_port_name_to_physical_port_list', mock.MagicMock(return_value=[0]))
def test_show_muxcable_cableinfo(self):
runner = CliRunner()
db = Db()

result = runner.invoke(show.cli.commands["muxcable"].commands["cableinfo"],
["Ethernet0"], obj=db)

assert result.exit_code == 0
jleveque marked this conversation as resolved.
Show resolved Hide resolved

@mock.patch('sonic_y_cable.y_cable.get_pn_number_and_vendor_name', mock.MagicMock(return_value=(False)))
@mock.patch('show.muxcable.platform_sfputil', mock.MagicMock(return_value=1))
@mock.patch('utilities_common.platform_sfputil_helper.logical_port_name_to_physical_port_list', mock.MagicMock(return_value=[0]))
def test_show_muxcable_cableinfo_incorrect_port(self):
runner = CliRunner()
db = Db()

result = runner.invoke(show.cli.commands["muxcable"].commands["cableinfo"],
["Ethernet0"], obj=db)
assert result.exit_code == 1

@mock.patch('sonic_y_cable.y_cable.get_pn_number_and_vendor_name', mock.MagicMock(return_value=(False)))
@mock.patch('show.muxcable.platform_sfputil', mock.MagicMock(return_value=1))
@mock.patch('utilities_common.platform_sfputil_helper.logical_port_name_to_physical_port_list', mock.MagicMock(return_value=0))
def test_show_muxcable_cableinfo_incorrect_port_return_value(self):
runner = CliRunner()
db = Db()

result = runner.invoke(show.cli.commands["muxcable"].commands["cableinfo"],
["Ethernet0"], obj=db)
assert result.exit_code == 1

@mock.patch('sonic_y_cable.y_cable.get_pn_number_and_vendor_name', mock.MagicMock(return_value=(False)))
@mock.patch('show.muxcable.platform_sfputil', mock.MagicMock(return_value=1))
@mock.patch('utilities_common.platform_sfputil_helper.logical_port_name_to_physical_port_list', mock.MagicMock(return_value=[0,1]))
def test_show_muxcable_cableinfo_incorrect_logical_port_return_value(self):
runner = CliRunner()
db = Db()

result = runner.invoke(show.cli.commands["muxcable"].commands["cableinfo"],
["Ethernet0"], obj=db)
assert result.exit_code == 1

@classmethod
def teardown_class(cls):
os.environ['UTILITIES_UNIT_TESTING'] = "0"
Expand Down
10 changes: 10 additions & 0 deletions utilities_common/platform_sfputil_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,13 @@ def platform_sfputil_read_porttab_mappings():
sys.exit(1)

return 0

def logical_port_name_to_physical_port_list(port_name):
if port_name.startswith("Ethernet"):
if platform_sfputil.is_logical_port(port_name):
return platform_sfputil.get_logical_to_physical(port_name)
else:
click.echo("Invalid port '{}'".format(port_name))
return None
else:
return [int(port_name)]