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 all 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
31 changes: 31 additions & 0 deletions show/muxcable.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,34 @@ 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
part_num = sonic_y_cable.y_cable.get_part_number(physical_port)
if part_num == False or part_num == -1:
click.echo("ERR: Unable to get cable info part number")
sys.exit(EXIT_FAIL)
vendor = sonic_y_cable.y_cable.get_vendor(physical_port)
if vendor == False or vendor == -1:
click.echo("ERR: Unable to get cable info vendor name")
sys.exit(EXIT_FAIL)
headers = ['Vendor', 'Model']

body = [[vendor, part_num]]
click.echo(tabulate(body, headers=headers))
59 changes: 59 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 @@ -153,6 +156,12 @@
}
"""

expected_muxcable_cableinfo_output = """\
Vendor Model
-------- ---------------
Credo CACL1X321P2PA1M
"""


class TestMuxcable(object):
@classmethod
Expand Down Expand Up @@ -487,6 +496,56 @@ def test_config_muxcable_disable_loopback(self):

assert result.exit_code == 100

@mock.patch('sonic_y_cable.y_cable.get_part_number', mock.MagicMock(return_value=("CACL1X321P2PA1M")))
@mock.patch('sonic_y_cable.y_cable.get_vendor', mock.MagicMock(return_value=("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
assert result.output == expected_muxcable_cableinfo_output

@mock.patch('sonic_y_cable.y_cable.get_part_number', mock.MagicMock(return_value=(False)))
@mock.patch('sonic_y_cable.y_cable.get_vendor', 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_part_number', mock.MagicMock(return_value=(False)))
@mock.patch('sonic_y_cable.y_cable.get_vendor', 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_part_number', mock.MagicMock(return_value=(False)))
@mock.patch('sonic_y_cable.y_cable.get_vendor', 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)]