Skip to content

Commit e09804a

Browse files
authored
[ssdhealth] Check for default device before falling back to discovery (#3725)
<!-- Please make sure you've read and understood our contributing guidelines: https://github.com/Azure/SONiC/blob/gh-pages/CONTRIBUTING.md failure_prs.log skip_prs.log Make sure all your commits include a signature generated with `git commit -s` ** If this is a bug fix, make sure your description includes "closes #xxxx", "fixes #xxxx" or "resolves #xxxx" so that GitHub automatically closes the related issue when the PR is merged. If you are adding/modifying/removing any command or utility script, please also make sure to add/modify/remove any unit tests from the tests directory as appropriate. If you are modifying or removing an existing 'show', 'config' or 'sonic-clear' subcommand, or you are adding a new subcommand, please make sure you also update the Command Line Reference Guide (doc/Command-Reference.md) to reflect your changes. Please provide the following information: --> #### What I did Some platforms may have multiple primary disks which makes it ambiguous to determine the device when there is no device specific in the ssdhealth command #### How I did it Update the show platform command to check platform.json #### How to verify it 1) UT's ``` vkarri@85964d14e169:/sonic/src/sonic-utilities$ pytest-3 tests/show_platform_test.py -k "ssdhealth" -v collected 8 items / 6 deselected / 2 selected tests/show_platform_test.py::TestShowPlatformSsdhealth::test_ssdhealth PASSED [ 50%] tests/show_platform_test.py::TestShowPlatformSsdhealth::test_ssdhealth_default_device PASSED [100%] ``` 2) Verified the CLI is printing the health output for expected device #### Previous command output (if the output of a command-line utility has changed) #### New command output (if the output of a command-line utility has changed)
1 parent 727f488 commit e09804a

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

show/platform.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,21 @@ def psustatus(index, json, verbose):
104104
def ssdhealth(device, verbose, vendor):
105105
"""Show SSD Health information"""
106106
if not device:
107-
device = os.popen("lsblk -o NAME,TYPE -p | grep disk").readline().strip().split()[0]
107+
platform_data = device_info.get_platform_json_data()
108+
# Check if there is any default disk for this platform
109+
# {
110+
# "chassis": {
111+
# ..........
112+
# "disk": {
113+
# "device" : "/dev/nvme0n1"
114+
# }
115+
# }
116+
# }
117+
device = platform_data.get("chassis", {}).get("disk", {}).get("device", None)
118+
if not device:
119+
# Fallback to discovery
120+
device = os.popen("lsblk -o NAME,TYPE -p | grep disk").readline().strip().split()[0]
121+
108122
cmd = ['sudo', 'ssdutil', '-d', str(device)]
109123
options = ["-v"] if verbose else []
110124
options += ["-e"] if vendor else []

tests/show_platform_test.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,41 @@ def test_verbose(self):
8787
CliRunner().invoke(show.cli.commands['platform'].commands['psustatus'], ['--verbose'])
8888
assert mock_run_command.call_count == 1
8989
mock_run_command.assert_called_with(['psushow', '-s'], display_cmd=True)
90+
91+
92+
class TestShowPlatformSsdhealth(object):
93+
# Test 'show platform ssdhealth'
94+
@mock.patch('utilities_common.cli.run_command')
95+
def test_ssdhealth(self, mock_run_command):
96+
result = CliRunner().invoke(show.cli.commands['platform'].commands['ssdhealth'], ["/dev/nvme0n1", '--verbose'])
97+
assert result.exit_code == 0, result.output
98+
assert mock_run_command.call_count == 1
99+
mock_run_command.assert_called_with(['sudo', 'ssdutil', '-d', '/dev/nvme0n1', '-v'], display_cmd=True)
100+
101+
@mock.patch('os.popen')
102+
@mock.patch('utilities_common.cli.run_command')
103+
@mock.patch('sonic_py_common.device_info.get_platform_json_data')
104+
def test_ssdhealth_default_device(self, mock_plat_json, mock_run_command, mock_open):
105+
mock_plat_json.return_value = {
106+
"chassis": {
107+
"name": "mock_platform"
108+
}
109+
}
110+
mock_fd = mock.MagicMock()
111+
mock_fd.readline.return_value = "/dev/nvme0n1 disk\n"
112+
mock_open.return_value = mock_fd
113+
CliRunner().invoke(show.cli.commands['platform'].commands['ssdhealth'], ['--verbose'])
114+
mock_open.assert_called_with("lsblk -o NAME,TYPE -p | grep disk")
115+
mock_run_command.assert_called_with(['sudo', 'ssdutil', '-d', '/dev/nvme0n1', '-v'], display_cmd=True)
116+
117+
mock_plat_json.return_value = {
118+
"chassis": {
119+
"name": "mock_platform2",
120+
"disk": {
121+
"device": "/dev/nvme0n1"
122+
}
123+
}
124+
}
125+
CliRunner().invoke(show.cli.commands['platform'].commands['ssdhealth'], ['--verbose'])
126+
mock_plat_json.assert_called_with()
127+
mock_run_command.assert_called_with(['sudo', 'ssdutil', '-d', '/dev/nvme0n1', '-v'], display_cmd=True)

tests/show_test.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,9 +684,15 @@ def test_syseeprom(self, mock_run_command):
684684
assert result.exit_code == 0
685685
mock_run_command.assert_called_once_with(['sudo', 'decode-syseeprom', '-d'], display_cmd=True)
686686

687+
@mock.patch('sonic_py_common.device_info.get_platform_json_data')
687688
@patch('utilities_common.cli.run_command')
688689
@patch('os.popen')
689-
def test_ssdhealth(self, mock_popen, mock_run_command):
690+
def test_ssdhealth(self, mock_popen, mock_run_command, mock_plat_json):
691+
mock_plat_json.return_value = {
692+
"chassis": {
693+
"name": "mock_platform"
694+
}
695+
}
690696
mock_popen.return_value.readline.return_value = '/dev/sda\n'
691697
runner = CliRunner()
692698
result = runner.invoke(show.cli.commands['platform'].commands['ssdhealth'], ['--verbose', '--vendor'])

0 commit comments

Comments
 (0)