Skip to content

Commit

Permalink
[sfputil] Add an enable parameter to eliminate the loopback modes wit…
Browse files Browse the repository at this point in the history
…h the -none suffix

Signed-off-by: xinyu <xinyu0123@gmail.com>
  • Loading branch information
xinyulin committed Sep 3, 2024
1 parent e1c7aa1 commit b78f125
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
14 changes: 7 additions & 7 deletions sfputil/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1968,12 +1968,12 @@ def debug():
# 'loopback' subcommand
@debug.command()
@click.argument('port_name', required=True, default=None)
@click.argument('loopback_mode', required=True, default="none",
type=click.Choice(["none", "host-side-input", "host-side-output",
"media-side-input", "media-side-output",
"host-side-input-none", "host-side-output-none",
"media-side-input-none", "media-side-output-none"]))
def loopback(port_name, loopback_mode):
@click.argument('loopback_mode', required=True, default=None,
type=click.Choice(["host-side-input", "host-side-output",
"media-side-input", "media-side-output"]))
@click.argument('enable', required=True, default=None,
type=click.Choice(["enable", "disable"]))
def loopback(port_name, loopback_mode, enable):
"""Set module diagnostic loopback mode
"""
physical_port = logical_port_to_physical_port_index(port_name)
Expand Down Expand Up @@ -2040,7 +2040,7 @@ def loopback(port_name, loopback_mode):
lane_mask = 0

try:
status = api.set_loopback_mode(loopback_mode, lane_mask=lane_mask)
status = api.set_loopback_mode(loopback_mode, lane_mask=lane_mask, enable=enable=='enable')
except AttributeError:
click.echo("{}: Set loopback mode is not applicable for this module".format(port_name))
sys.exit(ERROR_NOT_IMPLEMENTED)
Expand Down
24 changes: 12 additions & 12 deletions tests/sfputil_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1648,63 +1648,63 @@ def test_debug_loopback(self, mock_sonic_v2_connector, mock_config_db_connector,
runner = CliRunner()
mock_sfp.get_presence.return_value = False
result = runner.invoke(sfputil.cli.commands['debug'].commands['loopback'],
["Ethernet0", "host-side-input"])
["Ethernet0", "host-side-input", "enable"])
assert result.output == 'Ethernet0: SFP EEPROM not detected\n'
mock_sfp.get_presence.return_value = True

mock_sfp.get_xcvr_api = MagicMock(side_effect=NotImplementedError)
result = runner.invoke(sfputil.cli.commands['debug'].commands['loopback'],
["Ethernet0", "host-side-input"])
["Ethernet0", "host-side-input", "enable"])
assert result.output == 'Ethernet0: This functionality is not implemented\n'
assert result.exit_code == ERROR_NOT_IMPLEMENTED

mock_sfp.get_xcvr_api = MagicMock(return_value=mock_api)
result = runner.invoke(sfputil.cli.commands['debug'].commands['loopback'],
["Ethernet0", "host-side-input"])
["Ethernet0", "host-side-input", "enable"])
assert result.output == 'Ethernet0: Set host-side-input loopback\n'
assert result.exit_code != ERROR_NOT_IMPLEMENTED

mock_sfp.get_xcvr_api = MagicMock(return_value=mock_api)
result = runner.invoke(sfputil.cli.commands['debug'].commands['loopback'],
["Ethernet0", "media-side-input"])
["Ethernet0", "media-side-input", "enable"])
assert result.output == 'Ethernet0: Set media-side-input loopback\n'
assert result.exit_code != ERROR_NOT_IMPLEMENTED

mock_api.set_loopback_mode.return_value = False
result = runner.invoke(sfputil.cli.commands['debug'].commands['loopback'],
["Ethernet0", "none"])
assert result.output == 'Ethernet0: Set none loopback failed\n'
["Ethernet0", "media-side-output", "enable"])
assert result.output == 'Ethernet0: Set media-side-output loopback failed\n'
assert result.exit_code == EXIT_FAIL

mock_api.set_loopback_mode.return_value = True
mock_api.set_loopback_mode.side_effect = AttributeError
result = runner.invoke(sfputil.cli.commands['debug'].commands['loopback'],
["Ethernet0", "none"])
["Ethernet0", "host-side-input", "enable"])
assert result.output == 'Ethernet0: Set loopback mode is not applicable for this module\n'
assert result.exit_code == ERROR_NOT_IMPLEMENTED

mock_api.set_loopback_mode.side_effect = [TypeError, True]
result = runner.invoke(sfputil.cli.commands['debug'].commands['loopback'],
["Ethernet0", "none"])
assert result.output == 'Ethernet0: Set none loopback\n'
["Ethernet0", "host-side-input", "enable"])
assert result.output == 'Ethernet0: Set host-side-input loopback\n'

mock_config_db = MagicMock()
mock_config_db.get.side_effect = TypeError
mock_config_db_connector.return_value = mock_config_db
result = runner.invoke(sfputil.cli.commands['debug'].commands['loopback'],
["Ethernet0", "none"])
["Ethernet0", "media-side-input", "enable"])
assert result.output == 'Ethernet0: subport is not present in CONFIG_DB\n'
assert result.exit_code == EXIT_FAIL

mock_config_db_connector.return_value = None
result = runner.invoke(sfputil.cli.commands['debug'].commands['loopback'],
["Ethernet0", "none"])
["Ethernet0", "media-side-input", "enable"])
assert result.output == 'Ethernet0: Failed to connect to CONFIG_DB\n'
assert result.exit_code == EXIT_FAIL

mock_config_db_connector.return_value = MagicMock()
mock_sonic_v2_connector.return_value = None
result = runner.invoke(sfputil.cli.commands['debug'].commands['loopback'],
["Ethernet0", "none"])
["Ethernet0", "media-side-input", "enable"])
assert result.output == 'Ethernet0: Failed to connect to STATE_DB\n'
assert result.exit_code == EXIT_FAIL

0 comments on commit b78f125

Please sign in to comment.