From f13e549990f20156bd362d7c64ae585fbb2b3673 Mon Sep 17 00:00:00 2001 From: Ashwin Srinivasan Date: Fri, 24 May 2024 03:50:08 +0000 Subject: [PATCH] Fixed potential issue with parse_id_number not reconciling leading/trailing spaces --- sonic_platform_base/sonic_storage/ssd.py | 2 +- tests/test_ssd.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/sonic_platform_base/sonic_storage/ssd.py b/sonic_platform_base/sonic_storage/ssd.py index e95b228f3..0ae98660d 100644 --- a/sonic_platform_base/sonic_storage/ssd.py +++ b/sonic_platform_base/sonic_storage/ssd.py @@ -439,7 +439,7 @@ def parse_id_number(self, id, buffer): if buffer: buffer_lines = buffer.split('\n') for line in buffer_lines: - if line.startswith(str(id)): + if line.strip().startswith(str(id)): return line[len(str(id)):] return NOT_AVAILABLE diff --git a/tests/test_ssd.py b/tests/test_ssd.py index 319625660..085583f0a 100644 --- a/tests/test_ssd.py +++ b/tests/test_ssd.py @@ -152,6 +152,11 @@ Selective Self-tests/Logging not supported""" +output_ssd_leading_trailing_spaces = """ + 241 Host_Writes_32MiB 0x0002 100 001 000 Old_age Always - 178564 + 242 Host_Reads_32MiB 0x0002 100 001 000 Old_age Always - 760991 +""" + output_Innodisk_ssd = """smartctl 7.2 2020-12-30 r5155 [x86_64-linux-5.10.0-23-2-amd64] (local build) Copyright (C) 2002-20, Bruce Allen, Christian Franke, www.smartmontools.org @@ -1161,6 +1166,13 @@ def test_ssd(self): assert(ssd.get_disk_io_writes() == '178564') assert(ssd.get_reserved_blocks() == '146') + @mock.patch('sonic_platform_base.sonic_storage.ssd.SsdUtil._execute_shell', mock.MagicMock(return_value=output_ssd_leading_trailing_spaces)) + def test_ssd_leading_trailing_spaces(self): + # Test parsing a normal ssd info + ssd = SsdUtil('/dev/sda') + + assert(ssd.get_disk_io_writes() == '178564') + assert(ssd.get_disk_io_reads() == '760991') @mock.patch('sonic_platform_base.sonic_storage.ssd.SsdUtil._execute_shell', mock.MagicMock(return_value=output_lack_info_ssd)) def test_ssd_with_na_path(self):