Skip to content

Commit 7d3e674

Browse files
authored
[platform] Update 'show platform psustatus' tests to handle new expanded output (#3040)
Approach What is the motivation for this PR? Update testing of show platform psustatus to handle the new, expanded output provided by sonic-net/sonic-utilities#1416. Maintain backward-compatibility with the previous syntax in 201811 and 201911 versions. How did you do it? Support both output formats based on observed OS version How did you verify/test it? Tested against one platform with old and new output formats
1 parent c0bd153 commit 7d3e674

File tree

2 files changed

+59
-11
lines changed

2 files changed

+59
-11
lines changed

tests/platform_tests/cli/test_show_platform.py

+36-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# TODO: Add tests for `show platform firmware updates`
1010
# TODO: Add tests for `show platform firmware version`
1111

12+
import json
1213
import logging
1314
import re
1415

@@ -174,17 +175,49 @@ def test_show_platform_psustatus(duthosts, enum_supervisor_dut_hostname):
174175

175176
logging.info("Verifying output of '{}' on '{}' ...".format(cmd, duthost.hostname))
176177
psu_status_output_lines = duthost.command(cmd)["stdout_lines"]
177-
psu_line_pattern = re.compile(r"PSU\s+\d+\s+(OK|NOT OK|NOT PRESENT)")
178178

179-
# Check that all psus are showing valid status and also at-least one PSU is OK.
179+
if "201811" in duthost.os_version or "201911" in duthost.os_version:
180+
psu_line_pattern = re.compile(r"PSU\s+\d+\s+(OK|NOT OK|NOT PRESENT)")
181+
else:
182+
psu_line_pattern = re.compile(r"PSU\s+\d+\s+\w+\s+\w+\s+\w+\s+\w+\s+\w+\s+(OK|NOT OK|NOT PRESENT)\s+(green|amber|red|off)")
183+
184+
# Check that all PSUs are showing valid status and also at least one PSU is OK
180185
num_psu_ok = 0
186+
181187
for line in psu_status_output_lines[2:]:
182188
psu_match = psu_line_pattern.match(line)
183189
pytest_assert(psu_match, "Unexpected PSU status output: '{}' on '{}'".format(line, duthost.hostname))
184190
psu_status = psu_match.group(1)
185191
if psu_status == "OK":
186192
num_psu_ok += 1
187-
pytest_assert(num_psu_ok > 0, " No PSU's are displayed with OK status on '{}'".format(duthost.hostname))
193+
194+
pytest_assert(num_psu_ok > 0, "No PSUs are displayed with OK status on '{}'".format(duthost.hostname))
195+
196+
197+
def test_show_platform_psustatus_json(duthosts, rand_one_dut_hostname):
198+
"""
199+
@summary: Verify output of `show platform psustatus --json`
200+
"""
201+
duthost = duthosts[rand_one_dut_hostname]
202+
203+
if "201811" in duthost.os_version or "201911" in duthost.os_version:
204+
pytest.skip("JSON output not available in this version")
205+
206+
logging.info("Check pmon daemon status")
207+
pytest_assert(check_pmon_daemon_status(duthost), "Not all pmon daemons running.")
208+
209+
cmd = " ".join([CMD_SHOW_PLATFORM, "psustatus", "--json"])
210+
211+
logging.info("Verifying output of '{}' ...".format(cmd))
212+
psu_status_output = duthost.command(cmd)["stdout"]
213+
psu_info_list = json.loads(psu_status_output)
214+
215+
# TODO: Compare against expected platform-specific output
216+
for psu_info in psu_info_list:
217+
expected_keys = ["index", "name", "presence", "status", "led_status", "model", "serial", "voltage", "current", "power"]
218+
pytest_assert(all(key in psu_info for key in expected_keys), "Expected key(s) missing from JSON output: '{}'".format(psu_status_output))
219+
pytest_assert(psu_info["status"] in ["OK", "NOT OK", "NOT PRESENT"], "Unexpected PSU status value: '{}'".format(psu_info["status"]))
220+
pytest_assert(psu_info["led_status"] in ["green", "amber", "red", "off"], "Unexpected PSU led_status value: '{}'".format(psu_info["led_status"]))
188221

189222

190223
def verify_show_platform_fan_output(duthost, raw_output_lines):

tests/platform_tests/test_platform_info.py

+23-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
This script covers the test case 'Check platform information' in the SONiC platform test plan:
55
https://github.com/Azure/SONiC/blob/master/doc/pmon/sonic_platform_test_plan.md
66
"""
7+
import json
78
import logging
89
import re
910
import time
@@ -20,6 +21,7 @@
2021
]
2122

2223
CMD_PLATFORM_PSUSTATUS = "show platform psustatus"
24+
CMD_PLATFORM_PSUSTATUS_JSON = "{} --json".format(CMD_PLATFORM_PSUSTATUS)
2325
CMD_PLATFORM_FANSTATUS = "show platform fan"
2426
CMD_PLATFORM_TEMPER = "show platform temperature"
2527

@@ -145,7 +147,11 @@ def check_vendor_specific_psustatus(dut, psu_status_line):
145147
if dut.facts["asic_type"] in ["mellanox"]:
146148
from .mellanox.check_sysfs import check_psu_sysfs
147149

148-
psu_line_pattern = re.compile(r"PSU\s+(\d)+\s+(OK|NOT OK|NOT PRESENT)")
150+
if "201811" in dut.os_version or "201911" in dut.os_version:
151+
psu_line_pattern = re.compile(r"PSU\s+(\d)+\s+(OK|NOT OK|NOT PRESENT)")
152+
else:
153+
psu_line_pattern = re.compile(r"PSU\s+(\d+)\s+\w+\s+\w+\s+\w+\s+\w+\s+\w+\s+(OK|NOT OK|NOT PRESENT)\s+(green|amber|red|off)")
154+
149155
psu_match = psu_line_pattern.match(psu_status_line)
150156
psu_id = psu_match.group(1)
151157
psu_status = psu_match.group(2)
@@ -163,16 +169,25 @@ def turn_all_outlets_on(pdu_ctrl):
163169

164170

165171
def check_all_psu_on(dut, psu_test_results):
166-
cli_psu_status = dut.command(CMD_PLATFORM_PSUSTATUS)
167172
power_off_psu_list = []
168-
for line in cli_psu_status["stdout_lines"][2:]:
169-
fields = line.split()
170-
psu_test_results[fields[1]] = False
171-
if " ".join(fields[2:]) == "NOT OK":
172-
power_off_psu_list.append(fields[1])
173+
174+
if "201811" in dut.os_version or "201911" in dut.os_version:
175+
cli_psu_status = dut.command(CMD_PLATFORM_PSUSTATUS)
176+
for line in cli_psu_status["stdout_lines"][2:]:
177+
fields = line.split()
178+
psu_test_results[fields[1]] = False
179+
if " ".join(fields[2:]) == "NOT OK":
180+
power_off_psu_list.append(fields[1])
181+
else:
182+
# Use JSON output
183+
cli_psu_status = dut.command(CMD_PLATFORM_PSUSTATUS_JSON)
184+
psu_info_list = json.loads(cli_psu_status["stdout"])
185+
for psu_info in psu_info_list:
186+
if psu_info["status"] == "NOT OK":
187+
power_off_psu_list.append(psu_info["index"])
173188

174189
if power_off_psu_list:
175-
logging.warn('Power off PSU list: {}'.format(power_off_psu_list))
190+
logging.warn('Powered off PSUs: {}'.format(power_off_psu_list))
176191

177192
return len(power_off_psu_list) == 0
178193

0 commit comments

Comments
 (0)