Skip to content

Pr json support for show platform temperature #3874

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
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
57 changes: 43 additions & 14 deletions scripts/tempershow
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,13 +28,14 @@ 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')
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
Expand All @@ -40,22 +44,47 @@ 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')


if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Display the temperature Sensor information',
formatter_class=argparse.RawTextHelpFormatter,
epilog="""
Examples:
tempershow -j
""")

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)
5 changes: 4 additions & 1 deletion show/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,12 @@ def fan():

# 'temperature' subcommand ("show platform temperature")
@platform.command()
def temperature():
@click.option('-j', '--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)


Expand Down
26 changes: 26 additions & 0 deletions tests/show_platform_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,32 @@ 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'])

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'])

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):
"""
Note: `show platform psustatus` simply calls the `psushow` utility and
Expand Down
Loading