From aefc1f3973c9b30ea3624c3ada00a5713a8a75a8 Mon Sep 17 00:00:00 2001 From: vikumarks Date: Mon, 5 May 2025 16:44:55 -0700 Subject: [PATCH 1/4] Added json support for platform temperature --- scripts/tempershow | 54 +++++++++++++++++++++++++++++++++++----------- show/platform.py | 5 ++++- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/scripts/tempershow b/scripts/tempershow index 0711bce817..ad69493875 100644 --- a/scripts/tempershow +++ b/scripts/tempershow @@ -3,6 +3,9 @@ """ Script to show fan status. """ +import argparse +import json + from tabulate import tabulate from swsscommon.swsscommon import SonicV2Connector from natsort import natsorted @@ -32,6 +35,7 @@ class TemperShow(object): return table = [] + json_output = [] for key in natsorted(keys): key_list = key.split('|') if len(key_list) != 2: # error data in DB, log it and ignore @@ -40,22 +44,46 @@ class TemperShow(object): name = key_list[1] data_dict = self.db.get_all(self.db.STATE_DB, key) - table.append((name, - data_dict[TEMPER_FIELD_NAME], - data_dict[HIGH_THRESH_FIELD_NAME], - data_dict[LOW_THRESH_FIELD_NAME], - data_dict[CRIT_HIGH_THRESH_FIELD_NAME], - data_dict[CRIT_LOW_THRESH_FIELD_NAME], - data_dict[WARNING_STATUS_FIELD_NAME], - data_dict[TIMESTAMP_FIELD_NAME] - )) + if output_json: + json_output.append({ + "Sensor": name, + "Temperature": data_dict[TEMPER_FIELD_NAME], + "High_TH": data_dict[HIGH_THRESH_FIELD_NAME], + "Low_TH": data_dict[LOW_THRESH_FIELD_NAME], + "Crit_High_TH": data_dict[CRIT_HIGH_THRESH_FIELD_NAME], + "Crit_Low_TH": data_dict[CRIT_LOW_THRESH_FIELD_NAME], + "Warning": data_dict[WARNING_STATUS_FIELD_NAME], + "Timestamp": data_dict[TIMESTAMP_FIELD_NAME] + }) + + else: + table.append((name, + data_dict[TEMPER_FIELD_NAME], + data_dict[HIGH_THRESH_FIELD_NAME], + data_dict[LOW_THRESH_FIELD_NAME], + data_dict[CRIT_HIGH_THRESH_FIELD_NAME], + data_dict[CRIT_LOW_THRESH_FIELD_NAME], + data_dict[WARNING_STATUS_FIELD_NAME], + data_dict[TIMESTAMP_FIELD_NAME] + )) - if table: + if output_json: + print(json.dumps(json_output, indent=2)) + elif table: print(tabulate(table, header, tablefmt='simple', stralign='right')) else: - print('No tempeature data available\n') + print('No temperature data available\n') + + parser = argparse.ArgumentParser(description='Display the temperature Sensor information', + formatter_class=argparse.RawTextHelpFormatter, + epilog=""" +Examples: + tempershow -j +""") -if __name__ == "__main__": + parser.add_argument('-j', '--json', action='store_true', help='Display output in JSON format') + args = parser.parse_args() + output_json = args.json temperShow = TemperShow() - temperShow.show() + temperShow.show(output_json) diff --git a/show/platform.py b/show/platform.py index d3e45c90c4..3f73684ab6 100644 --- a/show/platform.py +++ b/show/platform.py @@ -150,9 +150,12 @@ def fan(): # 'temperature' subcommand ("show platform temperature") @platform.command() -def temperature(): +@click.option('--json', is_flag=True, help="Output in JSON format") +def temperature(json): """Show device temperature information""" cmd = ['tempershow'] + if json: + cmd += ["-j"] clicommon.run_command(cmd) From c7bc7b2939ac5a878e9096dd16875116f245dbf6 Mon Sep 17 00:00:00 2001 From: vikumarks Date: Fri, 9 May 2025 09:38:48 -0700 Subject: [PATCH 2/4] @PR comment fixes --- scripts/tempershow | 3 ++- show/platform.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/tempershow b/scripts/tempershow index ad69493875..2de716184e 100644 --- a/scripts/tempershow +++ b/scripts/tempershow @@ -28,7 +28,7 @@ class TemperShow(object): self.db = SonicV2Connector(host="127.0.0.1") self.db.connect(self.db.STATE_DB) - def show(self): + def show(self, output_json): keys = self.db.keys(self.db.STATE_DB, TEMPER_TABLE_NAME + '*') if not keys: print('Thermal Not detected\n') @@ -75,6 +75,7 @@ class TemperShow(object): print('No temperature data available\n') +if __name__ == "__main__": parser = argparse.ArgumentParser(description='Display the temperature Sensor information', formatter_class=argparse.RawTextHelpFormatter, epilog=""" diff --git a/show/platform.py b/show/platform.py index 3f73684ab6..7a4d870794 100644 --- a/show/platform.py +++ b/show/platform.py @@ -150,7 +150,7 @@ def fan(): # 'temperature' subcommand ("show platform temperature") @platform.command() -@click.option('--json', is_flag=True, help="Output in JSON format") +@click.option('-j', '--json', is_flag=True, help="Output in JSON format") def temperature(json): """Show device temperature information""" cmd = ['tempershow'] From e35b40a213d44834e45e667c9afbab2ac107ec4d Mon Sep 17 00:00:00 2001 From: vikumarks Date: Fri, 9 May 2025 18:35:45 -0700 Subject: [PATCH 3/4] "show platform temp" unit test added --- tests/show_platform_test.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/show_platform_test.py b/tests/show_platform_test.py index 2707ac7012..4ade1a83d5 100644 --- a/tests/show_platform_test.py +++ b/tests/show_platform_test.py @@ -51,6 +51,26 @@ def test_summary(self): assert result.output == textwrap.dedent(expected_output) +class TestShowPlatformTemperature(object): + """ + Note: `show platform temperature` simply calls the `tempershow` utility and + passes a variety of options. Here we test that the utility is called + with the appropriate option(s). The functionality of the underlying + `tempershow` utility is expected to be tested by a separate suite of unit tests + """ + def test_temperature(self): + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + CliRunner().invoke(show.cli.commands['platform'].commands['temperature'], []) + assert mock_run_command.call_count == 1 + mock_run_command.assert_called_with(['tempershow'], display_cmd=False) + + def test_all_temperature_json(self): + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + CliRunner().invoke(show.cli.commands['platform'].commands['temperature'], ['--json']) + assert mock_run_command.call_count == 1 + mock_run_command.assert_called_with(['tempershow', '-j'], display_cmd=False) + + class TestShowPlatformPsu(object): """ Note: `show platform psustatus` simply calls the `psushow` utility and From 0f150fbb8fcdf80b1c904f89a98aede912d4bf5d Mon Sep 17 00:00:00 2001 From: vikumarks Date: Fri, 9 May 2025 19:30:38 -0700 Subject: [PATCH 4/4] fixes for unit test --- tests/show_platform_test.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/show_platform_test.py b/tests/show_platform_test.py index 4ade1a83d5..2e8fc35437 100644 --- a/tests/show_platform_test.py +++ b/tests/show_platform_test.py @@ -62,13 +62,19 @@ def test_temperature(self): with mock.patch('utilities_common.cli.run_command') as mock_run_command: CliRunner().invoke(show.cli.commands['platform'].commands['temperature'], []) assert mock_run_command.call_count == 1 - mock_run_command.assert_called_with(['tempershow'], display_cmd=False) + mock_run_command.assert_called_with(['tempershow']) - def test_all_temperature_json(self): + def test_temperature_json(self): with mock.patch('utilities_common.cli.run_command') as mock_run_command: CliRunner().invoke(show.cli.commands['platform'].commands['temperature'], ['--json']) assert mock_run_command.call_count == 1 - mock_run_command.assert_called_with(['tempershow', '-j'], display_cmd=False) + mock_run_command.assert_called_with(['tempershow', '-j']) + + def test_temperature_short_json(self): + with mock.patch('utilities_common.cli.run_command') as mock_run_command: + CliRunner().invoke(show.cli.commands['platform'].commands['temperature'], ['-j']) + assert mock_run_command.call_count == 1 + mock_run_command.assert_called_with(['tempershow', '-j']) class TestShowPlatformPsu(object):