Skip to content

Commit

Permalink
[ssdhealth] Check for default device before falling back to discovery
Browse files Browse the repository at this point in the history
Signed-off-by: Vivek Reddy <vkarri@nvidia.com>
  • Loading branch information
vivekrnv committed Dec 5, 2024
1 parent b767cb8 commit 7094494
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
16 changes: 15 additions & 1 deletion show/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,21 @@ def psustatus(index, json, verbose):
def ssdhealth(device, verbose, vendor):
"""Show SSD Health information"""
if not device:
device = os.popen("lsblk -o NAME,TYPE -p | grep disk").readline().strip().split()[0]
platform_data = device_info.get_platform_json_data()
# Check if there is any default disk for this platform
# {
# "chassis": {
# ..........
# "disk": {
# "device" : "/dev/nvme0n1"
# }
# }
# }
device = platform_data.get("chassis", {}).get("disk", {}).get("device", None)
if not device:
# Fallback to discovery
device = os.popen("lsblk -o NAME,TYPE -p | grep disk").readline().strip().split()[0]

cmd = ['sudo', 'ssdutil', '-d', str(device)]
options = ["-v"] if verbose else []
options += ["-e"] if vendor else []
Expand Down
37 changes: 37 additions & 0 deletions tests/show_platform_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,40 @@ def test_verbose(self):
CliRunner().invoke(show.cli.commands['platform'].commands['psustatus'], ['--verbose'])
assert mock_run_command.call_count == 1
mock_run_command.assert_called_with(['psushow', '-s'], display_cmd=True)

class TestShowPlatformSsdhealth(object):
# Test 'show platform ssdhealth'
@mock.patch('utilities_common.cli.run_command')
def test_ssdhealth(self, mock_run_command):
result = CliRunner().invoke(show.cli.commands['platform'].commands['ssdhealth'], ["/dev/nvme0n1", '--verbose'])
assert result.exit_code == 0, result.output
assert mock_run_command.call_count == 1
mock_run_command.assert_called_with(['sudo', 'ssdutil', '-d', '/dev/nvme0n1', '-v'], display_cmd=True)

@mock.patch('os.popen')
@mock.patch('utilities_common.cli.run_command')
@mock.patch('sonic_py_common.device_info.get_platform_json_data')
def test_ssdhealth_default_device(self, mock_plat_json, mock_run_command, mock_open):
mock_plat_json.return_value = {
"chassis" : {
"name" : "mock_platform"
}
}
mock_fd = mock.MagicMock()
mock_fd.readline.return_value = "/dev/nvme0n1 disk\n"
mock_open.return_value = mock_fd
result = CliRunner().invoke(show.cli.commands['platform'].commands['ssdhealth'], ['--verbose'])
mock_open.assert_called_with("lsblk -o NAME,TYPE -p | grep disk")
mock_run_command.assert_called_with(['sudo', 'ssdutil', '-d', '/dev/nvme0n1', '-v'], display_cmd=True)

mock_plat_json.return_value = {
"chassis" : {
"name" : "mock_platform2",
"disk" : {
"device" : "/dev/nvme0n1"
}
}
}
result = CliRunner().invoke(show.cli.commands['platform'].commands['ssdhealth'], ['--verbose'])
mock_plat_json.assert_called_with()
mock_run_command.assert_called_with(['sudo', 'ssdutil', '-d', '/dev/nvme0n1', '-v'], display_cmd=True)

0 comments on commit 7094494

Please sign in to comment.