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

[ssdhealth] Check for default device before falling back to discovery #60

Closed
wants to merge 1 commit into from
Closed
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
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)
Loading