From 7094494723fec7f0343139462c79c0a7a2f8cfa2 Mon Sep 17 00:00:00 2001 From: Vivek Reddy Date: Thu, 5 Dec 2024 00:09:39 +0000 Subject: [PATCH] [ssdhealth] Check for default device before falling back to discovery Signed-off-by: Vivek Reddy --- show/platform.py | 16 +++++++++++++++- tests/show_platform_test.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/show/platform.py b/show/platform.py index d3e9fa098a..7a74a083ee 100644 --- a/show/platform.py +++ b/show/platform.py @@ -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 [] diff --git a/tests/show_platform_test.py b/tests/show_platform_test.py index a867f067ee..03ecdb5a11 100644 --- a/tests/show_platform_test.py +++ b/tests/show_platform_test.py @@ -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)